Module:wuu-pron

Definition from Wiktionary, the free dictionary
Jump to: navigation, search
The following documentation is located at Module:wuu-pron/documentation. [edit]
See also: subpages of this module.
Text-x-generic with pencil.svg This module needs documentation.
Please document this module by describing its purpose and usage on the documentation page.
Generates IPA based on romanisation for Wu Chinese.
local export = {}
 
local initial = {
	["p"] = 'p', ["ph"] = 'pʰ', ["b"] = 'b̻', ["m"] = 'm', ["'m"] = 'ʔm', ["f"] = 'f', ["v"] = 'v̻',
	["t"] = 't', ["th"] = 'tʰ', ["d"] = 'd̻', ["n"] = 'n', ["'n"] = 'ʔn', ["l"] = 'l', ["'l"] = 'ʔl',
	["ts"] = 't͡s', ["tsh"] = 't͡sʰ', ["s"] = 's', ["z"] = 'z̻', ["j"] = 't͡ɕ', ["q"] = 't͡ɕʰ',
	["jj"] = 'd̥͡ʑ', ["ny"] = 'ȵ', ["'ny"] = 'ʔȵ', ["x"] = 'ɕ', ["xx"] = 'ʑ̻', ["k"] = 'k',
	["kh"] = 'kʰ', ["g"] = 'g̊', ["ng"] = 'ŋ', ["'ng"] = 'ʔŋ', ["h"] = 'h', ["'"] = 'ʔ', ["hh"] = 'ɦ', [""] = ''
}
 
local final = {
	["a"] = 'ᴀ', ["o"] = 'o', ["au"] = 'ɔ', ["eu"] = 'ɜ', ["e"] = 'ᴇ', ["oe"] = 'ø', ["i"] = 'i',
	["ia"] = 'iᴀ', ["ie"] = 'iᴇ', ["io"] = 'io', ["iau"] = 'iɔ', ["ieu"] = 'iɜ', ["u"] = 'ɯ', ["ua"] = 'ɯᴀ',
	["ue"] = 'ɯᴇ', ["uoe"] = 'ɯø', ["y"] = 'y', ["yoe"] = 'yø', ["an"] = 'ã', ["aan"] = 'ɑ̃',
	["en"] = 'əɲ', ["on"] = 'ʊŋ', ["aq"] = 'ᴀʔ', ["oq"] = 'ʊʔ', ["eq"] = 'əʔ', ["ian"] = 'iã',
	["iaan"] = 'iɑ̃', ["in"] = 'ɪɲ', ["ion"] = 'i̯ʊŋ', ["iaq"] = 'iᴀʔ', ["ioq"] = 'i̯ʊʔ', ["iq"] = 'i̯ɪʔ',
	["uan"] = 'ɯã', ["uaan"] = 'ɯɑ̃', ["un"] = 'ɯə̯ɲ', ["uaq"] = 'ɯᴀʔ', ["ueq"] = 'ɯə̯ʔ', ["yn"] = 'ʏɲ',
	["yq"] = 'ɥ̯ɪʔ', ["er"] = 'əɻ', ["r"] = 'z̩'
}
 
local tone_contours = {
	['1-0'] = '',
	['1-1'] = '⁵³', ['1-2'] = '³⁴', ['1-3'] = '²³', ['1-4'] = '⁵⁵', ['1-5'] = '¹²',
	['2-1'] = '⁵⁵ ²¹', ['2-2'] = '³³ ⁴⁴', ['2-3'] = '²² ⁴⁴', ['2-4'] = '³³ ⁴⁴', ['2-5'] = '¹¹ ²³',
	['3-1'] = '⁵⁵ ³³ ²¹', ['3-2'] = '³³ ⁵⁵ ²¹', ['3-3'] = '²² ⁵⁵ ²¹', ['3-4'] = '³³ ⁵⁵ ²¹', ['3-5'] = '¹¹ ²² ²³',
	['4-1'] = '⁵⁵ ³³ ³³ ²¹', ['4-2'] = '³³ ⁵⁵ ³³ ²¹', ['4-3'] = '²² ⁵⁵ ³³ ²¹', ['4-4'] = '³³ ⁵⁵ ³³ ²¹', ['4-5'] = '²² ⁵⁵ ³³ ²¹'
}
 
local syllabic = {
	['mm'] = 'm̩', ['ngg'] = 'ŋ̍'
}
 
function export.ipa_conv(original_text)
	if type(original_text) == 'table' then original_text = original_text.args[1] end
	local text, conv_text = '',''
	local reading = mw.text.split(original_text, ",", true)
	local syllable = {}
	local syl_tone = {}
	for i = 1, #reading, 1 do
		parts = mw.text.split(reading[i], '+', true)
		for i = 1, #parts do
			text = parts[i]
			wordlen = string.len(text:gsub('[^ ]', '')) + 1
			if mw.ustring.match(mw.ustring.sub(text,1,1), '[0-9]') then
				tone = wordlen .. '-' .. text:sub(1,1)
				tone = tone_contours[tone] or tone
				text = text:sub(2,-1)
			else
				tone = tostring(text:gsub('%l', ''))
				tone = mw.ustring.gsub(tone, '[1-5]', {[1]='¹', [2]='²', [3]='³', [4]='⁴', [5]='⁵'})
				text = text:gsub('%d', '')
			end
			syllable = mw.text.split(text, ' ', true)
			syl_tone = mw.text.split(tone, ' ', true)
			for i = 1, wordlen, 1 do
				syllable[i] = export.ipa_syl_conv(syllable[i])
				syllable[i] = syllable[i] .. syl_tone[i]
			end
			parts[i] = table.concat(syllable, " ")
		end
		reading[i] = table.concat(parts, "  ")
	end
	return table.concat(reading, "/, /")
end
 
function export.ipa_syl_conv(text)
	if text:match('^h?h?[mn][mg]?g?$') then
		text = mw.ustring.gsub(text, "^(h?h?)([mn][mg]?g?)$", function(a, b) return (initial[a] or '') .. (syllabic[b] or error(("Invalid syllable: \"%s\""):format(text))) end)
	elseif text:match("^%'?ny") then
		text = mw.ustring.gsub(text, "^([\']?ny)([aeiouyr][aeou]?[aeu]?[nqr]?)$",
			function(a, b) return initial[a] .. (final[b] or error(("Unrecognised final: \"%s\""):format(b))) end)
	elseif text:match("^[\']?[pbmfvtdnlszjqxkghr%']?[sjgx]?h?[aeiouyr][aeou]?[aeu]?[nqr]?$") then
		text = mw.ustring.gsub(text, "^([\']?[pbmfvtdnlszjqxkghr%']?[sjgx]?[h]?)([aeiouyr][aeou]?[aeu]?[nqr]?)$",
			function(a, b) return
				(initial[a] or error(("Unrecognised initial: \"%s\""):format(a))) ..
				(final[b] or error(("Unrecognised final: \"%s\""):format(b))) end)
	else
		return error(("Invalid syllable: \"%s\""):format(text))
	end
	return text
end
 
function export.rom(text)
	if type(text) == 'table' then text = text.args[1] end
	text = mw.text.split(text, ",", true)
	for i = 1, #text, 1 do
		parts = mw.text.split(text[i], '+', true)
		for i = 1, #parts do
			if string.match(parts[i]:sub(1,1), '[0-9]') then
				parts[i] = parts[i]:sub(2,-1) .. ' (T' .. parts[i]:sub(1,1) .. ')'
			end
		end
		text[i] = table.concat(parts, ' + ')
	end
	return table.concat(text, "; ")
end
 
return export