Module:Transliterate Korean
Appearance
{documentation subpage}}
This module is rated as pre-alpha. It is unfinished, and may or may not be in active development. It should not be used from article namespace pages. Modules remain pre-alpha until the original editor (or someone who takes one over if it is abandoned for some time) is satisfied with the basic structure. |
This template provides automatic transliteration of Korean text into the Revised Romanization (RR) or McCune-Reischauer (MR) systems, with special handling for names.
Automatically determines the appropriate transliteration system and applies any necessary modifications for surnames and given names.
All templates using this support the following parameters:
- text – the Korean text to be transliterated.
- system – the Romanization system to use. Options are "RR" for Revised Romanization or "MR" for McCune-Reischauer. Defaults to "RR".
- name – if set to "true", the function will apply special rules for names, including common modified forms for surnames and hyphenating given names. Defaults to "false".
Examples
[edit]To transliterate the name "이민호" using the Revised Romanization system with name-specific rules:
{{Transliterate Korean | text = 이민호 | system = RR | name = true }}
which produces:
Other Examples:
- Using MR for a non-name text:
{{Transliterate Korean | text = 한글 | system = MR }}
- Using RR without name-specific rules:
{{Transliterate Korean | text = 한국어 | system = RR }}
-- 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