Module:udm-IPA

From Wiktionary, the free dictionary
Jump to navigation Jump to search

local export = {}

local m_IPA = require("Module:IPA")
local lang = require("Module:languages").getByCode("udm")
local rsub = mw.ustring.gsub
local rlower = mw.ustring.lower
local rlast = require("Module:string/replace last").replace_last

local phon = {
	-- consonants
	["б"]="b",	["в"]="ʋ",	["г"]="ɡ",	["д"]="d",	["ӟ"]="ӟ",  ["ӝ"]="ӝ",
	["ж"]="ʒ",	["з"]="z",	["й"]="j",	["к"]="k",	["л"]="l",	["м"]="m",
	["ӈ"]="ŋ",	["н"]="n",	["п"]="p",	["р"]="r",	["с"]="s",	["т"]="t",
	["ӵ"]="ӵ",  ["ч"]="ч",  ["ф"]="f",  ["х"]="x" , ["ц"]="ц",  ["ч"]="ч",
	["ш"]="ʃ",	["щ"]="ʃː",	["ў"]="w",
	-- vowels
	["а"]="ä",	["е"]="ʲɛ",	["и"]="ʲi",	["о"]="o",	["у"]="u",
	["я"]="ʲä",	["э"]="ɛ",	["ы"]="ɯ",	["ё"]="ʲo", ["ю"]="ʲu",
	["ь"]="ʲ",	["ъ"]="ъ",	["ӥ"]="i",	["ӧ"]="ɘ",	["ө"]="ʌ",
}

local function phonetic(text)
	text = rlower(text)
	-- general phonology
	text = rsub(text, ".", phon)
	-- assimilation
	local i = 0
	text = rsub(text, "b(ʲ?)$", "p%1")
	text = rsub(text, "d(ʲ?)$", "t%1")
	text = rsub(text, "ɡ(ʲ?)$", "k%1")
	text = rsub(text, "ӟ(ʲ?)$", "ч%1")
	text = rsub(text, "ӝ(ʲ?)$", "ӵ%1")
	while i <= 5 do
		text = rsub(text, "b(ʲ?[ptksчӵxfʃ])", "p%1")
		text = rsub(text, "d(ʲ?[ptksчӵxfʃ])", "t%1")
		text = rsub(text, "ɡ(ʲ?[ptksчӵxfʃ])", "k%1")
		text = rsub(text, "z(ʲ?[ptksчӵxfʃ])", "s%1")
		text = rsub(text, "ӟ(ʲ?[ptksчӵxfʃ])", "ч%1")
		text = rsub(text, "ӝ(ʲ?[ptksчӵxfʃ])", "ӵ%1")
		text = rsub(text, "ʒ(ʲ?[ptksчӵxfʃ])", "ʃ%1")
		text = rsub(text, "p(ʲ?[bdɡzӟӝʒ])", "b%1")
		text = rsub(text, "t(ʲ?[bdɡzӟӝʒ])", "d%1")
		text = rsub(text, "k(ʲ?[bdɡzӟӝʒ])", "ɡ%1")
		text = rsub(text, "s(ʲ?[bdɡzӟӝʒ])", "z%1")
		text = rsub(text, "ч(ʲ?[bdɡzӟӝʒ])", "ӟ%1")
		text = rsub(text, "ӵ(ʲ?[bdɡzӟӝʒ])", "ӝ%1")
		text = rsub(text, "ʃ(ʲ?[bdɡzӟӝʒ])", "ʒ%1")
		i = i + 1
	end
	i = 0
	text = rsub(text, "tч", "чː")
	text = rsub(text, "dӟ", "ӟː")
	text = rsub(text, "([nl])([чӟ])", "%1ʲ%2")
	-- palatalisation
	text = rsub(text, "dʲ", "ɟ")
	text = rsub(text, "tʲ", "c")
	text = rsub(text, "zʲ", "ʑ")
	text = rsub(text, "sʲ", "ɕ")
	text = rsub(text, "lʲ", "ʎ")
	text = rsub(text, "nʲ", "ɲ")
	text = rsub(text, "^ʲi", "i")
	text = rsub(text, "^ʲ", "j")
	text = rsub(text, "dɟ", "ɟː")
	text = rsub(text, "tc", "cː")
	text = rsub(text, "zʑ", "ʑː")
	text = rsub(text, "sɕ", "ɕː")
	text = rsub(text, "lʎ", "ʎː")
	text = rsub(text, "nɲ", "ɲː")
	text = rsub(text, "([äɛiouɯɘʌъ])ʲi", "%1i")
	text = rsub(text, "([äɛiouɯɘʌъ])ʲ", "%1j")
	text = rsub(text, "ʲ", "")
	-- stress
	if mw.ustring.find(text, "́") == nil then
		text = rlast(text, "([äɛiouɯɘʌ])", "ˈ%1", 1)
	else
		text = rsub(text, "([äɛiouɯɘʌ])́", "ˈ%1")
	end
	text = rsub(text, "([bʋɡdӟӝʒzjklmnŋprstӵчfxцʃwɟcʑɕʎɲ])ˈ", "ˈ%1")
	text = rsub(text, "([ӟчʃɟcʑɕʎɲ])ːˈ", "%1ˈ%1")
	text = rsub(text, "^([bʋɡdӟӝʒzjklmnŋprstӵчfxцʃwɟcʑɕʎɲ]ː?[bʋɡdӟӝʒzjklmnŋprstӵчfxцʃwɟcʑɕʎɲ]?ː?)ˈ", "ˈ%1")
	-- affricates
	text = rsub(text, "ц", "t͡s")
	text = rsub(text, "ч", "t͡ɕ")
	text = rsub(text, "ӵ", "t͡ʃ̺")
	text = rsub(text, "ӟ", "d͡ʑ")
	text = rsub(text, "ӝ", "d͡ʒ̺")
	-- final adjustments
	text = rsub(text, "ɯ", "ɯ̈")
	text = rsub(text, "ъ", "")
	return text
end

function export.IPA(frame)
	local words = {}
	
	for _, word in ipairs(frame:getParent().args) do
		table.insert(words, word)
	end
	
	if #words == 0 then
		words = {mw.title.getCurrentTitle().text}
	end
	
	local IPA_results = {}
	
	for _, word in ipairs(words) do
		table.insert(IPA_results, { pron = "[" .. phonetic(word) .. "]" })
	end
	
	return m_IPA.format_IPA_full(lang, IPA_results)
end

return export