logo

Module:Hsn-pron是什么意思_Module:Hsn-pron读音|解释_Module:Hsn-pron同义词|反义词

Module:Hsn-pron



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

local initialConv = {
	["b"] = "p", ["p"] = "pʰ", ["m"] = "m", ["f"] = "ɸ", 
	["d"] = "t", ["t"] = "tʰ", ["l"] = "l", 
	["z"] = "t͡s", ["c"] = "t͡sʰ", ["s"] = "s",
	["zh"] = "t͡ʂ", ["ch"] = "t͡ʂʰ", ["sh"] = "ʂ", ["r"] = "ʐ", 
	["j"] = "t͡ɕ", ["q"] = "t͡ɕʰ", ["x"] = "ɕ", ["ny"] = "ȵ",
	["g"] = "k", ["k"] = "kʰ", ["ng"] = "ŋ",
	["h"] = "x", [""] = ""
}

local finalConv = {
	["r"] = "z̩", ["rr"] = "ʐ̩", 
	["i"] = "i", 
	["u"] = "u", 
	["y"] = "y", 
	["a"] = "a̠", ["ia"] = "ia̠", ["ua"] = "ua̠", ["ya"] = "ya̠", 
	["o"] = "o", ["io"] = "io", 
	["e"] = "ɤ̞", ["ue"] = "uɤ̞", 
	["ai"] = "ai", ["uai"] = "uai", ["yai"] = "yai", 
	["ei"] = "e̞i", ["uei"] = "ue̞i", ["yei"] = "ye̞i", 
	["au"] = "ɒu", ["iau"] = "iɒu", 
	["ou"] = "əu", ["iou"] = "iəu", 
	["ie"] = "ie̞", ["ye"] = "ye̞", 
	["onn"] = "õ", 
	["enn"] = "ə̃",
	["ienn"] = "iẽ", ["yenn"] = "yẽ",
	["en"] = "ən", ["un"] = "uən", 
	["in"] = "in", 
	["yn"] = "yn", 
	["an"] = "an", ["ian"] = "iæn", ["uan"] = "uan", ["yan"] = "yæn", 
	["ong"] = "ʊŋ", ["iong"] = "iʊŋ", 
	["m"] = "m̩",
	["n"] = "n̩",
}

local toneConv = {
	["1"] = "³³", ["2"] = "¹³", ["3"] = "⁴¹", ["4"] = "⁴⁵", ["5"] = "²¹", ["6"] = "²⁴", ["7"] = "³",
	["5-ts"] = "²¹⁻¹¹", ["6-high"] = "²⁴⁻⁴⁴", ["6-low"] = "²⁴⁻²²", ["6*"] = "²⁴",
}

function export.ipa(text, style)
	if type(text) == "table" then
		text = text.args[1]
	end
	local syllables, initial, final, tone, ipa, result = {}, {}, {}, {}, {}, {}
	words = mw.text.split(text, "/")
	for _, word in ipairs(words) do
		syllables = mw.text.split(word, " ")
		for index, syllable in ipairs(syllables) do
			initial[index] = match(syllable, "^[bpmfdtlnzcsrjqxgkh]?[hgy]?")
			if match(initial[index], "^.y$") and initial[index] ~= "ny" then
				initial[index] = sub(initial[index], 1, 1)
			end
			initial[index] = initial[index] == "y" and "" or initial[index]
			final[index] = match(sub(syllable, len(initial[index]) + 1, -1), "^[^1-7\*]*")
			if final[index] == "" then
				final[index] = initial[index]
				initial[index] = ""
			end
			tone[index] = match(syllable, "[1-7\*]+$") or "7"
			if style == "new" then
				initial[index] = gsub(initial[index], "([zcs])h", "%1")
				initial[index] = match(final[index], "^i") and gsub(initial[index], "[zcs]$", { ["z"] = "j", ["c"] = "q", ["s"] = "x" }) or initial[index]
				final[index] = gsub(final[index], "(i?ong)", { ["iong"] = "in", ["ong"] = "en" })
			end
			initial[index] = initialConv[initial[index]] or error(("Unrecognised initial: \"%s\""):format(initial[index]))
			final[index] = ((match(initial[index], "[ʂʐ]") or initial[index] == "") and final[index] == "r") and "rr" or final[index]
			final[index] = finalConv[final[index]] or error(("Unrecognised final: \"%s\""):format(final[index]))
			if initial[index] == "l" and ((not match(final[index], "^i") and final[index] ~= "ən" and
				match(final[index], "[ŋ̃n]")) or match(final[index], "^i.*̃")) then
				initial[index] = "l̃"
			end
			if tone[index] == "5" and #syllables > 1 then
				tone[index] = "5-ts"
			elseif tone[index] == "6" then
				if match(tone[index-1] or "", "[126]") then
					tone[index] = "6-high"
				elseif match(tone[index-1] or "", "[345]") then
					tone[index] = "6-low"
				end
			end
			tone[index] = toneConv[tone[index]] or error(("Unrecognised tone: \"%s\""):format(tone[index]))
			ipa[index] = initial[index] .. final[index] .. tone[index]
		end
		table.insert(result, table.concat(ipa, " "))
	end
	return table.concat(result, "/, /")
end

function export.rom(text)
	text = gsub(text, "/", " / ")
	text = gsub(text, '([1-9-]+)', '<sup>%1</sup>')
	return text
end

function export.stylediff(text)
	if match(text, "[zcs]h") or match(text, "[zcs]i") or match(text, "i?ong") then
		return true
	end
	return false
end

return export