-- Initialize the modulelocal p =
-- Import the Hangul data modulelocal data = require 'Module:Hangul/data'
-- Known exceptions for Korean surnameslocal surname_exceptions =
-- Define a function to get the codepoint of a characterlocal tocodepoint = mw.ustring.codepoint
-- Function to convert the index of a Hangul syllable into jamo indiceslocal function syllableIndex2JamoIndices(syllableIndex) local lIndex = math.floor(syllableIndex / 588) local vIndex = math.floor((syllableIndex % 588) / 28) local tIndex = syllableIndex % 28
return lIndex, vIndex, tIndexend
-- Function to find the index of a value in an arraylocal function indexof(arr, val) for i, v in ipairs(arr) do if v
-- Function to convert a Hangul character to RRlocal 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 charend
-- Function to convert a Hangul character to MRlocal 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 charend
-- Function to handle special cases for nameslocal 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
'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 textlocal 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
'MR' then table.insert(transliteration, hangulToMR(char, false)) end end return table.concat(transliteration) endend
-- Expose the transliteration functionfunction p.transKorean(frame) local text = frame.args[1] or "" local system = frame.args[2] or "RR" local isName = frame.args[3]
return p