local export = {}
local m_IPA = require("Module:IPA")
local m_table = require("Module:table")
local lang = require("Module:languages").getByCode("zlw-opl")
local rsub = mw.ustring.gsub
local rlower = mw.ustring.lower
local rfind = mw.ustring.find
local consonants = "bdfɡxjkɫlmnprstvzʒʃɲɕʑʲ"
local C = ""
local vowels = "aiɨuɛɔæ̃ɑ̃"
local V = ""
local voiceless_consonant = "ptsʃɕkx"
local voiced_consonant = "bdzʒʑɡ"
local function track(page)
require("Module:debug").track("zlw-opl-IPA/" .. page)
return true
end
local function rsub_repeatedly(term, foo, bar)
while true do
local new_term = rsub(term, foo, bar)
if new_term == term then
return term
end
term = new_term
end
end
local digraphs = {
= "x",
= "tʃ",
= "bʲ",
= "R",
= "ʃ"
}
local devoicing = {
= "p",
= "t",
= "s",
= "ʃ",
= "ɕ",
= "k",
= "f"
}
local voicing = {
= "b",
= "d",
= "z",
= "ʒ",
= "ʑ",
= "ɡ",
= "v"
}
local function flatmap(items, fun)
local new = {}
for _, item in ipairs(items) do
local results = fun(item)
for _, result in ipairs(results) do
table.insert(new, result)
end
end
return new
end
local phon = {
= "a",
= "ɑ̃",
= "b",
= "ts",
= "tɕ",
= "d",
= "ɛ",
= "æ̃",
= "f",
= "ɡ",
= "x",
= "i",
= "j",
= "k",
= "lʲ",
= "ɫ",
= "m",
= "mʲ",
= "n",
= "ɲ",
= "ɔ",
= "ɔː",
= "p",
= "pʲ",
= "r",
= "s",
= "ɕ",
= "t",
= "u",
= "v",
= "vʲ",
= "ɨ",
= "z",
= "ʒ",
= "ʑ",
= "ː",
= "(ː)"
}
function export.phonemic(text, post)
text = rlower(text)
text = rsub(text, " | ", "# | #")
text = "##" .. rsub(text, " ", "# #") .. "##"
-- basic phonology
text = rsub(text, "ts", "tss")
for digraph, replacement in pairs(digraphs) do
text = rsub(text, digraph, replacement)
end
text = rsub(text, "r̝", "R")
text = rsub(text, ".", phon)
text = rsub(text, "si(" .. V .. ")", "ɕ%1")
text = rsub(text, "zi(" .. V .. ")", "ʑ%1")
text = rsub(text, "si", "ɕi")
text = rsub(text, "zi", "ʑi")
-- palatalisation
text = rsub(text, "(" .. C .. ")i(" .. V .. ")", "%1j%2")
text = rsub(text, "Ri(" .. V .. ")", "Rj%1")
text = rsub(text, "ni(" .. C .. ")", "ɲi%1")
text = rsub(text, "nʲ", "ɲ")
text = rsub(text, "nj", "ɲ")
text = rsub(text, "sʲ", "ɕ")
text = rsub(text, "zʲ", "ʑ")
text = rsub(text, "jʲ", "j")
local function voice_backward(sound, following)
return voicing .. following
end
local function devoice_backward(sound, following)
return devoicing .. following
end
local function voice_forward(preceding, sound)
return preceding .. voicing
end
local function devoice_forward(preceding, sound)
return preceding .. devoicing
end
local function final_devoicing(sound, following)
return devoicing .. following .. "#"
end
text = rsub_repeatedly(text, "()(?)#", final_devoicing)
text = rsub_repeatedly(text, "v()", "f%1")
text = rsub_repeatedly(text, "f()", "v%1")
text = rsub_repeatedly(text, "()()", voice_forward)
text = rsub_repeatedly(text, "()()", devoice_forward)
text = rsub_repeatedly(text, "()()", devoice_backward)
text = rsub_repeatedly(text, "()()", voice_backward)
-- rzy
text = rsub(text, "Rɨ", "Ri")
text = rsub(text, "(" .. V .. ")'", "'%1")
text = rsub(text, "#(" .. C .. "+ʲ?)'", "#'%1")
text = rsub(text, " (" .. C .. "+ʲ?)'", " '%1")
text = rsub(text, "(" .. C .. "ʲ?)'", "'%1")
text = rsub(text, "t's", "'ts")
text = rsub(text, "'", "ˈ")
text = rsub(text, "-", "");
-- affricates
text = rsub(text, "", "%1ʲ")
-- affricates
text = rsub(text, "t()", "t͡%1")
text = rsub(text, "d()", "d͡%1")
-- suffixes
text = rsub(text, "^ˈ%-", "-")
-- resolution
text = rsub(text, "jʲ", "j")
text = rsub(text, "ʃʲ#", "ʃ#")
text = rsub(text, "R", "r̝")
local variants = {text}
local function flatmap_and_sub_pre(from, to1, to2)
variants =
flatmap(
variants,
function(item)
if rfind(item, from) then
local retval = {rsub_repeatedly(item, from, to1)}
if to2 then
m_table.insertIfNot(retval, rsub_repeatedly(item, from, to2))
end
return retval
else
return {item}
end
end
)
end
if post ~= nil then
flatmap_and_sub_pre("iː", "i")
flatmap_and_sub_pre("ɨː", "ɨ")
flatmap_and_sub_pre("uː", "u")
flatmap_and_sub_pre("ɔː", "o")
flatmap_and_sub_pre("ɛː", "e")
flatmap_and_sub_pre("aː", "ɒ")
flatmap_and_sub_pre("i%(ː%)", "i")
flatmap_and_sub_pre("ɨ%(ː%)", "ɨ")
flatmap_and_sub_pre("u%(ː%)", "u")
flatmap_and_sub_pre("a%(ː%)", "a", "ɒ")
flatmap_and_sub_pre("ɔ%(ː%)", "ɔ", "o")
flatmap_and_sub_pre("ɛ%(ː%)", "ɛ", "e")
end
flatmap_and_sub_pre("#", "")
return variants
end
function export.IPA(frame)
local terms = {}
local args = frame:getParent().args
local currentTitle = mw.title.getCurrentTitle().text
for _, term in ipairs(args) do
if term == currentTitle then track("redundant") end
table.insert(terms, term)
end
if #terms == 0 then
terms = {currentTitle}
end
local results1, results2 = {}, {}
for _, term in ipairs(terms) do
local variantsPre = export.phonemic(term)
for _, variant in ipairs(variantsPre) do
table.insert(results1, {pron = "/" .. variant .. "/"})
end
local variantsPost = export.phonemic(term, true)
for _, variant in ipairs(variantsPost) do
table.insert(results2, {pron = "/" .. variant .. "/"})
end
end
results1.qualifiers = {"10<sup>th</sup>–15<sup>th</sup> CE"}
results2.qualifiers = {"15<sup>th</sup> CE"}
return "*" .. m_IPA.format_IPA_full { lang = lang, items = results1 }
.. "\n*" .. m_IPA.format_IPA_full { lang = lang, items = results2 }
end
return export