local export = {}
local hangul = require('Module:ko-hangul')
local II, VI, FI = hangul.initials, hangul.vowels, hangul.finals
local vowels = {
[VI.A ] = "a",
[VI.AE ] = "ae",
[VI.YA ] = "ya",
[VI.YAE] = "yae",
[VI.EO ] = "eo",
[VI.E ] = "e",
[VI.YEO] = "yeo",
[VI.YE ] = "ye",
[VI.O ] = "o",
[VI.WA ] = "wa",
[VI.WAE] = "wae",
[VI.OE ] = "oe",
[VI.YO ] = "yo",
[VI.U ] = "u",
[VI.WEO] = "wo",
[VI.WE ] = "we",
[VI.WI ] = "wi",
[VI.YU ] = "yu",
[VI.EU ] = "eu",
[VI.YI ] = "ui",
[VI.I ] = "i"
}
local initials = {
[II.KIYEOK ] = "g",
[II.SSANGKIYEOK] = "kk",
[II.NIEUN ] = "n",
[II.TIKEUT ] = "d",
[II.SSANGTIKEUT] = "tt",
[II.RIEUL ] = "r",
[II.MIEUM ] = "m",
[II.PIEUP ] = "b",
[II.SSANGPIEUP ] = "pp",
[II.SIOS ] = "s",
[II.SSANGSIOS ] = "ss",
[II.IEUNG ] = "",
[II.CIEUC ] = "j",
[II.SSANGCIEUC ] = "jj",
[II.CHIEUCH ] = "ch",
[II.KHIEUKH ] = "k",
[II.THIEUTH ] = "t",
[II.PHIEUPH ] = "p",
[II.HIEUH ] = "h"
}
local finals = {
[0 ] = "" ,
[FI.KYEOK ] = "k" ,
[FI.SSANGKIYEOK ] = "k" ,
[FI.KYEOK_SIOS ] = "kt", -- CONJECTURE
[FI.NIEUN ] = "n" ,
[FI.NIEUN_CIEUC ] = "nt", -- CONJECTURE
[FI.NIEUN_HIEUH ] = "nt", -- CONJECTURE
[FI.TIKEUT ] = "t" ,
[FI.RIEUL ] = "l" ,
[FI.RIEUL_KIYEOK ] = "lk", -- CONJECTURE
[FI.RIEUL_MIEUM ] = "lm", -- CONJECTURE
[FI.RIEUL_PIEUP ] = "lp",
[FI.RIEUL_SIOS ] = "lt", -- CONJECTURE
[FI.RIEUL_THIEUTH] = "lt", -- CONJECTURE
[FI.RIEUL_PHIEUPH] = "lp", -- CONJECTURE
[FI.RIEUL_HIEUH ] = "rh", -- CONJECTURE
[FI.MIEUM ] = "m" ,
[FI.PIEUP ] = "p" ,
[FI.PIEUP_SIOS ] = "pt", -- XXX: the consonant combination rules below say "ps", but testcase requires it to be "pt"
[FI.SIOS ] = "t" ,
[FI.SSANGSIOS ] = "t" ,
[FI.IEUNG ] = "ng",
[FI.CIEUC ] = "t" ,
[FI.CHIEUCH ] = "t" ,
[FI.KHIEUKH ] = "k" ,
[FI.THIEUTH ] = "t" ,
[FI.PHIEUPH ] = "p" ,
[FI.HIEUH ] = "t" ,
[FI.KIYEOK_RIEUL ] = "kl" -- CONJECTURE
}
local combinations = {
[FI.KYEOK] = {
[II.IEUNG ] = { "g" , "" },
[II.KIYEOK ] = { "k" , "g" },
[II.NIEUN ] = { "ng", "n" },
[II.TIKEUT ] = { "k" , "d" },
[II.RIEUL ] = { "ng", "n" },
[II.MIEUM ] = { "ng", "m" },
[II.PIEUP ] = { "k" , "b" },
[II.SIOS ] = { "k" , "s" },
[II.CIEUC ] = { "k" , "j" },
[II.CHIEUCH] = { "k" , "ch" },
[II.KHIEUKH] = { "k" , "k" , "-" },
[II.THIEUTH] = { "k" , "t" },
[II.PHIEUPH] = { "k" , "p" },
[II.HIEUH ] = { "" , "kh" } -- kh, k
},
[FI.NIEUN] = {
[II.IEUNG ] = { "n" , "" },
[II.KIYEOK ] = { "n" , "g" , "-" },
[II.NIEUN ] = { "n" , "n" },
[II.TIKEUT ] = { "n" , "d" },
[II.RIEUL ] = { "n" , "n" }, -- ll, nn
[II.MIEUM ] = { "n" , "m" },
[II.PIEUP ] = { "n" , "b" },
[II.SIOS ] = { "n" , "s" },
[II.CIEUC ] = { "n" , "j" },
[II.CHIEUCH] = { "n" , "ch" },
[II.KHIEUKH] = { "n" , "k" },
[II.THIEUTH] = { "n" , "t" },
[II.PHIEUPH] = { "n" , "p" },
[II.HIEUH ] = { "n" , "h" }
},
[FI.TIKEUT] = {
[II.IEUNG ] = { "d" , "" }, -- d, j
[II.KIYEOK ] = { "t" , "g" },
[II.NIEUN ] = { "n" , "n" },
[II.TIKEUT ] = { "t" , "d" },
[II.RIEUL ] = { "n" , "n" },
[II.MIEUM ] = { "n" , "m" },
[II.PIEUP ] = { "t" , "b" },
[II.SIOS ] = { "t" , "s" },
[II.CIEUC ] = { "t" , "j" },
[II.CHIEUCH] = { "t" , "ch" },
[II.KHIEUKH] = { "t" , "k" },
[II.THIEUTH] = { "t" , "t" , "-" },
[II.PHIEUPH] = { "t" , "p" },
[II.HIEUH ] = { "t" , "h" } -- th, t, ch
},
[FI.RIEUL] = {
[II.IEUNG ] = { "r" , "" },
[II.KIYEOK ] = { "l" , "g" },
[II.NIEUN ] = { "l" , "l" }, -- ll, nn
[II.TIKEUT ] = { "l" , "d" },
[II.RIEUL ] = { "l" , "l" },
[II.MIEUM ] = { "l" , "m" },
[II.PIEUP ] = { "l" , "b" },
[II.SIOS ] = { "l" , "s" },
[II.CIEUC ] = { "l" , "j" },
[II.CHIEUCH] = { "l" , "ch" },
[II.KHIEUKH] = { "l" , "k" },
[II.THIEUTH] = { "l" , "t" },
[II.PHIEUPH] = { "l" , "p" },
[II.HIEUH ] = { "l" , "h" }
},
[FI.MIEUM] = {
[II.IEUNG ] = { "m" , "" },
[II.KIYEOK ] = { "m" , "g" },
[II.NIEUN ] = { "m" , "n" },
[II.TIKEUT ] = { "m" , "d" },
[II.RIEUL ] = { "m" , "n" },
[II.MIEUM ] = { "m" , "m" },
[II.PIEUP ] = { "m" , "b" },
[II.SIOS ] = { "m" , "s" },
[II.CIEUC ] = { "m" , "j" },
[II.CHIEUCH] = { "m" , "ch" },
[II.KHIEUKH] = { "m" , "k" },
[II.THIEUTH] = { "m" , "t" },
[II.PHIEUPH] = { "m" , "p" },
[II.HIEUH ] = { "m" , "h" }
},
[FI.PIEUP] = {
[II.IEUNG ] = { "b" , "" },
[II.KIYEOK ] = { "p" , "g" },
[II.NIEUN ] = { "m" , "n" },
[II.TIKEUT ] = { "p" , "d" },
[II.RIEUL ] = { "m" , "n" },
[II.MIEUM ] = { "m" , "m" },
[II.PIEUP ] = { "p" , "b" },
[II.SIOS ] = { "p" , "s" },
[II.CIEUC ] = { "p" , "j" },
[II.CHIEUCH] = { "p" , "ch" },
[II.KHIEUKH] = { "p" , "k" },
[II.THIEUTH] = { "p" , "t" },
[II.PHIEUPH] = { "p" , "p" , "-" },
[II.HIEUH ] = { "p" , "h" } -- ph, p
},
[FI.IEUNG] = {
[II.IEUNG ] = { "ng", "" , "-" },
[II.KIYEOK ] = { "ng", "g" },
[II.NIEUN ] = { "ng", "n" },
[II.TIKEUT ] = { "ng", "d" },
[II.RIEUL ] = { "ng", "n" },
[II.MIEUM ] = { "ng", "m" },
[II.PIEUP ] = { "ng", "b" },
[II.SIOS ] = { "ng", "s" },
[II.CIEUC ] = { "ng", "j" },
[II.CHIEUCH] = { "ng", "ch" },
[II.KHIEUKH] = { "ng", "k" },
[II.THIEUTH] = { "ng", "t" },
[II.PHIEUPH] = { "ng", "p" },
[II.HIEUH ] = { "ng", "h" }
}
}
function export.tr_revised(text, lang, sc, syldelim)
if type(text) == 'table' then
text, syldelim = text.args[1], text.args[4] -- XXX: lang and sc are currently not needed
end
if (sc == 'Hani') or (sc == 'Hant') or (sc == 'Hans') then -- XXX: is Hans ever used for Korean? doubtful, but here it goes.
return -- we have not learned our Hanja :(
end
local result = ''
syldelim = syldelim or '-'
local intersyl = ''
local lfi -- last final jamo index
for char in mw.ustring.gcodepoint(text) do
if hangul.isHangulSyllable(char) then
local ii, vi, fi = hangul.syllableIndex2JamoIndices(char - 0xac00)
local initial = (lfi and finals[lfi] or "") .. ((intersyl ~= '') and intersyl or ((lfi and finals[lfi]) and syldelim or "")) .. initials[ii]
mw.log(lfi, ii, vi, fi)
if combinations[lfi] and combinations[lfi][ii] then
local newf, newi, sylforce = unpack(combinations[lfi][ii])
initial = newf .. ((intersyl ~= '') and intersyl or (syldelim or sylforce or "")) .. newi
end
result = result .. ("%s%s"):format(initial, vowels[vi])
intersyl = ''
lfi = fi
elseif (char == 0x0029) then -- RIGHT PARENTHESIS
intersyl = intersyl .. mw.ustring.char(char)
else
result = result .. (lfi and finals[lfi] or "") .. intersyl .. mw.ustring.char(char)
intersyl = ''
lfi = nil
end
end
result = result .. (lfi and finals[lfi] or "") .. intersyl
if (result == text) then
return -- our transliteration is useless.
end
return result
end
export.tr = export.tr_revised
return export