Jump to content

Module:Th-rm: Difference between revisions

From The Petit Planet Wiki
ReisuDesign (talk | contribs)
No edit summary
ReisuDesign (talk | contribs)
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


local U = mw.ustring
local arguments = require('Module:Arguments')
local gsub = U.gsub
local tt = require('Module:Tt')
local match = U.match
local sub = U.sub
local len = U.len


local INITIAL = {
local AUTO_TOOLTIP = 'This romanization was generated automatically. Please add a user-curated romanization as soon as possible.'
 
local THAI = '[ก-๛]'
 
-- Initial consonants.
local consonants = {
['ก'] = 'k',
['ก'] = 'k',
['ข'] = 'kh', ['ฃ'] = 'kh', ['ค'] = 'kh', ['ฅ'] = 'kh', ['ฆ'] = 'kh',
['ข'] = 'kh',
['ฃ'] = 'kh',
['ค'] = 'kh',
['ฅ'] = 'kh',
['ฆ'] = 'kh',
['ง'] = 'ng',
['ง'] = 'ng',
['จ'] = 'ch',
['จ'] = 'ch',
['ฉ'] = 'ch', ['ช'] = 'ch',
['ฉ'] = 'ch',
['ช'] = 'ch',
['ซ'] = 's',
['ซ'] = 's',
['ฌ'] = 'ch',
['ฌ'] = 'ch',
Line 18: Line 25:
['ฎ'] = 'd',
['ฎ'] = 'd',
['ฏ'] = 't',
['ฏ'] = 't',
['ฐ'] = 'th', ['ฑ'] = 'th', ['ฒ'] = 'th',
['ฐ'] = 'th',
['ฑ'] = 'th',
['ฒ'] = 'th',
['ณ'] = 'n',
['ณ'] = 'n',
['ด'] = 'd',
['ด'] = 'd',
['ต'] = 't',
['ต'] = 't',
['ถ'] = 'th', ['ท'] = 'th', ['ธ'] = 'th',
['ถ'] = 'th',
['ท'] = 'th',
['ธ'] = 'th',
['น'] = 'n',
['น'] = 'n',
['บ'] = 'b',
['บ'] = 'b',
Line 36: Line 47:
['ล'] = 'l',
['ล'] = 'l',
['ว'] = 'w',
['ว'] = 'w',
['ศ'] = 's', ['ษ'] = 's', ['ส'] = 's',
['ศ'] = 's',
['ษ'] = 's',
['ส'] = 's',
['ห'] = 'h',
['ห'] = 'h',
['ฬ'] = 'l',
['ฬ'] = 'l',
['ฮ'] = 'h'
['ฮ'] = 'h',
['อ'] = ''
}
}


local FINAL = {
-- Final consonants.
local finals = {
['ก'] = 'k',
['ก'] = 'k',
['ข'] = 'k', ['ฃ'] = 'k', ['ค'] = 'k', ['ฅ'] = 'k', ['ฆ'] = 'k',
['ข'] = 'k',
['ง'] = 'ng',
['ฃ'] = 'k',
['ค'] = 'k',
['ฅ'] = 'k',
['ฆ'] = 'k',
 
['จ'] = 't',
['จ'] = 't',
['ฉ'] = 't', ['ช'] = 't', ['ซ'] = 't',
['ฉ'] = 't',
['ฌ'] = '',
['ช'] = 't',
['ญ'] = 'n',
['ซ'] = 't',
['ฎ'] = 't', ['ฏ'] = 't',
['ฌ'] = 't',
['ฐ'] = 't', ['ฑ'] = 't', ['ฒ'] = 't',
['ฎ'] = 't',
['ณ'] = 'n',
['ฏ'] = 't',
['ฐ'] = 't',
['ฑ'] = 't',
['ฒ'] = 't',
['ด'] = 't',
['ด'] = 't',
['ต'] = 't',
['ต'] = 't',
['ถ'] = 't', ['ท'] = 't', ['ธ'] = 't',
['ถ'] = 't',
['น'] = 'n',
['ท'] = 't',
['ธ'] = 't',
['ศ'] = 't',
['ษ'] = 't',
['ส'] = 't',
 
['บ'] = 'p',
['บ'] = 'p',
['ป'] = 'p',
['ป'] = 'p',
['ผ'] = '', ['ฝ'] = '',
['ผ'] = 'p',
['พ'] = 'p', ['ฟ'] = 'p', ['ภ'] = 'p',
['ฝ'] = 'p',
['ม'] = 'm',
['พ'] = 'p',
['ย'] = '',
['ฟ'] = 'p',
['ภ'] = 'p',
 
['ง'] = 'ng',
 
['ญ'] = 'n',
['ณ'] = 'n',
['น'] = 'n',
['ร'] = 'n',
['ร'] = 'n',
['ล'] = 'n',
['ล'] = 'n',
['ว'] = '',
['ศ'] = 't', ['ษ'] = 't', ['ส'] = 't',
['ห'] = '',
['ฬ'] = 'n',
['ฬ'] = 'n',
['ฮ'] = ''
 
['ม'] = 'm'
}
}


local CONSONANTS = 'กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรลวศษสหฬฮ'
-- Vowels.
local vowels = {
['เียะ'] = 'ia',
['เีย'] = 'ia',
['เือะ'] = 'uea',
['เือ'] = 'uea',
['เือย'] = 'ueai',
['เียว'] = 'iao',
 
['ัวะ'] = 'ua',
['ัว'] = 'ua',
 
['เิ็ย'] = 'oei',
['เิย'] = 'oei',
['เอย'] = 'oei',
 
['็อย'] = 'oi',
['อย'] = 'oi',
 
['ุย'] = 'ui',
['วย'] = 'uai',
 
['ัย'] = 'ai',
['ไย'] = 'ai',
['าย'] = 'ai',
['ใ'] = 'ai',
['ไ'] = 'ai',
 
['เ็ว'] = 'eo',
['แ็ว'] = 'aeo',
['เา'] = 'ao',
['าว'] = 'ao',
['แว'] = 'aeo',
['เว'] = 'eo',
 
['ิว'] = 'io',
['ีว'] = 'io',
 
['ำ'] = 'am',
 
['ือ'] = 'ue',
['ื'] = 'ue',
['ึ'] = 'ue',
 
['ิ'] = 'i',
['ี'] = 'i',
 
['ุ'] = 'u',
['ู'] = 'u',
 
['เ็'] = 'e',
['เ'] = 'e',
 
['แ็'] = 'ae',
['แ'] = 'ae',
 
['โะ'] = 'o',
['โ'] = 'o',
 
['เาะ'] = 'o',
['อ'] = 'o',
 
['เอะ'] = 'oe',
['เอ'] = 'oe',
['เะ'] = 'e',
 
['ะ'] = 'a',
['ั'] = 'a',
['า'] = 'a'
}
 
-- Longest vowel forms must be checked first.
local vowel_order = {
'เือย',
'เียว',
'เือะ',
'เียะ',
'เีย',
'เือ',
'ัวะ',
'ัว',
'เิ็ย',
'เิย',
'็อย',
'เ็ว',
'แ็ว',
'เอย',
'เา',
'าว',
'เว',
'แว',
'าย',
'ไย',
'ัย',
'ุย',
'วย',
'อย',
'ำ',
'ิว',
'ีว',
'ิ',
'ี',
'ึ',
'ื',
'ุ',
'ู',
'เ็',
'แ็',
'เาะ',
'เอะ',
'เอ',
'เะ',
'ะ',
'ั',
'า',
'เ',
'แ',
'โ',
'อ',
'ใ',
'ไ'
}
 
local consonant_set = {}
 
for c in pairs(consonants) do
consonant_set[c] = true
end
 
-- Thai consonant clusters.
local valid_clusters = {
['กร'] = true,
['กล'] = true,
['กว'] = true,
['ขร'] = true,
['ขล'] = true,
['ขว'] = true,
['คร'] = true,
['คล'] = true,
['คว'] = true,
['ฆร'] = true,
['ฆล'] = true,
['จร'] = true,
['ตร'] = true,
['ปร'] = true,
['ปล'] = true,
['พร'] = true,
['พล'] = true,
['ผล'] = true,
['ทร'] = true,
['ธร'] = true,
['ศร'] = true,
['สร'] = true,
['สล'] = true,
['สว'] = true,
['ซร'] = true,
['ฝร'] = true,
['ฟร'] = true
}
 
-- ห is silent in these clusters when followed by a vowel.
local h_silent_clusters = {
['หง'] = true,
['หน'] = true,
['หม'] = true,
['หย'] = true,
['หร'] = true,
['หล'] = true,
['หว'] = true,
['หญ'] = true
}
 
-- ร is silent in these clusters when followed by a vowel.
local r_silent_clusters = {
['จร'] = true,
['ซร'] = true,
['ศร'] = true,
['สร'] = true
}
 
local function chars(text)
local result = {}
 
for c in mw.text.gsplit(text, '') do
table.insert(result, c)
end
 
return result
end


local function isConsonant(c)
local function isConsonant(c)
return c and match(c, '[' .. CONSONANTS .. ']') ~= nil
return c and consonant_set[c] == true
end
end


local function isThai(c)
local function isThai(c)
return c and match(c, '[ก-๛]') ~= nil
return c and mw.ustring.find(c, THAI) ~= nil
end
end


local function isVowelMark(c)
local function isFrontVowel(c)
return c and match(c, '[ะาิีึืุูัำเแโใไอ]') ~= nil
return c == 'เ' or c == 'แ' or c == 'โ' or c == 'ใ' or c == 'ไ'
end
end


local function clean(str)
local function skipMarks(c, pos)
str = mw.text.unstrip(str or '')
while c[pos] and (
str = gsub(str, '<br%s*/?>', ' ')
c[pos] == '็' or
str = gsub(str, '<[^>]+>', '')
c[pos] == '่' or
return str
c[pos] == '้' or
c[pos] == '๊' or
c[pos] == '๋' or
c[pos] == 'ํ' or
c[pos] == '๎'
) do
pos = pos + 1
end
 
return pos
end
end


local function removeMarks(str)
local function removeSilent(chars_)
-- Tone marks are not represented in RTGS.
local result = {}
str = gsub(str, '[่้๊๋]', '')
local i = 1


-- Mai thanthakhat makes the marked consonant silent.
while chars_[i] do
str = gsub(str, '([ก-ฮ])์', '')
if chars_[i] == '์' then
-- ์ makes the following consonant(s) silent.
i = i + 1


return str
while chars_[i] and (
isConsonant(chars_[i]) or
chars_[i] == '็' or
chars_[i] == '่' or
chars_[i] == '้' or
chars_[i] == '๊' or
chars_[i] == '๋'
) do
i = i + 1
end
else
table.insert(result, chars_[i])
i = i + 1
end
end
 
return result
end
end


local function getVowel(str, pos)
local function getVowel(c, pos)
local s = sub(str, pos, -1)
local remaining = table.concat(c, '', pos)


-- Longest / most specific patterns first.
for _, form in ipairs(vowel_order) do
local patterns = {
local length = mw.ustring.len(form)
{ 'เ', 'ื', 'อ', 'ย', 'ueai' },
{ 'เ', 'ี', 'ย', 'ว', 'iao' },
{ 'เ', 'ี', 'ย', 'ะ', 'ia' },
{ 'เ', 'ี', 'ย', '', 'ia' },
{ 'เ', 'ื', 'อ', 'ะ', 'uea' },
{ 'เ', 'ื', 'อ', '', 'uea' },


{ '', 'ั', 'ว', 'ะ', 'ua' },
if mw.ustring.sub(remaining, 1, length) == form then
{ '', 'ั', 'ว', '', 'ua' },
return form, vowels[form]
end
end


{ 'แ', '็', 'ว', '', 'aeo' },
return nil, nil
{ 'แ', 'ว', '', '', 'aeo' },
end
{ 'เ', '็', 'ว', '', 'eo' },
{ 'เ', 'ว', '', '', 'eo' },


{ '', 'ิ', 'ว', '', 'io' },
local function getInitial(c, pos)
{ '', 'ุ', 'ย', '', 'ui' },
local c1 = c[pos]
{ '', 'ว', 'ย', '', 'uai' },


{ 'เ', 'ื', 'อ', '', 'ue' },
if not isConsonant(c1) then
{ 'เ', 'ื', '', '', 'ue' },
return '', pos
end


{ 'เ', 'า', '', '', 'ao' },
local c2 = c[pos + 1]
{ '', 'า', 'ว', '', 'ao' },


{ 'โ', 'ย', '', '', 'oi' },
-- ห is not pronounced in หง หน หม หย หร หล หว หญ.
{ '', 'อ', 'ย', '', 'oi' },
if c1 == 'ห' and c2 and h_silent_clusters[c1 .. c2] then
return consonants[c2], pos + 2, true
end


{ 'เ', 'ย', '', '', 'oei' },
-- ทร is pronounced s in the relevant Sanskrit-derived words.
-- This is the broad automatic fallback; exceptions can be manually
-- supplied with th_rm.
if c1 == 'ท' and c2 == 'ร' then
return 's', pos + 2, false
end


{ 'ใ', '', '', '', 'ai' },
if c2 and valid_clusters[c1 .. c2] then
{ 'ไ', '', '', '', 'ai' },
-- ร is silent in these clusters.
{ '', 'ั', 'ย', '', 'ai' },
if r_silent_clusters[c1 .. c2] then
{ '', 'า', 'ย', '', 'ai' },
return consonants[c1], pos + 2, false
end


{ 'เ', 'าะ', '', '', 'o' },
return consonants[c1] .. consonants[c2], pos + 2, false
{ 'โ', 'ะ', '', '', 'o' },
end
{ 'โ', '', '', '', 'o' },
{ '', 'อ', '', '', 'o' },


{ 'เ', 'อะ', '', '', 'oe' },
return consonants[c1], pos + 1, false
{ 'เ', 'ิ', '', '', 'oe' },
end
{ 'เ', 'อ', '', '', 'oe' },


{ 'แ', 'ะ', '', '', 'ae' },
local function getCoda(c, pos)
{ 'แ', '', '', '', 'ae' },
local current = c[pos]


{ 'เ', 'ะ', '', '', 'e' },
if not current or not isConsonant(current) then
{ 'เ', '็', '', '', 'e' },
return '', pos
{ 'เ', '', '', '', 'e' },
end


{ '', 'ำ', '', '', 'am' },
-- These do not act as ordinary codas here.
if current == 'อ' or current == 'ห' or current == 'ฮ' then
return '', pos
end


{ '', 'ะ', '', '', 'a' },
-- ย and ว are generally part of the vowel/diphthong.
{ '', 'ั', '', '', 'a' },
if current == 'ย' or current == 'ว' then
{ '', 'า', '', '', 'a' },
return '', pos
end


{ '', 'ิ', '', '', 'i' },
if finals[current] then
{ '', 'ี', '', '', 'i' },
return finals[current], pos + 1
end


{ '', 'ึ', '', '', 'ue' },
return '', pos + 1
{ '', 'ื', '', '', 'ue' },
end


{ '', 'ุ', '', '', 'u' },
local function startsNextSyllable(c, pos)
{ '', 'ู', '', '', 'u' }
if not c[pos] or not isConsonant(c[pos]) then
}
return false
end


for _, v in ipairs(patterns) do
if isFrontVowel(c[pos + 1]) then
local a = v[1] or ''
return true
local b = v[2] or ''
end
local c = v[3] or ''
local d = v[4] or ''
local roman = v[5] or ''


local pattern = a .. b .. c .. d
local _, vowel = getVowel(c, pos + 1)


if pattern ~= '' and sub(s, 1, len(pattern)) == pattern then
return vowel ~= nil
return roman, len(pattern)
end
end
 
-- Implied a.
return 'a', 0
end
end


local function romanizeSyllable(str, start)
local function parseSyllable(c, start)
local pos = start
local pos = start
local n = len(str)
 
if not c[pos] then
return nil, pos
end


local initial = ''
local initial = ''
local initialRom = ''
local cluster = false


-- Preposed vowels are written before the initial consonant.
-- Preposed vowels.
local pre = sub(str, pos, pos)
local front = ''


if pre == 'เ' or pre == 'แ' or pre == 'โ' or pre == 'ใ' or pre == 'ไ' then
if isFrontVowel(c[pos]) then
front = c[pos]
pos = pos + 1
pos = pos + 1
end
end


local first = sub(str, pos, pos)
local initialPos = pos
 
initial, pos, _, cluster =
getInitial(c, pos)


if not isConsonant(first) then
if initial == '' and not isConsonant(c[initialPos]) then
return first, 1
return nil, start + 1
end
end


initial = first
local vowel
initialRom = INITIAL[first] or ''
local vowelForm
pos = pos + 1
 
if front ~= '' then
-- Handle the vowel forms which contain a preposed vowel.
if front == 'เ' and c[pos] == 'ี' and c[pos + 1] == 'ย' then
vowel = 'ia'
vowelForm = 'เีย'
pos = pos + 2
 
elseif front == 'เ' and c[pos] == 'ื' and c[pos + 1] == 'อ' then
vowel = 'uea'
vowelForm = 'เือ'
pos = pos + 2
 
elseif front == 'เ' and c[pos] == 'อ' then
vowel = 'oe'
vowelForm = 'เอ'
pos = pos + 1
 
elseif front == 'เ' and c[pos] == 'า' then
vowel = 'ao'
vowelForm = 'เา'
pos = pos + 1
 
elseif front == 'เ' and c[pos] == '็' then
vowel = 'e'
vowelForm = 'เ็'
pos = pos + 1
 
elseif front == 'แ' and c[pos] == '็' then
vowel = 'ae'
vowelForm = 'แ็'
pos = pos + 1
 
elseif front == 'โ' and c[pos] == 'ะ' then
vowel = 'o'
vowelForm = 'โะ'
pos = pos + 1
 
elseif front == 'ใ' or front == 'ไ' then
vowel = 'ai'
vowelForm = front
 
else
vowel = vowels[front]
vowelForm = front
end
else
vowelForm, vowel = getVowel(c, pos)


-- ห + sonorant consonant: ห is not romanized.
if vowelForm then
local second = sub(str, pos, pos)
pos = pos + mw.ustring.len(vowelForm)
end
end


if initial == 'ห' and second and match(second, '[งนมยรลวญณฬ]') then
-- Implied vowels.
initial = second
if not vowel then
initialRom = INITIAL[second]
if c[pos] and isConsonant(c[pos]) then
pos = pos + 1
if startsNextSyllable(c, pos) then
vowel = 'a'
else
vowel = 'o'
end
else
vowel = 'a'
end
end
end


-- จร-, ซร-, ศร-, สร-: ร is not romanized.
pos = skipMarks(c, pos)
second = sub(str, pos, pos)
 
-- Final consonant.
local coda = ''
local codaPos = pos


if second == 'ร'
if c[pos] and isConsonant(c[pos]) then
and (initial == 'จ' or initial == 'ซ' or initial == 'ศ' or initial == 'ส') then
-- If another consonant begins the next syllable, don't consume
pos = pos + 1
-- it as a coda.
if startsNextSyllable(c, pos) then
coda = ''
else
coda, codaPos = getCoda(c, pos)
end
end
end


-- Read the vowel.
pos = codaPos
local vowelPos
pos = skipMarks(c, pos)


if pre == 'เ' or pre == 'แ' or pre == 'โ' or pre == 'ใ' or pre == 'ไ' then
return {
vowelPos = start
initial = initial,
else
vowel = vowel,
vowelPos = pos
coda = coda,
next = pos
}, pos
end
 
local function shouldHyphen(previous, current)
if not previous or not current then
return false
end
end


local vowel, vowelLen = getVowel(str, vowelPos)
-- Before a succeeding syllable beginning with a vowel.
if current.initial == '' and current.vowel ~= '' then
return true
end


-- Advance over the vowel.
-- Before ng when the preceding syllable ends in a vowel.
if vowelLen > 0 then
if previous.coda == '' and current.initial == 'ng' then
pos = vowelPos + vowelLen
return true
end
end


-- If there was a preposed vowel, getVowel consumed from its position,
return false
-- so the resulting position already includes it.
end
if vowelLen == 0 and (pre == 'เ' or pre == 'แ' or pre == 'โ' or pre == 'ใ' or pre == 'ไ') then
 
pos = start + 1
local function romanizeWord(word)
end
local c = chars(word)
c = removeSilent(c)
 
local syllables = {}
local pos = 1
local safety = 0


-- Final consonant.
while pos <= #c and safety < 1000 do
local final = sub(str, pos, pos)
safety = safety + 1
local finalRom = ''


if isConsonant(final) then
if isConsonant(c[pos]) or isFrontVowel(c[pos]) then
local after = sub(str, pos + 1, pos + 1)
local syllable, nextPos = parseSyllable(c, pos)


-- If another vowel begins immediately afterward, this consonant
if syllable then
-- belongs to the following syllable.
table.insert(syllables, syllable)
if not isVowelMark(after) and not (after == 'เ' or after == 'แ' or after == 'โ' or after == 'ใ' or after == 'ไ') then
finalRom = FINAL[final] or ''
pos = pos + 1


-- Final -ร after another final consonant.
if nextPos <= pos then
if final == 'ร' then
pos = pos + 1
finalRom = 'n'
else
pos = nextPos
end
else
-- Fail safely rather than looping forever.
pos = pos + 1
end
end
else
-- Thai punctuation/marks that aren't part of a syllable.
pos = pos + 1
end
end
end
end


-- ทร- exception.
local output = {}
if initial == 'ท' and sub(str, pos, pos) == 'ร' then
initialRom = 's'
pos = pos + 1
end


-- -รร-
for i, syllable in ipairs(syllables) do
-- With a final consonant, รร = a.
if shouldHyphen(syllables[i - 1], syllable) then
-- Without a final consonant, รร = an.
table.insert(output, '-')
if sub(str, vowelPos, vowelPos + 1) == 'รร' then
if finalRom ~= '' then
vowel = 'a'
else
vowel = 'an'
end
end
table.insert(
output,
(syllable.initial or '') ..
(syllable.vowel or '') ..
(syllable.coda or '')
)
end
end


return initialRom .. vowel .. finalRom, math.max(1, pos - start)
return table.concat(output)
end
end


local function romanizeWord(word)
-- Parse the text while treating <> and <-> as hard boundaries.
--
-- <>  = literal space
-- <-> = literal hyphen
--
-- Because each Thai section is romanized independently, automatic
-- hyphenation can never cross one of these markers.
local function romanizeThaiText(text)
local result = {}
local result = {}
local pos = 1
local pos = 1
local n = len(word)
local length = mw.ustring.len(text)


while pos <= n do
while pos <= length do
local c = sub(word, pos, pos)
-- Manual hyphen.
if mw.ustring.sub(text, pos, pos + 2) == '<->' then
table.insert(result, '-')
pos = pos + 3


if c == '<' then
-- Manual space.
local close = mw.ustring.find(word, '>', pos + 1, true)
elseif mw.ustring.sub(text, pos, pos + 1) == '<>' then
table.insert(result, ' ')
pos = pos + 2


if close then
-- Thai run.
local marker = sub(word, pos, close)
elseif isThai(mw.ustring.sub(text, pos, pos)) then
local start = pos


if marker == '< >' then
while pos <= length do
table.insert(result, ' ')
local character = mw.ustring.sub(text, pos, pos)
elseif marker == '<->' then
 
table.insert(result, '-')
if not isThai(character) then
else
break
table.insert(result, marker)
end
 
-- Stop before a manual control marker.
if mw.ustring.sub(text, pos, pos + 2) == '<->' then
break
end
 
if mw.ustring.sub(text, pos, pos + 1) == '<>' then
break
end
end


pos = close + 1
else
table.insert(result, c)
pos = pos + 1
pos = pos + 1
end
end
elseif isThai(c) then
 
local roman, consumed = romanizeSyllable(word, pos)
local word = mw.ustring.sub(text, start, pos - 1)
table.insert(result, roman)
 
pos = pos + consumed
table.insert(result, romanizeWord(word))
 
-- Everything else is preserved.
else
else
table.insert(result, c)
table.insert(
result,
mw.ustring.sub(text, pos, pos)
)
 
pos = pos + 1
pos = pos + 1
end
end
Line 336: Line 690:
end
end


local function capitalizeWords(str)
local function convertThaiDigits(text)
local chars = mw.text.split(str, '')
local digits = {
local capitalize = true
['๐'] = '0',
['๑'] = '1',
['๒'] = '2',
['๓'] = '3',
['๔'] = '4',
['๕'] = '5',
['๖'] = '6',
['๗'] = '7',
['๘'] = '8',
['๙'] = '9'
}


for i, c in ipairs(chars) do
for thai, latin in pairs(digits) do
if match(c, '[A-Za-z]') then
text = mw.ustring.gsub(text, thai, latin)
if capitalize then
end
chars[i] = U.upper(c)
 
capitalize = false
return text
end
end
elseif c == ' ' or c == '-' then
 
capitalize = true
local function hasThai(text)
return mw.ustring.find(text, THAI) ~= nil
end
 
local function validateRomanization(text)
-- Never allow Thai characters to leak into the final result.
if hasThai(text) then
return false
end
 
return true
end
 
local function capitalizeWords(text)
return mw.ustring.gsub(
text,
'(%a)([%a]*)',
function(first, rest)
return mw.ustring.upper(first) .. rest
end
end
)
end
local function capitalizeSentence(text)
local prefix, first = mw.ustring.match(
text,
'^([%s%p]*)(%a)'
)
if not first then
return text
end
end


return table.concat(chars)
return prefix ..
mw.ustring.upper(first) ..
mw.ustring.sub(text, mw.ustring.len(prefix) + 2)
end
end


local function sentenceCase(str)
function p.romanize(text)
local chars = mw.text.split(str, '')
if not text or mw.text.trim(text) == '' then
local capitalize = true
return ''
end
 
-- Do not automatically romanize Latin-only text.
if not hasThai(text) then
return ''
end
 
text = convertThaiDigits(text)
 
local result = romanizeThaiText(text)


for i, c in ipairs(chars) do
result = mw.ustring.gsub(result, '%s+', ' ')
if match(c, '[A-Za-z]') then
result = mw.text.trim(result)
if capitalize then
 
chars[i] = U.upper(c)
if not validateRomanization(result) then
capitalize = false
return ''
end
elseif match(c, '[.!?]') then
capitalize = true
end
end
end


return table.concat(chars)
return result
end
end


function p.main(frame)
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
local args = arguments.getArgs(frame, {
parentFirst = true,
parentFirst = true,
wrappers = {'Template:Th-rm'}
wrappers = {'Template:Th-rm'}
})
})


if not args[1] or args[1] == '' then
local text = args[1]
 
if not text or mw.text.trim(text) == '' then
return ''
return ''
end
end


local str = clean(args[1])
if not hasThai(text) then
 
-- Nothing to romanize.
if not match(str, '[ก-๛]') then
return ''
return ''
end
end


str = removeMarks(str)
local result = p.romanize(text)


local result = romanizeWord(str)
if result == '' then
return ''
end


if args.capi then
if args.capi then
result = capitalizeWords(result)
result = capitalizeWords(result)
elseif args.sent then
result = sentenceCase(result)
end
end


return result
if args.sent then
result = capitalizeSentence(result)
end
 
-- Automatically generated romanizations get a Tt warning.
return tt._main({
[1] = result,
[2] = AUTO_TOOLTIP,
['class'] = args.class
})
end
end


return p
return p

Latest revision as of 18:00, 2 September 2026

Module documentation

This module implements {{th-rm}}.

local p = {}

local arguments = require('Module:Arguments')
local tt = require('Module:Tt')

local AUTO_TOOLTIP = 'This romanization was generated automatically. Please add a user-curated romanization as soon as possible.'

local THAI = '[ก-๛]'

-- Initial consonants.
local consonants = {
	['ก'] = 'k',
	['ข'] = 'kh',
	['ฃ'] = 'kh',
	['ค'] = 'kh',
	['ฅ'] = 'kh',
	['ฆ'] = 'kh',
	['ง'] = 'ng',
	['จ'] = 'ch',
	['ฉ'] = 'ch',
	['ช'] = 'ch',
	['ซ'] = 's',
	['ฌ'] = 'ch',
	['ญ'] = 'y',
	['ฎ'] = 'd',
	['ฏ'] = 't',
	['ฐ'] = 'th',
	['ฑ'] = 'th',
	['ฒ'] = 'th',
	['ณ'] = 'n',
	['ด'] = 'd',
	['ต'] = 't',
	['ถ'] = 'th',
	['ท'] = 'th',
	['ธ'] = 'th',
	['น'] = 'n',
	['บ'] = 'b',
	['ป'] = 'p',
	['ผ'] = 'ph',
	['ฝ'] = 'f',
	['พ'] = 'ph',
	['ฟ'] = 'f',
	['ภ'] = 'ph',
	['ม'] = 'm',
	['ย'] = 'y',
	['ร'] = 'r',
	['ล'] = 'l',
	['ว'] = 'w',
	['ศ'] = 's',
	['ษ'] = 's',
	['ส'] = 's',
	['ห'] = 'h',
	['ฬ'] = 'l',
	['ฮ'] = 'h',
	['อ'] = ''
}

-- Final consonants.
local finals = {
	['ก'] = 'k',
	['ข'] = 'k',
	['ฃ'] = 'k',
	['ค'] = 'k',
	['ฅ'] = 'k',
	['ฆ'] = 'k',

	['จ'] = 't',
	['ฉ'] = 't',
	['ช'] = 't',
	['ซ'] = 't',
	['ฌ'] = 't',
	['ฎ'] = 't',
	['ฏ'] = 't',
	['ฐ'] = 't',
	['ฑ'] = 't',
	['ฒ'] = 't',
	['ด'] = 't',
	['ต'] = 't',
	['ถ'] = 't',
	['ท'] = 't',
	['ธ'] = 't',
	['ศ'] = 't',
	['ษ'] = 't',
	['ส'] = 't',

	['บ'] = 'p',
	['ป'] = 'p',
	['ผ'] = 'p',
	['ฝ'] = 'p',
	['พ'] = 'p',
	['ฟ'] = 'p',
	['ภ'] = 'p',

	['ง'] = 'ng',

	['ญ'] = 'n',
	['ณ'] = 'n',
	['น'] = 'n',
	['ร'] = 'n',
	['ล'] = 'n',
	['ฬ'] = 'n',

	['ม'] = 'm'
}

-- Vowels.
local vowels = {
	['เียะ'] = 'ia',
	['เีย'] = 'ia',
	['เือะ'] = 'uea',
	['เือ'] = 'uea',
	['เือย'] = 'ueai',
	['เียว'] = 'iao',

	['ัวะ'] = 'ua',
	['ัว'] = 'ua',

	['เิ็ย'] = 'oei',
	['เิย'] = 'oei',
	['เอย'] = 'oei',

	['็อย'] = 'oi',
	['อย'] = 'oi',

	['ุย'] = 'ui',
	['วย'] = 'uai',

	['ัย'] = 'ai',
	['ไย'] = 'ai',
	['าย'] = 'ai',
	['ใ'] = 'ai',
	['ไ'] = 'ai',

	['เ็ว'] = 'eo',
	['แ็ว'] = 'aeo',
	['เา'] = 'ao',
	['าว'] = 'ao',
	['แว'] = 'aeo',
	['เว'] = 'eo',

	['ิว'] = 'io',
	['ีว'] = 'io',

	['ำ'] = 'am',

	['ือ'] = 'ue',
	['ื'] = 'ue',
	['ึ'] = 'ue',

	['ิ'] = 'i',
	['ี'] = 'i',

	['ุ'] = 'u',
	['ู'] = 'u',

	['เ็'] = 'e',
	['เ'] = 'e',

	['แ็'] = 'ae',
	['แ'] = 'ae',

	['โะ'] = 'o',
	['โ'] = 'o',

	['เาะ'] = 'o',
	['อ'] = 'o',

	['เอะ'] = 'oe',
	['เอ'] = 'oe',
	['เะ'] = 'e',

	['ะ'] = 'a',
	['ั'] = 'a',
	['า'] = 'a'
}

-- Longest vowel forms must be checked first.
local vowel_order = {
	'เือย',
	'เียว',
	'เือะ',
	'เียะ',
	'เีย',
	'เือ',
	'ัวะ',
	'ัว',
	'เิ็ย',
	'เิย',
	'็อย',
	'เ็ว',
	'แ็ว',
	'เอย',
	'เา',
	'าว',
	'เว',
	'แว',
	'าย',
	'ไย',
	'ัย',
	'ุย',
	'วย',
	'อย',
	'ำ',
	'ิว',
	'ีว',
	'ิ',
	'ี',
	'ึ',
	'ื',
	'ุ',
	'ู',
	'เ็',
	'แ็',
	'เาะ',
	'เอะ',
	'เอ',
	'เะ',
	'ะ',
	'ั',
	'า',
	'เ',
	'แ',
	'โ',
	'อ',
	'ใ',
	'ไ'
}

local consonant_set = {}

for c in pairs(consonants) do
	consonant_set[c] = true
end

-- Thai consonant clusters.
local valid_clusters = {
	['กร'] = true,
	['กล'] = true,
	['กว'] = true,
	['ขร'] = true,
	['ขล'] = true,
	['ขว'] = true,
	['คร'] = true,
	['คล'] = true,
	['คว'] = true,
	['ฆร'] = true,
	['ฆล'] = true,
	['จร'] = true,
	['ตร'] = true,
	['ปร'] = true,
	['ปล'] = true,
	['พร'] = true,
	['พล'] = true,
	['ผล'] = true,
	['ทร'] = true,
	['ธร'] = true,
	['ศร'] = true,
	['สร'] = true,
	['สล'] = true,
	['สว'] = true,
	['ซร'] = true,
	['ฝร'] = true,
	['ฟร'] = true
}

-- ห is silent in these clusters when followed by a vowel.
local h_silent_clusters = {
	['หง'] = true,
	['หน'] = true,
	['หม'] = true,
	['หย'] = true,
	['หร'] = true,
	['หล'] = true,
	['หว'] = true,
	['หญ'] = true
}

-- ร is silent in these clusters when followed by a vowel.
local r_silent_clusters = {
	['จร'] = true,
	['ซร'] = true,
	['ศร'] = true,
	['สร'] = true
}

local function chars(text)
	local result = {}

	for c in mw.text.gsplit(text, '') do
		table.insert(result, c)
	end

	return result
end

local function isConsonant(c)
	return c and consonant_set[c] == true
end

local function isThai(c)
	return c and mw.ustring.find(c, THAI) ~= nil
end

local function isFrontVowel(c)
	return c == 'เ' or c == 'แ' or c == 'โ' or c == 'ใ' or c == 'ไ'
end

local function skipMarks(c, pos)
	while c[pos] and (
		c[pos] == '็' or
		c[pos] == '่' or
		c[pos] == '้' or
		c[pos] == '๊' or
		c[pos] == '๋' or
		c[pos] == 'ํ' or
		c[pos] == '๎'
	) do
		pos = pos + 1
	end

	return pos
end

local function removeSilent(chars_)
	local result = {}
	local i = 1

	while chars_[i] do
		if chars_[i] == '์' then
			-- ์ makes the following consonant(s) silent.
			i = i + 1

			while chars_[i] and (
				isConsonant(chars_[i]) or
				chars_[i] == '็' or
				chars_[i] == '่' or
				chars_[i] == '้' or
				chars_[i] == '๊' or
				chars_[i] == '๋'
			) do
				i = i + 1
			end
		else
			table.insert(result, chars_[i])
			i = i + 1
		end
	end

	return result
end

local function getVowel(c, pos)
	local remaining = table.concat(c, '', pos)

	for _, form in ipairs(vowel_order) do
		local length = mw.ustring.len(form)

		if mw.ustring.sub(remaining, 1, length) == form then
			return form, vowels[form]
		end
	end

	return nil, nil
end

local function getInitial(c, pos)
	local c1 = c[pos]

	if not isConsonant(c1) then
		return '', pos
	end

	local c2 = c[pos + 1]

	-- ห is not pronounced in หง หน หม หย หร หล หว หญ.
	if c1 == 'ห' and c2 and h_silent_clusters[c1 .. c2] then
		return consonants[c2], pos + 2, true
	end

	-- ทร is pronounced s in the relevant Sanskrit-derived words.
	-- This is the broad automatic fallback; exceptions can be manually
	-- supplied with th_rm.
	if c1 == 'ท' and c2 == 'ร' then
		return 's', pos + 2, false
	end

	if c2 and valid_clusters[c1 .. c2] then
		-- ร is silent in these clusters.
		if r_silent_clusters[c1 .. c2] then
			return consonants[c1], pos + 2, false
		end

		return consonants[c1] .. consonants[c2], pos + 2, false
	end

	return consonants[c1], pos + 1, false
end

local function getCoda(c, pos)
	local current = c[pos]

	if not current or not isConsonant(current) then
		return '', pos
	end

	-- These do not act as ordinary codas here.
	if current == 'อ' or current == 'ห' or current == 'ฮ' then
		return '', pos
	end

	-- ย and ว are generally part of the vowel/diphthong.
	if current == 'ย' or current == 'ว' then
		return '', pos
	end

	if finals[current] then
		return finals[current], pos + 1
	end

	return '', pos + 1
end

local function startsNextSyllable(c, pos)
	if not c[pos] or not isConsonant(c[pos]) then
		return false
	end

	if isFrontVowel(c[pos + 1]) then
		return true
	end

	local _, vowel = getVowel(c, pos + 1)

	return vowel ~= nil
end

local function parseSyllable(c, start)
	local pos = start

	if not c[pos] then
		return nil, pos
	end

	local initial = ''
	local cluster = false

	-- Preposed vowels.
	local front = ''

	if isFrontVowel(c[pos]) then
		front = c[pos]
		pos = pos + 1
	end

	local initialPos = pos

	initial, pos, _, cluster =
		getInitial(c, pos)

	if initial == '' and not isConsonant(c[initialPos]) then
		return nil, start + 1
	end

	local vowel
	local vowelForm

	if front ~= '' then
		-- Handle the vowel forms which contain a preposed vowel.
		if front == 'เ' and c[pos] == 'ี' and c[pos + 1] == 'ย' then
			vowel = 'ia'
			vowelForm = 'เีย'
			pos = pos + 2

		elseif front == 'เ' and c[pos] == 'ื' and c[pos + 1] == 'อ' then
			vowel = 'uea'
			vowelForm = 'เือ'
			pos = pos + 2

		elseif front == 'เ' and c[pos] == 'อ' then
			vowel = 'oe'
			vowelForm = 'เอ'
			pos = pos + 1

		elseif front == 'เ' and c[pos] == 'า' then
			vowel = 'ao'
			vowelForm = 'เา'
			pos = pos + 1

		elseif front == 'เ' and c[pos] == '็' then
			vowel = 'e'
			vowelForm = 'เ็'
			pos = pos + 1

		elseif front == 'แ' and c[pos] == '็' then
			vowel = 'ae'
			vowelForm = 'แ็'
			pos = pos + 1

		elseif front == 'โ' and c[pos] == 'ะ' then
			vowel = 'o'
			vowelForm = 'โะ'
			pos = pos + 1

		elseif front == 'ใ' or front == 'ไ' then
			vowel = 'ai'
			vowelForm = front

		else
			vowel = vowels[front]
			vowelForm = front
		end
	else
		vowelForm, vowel = getVowel(c, pos)

		if vowelForm then
			pos = pos + mw.ustring.len(vowelForm)
		end
	end

	-- Implied vowels.
	if not vowel then
		if c[pos] and isConsonant(c[pos]) then
			if startsNextSyllable(c, pos) then
				vowel = 'a'
			else
				vowel = 'o'
			end
		else
			vowel = 'a'
		end
	end

	pos = skipMarks(c, pos)

	-- Final consonant.
	local coda = ''
	local codaPos = pos

	if c[pos] and isConsonant(c[pos]) then
		-- If another consonant begins the next syllable, don't consume
		-- it as a coda.
		if startsNextSyllable(c, pos) then
			coda = ''
		else
			coda, codaPos = getCoda(c, pos)
		end
	end

	pos = codaPos
	pos = skipMarks(c, pos)

	return {
		initial = initial,
		vowel = vowel,
		coda = coda,
		next = pos
	}, pos
end

local function shouldHyphen(previous, current)
	if not previous or not current then
		return false
	end

	-- Before a succeeding syllable beginning with a vowel.
	if current.initial == '' and current.vowel ~= '' then
		return true
	end

	-- Before ng when the preceding syllable ends in a vowel.
	if previous.coda == '' and current.initial == 'ng' then
		return true
	end

	return false
end

local function romanizeWord(word)
	local c = chars(word)
	c = removeSilent(c)

	local syllables = {}
	local pos = 1
	local safety = 0

	while pos <= #c and safety < 1000 do
		safety = safety + 1

		if isConsonant(c[pos]) or isFrontVowel(c[pos]) then
			local syllable, nextPos = parseSyllable(c, pos)

			if syllable then
				table.insert(syllables, syllable)

				if nextPos <= pos then
					pos = pos + 1
				else
					pos = nextPos
				end
			else
				-- Fail safely rather than looping forever.
				pos = pos + 1
			end
		else
			-- Thai punctuation/marks that aren't part of a syllable.
			pos = pos + 1
		end
	end

	local output = {}

	for i, syllable in ipairs(syllables) do
		if shouldHyphen(syllables[i - 1], syllable) then
			table.insert(output, '-')
		end

		table.insert(
			output,
			(syllable.initial or '') ..
			(syllable.vowel or '') ..
			(syllable.coda or '')
		)
	end

	return table.concat(output)
end

-- Parse the text while treating <> and <-> as hard boundaries.
--
-- <>  = literal space
-- <-> = literal hyphen
--
-- Because each Thai section is romanized independently, automatic
-- hyphenation can never cross one of these markers.
local function romanizeThaiText(text)
	local result = {}
	local pos = 1
	local length = mw.ustring.len(text)

	while pos <= length do
		-- Manual hyphen.
		if mw.ustring.sub(text, pos, pos + 2) == '<->' then
			table.insert(result, '-')
			pos = pos + 3

		-- Manual space.
		elseif mw.ustring.sub(text, pos, pos + 1) == '<>' then
			table.insert(result, ' ')
			pos = pos + 2

		-- Thai run.
		elseif isThai(mw.ustring.sub(text, pos, pos)) then
			local start = pos

			while pos <= length do
				local character = mw.ustring.sub(text, pos, pos)

				if not isThai(character) then
					break
				end

				-- Stop before a manual control marker.
				if mw.ustring.sub(text, pos, pos + 2) == '<->' then
					break
				end

				if mw.ustring.sub(text, pos, pos + 1) == '<>' then
					break
				end

				pos = pos + 1
			end

			local word = mw.ustring.sub(text, start, pos - 1)

			table.insert(result, romanizeWord(word))

		-- Everything else is preserved.
		else
			table.insert(
				result,
				mw.ustring.sub(text, pos, pos)
			)

			pos = pos + 1
		end
	end

	return table.concat(result)
end

local function convertThaiDigits(text)
	local digits = {
		['๐'] = '0',
		['๑'] = '1',
		['๒'] = '2',
		['๓'] = '3',
		['๔'] = '4',
		['๕'] = '5',
		['๖'] = '6',
		['๗'] = '7',
		['๘'] = '8',
		['๙'] = '9'
	}

	for thai, latin in pairs(digits) do
		text = mw.ustring.gsub(text, thai, latin)
	end

	return text
end

local function hasThai(text)
	return mw.ustring.find(text, THAI) ~= nil
end

local function validateRomanization(text)
	-- Never allow Thai characters to leak into the final result.
	if hasThai(text) then
		return false
	end

	return true
end

local function capitalizeWords(text)
	return mw.ustring.gsub(
		text,
		'(%a)([%a]*)',
		function(first, rest)
			return mw.ustring.upper(first) .. rest
		end
	)
end

local function capitalizeSentence(text)
	local prefix, first = mw.ustring.match(
		text,
		'^([%s%p]*)(%a)'
	)

	if not first then
		return text
	end

	return prefix ..
		mw.ustring.upper(first) ..
		mw.ustring.sub(text, mw.ustring.len(prefix) + 2)
end

function p.romanize(text)
	if not text or mw.text.trim(text) == '' then
		return ''
	end

	-- Do not automatically romanize Latin-only text.
	if not hasThai(text) then
		return ''
	end

	text = convertThaiDigits(text)

	local result = romanizeThaiText(text)

	result = mw.ustring.gsub(result, '%s+', ' ')
	result = mw.text.trim(result)

	if not validateRomanization(result) then
		return ''
	end

	return result
end

function p.main(frame)
	local args = arguments.getArgs(frame, {
		parentFirst = true,
		wrappers = {'Template:Th-rm'}
	})

	local text = args[1]

	if not text or mw.text.trim(text) == '' then
		return ''
	end

	if not hasThai(text) then
		return ''
	end

	local result = p.romanize(text)

	if result == '' then
		return ''
	end

	if args.capi then
		result = capitalizeWords(result)
	end

	if args.sent then
		result = capitalizeSentence(result)
	end

	-- Automatically generated romanizations get a Tt warning.
	return tt._main({
		[1] = result,
		[2] = AUTO_TOOLTIP,
		['class'] = args.class
	})
end

return p