Jump to content

Module:Transliterate Korean

From Wikipedia, the free encyclopedia

-- Initialize the module
local p = {}

-- Import the Hangul data module
local data = require 'Module:Hangul/data'

-- Known exceptions for Korean surnames
local surname_exceptions = {
    ["김"] = "Kim",
    ["이"] = "Lee",
    ["박"] = "Park",
    ["최"] = "Choi",
    ["정"] = "Jung",
    ["강"] = "Kang",
    ["조"] = "Cho",
    ["윤"] = "Yoon",
    ["장"] = "Jang",
    ["임"] = "Lim"
}

-- Define a function to get the codepoint of a character
local tocodepoint = mw.ustring.codepoint

-- Function to convert the index of a Hangul syllable into jamo indices
local function syllableIndex2JamoIndices(syllableIndex)
    local lIndex = math.floor(syllableIndex / 588)
    local vIndex = math.floor((syllableIndex % 588) / 28)
    local tIndex = syllableIndex % 28

    return lIndex, vIndex, tIndex
end

-- Function to find the index of a value in an array
local function indexof(arr, val)
    for i, v in ipairs(arr) do
        if v == val then
            return i
        end
    end
    return -1
end

-- Function to convert a Hangul character to RR
local function hangulToRR(char, name)
    local codepoint = tocodepoint(char)
    if 0xAC00 <= codepoint and codepoint <= 0xD7A3 then
        local li, vi, ti = syllableIndex2JamoIndices(codepoint - 0xAC00)
        return data.leads[li] .. data.vowels[vi] .. data.trails[ti]
    end
    return char
end

-- Function to convert a Hangul character to MR
local function hangulToMR(char, name)
    local codepoint = tocodepoint(char)
    if 0xAC00 <= codepoint and codepoint <= 0xD7A3 then
        local li, vi, ti = syllableIndex2JamoIndices(codepoint - 0xAC00)
        return data.leads[li] .. data.vowels[vi] .. data.trails[ti]
    end
    return char
end

-- Function to handle special cases for names
local function handleNames(text, system)
    local transliteration = {}
    local name_parts = mw.text.split(text, "")
    local is_surname = true

    for _, char in ipairs(name_parts) do
        local transliterated

        if is_surname and surname_exceptions[char] then
            transliterated = surname_exceptions[char]
            is_surname = false
        else
            if system == 'RR' then
                transliterated = hangulToRR(char, true)
            elseif system == 'MR' then
                transliterated = hangulToMR(char, true)
            end
            is_surname = false
        end

        table.insert(transliteration, transliterated)
    end

    return table.concat(transliteration, "-")
end

-- Main function to transliterate text
local function transKorean(text, system, isName)
    if isName then
        return handleNames(text, system)
    else
        local transliteration = {}
        for char in mw.ustring.gmatch(text, ".") do
            if system == 'RR' then
                table.insert(transliteration, hangulToRR(char, false))
            elseif system == 'MR' then
                table.insert(transliteration, hangulToMR(char, false))
            end
        end
        return table.concat(transliteration)
    end
end

-- Expose the transliteration function
function p.transKorean(frame)
    local text = frame.args[1] or ""
    local system = frame.args[2] or "RR"
    local isName = frame.args[3] == "true"
    return transKorean(text, system, isName)
end

return p