Module:cdo-pron

Definition from Wiktionary, the free dictionary
Jump to: navigation, search

Min Dong pronunciation module.


local export = {}
local gsub = mw.ustring.gsub
local sub = mw.ustring.sub
local len = mw.ustring.len
local match = mw.ustring.match
local lower = mw.ustring.lower

local split_tone = {
	["ă"] = "a".."̆", ["ĕ"] = "e".."̆", ["ĭ"] = "i".."̆", ["ŏ"] = "o".."̆", ["ŭ"] = "u".."̆",
	["ā"] = "a".."̄", ["ē"] = "e".."̄", ["ī"] = "i".."̄", ["ō"] = "o".."̄", ["ū"] = "u".."̄",
	["á"] = "a".."́", ["é"] = "e".."́", ["í"] = "i".."́", ["ó"] = "o".."́", ["ú"] = "u".."́",
	["à"] = "a".."̀", ["è"] = "e".."̀", ["ì"] = "i".."̀", ["ò"] = "o".."̀", ["ù"] = "u".."̀",
	["â"] = "a".."̂", ["ê"] = "e".."̂", ["î"] = "i".."̂", ["ô"] = "o".."̂", ["û"] = "u".."̂",
}

local tone_from_mark = {
	[""] = 1, ["̆"] = 1, ["̆k"] = 1, ["̆h"] = 7,
	["̄"] = 2,
	["́"] = 3,
	["́k"] = "4A", ["́h"] = "4B",
	["̀"] = 5,
	["̂"] = 6
}

local initial_ipa = {
	["b"] = { ["unchanged"] = "p", ["lenited"] = "<sup>(p-)</sup>β", ["nasal"] = "<sup>(p-)</sup>m" },
	["p"] = { ["unchanged"] = "pʰ", ["lenited"] = "<sup>(pʰ-)</sup>β", ["nasal"] = "<sup>(pʰ-)</sup>m" },
	["m"] = { ["unchanged"] = "m", ["lenited"] = "m", ["nasal"] = "m" },
	["d"] = { ["unchanged"] = "t", ["lenited"] = "<sup>(t-)</sup>l", ["nasal"] = "<sup>(t-)</sup>n" },
	["t"] = { ["unchanged"] = "tʰ", ["lenited"] = "<sup>(tʰ-)</sup>l", ["nasal"] = "<sup>(tʰ-)</sup>n" },
	["n"] = { ["unchanged"] = "nˡ", ["lenited"] = "nˡ", ["nasal"] = "nˡ" },
	["l"] = { ["unchanged"] = "l̃", ["lenited"] = "l̃", ["nasal"] = "<sup>(l-)</sup>nˡ" },
	["g"] = { ["unchanged"] = "k", ["lenited"] = "<sup>(k-)</sup>", ["nasal"] = "<sup>(k-)</sup>ŋ" },
	["k"] = { ["unchanged"] = "kʰ", ["lenited"] = "<sup>(kʰ-)</sup>", ["nasal"] = "<sup>(kʰ-)</sup>ŋ" },
	["ng"] = { ["unchanged"] = "ŋ", ["lenited"] = "ŋ", ["nasal"] = "ŋ" },
	["h"] = { ["unchanged"] = "h", ["lenited"] = "<sup>(h-)</sup>", ["nasal"] = "<sup>(h-)</sup>ŋ" },
	["c"] = { ["unchanged"] = "t͡s", ["lenited"] = "<sup>(t͡s-)</sup>ʒ", ["nasal"] = "<sup>(t͡s-)</sup>ʒ" },
	["ch"] = { ["unchanged"] = "t͡sʰ", ["lenited"] = "<sup>(t͡sʰ-)</sup>ʒ", ["nasal"] = "<sup>(t͡sʰ-)</sup>ʒ" },
	["s"] = { ["unchanged"] = "s", ["lenited"] = "<sup>(s-)</sup>l", ["nasal"] = "<sup>(s-)</sup>n" },
	[""] = { ["unchanged"] = "", ["lenited"] = "", ["nasal"] = "<sup>(Ø-)</sup>ŋ" },
}

local final_ipa = {
	["a"]	= { ["close"] = "a",		["open"] = "ɑ"	}, 
	["ia"]	= { ["close"] = "ia",	["open"] = "iɑ"	}, 
	["ua"]	= { ["close"] = "ua",	["open"] = "uɑ"	}, 
	["a̤"]	= { ["close"] = "ɛ",		["open"] = "ɑ"	}, 
	["ie"]	= { ["close"] = "ie",	["open"] = "iɛ"	}, 
	["o̤"]	= { ["close"] = "o",		["open"] = "ɔ"	}, 
	["io"]	= { ["close"] = "yo",	["open"] = "yɔ"	}, 
	["uo"]	= { ["close"] = "uo",	["open"] = "uɔ"	}, 
	["e̤"]	= { ["close"] = "œ",		["open"] = "ɔ"	}, 
	["ae̤"]	= { ["close"] = "œ",		["open"] = "ɔ"	}, 
	["au"]	= { ["close"] = "au",	["open"] = "ɑu"	}, 
	["iau"]	= { ["close"] = "iau",	["open"] = "iau"	}, 
	["eu"]	= { ["close"] = "ɛu",	["open"] = "ɑu"	}, 
	["aiu"]	= { ["close"] = "ɛu",	["open"] = "ɑu"	}, 
	["ieu"]	= { ["close"] = "ieu",	["open"] = "iɛu"	}, 
	["iu"]	= { ["close"] = "ieu",	["open"] = "iɛu"	}, 
	["eu"]	= { ["close"] = "ieu",	["open"] = "iɛu"	}, 
	["oi"]	= { ["close"] = "øy",	["open"] = "ɔy"	}, 
	["o̤i"]	= { ["close"] = "øy",	["open"] = "ɔy"	}, 
	["ai"]	= { ["close"] = "ai",	["open"] = "ɑi"	}, 
	["uai"]	= { ["close"] = "uai",	["open"] = "uɑi"	}, 
	["uoi"]	= { ["close"] = "uoi",	["open"] = "uɔi"	}, 
	["ui"]	= { ["close"] = "uoi",	["open"] = "uɔi"	}, 
	["oi"]	= { ["close"] = "uoi",	["open"] = "uɔi"	}, 
	["i"]	= { ["close"] = "i",		["open"] = "ɛi"	}, 
	["e"]	= { ["close"] = "i",		["open"] = "ɛi"	}, 
	["u"]	= { ["close"] = "u",		["open"] = "ou"	}, 
	["o"]	= { ["close"] = "u",		["open"] = "ou"	}, 
	["ṳ"]	= { ["close"] = "y",		["open"] = "øy"	}, 
	["e̤ṳ"]	= { ["close"] = "y",		["open"] = "øy"	}, 
	["eh"]	= { ["close"] = "ɛiʔ",	["open"] = "ɛiʔ"	},--see [[茉莉]]
	["ah"]	= { ["close"] = "aʔ",	["open"] = "ɑʔ"	}, 
	["iah"]	= { ["close"] = "iaʔ",	["open"] = "iɑʔ"	}, 
	["uah"]	= { ["close"] = "uaʔ",	["open"] = "uɑʔ"	}, 
	["a̤h"]	= { ["close"] = "ɛʔ",	["open"] = "ɛʔ"	}, 
	["ieh"]	= { ["close"] = "ieʔ",	["open"] = "iɛʔ"	}, 
	["o̤h"]	= { ["close"] = "oʔ",	["open"] = "ɔʔ"	}, 
	["ioh"]	= { ["close"] = "yoʔ",	["open"] = "yɔʔ"	}, 
	["uoh"]	= { ["close"] = "uoʔ",	["open"] = "uɔʔ"	}, 
	["ih"]	= { ["close"] = "iʔ",	["open"] = "iʔ"	}, 
	["e̤h"]	= { ["close"] = "œʔ",	["open"] = "œʔ"	}, 
	["ae̤h"]	= { ["close"] = "øyʔ",	["open"] = "ɔyʔ"	},--see [[茉莉]]
	["ang"]	= { ["close"] = "aŋ",	["open"] = "ɑŋ"	}, 
	["iang"]	= { ["close"] = "iaŋ",	["open"] = "iɑŋ"	}, 
	["uang"]	= { ["close"] = "uaŋ",	["open"] = "uɑŋ"	}, 
	["ieng"]	= { ["close"] = "ieŋ",	["open"] = "iɛŋ"	}, 
	["iong"]	= { ["close"] = "yoŋ",	["open"] = "yɔŋ"	}, 
	["uong"]	= { ["close"] = "uoŋ",	["open"] = "uɔŋ"	}, 
	["ing"]	= { ["close"] = "iŋ",	["open"] = "ɛiŋ"	}, 
	["ung"]	= { ["close"] = "uŋ",	["open"] = "ouŋ"	}, 
	["ong"]	= { ["close"] = "uŋ",	["open"] = "ouŋ"	}, 
	["ṳng"]	= { ["close"] = "yŋ",	["open"] = "øyŋ"	}, 
	["e̤ṳng"]	= { ["close"] = "yŋ",	["open"] = "øyŋ"	}, 
	["eng"]	= { ["close"] = "ɛiŋ",	["open"] = "ɛiŋ"	}, 
	["aing"]	= { ["close"] = "ɛiŋ",	["open"] = "aiŋ"	}, 
	["ong2"]	= { ["close"] = "ouŋ",	["open"] = "ɔuŋ"	}, 
	["aung"]	= { ["close"] = "ouŋ",	["open"] = "ɔuŋ"	}, 
	["e̤ng"]	= { ["close"] = "øyŋ",	["open"] = "ɔyŋ"	}, 
	["ae̤ng"]	= { ["close"] = "øyŋ",	["open"] = "ɔyŋ"	}, 
	["ak"]	= { ["close"] = "aʔ",	["open"] = "ɑʔ"	}, 
	["iak"]	= { ["close"] = "iaʔ",	["open"] = "iɑʔ"	}, 
	["uak"]	= { ["close"] = "uaʔ",	["open"] = "uɑʔ"	}, 
	["iek"]	= { ["close"] = "ieʔ",	["open"] = "iɛʔ"	}, 
	["iok"]	= { ["close"] = "yoʔ",	["open"] = "yɔʔ"	}, 
	["uok"]	= { ["close"] = "uoʔ",	["open"] = "uɔʔ"	}, 
	["ik"]	= { ["close"] = "iʔ",	["open"] = "ɛiʔ"	}, 
	["ek"]	= { ["close"] = "iʔ",	["open"] = "ɛiʔ"	}, 
	["uk"]	= { ["close"] = "uʔ",	["open"] = "ouʔ"	}, 
	["ok"]	= { ["close"] = "uʔ",	["open"] = "ouʔ"	}, 
	["o̤k"]	= { ["close"] = "oʔ",	["open"] = "ɔʔ"	}, --see [[汝各儂]]
	["ṳk"]	= { ["close"] = "yʔ",	["open"] = "øyʔ"	}, 
	["e̤ṳk"]	= { ["close"] = "yʔ",	["open"] = "øyʔ"	}, 
	["aik"]	= { ["close"] = "ɛiʔ",	["open"] = "aiʔ"	}, 
	["ok2"]	= { ["close"] = "ouʔ",	["open"] = "ɔuʔ"	}, 
	["auk"]	= { ["close"] = "ouʔ",	["open"] = "ɔuʔ"	}, 
	["e̤k"]	= { ["close"] = "øyʔ",	["open"] = "ɔyʔ"	}, 
	["ae̤k"]	= { ["close"] = "øyʔ",	["open"] = "ɔyʔ"	},
	["ng"]	= { ["close"] = "ŋ̍",		["open"] = "ŋ̍"	},
}

local tone_ipa = {
	[1] = "⁵⁵", --陰平
	[2] = "³³", --上聲
	[3] = "²¹³", --陰去
	["4A"] = "²⁴", --陰入-甲 (-k)
	["4B"] = "²⁴", --陰入-乙 (-h)
	[5] = "⁵³", --陽平
	[6] = "²⁴²", --陽去
	[7] = "⁵", --陽入
	[8] = "²¹", --半陰去
	[9] = "³⁵", --半陽去
	["-"] = "⁻",
}

local tone_sandhi = {
	["A-I"] = "1", ["A-II"] = "5", ["A-III"] = "5",
	["B-I"] = "8", ["B-II"] = "9", ["B-III"] = "1",
	["C-I"] = "2", ["C-II"] = "2", ["C-III"] = "8",

	["A-A-I"] = "8-1", ["A-A-II"] = "8-5", ["A-A-III"] = "8-5",
	["A-B-I"] = "8-8", ["A-B-II"] = "8-9", ["A-B-III"] = "8-1",
	
	["B-A-I"] = "8-1", ["B-A-II"] = "8-5", ["B-A-III"] = "8-5",
	["B-B-I"] = "8-8", ["B-B-II"] = "8-9", ["B-B-III"] = "8-1",
	
	["C-A-I"] = "8-1", ["C-A-II"] = "8-5", ["C-A-III"] = "8-5",
	["C-B-I"] = "8-8", ["C-B-II"] = "8-9", ["C-B-III"] = "8-1",
	
	["A-C-I"] = "1-1", ["A-C-II"] = "5-2", ["A-C-III"] = "5-8",
	["B-C-I"] = "9-2", ["B-C-II"] = "9-2", ["B-C-III"] = "1-8",
	["C-C-I"] = "2-2", ["C-C-II"] = "2-2", ["C-C-III"] = "8-8",
}

local sylcat = {
	[1] = {
		["1"] = "A", ["3"] = "A", ["4B"] = "A", ["6"] = "A",
		["2"] = "B", ["4A"] = "B",
		["5"] = "C", ["7"] = "C"
	},
	[2] = {
		["1"] = "I", ["5"] = "I", ["7"] = "I",
		["2"] = "II",
		["3"] = "III", ["6"] = "III", ["4A"] = "III", ["4B"] = "III"
	}
}

local neg_assim = {
	["labial"] = "<sup>(ŋ̍-)</sup>m̩",
	["dental"] = "<sup>(ŋ̍-)</sup>n̩",
	["velar"] = "<sup>(ŋ̍-)</sup>ŋ̍",
	["alone"] = "<sup>(ŋ̍-)</sup>ŋ̍/m̩/n̩",
}

local neg_type = {
	["b"] = "labial", ["p"] = "labial", ["m"] = "labial",
	["d"] = "dental", ["t"] = "dental", ["n"] = "dental", ["l"] = "dental", ["s"] = "dental", ["c"] = "dental",
	["✘"] = "alone",
}

local initial_string = "^([bpmdtnlgkhcs]?[gh]?)"

function export.rom(text)
	text = gsub(text, "/", " / ")
	text = gsub(text, "\*([^\* \-]+)", "<sup>→%1</sup>")
	return text
end

function export.sentence(text)
	local sentence = {}
	text = gsub(text, "[,%.%?!]", "")
	for word in mw.text.gsplit(lower(text), " ", true) do
		table.insert(sentence, export.ipa(word))
	end
	return table.concat(sentence, " ")
end

function determ_tone(text)
	tone = gsub(gsub(text, ".", split_tone), "^[^̆̄́̀̂hk]*([̆̄́̀̂]?)[^̆̄́̀̂hk]*([hk]?)$", function(tone_symbol, coda)
		return tone_from_mark[tone_symbol..coda] end)
	return tone
end

function export.ipa(text, feature)
	if type(text) == "table" then
		text = text.args[1]
	end
	text = lower(text)
	local phrase_result = {}
	words = mw.text.split(text, "/")
	for _, word in ipairs(words) do
		local word_result = {}
		parts = mw.text.split(word, " ")
		for _, part in ipairs(parts) do
			local initial, final, tone, tone_conv, ipa, exc = {}, {}, {}, {}, {}, {}
			syllables = mw.text.split(part, "-")
			for index, syllable in ipairs(syllables) do
				local lenition_blocked = false
				syllable = gsub(syllable, "([\{\}])", function(captured_initial)
					lenition_blocked = true
					return "" end)
				if match(syllable, "\*") then
					tone[index] = determ_tone(gsub(gsub(gsub(syllable, "\*[^\*]+$", ""), initial_string, ""), ".", split_tone))
					syllable = gsub(syllable, "[^\*]+\*", "")
					exc[index] = determ_tone(gsub(syllable, initial_string, ""))
				end
				initial[index] = match(syllable, initial_string)
				final[index] = sub(syllable, len(initial[index]) + 1, -1)
				final[index] = gsub(final[index], ".", split_tone)
				tone[index] = exc[index] and tone[index] or determ_tone(final[index])
				final[index] = gsub(final[index], "[̆̄́̀̂]", "")
				if (final[index] == "ong" or final[index] == "ok") and match(tostring(tone[index]), "[1257]") then
					final[index] = final[index] .. "2"
				end
				final[index] = match(initial[index] .. final[index], "[dtnlcs]h?io") and gsub(final[index], "io", "uo") or final[index]
				if (initial[index] .. final[index]) == "ng" then
					initial[index], final[index] = "", "ng"
				end
				if match(tostring(tone[index]), "[346]") and (#syllables == 1 or index == #syllables) then
					final[index] = final_ipa[final[index]]["open"]
				else
					final[index] = final_ipa[final[index]]["close"]
				end
				initial_state = (index == 1 or match(syllables[index-1], "k$") or lenition_blocked)
					and "unchanged" or (match(final[index-1], "[ŋ̍]$") and "nasal" or "lenited")
					
				initial[index] = initial_ipa[initial[index]][initial_state]
			end
			
			if #syllables == 1 or feature == "no_sandhi" then
				tone_conv = tone
			
			elseif #syllables == 2 then
				tone_conv = {
					tone[1].."-"..(tone_sandhi[sylcat[1][exc[1] or tone[1]].."-"..sylcat[2][tone[2]]]),
					tone[2]
				}
		
			elseif #syllables == 3 then
				sandhi = mw.text.split(tone_sandhi[sylcat[1][exc[1] or tone[1]].."-"..
					sylcat[1][exc[2] or tone[2]].."-"..sylcat[2][tone[3]]], "-")
				tone_conv = {
					tone[1].."-"..sandhi[1],
					tone[2].."-"..sandhi[2],
					tone[3]
				}
		
			elseif #syllables == 4 then
				tone_conv = {
					tone[1].."-"..tone_sandhi[sylcat[1][exc[1] or tone[1]].."-"..sylcat[2][tone[2]]],
					tone[2],
					tone[3].."-"..tone_sandhi[sylcat[1][exc[3] or tone[3]].."-"..sylcat[2][tone[4]]],
					tone[4]
				}
				
			end
			
			for index = 1, #syllables do
				if final[index] == "ŋ̍" then
					final[index] = neg_assim[neg_type[sub(syllables[index + 1] or "✘", 1, 1)] or "velar"]
				end
				tone_conv[index] = gsub(tone_conv[index], "([1-9AB]+)\-([1-9AB]+)", function(original, sandhi)
					if original == sandhi then
						return original
					end end)
				tone_conv[index] = gsub(tone_conv[index], "(.[AB]?)", tone_ipa)
				ipa[index] = initial[index] .. final[index] .. tone_conv[index]
			end
			table.insert(word_result, table.concat(ipa, " "))
		end
		table.insert(phrase_result, table.concat(word_result, " "))
	end
	return table.concat(phrase_result, "/, /")
end

return export