Jump to content

Module:Transliterate Korean

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Nonabelian (talk | contribs) at 20:22, 31 July 2024 (Created page with '-- 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 lo...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

-- 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 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.rr.leads[li + 1] .. data.rr.vowels[vi + 1] .. data.rr.trails[ti + 1]
    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.mr.leads[li + 1] .. data.mr.vowels[vi + 1] .. data.mr.trails[ti + 1]
    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 transliterate(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.transliterate(frame)
    local text = frame.args[1] or ""
    local system = frame.args[2] or "RR"
    local isName = frame.args[3] == "true"
    return transliterate(text, system, isName)
end

return p