มอดูล:Thai number

จากวิกิพีเดีย สารานุกรมเสรี
Documentation icon คู่มือการใช้งานมอดูล[สร้าง]
local p = {}

local data = {
    ['normal'] = {
        [0] = '',
        [1] = 'หนึ่ง',
        [2] = 'สอง',
        [3] = 'สาม',
        [4] = 'สี่',
        [5] = 'ห้า',
        [6] = 'หก',
        [7] = 'เจ็ด',
        [8] = 'แปด',
        [9] = 'เก้า',
    },
    [0] = {
        [0] = '',
        [1] = 'เอ็ด',
        [2] = 'สอง',
        [3] = 'สาม',
        [4] = 'สี่',
        [5] = 'ห้า',
        [6] = 'หก',
        [7] = 'เจ็ด',
        [8] = 'แปด',
        [9] = 'เก้า',
        ['value'] = '',
        ['mode'] = 1,
    },
    [1] = {
        [0] = '',
        [1] = ' ',
        [2] = 'ยี่',
        [3] = 'สาม',
        [4] = 'สี่',
        [5] = 'ห้า',
        [6] = 'หก',
        [7] = 'เจ็ด',
        [8] = 'แปด',
        [9] = 'เก้า',
        ['value'] = 'สิบ',
        ['mode'] = 1,
    },
    [2] = {
        ['value'] = 'ร้อย',
        ['mode'] = 0,
    },
    [3] = {
        ['value'] = 'พัน',
        ['mode'] = 0,
    },
    [4] = {
        ['value'] = 'หมื่น',
        ['mode'] = 0,
    },
    [5] = {
        ['value'] = 'แสน',
        ['mode'] = 0,
    },
    [6] = {
        [0] = '',
        [1] = 'เอ็ด',
        [2] = 'สอง',
        [3] = 'สาม',
        [4] = 'สี่',
        [5] = 'ห้า',
        [6] = 'หก',
        [7] = 'เจ็ด',
        [8] = 'แปด',
        [9] = 'เก้า',
        ['value'] = 'ล้าน',
        ['mode'] = 1,
    },
}

local function _toWord(num)
    number = tonumber(num)
    if not number or number < 0 or number == math.huge then
        return
    end
    
    if num == '0' then
        return 'ศูนย์'
    end
    local level = 0
    result = {}
    for i = #num, 1, -1 do
        table.insert(result, data[level]['value'])
        if data[level]['mode'] == 0 then
            table.insert(result, data['normal'][tonumber(num:sub(i, i))])
        else
            table.insert(result, data[level][tonumber(num:sub(i, i))])
        end
        level = level + 1
        if level > 6 then
            level = 1
        end
    end
    
    for i = 1, #result do
        if result[i] == 'เอ็ด' and result[i + 2] == nil then
            result[i] = 'หนึ่ง'
        end
        if i % 2 == 0 and result[i] == '' and (i > 2 and (i - 2) % 12 ~= 0) then
            result[i - 1] = ''
        end
        if result[i] == ' ' then
            result[i] = ''
        end
    end
    
    newresult = {}
    for i = #result, 1, -1 do
        table.insert(newresult, result[i])
    end
    return table.concat(newresult)
end

local function _toNumber(num)
    number = tonumber(num)
    if not number or number < 0 or number == math.huge then
        return
    end
    
    num, _cnt = num:gsub('0', '๐')
                   :gsub('1', '๑')
                   :gsub('2', '๒')
                   :gsub('3', '๓')
                   :gsub('4', '๔')
                   :gsub('5', '๕')
                   :gsub('6', '๖')
                   :gsub('7', '๗')
                   :gsub('8', '๘')
                   :gsub('9', '๙')
    return num
end

local function parseInput(frame)
    local origArgs
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
        for k, v in pairs( frame.args ) do
            origArgs = frame.args
            break
        end
    else
        origArgs = frame
    end
    -- Trim whitespace and remove blank arguments.
    local args = {}
    for k, v in pairs( origArgs ) do
        if type( v ) == 'string' then
            v = mw.text.trim( v )
        end
        if v ~= '' then
            args[k] = v
        end
    end
    return args
end

function p.toNumber( frame )
    -- If called via #invoke, use the args passed into the invoking
    -- template, or the args passed to #invoke if any exist. Otherwise
    -- assume args are being passed directly in from the debug console
    -- or from another Lua module.
    args = parseInput(frame)
    return _toNumber(args[1])
end

function p.toWord( frame )
    -- If called via #invoke, use the args passed into the invoking
    -- template, or the args passed to #invoke if any exist. Otherwise
    -- assume args are being passed directly in from the debug console
    -- or from another Lua module.
    args = parseInput(frame)
    return _toWord(args[1])
end
 
return p