local export = {}
local m_string_utils = require("Module:string utilities")
local gsub = m_string_utils.gsub
local sub = m_string_utils.sub
local match = m_string_utils.match
local find = m_string_utils.find
local len = m_string_utils.len
local lower = m_string_utils.lower
local toNFD = mw.ustring.toNFD
function export.hrs_to_ipa(text, dialect)
local initial_conv = {
= "p", = "pʰ", = "m", = "f", = "v", = "b",
= "t", = "tʰ", = "n", = "l",
= "k", = "kʰ", = "ŋ", = "h",
= "t͡s", = "t͡sʰ", = "s",
= "t͡s", = "t͡sʰ", = "s",
= "t͡ʃ", = "t͡ʃʰ", = "ʃ", = "ʒ",
= "",
}
local final_conv = {
= "ɨ",
= "i", = "e", = "a", = "o", = "u",
= "ie", = "eu", = "ieu",
= "ia", = "ua",
= "ai", = "iai", = "uai",
= "au", = "iau",
= "io", = "oi", = "ioi",
= "iu", = "ui", = "iui",
= "ue",
= "ɨm", = "im",
= "em", = "iem",
= "am", = "iam",
= "ɨn", = "in",
= "en", = "ien", = "uen",
= "an", = "ian", = "uan",
= "on", = "ion",
= "un", = "iun",
= "aŋ", = "iaŋ", = "uaŋ",
= "oŋ", = "ioŋ",
= "uŋ", = "iuŋ",
= "ə",
= "ɨp", = "ip",
= "ep", = "iep",
= "ap", = "iap",
= "ɨt", = "it",
= "et", = "iet", = "uet",
= "at", = "iat", = "uat",
= "ot", = "iot",
= "ut", = "iut",
= "ak", = "iak", = "uak",
= "ok", = "iok",
= "uk", = "iuk",
= "m̩", = "n̩", = "ŋ̍",
}
local function get_tone(final, tone_mark, dialect)
local mark_to_value = {
= {
= "53",
= "55",
= "24",
= "11",
= "33",
= "5",
= "2",
}
}
local mark = (find(final, "$") and "d" or "") .. tone_mark
return mark_to_value or ""
end
local function get_sandhi(syl_count, i, tone, dialect)
if dialect == "Hailu" then
if i < syl_count then
if tone == "24" then
return "33"
elseif tone == "5" then
return "2"
end
end
end
return ""
end
local sup = {
= "¹", = "²", = "³", = "⁴", = "⁵", = "⁻",
}
local function hrs_check_invalid(text)
if not text then
return nil
end
local common_errors = ""
local error_correction = {
= "ˊ",
= "ˋ",
= "˖",
= "˖",
= "ˆ",
}
local correct = gsub(text, common_errors, error_correction)
if text ~= correct then
error("Invalid Hakka Romanization \"" .. text .. "\": please change it to \"" .. correct .. "\"")
end
end
--check for common errors in input
hrs_check_invalid(text)
local syllables, initial, final, tone, sandhi, ipa = {}, {}, {}, {}, {}, {}
syllables = mw.text.split(text, " ")
for i, syllable in ipairs(syllables) do
syllable = gsub(syllable, ",", "")
--find initial, final, tone
initial = match(syllable, "^(?)") or ""
tone = match(syllable, "()$") or ""
final = sub(syllable, len(initial) + 1, -1 - len(tone))
--convert initial, final, tone
initial = initial_conv] or ""
final = final_conv] or ""
tone = get_tone(final, tone, dialect)
sandhi = get_sandhi(#syllables, i, tone, dialect)
ipa = initial .. final ..
gsub(tone .. (sandhi ~= "" and "-" or "") .. sandhi, "", sup)
end
return gsub(table.concat(ipa, " "), ",", "")
end
return export