ข้ามไปเนื้อหา

มอดูล:Th-utilities

จาก วิกิซอร์ซ

เอกสารการใช้งานสำหรับมอดูลนี้อาจสร้างขึ้นที่ มอดูล:Th-utilities/doc

local export = {}

local sub = mw.ustring.sub
local gsub = mw.ustring.gsub
local len = mw.ustring.len
local find = mw.ustring.find
local match = mw.ustring.match

local thai_digits = {"๐", "๑", "๒", "๓", "๔", "๕", "๖", "๗", "๘", "๙"}
local thai_words = {"ศูนย์", "หนึ่ง", "สอง", "สาม", "สี่", "ห้า", "หก", "เจ็ด", "แปด", "เก้า"}
local thai_words2 = {["."]="จุด",["-"]="ขีด",["+"]="บวก",["−"]="ลบ",["/"]="ทับ"}

-- Converts Arabic numbers (e.g., "123") to Thai numbers (e.g., "๑๒๓")
function export.arabic_to_thai_digits(num)
    local str = tostring(num)
    for i = 0, 9 do
        str = gsub(str, tostring(i), thai_digits[i + 1])
    end
    return str
end

-- Converts Thai numbers (e.g., "๑๒๓") to Arabic numbers (e.g., "123")
function export.thai_to_arabic_digits(str)
    str = tostring(str)
    for i, thai_char in ipairs(thai_digits) do
        str = gsub(str, thai_char, tostring(i - 1))
    end
    return str
end

-- Read Number as Value
function export.number_to_thai_words(num)
    local str = tostring(num)
    str = gsub(str, ",", "") 
    str = export.thai_to_arabic_digits(str)
    
    -- Handle signs
    local prefix = ""
    local first_char = sub(str, 1, 1)
    
    if first_char == "-" then
        prefix = thai_words2["−"] -- Mathematical "ลบ"
        str = sub(str, 2)
    elseif first_char == "+" then
        prefix = thai_words2["+"]
        str = sub(str, 2)
    end
    
    -- Split string into integer and decimal parts
    local intPart, decPart = match(str, "^(%d*)%.?(%d*)$")
    if (not intPart or intPart == "") and (not decPart or decPart == "") then
        return "Invalid input"
    end
    
    -- Strip leading zeros from integer part
    if intPart == "" then intPart = "0" end
    intPart = match(intPart, "^0*(.+)$") or "0"
    
    -- Helper: Process Integer Part
    local function get_integer_words(s)
        if s == "0" then return thai_words[1] end 
        
        local positions = {"", "สิบ", "ร้อย", "พัน", "หมื่น", "แสน"}
        local result = ""
        local length = len(s)
        
        for i = 1, length do
            local d = tonumber(sub(s, i, i))
            local pos = length - i + 1
            local effPos = ((pos - 1) % 6) + 1
            
            if d ~= 0 then
                local word = thai_words[d + 1]
                
                if d == 1 and effPos == 1 and i > 1 then
                    word = "เอ็ด"
                end
                
                if d == 1 and effPos == 2 then
                    word = ""
                end
                
                if d == 2 and effPos == 2 then
                    word = "ยี่"
                end
                
                result = result .. word .. positions[effPos]
            end
            
            if pos > 1 and effPos == 1 then
                result = result .. "ล้าน"
            end
        end
        return result
    end
    
    -- Helper: Process Decimal Part
    local function get_decimal_words(s)
        if not s or s == "" then return "" end
        local result = thai_words2["."]
        for i = 1, len(s) do
            local d = tonumber(sub(s, i, i))
            result = result .. thai_words[d + 1]
        end
        return result
    end
    
    return prefix .. get_integer_words(intPart) .. get_decimal_words(decPart)
end

-- Read Number in Sequence
function export.read_number_in_sequence(num)
    local str = tostring(num)
    str = gsub(str, ",", "") 
    str = export.thai_to_arabic_digits(str)
    
    local has_math_minus = false
    if find(str, "−") then
        has_math_minus = true
        str = gsub(str, "−", "") 
    end
    
    local result = ""
    
    if has_math_minus then
        result = result .. thai_words2["−"]
    end
    
    for i = 1, len(str) do
        local char = sub(str, i, i)
        local d = tonumber(char)
        
        if d then
            result = result .. thai_words[d + 1]
        elseif thai_words2[char] then
            result = result .. thai_words2[char]
        end
    end
    
    return result
end

return export