Module:cmn-pron

Definition from Wiktionary, the free dictionary
Jump to: navigation, search
The following documentation is located at Module:cmn-pron/documentation. [edit]

This module does work for Template:cmn-pron and Template:zh-pron.

Homophone data stored at Module:zh/data/cmn-hom.

Testcases[edit]

Module:cmn-pron/testcases


local export = {}
 
local gsub = mw.ustring.gsub
local len = mw.ustring.len
local match = mw.ustring.match
local sub = mw.ustring.sub
 
local m_zh = require("Module:zh")
local m_zh_data = mw.loadData("Module:zh/data")
local hom_data = mw.loadData("Module:zh/data/cmn-hom")
 
local py_detone = {
	['ā'] = 'a', ['á'] = 'a', ['ǎ'] = 'a', ['à'] = 'a', 
	['ō'] = 'o', ['ó'] = 'o', ['ǒ'] = 'o', ['ò'] = 'o', 
	['ē'] = 'e', ['é'] = 'e', ['ě'] = 'e', ['è'] = 'e', 
	['ī'] = 'i', ['í'] = 'i', ['ǐ'] = 'i', ['ì'] = 'i', 
	['ū'] = 'u', ['ú'] = 'u', ['ǔ'] = 'u', ['ù'] = 'u', 
	['ǖ'] = 'ü', ['ǘ'] = 'ü', ['ǚ'] = 'ü', ['ǜ'] = 'ü',
	['ḿ'] = 'm', ['ń'] = 'n', ['ň'] = 'n', ['ǹ'] = 'n',
}
 
local py_tone = {
	['ā'] = '1', ['ō'] = '1', ['ē'] = '1', ['ī'] = '1', ['ū'] = '1', ['ǖ'] = '1', 
	['á'] = '2', ['ó'] = '2', ['é'] = '2', ['í'] = '2', ['ú'] = '2', ['ǘ'] = '2', 
	['ǎ'] = '3', ['ǒ'] = '3', ['ě'] = '3', ['ǐ'] = '3', ['ǔ'] = '3', ['ǚ'] = '3', 
	['à'] = '4', ['ò'] = '4', ['è'] = '4', ['ì'] = '4', ['ù'] = '4', ['ǜ'] = '4',
	['ḿ'] = '2', ['ń'] = '2', ['ň'] = '3', ['ǹ'] = '4',
}
 
function export.py_transform(text, detone, not_spaced)
	if type(text) == 'table' then text, detone, not_spaced = text.args[1], text.args[2], text.args[3] end
	if match(text, '​') then
		error("Pinyin contains the hidden character: ​ (U+200B). Please remove that character from the text.")
	end
	if detone == '' then detone = nil end
	if not_spaced == '' then not_spaced = nil end
	if match(mw.ustring.lower(text), '[āōēīūǖáóéíúǘǎǒěǐǔǚàòèìùǜ][aeiou]?[āōēīūǖáóéíúǘǎǒěǐǔǚàòèìùǜ]') and not not_spaced then
		error(("Missing apostrophe before null-initial syllable - should be \"%s\" instead."):format(gsub(text, '([āōēīūǖáóéíúǘǎǒěǐǔǚàòèìùǜ][aeiou]?)([āōēīūǖáóéíúǘǎǒěǐǔǚàòèìùǜ])', "%1'%2"))) end
	text = mw.ustring.lower(text)
	if not mw.ustring.match(text, '[āōēīūǖáóéíúǘǎǒěǐǔǚàòèìùǜḿńňǹ]') and text:match('[1-5]') then
		return gsub(text, '(%d)(%l)', '%1 %2')
	end
	if match(text, '[一不,.?]') then
		text = gsub(text, '一 ', ' yī ')
		text = gsub(text, '([一不])$', {['一'] = ' yī', ['不'] = ' bù'})
		text = gsub(text, '([一不])', ' %1 ')
		text = gsub(text, '([,.?])', ' %1 ')
		text = gsub(text, ' +', ' ')
		text = gsub(text, '^ ', '')
		text = gsub(text, ' $', '')
		text = gsub(text, '%. %. %.', '...')
	end
	text = gsub(text, "['%-]", ' ')
	text = gsub(text, '̀', '4')
	text = gsub(text, '([aoeiuüāōēīūǖáóéíúǘǎǒěǐǔǚàòèìùǜ]n?g?r?)([bpmfdtlkhjqxzcsywrng]h?)', '%1 %2')
	text = gsub(text, ' ([grn])$', '%1')
	text = gsub(text, ' ([grn]) ', '%1 ')
	if detone then
		text = mw.ustring.gsub(text, '([āōēīūǖáóéíúǘǎǒěǐǔǚàòèìùǜḿńňǹ])', function(a) return py_detone[a] .. py_tone[a] end)
		text = gsub(text, '([1234])([^ ]*)', '%2%1')
		text = mw.ustring.gsub(text, '([%lüê]) ', '%15 ')
		text = mw.ustring.gsub(text, '([%lüê])$', '%15')
	end
	if not_spaced then
		text = gsub(text, ' ', '')
	end
	return text
end
 
function export.py_ipa(text)
	local ipa_initial = {
		['b'] = 'p', ['p'] = 'pʰ', ['m'] = 'm', ['f'] = 'f', 
		['d'] = 't', ['t'] = 'tʰ', ['n'] = 'n', ['l'] = 'l', 
		['g'] = 'k', ['k'] = 'kʰ', ['h'] = 'x', 
		['j'] = 't͡ɕ', ['q'] = 't͡ɕʰ', ['x'] = 'ɕ', 
		['z'] = 't͡s', ['c'] = 't͡sʰ', ['s'] = 's', ['r'] = 'ʐ', 
		['zh'] = 'ʈ͡ʂ', ['ch'] = 'ʈ͡ʂʰ', ['sh'] = 'ʂ', 
		[''] = ''
	}
 
	local ipa_initial_tl = {
		['p'] = 'b̥', ['t'] = 'd̥', ['k'] = 'g̊', ['t͡ɕ'] = 'd͡ʑ̥', ['t͡s'] = 'd͡z̥', ['ʈ͡ʂ'] = 'ɖ͡ʐ̥'
	}
 
	local ipa_final = {
		['yuanr'] = 'ɥɑɻ', ['iangr'] = 'jɑ̃ɻ', ['yangr'] = 'jɑ̃ɻ', ['uangr'] = 'wɑ̃ɻ', ['wangr'] = 'wɑ̃ɻ', ['yingr'] = 'jɤ̃ɻ', ['wengr'] = 'ʊ̃ɻ', ['iongr'] = 'jʊ̃ɻ', ['yongr'] = 'jʊ̃ɻ', 
		['yuan'] = 'y̯ɛn', ['iang'] = 'i̯ɑŋ', ['yang'] = 'i̯ɑŋ', ['uang'] = 'u̯ɑŋ', ['wang'] = 'u̯ɑŋ', ['ying'] = 'iŋ', ['weng'] = 'u̯əŋ', ['iong'] = 'i̯ʊŋ', ['yong'] = 'i̯ʊŋ', ['ianr'] = 'jɑɻ', ['yanr'] = 'jɑɻ', ['uair'] = 'wɑɻ', ['wair'] = 'wɑɻ', ['uanr'] = 'wɑɻ', ['wanr'] = 'wɑɻ', ['iaor'] = 'jaʊɻʷ', ['yaor'] = 'jaʊɻʷ', ['üanr'] = 'ɥɑɻ', ['vanr'] = 'ɥɑɻ', ['angr'] = 'ɑ̃ɻ', ['yuer'] = 'ɥɛɻ', ['weir'] = 'wəɻ', ['wenr'] = 'wəɻ', ['your'] = 'jɤʊɻʷ', ['yinr'] = 'jəɻ', ['yunr'] = 'ɥəɻ', ['engr'] = 'ɤ̃ɻ', ['ingr'] = 'jɤ̃ɻ', ['ongr'] = 'ʊ̃ɻ', 
		['uai'] = 'u̯aɪ̯', ['wai'] = 'u̯aɪ̯', ['yai'] = 'i̯aɪ̯', ['iao'] = 'i̯ɑʊ̯', ['yao'] = 'i̯ɑʊ̯', ['ian'] = 'i̯ɛn', ['yan'] = 'i̯ɛn', ['uan'] = 'u̯a̠n', ['wan'] = 'u̯a̠n', ['üan'] = 'y̯ɛn', ['van'] = 'y̯ɛn', ['ang'] = 'ɑŋ', ['yue'] = 'y̯œ', ['wei'] = 'u̯eɪ̯', ['you'] = 'i̯oʊ̯', ['yin'] = 'in', ['wen'] = 'u̯ən', ['yun'] = 'yn', ['eng'] = 'ɤŋ', ['ing'] = 'iŋ', ['ong'] = 'ʊŋ', ['air'] = 'ɑɻ', ['anr'] = 'ɑɻ', ['iar'] = 'jɑɻ', ['yar'] = 'jɑɻ', ['uar'] = 'wɑɻ', ['war'] = 'wɑɻ', ['aor'] = 'aʊɻʷ', ['ier'] = 'jɛɻ', ['yer'] = 'jɛɻ', ['uor'] = 'wɔɻ', ['wor'] = 'wɔɻ', ['üer'] = 'ɥɛɻ', ['ver'] = 'ɥɛɻ', ['eir'] = 'əɻ', ['enr'] = 'əɻ', ['uir'] = 'wəɻ', ['unr'] = 'wəɻ', ['our'] = 'ɤʊɻʷ', ['iur'] = 'jɤʊɻ', ['inr'] = 'jəɻ', ['ünr'] = 'ɥəɻ', ['vnr'] = 'ɥəɻ', ['yir'] = 'jəɻ', ['wur'] = 'wuɻ', ['yur'] = 'ɥəɻ', 
		['yo'] = 'i̯ɔ', ['ia'] = 'i̯a̠', ['ya'] = 'i̯a̠', ['ua'] = 'u̯a̠', ['wa'] = 'u̯a̠', ['ai'] = 'aɪ̯', ['ao'] = 'ɑʊ̯', ['an'] = 'a̠n', ['ie'] = 'i̯ɛ', ['ye'] = 'i̯ɛ', ['uo'] = 'u̯ɔ', ['wo'] = 'u̯ɔ', ['ue'] = 'ɥ̯œ', ['üe'] = 'ɥ̯œ', ['ve'] = 'ɥ̯œ', ['ei'] = 'eɪ̯', ['ui'] = 'u̯eɪ̯', ['ou'] = 'oʊ̯', ['iu'] = 'i̯oʊ̯', ['en'] = 'ən', ['in'] = 'in', ['un'] = 'u̯ən', ['ün'] = 'yn', ['vn'] = 'yn', ['yi'] = 'i', ['wu'] = 'u', ['yu'] = 'y', ['mˋ'] = 'm̩', ['ng'] = 'ŋ̩', ['ňg'] = 'ŋ̩', ['ńg'] = 'ŋ̩', ['ê̄'] = 'ɛ', ['ê̌'] = 'ɛ', ['ar'] = 'ɑɻ', ['er'] = 'ɤɻ', ['or'] = 'wɔɻ', ['ir'] = 'iəɻ', ['ur'] = 'uɻ', ['ür'] = 'yəɻ', ['vr'] = 'yəɻ', 
		['a'] = 'a̠', ['e'] = 'ɤ', ['o'] = 'u̯ɔ', ['i'] = 'i', ['u'] = 'u', ['ü'] = 'y', ['v'] = 'y', ['m'] = 'm̩', ['ḿ'] = 'm̩', ['n'] = 'n̩', ['ń'] = 'n̩', ['ň'] = 'n̩', ['ê'] = 'ɛ', ['ế'] = 'ɛ', ['ề'] = 'ɛ'
	}
 
	local ipa_null = {
		['a'] = true, ['o'] = true, ['e'] = true,
		['ai'] = true, ['ei'] = true, ['ao'] = true, ['ou'] = true,
		['an'] = true, ['en'] = true, ['er'] = true, 
		['ang'] = true, ['ong'] = true, ['eng'] = true
	}
 
	local ipa_tl_ts = {
		['1'] = '²', ['2'] = '³', ['3'] = '⁴', ['4'] = '¹', ['5'] = '¹'
	}
 
	local ipa_third_t_ts = {
		['1'] = '²¹⁴⁻²¹¹', ['3'] = '²¹⁴⁻³⁵', ['5'] = '²¹⁴', ['2'] = '²¹⁴⁻²¹¹', ['1-2'] = '²¹⁴⁻²¹¹', ['4-2'] = '²¹⁴⁻²¹¹', ['4'] = '²¹⁴⁻²¹¹', ['1-4'] = '²¹⁴⁻²¹¹'
	}
 
	local ipa_t_values = {
		['4'] = '⁵¹', ['1-4'] = '⁵⁵⁻⁵¹', ['1'] = '⁵⁵', ['2'] = '³⁵', ['1-2'] = '⁵⁵⁻³⁵', ['4-2'] = '⁵¹⁻³⁵'
	}
 
	local tone = {}
	local tone_cat = {}
	local text = gsub(export.py_transform(text), '[,.]', '')
	text = gsub(text, ' +', ' ')
	local p = mw.text.split(text, " ")
 
	for i = 1, #p do
		tone_cat[i] = m_zh.tone_determ(p[i])
		p[i] = gsub(p[i], '.', py_detone)
 
		if p[i] == '一' then
			if m_zh.tone_determ(p[i+1]) == '4' then tone_cat[i] = '1-2' else tone_cat[i] = '1-4' end
			p[i] = 'yi'
		elseif p[i] == '不' then
			if m_zh.tone_determ(p[i+1]) == '4' then tone_cat[i] = '4-2' else tone_cat[i] = '4' end
			p[i] = 'bu'
		end
	end
 
	for i = 1, #p do
		if ipa_null[p[i]] then p[i] = 'ˀ' .. p[i] end
		p[i] = gsub(p[i], '([jqx])u', '%1ü')
 
		p[i] = gsub(p[i], '^(ˀ?)([bcdfghjklmnpqrstxz]?h?)(.+)$', 
			function(a, b, c) return a ..
				(ipa_initial[b] or error(("Unrecognised initial: \"%s\""):format(b))) .. 
				(ipa_final[c] or error(("Unrecognised final: \"%s\". Are you missing an apostrophe before the null-initial syllable, or using an invalid Pinyin final?"):format(c))) end)
 
		p[i] = gsub(p[i], '(ʈ?͡?[ʂʐ]ʰ?)i', '%1ʐ̩')
		p[i] = gsub(p[i], '(t?͡?sʰ?)i', '%1z̩')
		p[i] = gsub(p[i], 'ˀu̯ɔ', 'ˀ̯ɔ')
		p[i] = gsub(p[i], 'ʐʐ̩', 'ʐ̩')
 
		if tone_cat[i] == '5' then
			p[i] = gsub(p[i], '^([ptk])([^͡ʰ])', function(a, b) return ipa_initial_tl[a] .. b end)
			p[i] = gsub(p[i], '^([tʈ]͡[sɕʂ])([^ʰ])', function(a, b) return ipa_initial_tl[a] .. b end)
			p[i] = gsub(p[i], 'ɤ$', 'ə')
			tone[i] = ipa_tl_ts[tone_cat[i-1]] or ""
 
		elseif tone_cat[i] == '3' then
			if i == #tone_cat then
				if i == 1 then tone[i] = '²¹⁴' else tone[i] = '²¹⁴⁻²¹⁽⁴⁾' end
			else
				tone[i] = ipa_third_t_ts[tone_cat[i+1]]
			end
 
		elseif tone_cat[i] == '4' and tone_cat[i+1] == '4' then
			tone[i] = '⁵¹⁻⁵³'
 
		elseif tone_cat[i] == '4' and tone_cat[i+1] == '1-4' then
			tone[i] = '⁵¹⁻⁵³'
 
		elseif tone_cat[i] == '1-4' and tone_cat[i+1] == '4' then
			tone[i] = '⁵⁵⁻⁵³'
 
		else
			tone[i] = ipa_t_values[tone_cat[i]]
		end
		p[i] = p[i] .. tone[i]
	end
	return table.concat(p, " ")
end
 
function export.py_zhuyin(text)
	local zhuyin_initial = {
		['b'] = 'ㄅ', ['p'] = 'ㄆ', ['m'] = 'ㄇ', ['f'] = 'ㄈ', 
		['d'] = 'ㄉ', ['t'] = 'ㄊ', ['n'] = 'ㄋ', ['l'] = 'ㄌ', 
		['g'] = 'ㄍ', ['k'] = 'ㄎ', ['h'] = 'ㄏ', 
		['j'] = 'ㄐ', ['q'] = 'ㄑ', ['x'] = 'ㄒ', 
		['z'] = 'ㄗ', ['c'] = 'ㄘ', ['s'] = 'ㄙ', ['r'] = 'ㄖ', 
		['zh'] = 'ㄓ', ['ch'] = 'ㄔ', ['sh'] = 'ㄕ', 
		[''] = ''
	}
 
	local zhuyin_final = {
		['yuan'] = 'ㄩㄢ', ['iang'] = 'ㄧㄤ', ['yang'] = 'ㄧㄤ', ['uang'] = 'ㄨㄤ', ['wang'] = 'ㄨㄤ', ['ying'] = 'ㄧㄥ', ['weng'] = 'ㄨㄥ', ['iong'] = 'ㄩㄥ', ['yong'] = 'ㄩㄥ', 
		['uai'] = 'ㄨㄞ', ['wai'] = 'ㄨㄞ', ['yai'] = 'ㄧㄞ', ['iao'] = 'ㄧㄠ', ['yao'] = 'ㄧㄠ', ['ian'] = 'ㄧㄢ', ['yan'] = 'ㄧㄢ', ['uan'] = 'ㄨㄢ', ['wan'] = 'ㄨㄢ', ['üan'] = 'ㄩㄢ', ['ang'] = 'ㄤ', ['yue'] = 'ㄩㄝ', ['wei'] = 'ㄨㄟ', ['you'] = 'ㄧㄡ', ['yin'] = 'ㄧㄣ', ['wen'] = 'ㄨㄣ', ['yun'] = 'ㄩㄣ', ['eng'] = 'ㄥ', ['ing'] = 'ㄧㄥ', ['ong'] = 'ㄨㄥ', 
		['yo'] = 'ㄧㄛ', ['ia'] = 'ㄧㄚ', ['ya'] = 'ㄧㄚ', ['ua'] = 'ㄨㄚ', ['wa'] = 'ㄨㄚ', ['ai'] = 'ㄞ', ['ao'] = 'ㄠ', ['an'] = 'ㄢ', ['ie'] = 'ㄧㄝ', ['ye'] = 'ㄧㄝ', ['uo'] = 'ㄨㄛ', ['wo'] = 'ㄨㄛ', ['ue'] = 'ㄩㄝ', ['üe'] = 'ㄩㄝ', ['ei'] = 'ㄟ', ['ui'] = 'ㄨㄟ', ['ou'] = 'ㄡ', ['iu'] = 'ㄧㄡ', ['en'] = 'ㄣ', ['in'] = 'ㄧㄣ', ['un'] = 'ㄨㄣ', ['ün'] = 'ㄩㄣ', ['yi'] = 'ㄧ', ['wu'] = 'ㄨ', ['yu'] = 'ㄩ', 
		['a'] = 'ㄚ', ['e'] = 'ㄜ', ['o'] = 'ㄛ', ['i'] = 'ㄧ', ['u'] = 'ㄨ', ['ü'] = 'ㄩ', ['ê'] = 'ㄝ', [''] = ''
	}
 
	local zhuyin_er = {
		['r'] = 'ㄦ', [''] = ''
	}
 
	local zhuyin_tone = {
		['1'] = '', ['2'] = 'ˊ', ['3'] = 'ˇ', ['4'] = 'ˋ', ['5'] = '˙'
	}
 
	if type(text) == 'table' then
		if text.args[1] == '' then
			text = mw.title.getCurrentTitle().text
		else
			text = text.args[1]
		end
	end
	text = export.py_transform(text, true)
	text = gsub(text, '([jqx])u', '%1ü')
	text = gsub(text, '([zcs]h?)i', '%1')
	word = mw.text.split(text, " ", true)
	for i = 1, #word do
		if match(word[i], '^[hn][mg][12345]$') then
			word[i] = gsub(word[i], '^([hn][mg])([12345])$', function(a, b) return gsub(a, '[hn][mg]', {['ng'] = '兀', ['hm'] = 'ㄏㄇ'}) .. zhuyin_tone[b] end)
		elseif match(word[i], '^er[12345]$') then
			word[i] = gsub(word[i], '^er([12345])$', function(a) return 'ㄦ' .. zhuyin_tone[a] end)
		else
			word[i] = gsub(word[i], '^([bpmfdtnlgkhjqxzcsr]?h?)([aeiouêüyw]?[aeioun]?[aeioung]?[ng]?)(r?)([12345])$', 
				function(a, b, c, d) return zhuyin_initial[a] .. zhuyin_final[b] .. zhuyin_tone[d] .. zhuyin_er[c] end)
		end
		if match(word[i], '[%l%d]') then
			error(("Zhuyin conversion unsuccessful: \"%s\". Are you using a valid Pinyin syllable? Is the text using a breve letter instead of a caron one?"):format(word[i]))
		end
	end
	text = gsub(table.concat(word, " "), ' , ', ', ')
	return text
end
 
function export.zhuyin_py(text)
	local zhuyin_py_initial = {
		["ㄅ"] = "b", ["ㄆ"] = "p", ["ㄇ"] = "m", ["ㄈ"] = "f", 
		["ㄉ"] = "d", ["ㄊ"] = "t", ["ㄋ"] = "n", ["ㄌ"] = "l", 
		["ㄍ"] = "g", ["ㄎ"] = "k", ["ㄏ"] = "h", 
		["ㄐ"] = "j", ["ㄑ"] = "q", ["ㄒ"] = "x", 
		["ㄓ"] = "zh", ["ㄔ"] = "ch", ["ㄕ"] = "sh", ["ㄖ"] = "r", 
		["ㄗ"] = "z", ["ㄘ"] = "c", ["ㄙ"] = "s", 
		[""] = ""
	}
 
	local zhuyin_py_final = {
		['ㄚ'] = 'a', ['ㄛ'] = 'o', ['ㄜ'] = 'e', ['ㄝ'] = 'ê', ['ㄞ'] = 'ai', ['ㄟ'] = 'ei', ['ㄠ'] = 'ao', ['ㄡ'] = 'ou', ['ㄢ'] = 'an', ['ㄣ'] = 'en', ['ㄤ'] = 'ang', ['ㄥ'] = 'eng', 
		['ㄧ'] = 'i', ['ㄧㄚ'] = 'ia', ['ㄧㄛ'] = 'io', ['ㄧㄝ'] = 'ie', ['ㄧㄞ'] = 'iai', ['ㄧㄠ'] = 'iao', ['ㄧㄡ'] = 'iu', ['ㄧㄢ'] = 'ian', ['ㄧㄣ'] = 'in', ['ㄧㄤ'] = 'iang', ['ㄧㄥ'] = 'ing', 
		['ㄨ'] = 'u', ['ㄨㄚ'] = 'ua', ['ㄨㄛ'] = 'uo', ['ㄨㄞ'] = 'uai', ['ㄨㄟ'] = 'ui', ['ㄨㄢ'] = 'uan', ['ㄨㄣ'] = 'un', ['ㄨㄤ'] = 'uang', ['ㄨㄥ'] = 'ong', 
		['ㄩ'] = 'ü', ['ㄩㄝ'] = 'ue', ['ㄩㄝ'] = 'üe', ['ㄩㄢ'] = 'üan', ['ㄩㄣ'] = 'ün', ['ㄩㄥ'] = 'iong', 
		['ㄦ'] = 'er', ['ㄫ'] = 'ng', ['ㄇ'] = 'm', [''] = 'i'
	}
 
	local zhuyin_py_tone = {
		["ˊ"] = "\204\129", ["ˇ"] = "\204\140", ["ˋ"] = "\204\128", ["˙"] = "", [""] = "\204\132"	
	}
 
  if type(text) == "table" then text = text.args[1] end
 	word = mw.text.split(text, " ", true)
 
 	for i = 1, #word do
 		word[i] = gsub(word[i], '^([ㄓㄔㄕㄖㄗㄘㄙ])([ˊˇˋ˙]?)$', '%1ㄧ%2')
 		word[i] = gsub(word[i], '([ㄅㄆㄇㄈㄉㄊㄋㄌㄍㄎㄏㄐㄑㄒㄓㄔㄕㄖㄗㄘㄙ]?)([ㄧㄨㄩ]?[ㄚㄛㄜㄝㄞㄟㄠㄡㄢㄣㄤㄥㄦㄫㄧㄨㄩㄇ])([ˊˇˋ˙]?)(ㄦ?)', function(initial, final, tone, erhua)
			initial = zhuyin_py_initial[initial]
			final = zhuyin_py_final[final]
 
			if erhua ~= '' then
				final = final .. 'r'
			end
			if initial == '' then
				final = final:gsub('^([iu])(n?g?)$', function(a, b) return a:gsub('[iu]', {['i'] = 'yi', ['u'] = 'wu'}) .. b end)
				final = final:gsub('^(w?u)([in])$', 'ue%2')
				final = final:gsub('^iu$', 'iou')
				final = final:gsub('^([iu])', {['i'] = 'y', ['u'] = 'w'})
				final = final:gsub('^ong', 'weng')
				final = gsub(final, '^ü', 'yu')
			end
			if initial:match('[jqx]') then
				final = gsub(final, '^ü', 'u')
			end
			tone = zhuyin_py_tone[tone]
 
			if final:find('[ae]') then
				final = final:gsub("([ae])", "%1" .. tone)
			elseif final:find('i[ou]') then
				final = final:gsub("(i[ou])", "%1" .. tone)
			elseif final:find('[io]') then
				final = final:gsub("([io])", "%1" .. tone)
			else
				final = gsub(final, "^([wy]?)(.)", "%1" .. "%2" .. tone)
			end
 
 			return initial .. final
 		end)
 	end
  return mw.ustring.toNFC(table.concat(word, " "))
end
 
function export.py_wg(text)
	local py_wg_initial = {
		["b"] = "p", ["p"] = "p'", 
		["d"] = "t", ["t"] = "t'", 
		["g"] = "k", ["k"] = "k'", 
		["j"] = "ch", ["q"] = "ch'", ["x"] = "hs", 
		["z"] = "ts", ["c"] = "ts'", ["r"] = "j", 
		["zh"] = "ch", ["ch"] = "ch'", 
	}
 
	local py_wg_final = {
		["^([yw]?)e([^ih])"] = "%1ê%2", 
		["^e$"] = "ê", 
		["([iy])an$"] = "%1en", 
		["(i?)ong"] = "%1ung", 
		["([iy])e$"] = '%1eh', 
		["[uü]e"] = "üeh", 
		["r$"] = "rh", 
		["^ê$"] = "eh", 
		["^i$"] = "ih", 
		["yi$"] = "i", 
	}
 
	local py_wg_syl = {
		["(ts'?)uo"] = "%1o", 
		["^([kh]'?)uo"] = "%1o", 
		["tsih"] = "tzu", ["ts'ih"] = "tz'u", ["^sih"] = "szu", 
		["^([kh]?'?)ê$"] = "%1o", 
		["yên"] = "yen", 
		["you"] = "yu", ["^ih"] = "i"
	}
 
	if type(text) == 'table' then text = text.args[1] end
	local text = gsub(export.py_transform(text, true), '[,%.]', '')
	text = gsub(gsub(text, ' +', ' '), '[一不]', {['一'] = 'yi1', ['不'] = 'bu4'})
	text = gsub(text, '([jqxy])u', '%1ü')
	local p = mw.text.split(text, " ", true)
 
	for i = 1, #p do
		p[i] = gsub(p[i], '^([bcdfghjklmnpqrstxz]?h?)(.+)([1-5])$', function(initial, final, tone)
			for text, replace in pairs(py_wg_final) do
				final = gsub(final, text, replace)
			end
			untoned = (py_wg_initial[initial] or initial) .. final
			for text, replace in pairs(py_wg_syl) do
				untoned = gsub(untoned, text, replace)
			end
			untoned = gsub(untoned, "k('?)ui", "k%1uei")
			return untoned .. '<sup>' .. tone .. '</sup>' end)
	end
	return table.concat(p, " ")
end
 
function make_bg(text, bg)
	if bg == 'y' then
		return '<span style="background-color:#F5DEB3">' .. text .. '</span>'
	else
		return text
	end
end
 
function export.py_gwoyeu(text, original_text)
	local initials = {
		['b'] = 'b',  ['p'] = 'p',  ['m'] = 'm',  ['f'] = 'f', 
		['d'] = 'd',  ['t'] = 't',  ['n'] = 'n',  ['l'] = 'l', 
		['g'] = 'g',  ['k'] = 'k',  ['h'] = 'h', 
		['j'] = 'j',  ['q'] = 'ch',  ['x'] = 'sh', 
		['zh'] = 'j', ['ch'] = 'ch', ['sh'] = 'sh', ['r'] = 'r', 
		['z'] = 'tz', ['c'] = 'ts',  ['s'] = 's', 
		['y'] = 'i',  ['w'] = 'u', 
		[''] = ''
	}
	local finals = {
		['a'] = 'a',   ['ai'] = 'ai',  ['ao'] = 'au',   ['an'] = 'an',   ['ang'] = 'ang',   ['e'] = 'e',   ['ei'] = 'ei',  ['ou'] = 'ou',  ['en'] = 'en',  ['eng'] = 'eng',   ['o'] = 'o', 
		['ia'] = 'ia',         ['iao'] = 'iau',  ['ian'] = 'ian',  ['iang'] = 'iang',  ['ie'] = 'ie',          ['iu'] = 'iou',  ['in'] = 'in',  ['ing'] = 'ing',   ['i'] = 'i', 
		['ua'] = 'ua',  ['uai'] = 'uai',         ['uan'] = 'uan',  ['uang'] = 'uang',  ['uo'] = 'uo',  ['ui'] = 'uei',         ['un'] = 'uen', ['ong'] = 'ong',   ['u'] = 'u', 
		['ɨ'] = 'y',                  ['üan'] = 'iuan',          ['üe'] = 'iue',                 ['ün'] = 'iun', ['iong'] = 'iong',  ['ü'] = 'iu', 
		--erhua
		['ar'] = 'al',  ['air'] = 'al',  ['aor'] = 'aul',  ['anr'] = 'al',  ['angr'] = 'angl',  ['er'] = 'el',  ['eir'] = 'eil', ['our'] = 'oul', ['enr'] = 'el', ['engr'] = 'engl',  ['or'] = 'ol', 
		['iar'] = 'ial',        ['iaor'] = 'iaul', ['ianr'] = 'ial', ['iangr'] = 'iangl', ['ier'] = 'iel',         ['iur'] = 'ioul', ['inr'] = 'iel', ['ingr'] = 'iengl', ['ir'] = 'iel', 
		['uar'] = 'ual', ['uair'] = 'ual',         ['uanr'] = 'ual', ['uangr'] = 'uangl', ['uor'] = 'uol', ['uir'] = 'ueil',        ['unr'] = 'uel', ['ongr'] = 'ongl',  ['ur'] = 'ul', 
		['ɨr'] = 'el',                 ['üanr'] = 'iual',          ['üer'] = 'iuel',                ['ünr'] = 'iul', ['iongr'] = 'iongl', ['ür'] = 'iuel', 
	}
	if type(text) == 'table' then text = text.args[1] end
	if text:match('^%s') or text:match('%s$') then error('invalid spacing') end
	words = mw.text.split(text, " ")
	local count = 0
	for i = 1, #words do
		if words[i]:match('^%u') then uppercase = true else uppercase = false end
		words[i] = export.py_transform(words[i], true, true)
		words[i] = gsub(words[i], "([1-5])", "%1 ")
		words[i] = gsub(words[i], " $", "")
		words[i] = gsub(words[i], '([!-/:-@%[-`{|}~!-/:-@[-`{-・])', ' %1 ')
		words[i] = gsub(words[i], ' +', ' ')
		words[i] = gsub(words[i], ' $', '')
		words[i] = gsub(words[i], '^ ', '')
		local syllables = mw.text.split(words[i], " ")
		for j = 1, #syllables do
			count = count + 1
			if not mw.ustring.find(syllables[j], '^[!-/:-@%[-`{|}~!-/:-@[-`{-・]+$') then
				local current = sub(mw.title.getCurrentTitle().text, count, count)
				if current == '一' or current == '七' or current == '八' or current == '不' then
					syllables[j] = gsub(current, '.', {['一'] = 'i', ['七'] = 'chi', ['八'] = 'ba', ['不'] = 'bu'})
				else
					initial, final, tone = '', '', ''
					syllables[j] = gsub(syllables[j], '([jqxy])u', '%1ü')
					syllables[j] = gsub(syllables[j], '^([zcsr]h?)i(r?[1-5])$', '%1ɨ%2')
					if mw.ustring.find(syllables[j], '([bpmfdtnlgkhjqxzcsryw]?h?)([iuü]?[aoeiɨuü][ioun]?g?r?)([1-5])') then
						syllables[j] = gsub(syllables[j], '([bpmfdtnlgkhjqxzcsryw]?h?)([iuü]?[aoeiɨuü][ioun]?g?r?)([1-5])', function(a, b, c)
							initial = initials[a] or error('Unrecognised initial:' .. a); final = finals[b] or error('Unrecognised final:' .. b); tone = c
							return (initial .. final .. tone) end)
					else
						error('Unrecognised syllable:' .. syllables[j])
					end
					local original = initial..final..tone
					if initial:find('^[iu]$') then
						final = initial .. final
						initial = ''
					end
					final = gsub(final, '([iu])%1', '%1')
					local len = len(initial) + len(final)
					local detone = initial..final
					local replace = detone
					local fullstop = false
					if tone == 5 or tone == '5' then
						fullstop = true
						if original_text then
							tone = mw.text.split(export.py_transform(original_text, true), ' ')[count]:match('[1-5]')
						else tone = 1 end
						if tone == 5 or tone == '5' then
							tone = m_zh.tone_determ(m_zh.py(current))
						end
					end
					if tone == 1 or tone == '1' then
						if initial == 'l' or initial == 'm' or initial == 'n' or initial == 'r' then
							replace = initial .. 'h' .. sub(detone, 2, len)
						else
							replace = detone
						end
					elseif tone == 2 or tone == '2' then
						if not (initial == 'l' or initial == 'm' or initial == 'n' or initial == 'r') then
							if final:sub(1, 1) == 'i' or final:sub(1, 1) == 'u' then
								replace = gsub(detone, '[iu]', {['i'] = 'y', ['u'] = 'w'}, 1)
								if replace:sub(len, len) == 'y' or replace:sub(len, len) == 'w' then
									replace = gsub(replace, '[yw]$', {['y'] = 'yi', ['w'] = 'wu'})
								end
							else
								replace = detone .. 'r'
							end
						else
							replace = detone
						end
					elseif tone == 3 or tone == '3' then
						if detone:find('^[iu]') then
							detone = detone:gsub('^[iu]', {['i'] = 'yi', ['u'] = 'wu'})
						end
						if final:find('[aeiou][aeiou]') and not final:find('^[ie][ie]') and not final:find('^[uo][uo]') then
							replace = detone:gsub('[iu]', {['i'] = 'e', ['u'] = 'o'}, 1)
						else
							if final:find('a') then replace = detone:gsub('a', 'aa', 1)
							elseif final:find('o') then replace = detone:gsub('o', 'oo', 1)
							elseif final:find('e') then replace = detone:gsub('e', 'ee', 1)
							elseif final:find('i') then replace = detone:gsub('i', 'ii', 1)
							elseif final:find('u') then replace = detone:gsub('u', 'uu', 1)
							elseif final:find('y') then replace = detone:gsub('y', 'yy', 1)
							else error('Unrecognised final:'..final)
							end
						end
					elseif tone == 4 or tone == '4' then
						if detone:find('^[iu]') then
							detone = detone:gsub('^[iu]', {['i'] = 'yi', ['u'] = 'wu'})
						end
						if detone:find('[aeiou][iuln]g?$') then
							replace = detone:gsub('[iuln]g?$', {['i'] = 'y', ['u'] = 'w', ['l'] = 'll', ['n'] = 'nn', ['ng'] = 'nq'})
						else
							replace = detone .. 'h'
						end
						replace = replace:gsub('yi([aeiou])', 'y%1')
						replace = replace:gsub('wu([aeiou])', 'w%1')
					end
					if fullstop then replace = '.' .. replace end
					syllables[j] = syllables[j]:gsub(original, replace)
				end
			end
		end
		words[i] = table.concat(syllables, "")
		if uppercase then
			words[i] = gsub(words[i], '^%l', mw.ustring.upper)
		end
	end
	return table.concat(words, " ")
end
 
function export.py_format(text, cap, bg, simple, nolink)
	if cap == false then cap = nil end
	if bg == false then bg = 'n' else bg = 'y' end
	if simple == false then simple = nil end
	if nolink == false then nolink = nil end
	local phon = ''
	local cat = ''
	if text:match('[一不]') and not simple then
		if text:match('一 ') then
			cat = cat .. '[[Category:Mandarin words containing 一 not undergoing tone sandhi]]'
			text = gsub(text, '一 ', 'yī ')
		end
		text = gsub(text, '[一不]$', {['一'] = 'yī', ['不'] = 'bù'})
 
		if match(text, '一') then
			if match(text, '一[^ āōēīūǖáóéíúǘǎǒěǐǔǚàòèìùǜ]*[āōēīūǖáóéíúǘǎǒěǐǔǚ]') then
				cat = cat .. '[[Category:Mandarin words containing 一 undergoing tone sandhi to the fourth tone]]'
				phon = gsub(text, '一([^ āōēīūǖáóéíúǘǎǒěǐǔǚàòèìùǜ]*[āōēīūǖáóéíúǘǎǒěǐǔǚ])', function(a) return make_bg('yì', bg) .. a end)
				text = gsub(text, '一([^ āōēīūǖáóéíúǘǎǒěǐǔǚàòèìùǜ]*[āōēīūǖáóéíúǘǎǒěǐǔǚ])', 'yī%1')
			end
			if match(text, '一[^ āōēīūǖáóéíúǘǎǒěǐǔǚàòèìùǜ]*[àòèìùǜ]') then
				cat = cat .. '[[Category:Mandarin words containing 一 undergoing tone sandhi to the second tone]]'
				if phon == '' then phon = text end
				phon = gsub(phon, '一([^ āōēīūǖáóéíúǘǎǒěǐǔǚàòèìùǜ]*[àòèìùǜ])', function(a) return make_bg('yí', bg) .. a end)
				text = gsub(text, '一([^ āōēīūǖáóéíúǘǎǒěǐǔǚàòèìùǜ]*[āōēīūǖáóéíúǘǎǒěǐǔǚ])', 'yī%1')
			end
		end
		if match(text, '不[bpmfdtnlgkhjqxzcsrwy]?h?[aeiouāōēīūǖáóéíúǘǎǒěǐǔǚàòèìùǜ]*[àòèìùǜ]') then
			if phon == '' then phon = text end
			cat = cat .. '[[Category:Mandarin words containing 不 undergoing tone sandhi|2]]'
			phon = gsub(phon, '不([bpmfdtnlgkhjqxzcsrwy]?h?[aeiouāōēīūǖáóéíúǘǎǒěǐǔǚàòèìùǜ]*[àòèìùǜ])', function(a) return make_bg('bú', bg) .. a end)
		end
	end
	text = gsub(text, '[一不]', {['一'] = 'yī', ['不'] = 'bù'})
	text = gsub(text, '兒', function(a) return make_bg('r', bg) end)
	if not simple then
		if cap then
			text = gsub(text, '^%l', string.upper)
			phon = gsub(phon, '^%l', string.upper)
		end
		if not nolink then
			text = '[[' .. text .. ']]'
		end
		if phon ~= '' then
			phon = gsub(phon, '[一不]', {['一'] = 'yī', ['不'] = 'bù'})
			text = text .. ' [Phonetic: ' .. phon .. ']'
		end
		if mw.title.getCurrentTitle().nsText ~= 'Template' and not nolink then
			text = text .. cat
		end
	end
	return text
end
 
function export.make_tl(original_text, tl_pos, bg, cap)
	if bg == false then bg = 'n' else bg = 'y' end
	_, countoriginal = gsub(original_text, " ", " ")
	cats = ''
	spaced = export.py_transform(original_text)
	if sub(spaced, -1, -1) == ' ' then spaced = sub(spaced, 1, -2) end
	_, count = gsub(spaced, " ", " ")
	index = {}
	i = 1
	pos = 1
	for i = 1, count, 1 do
		if i ~= 1 then pos = (index[i-1] + 1) end
		index[i] = mw.ustring.find(spaced, ' ', pos)
	end
	if tl_pos == 2 then
		start = index[count-1] - count + countoriginal + 2
		finish = index[count] - count + countoriginal
	elseif tl_pos == 3 then
		start = index[count-2] - count + countoriginal + 2
		finish = index[count-1] - count + countoriginal
	else
		start = index[count] - count + countoriginal + 1
		finish = -1
	end
	text = (sub(original_text, 1, start-1) .. make_bg(gsub(sub(original_text, start, finish), '.', py_detone), bg))
	if finish ~= -1 then text = (text .. sub(original_text, finish+1, -1)) end
	if cap == true then text = gsub(text, '^%l', upper) end
	return text
end
 
function export.tag(first, second, third, fourth, fifth)
	local text = "(''"
	local tag = {}
	local tagg = first or "[[w:Standard Chinese|Standard Chinese]], Beijing"
	if second == '' then tag[1] = "[[w:Standard Chinese|Standard Chinese]]" else tag[1] = second end
	if third == '' then tag[2] = "Beijing" else tag[2] = third end
	if fourth == '' then tag[3] = nil else tag[3] = fourth end
	if fifth == '' then tag[4] = nil else tag[4] = fifth end
	if tagg == '' then
		text = (text .. table.concat(tag, ", ") .. "'')")
	else
		text = (text .. tagg .. "'')")
	end
	text = gsub(text, 'Beijing', "[[w:Beijing dialect|Beijing]]")
	text = gsub(text, 'Mainland', "[[w:Beijing dialect|Mainland]]")
	text = gsub(text, 'Taiwan', "[[w:Taiwanese Mandarin|Taiwan]]")
	text = gsub(text, 'Min Nan', "[[w:Min Nan|Min Nan]]")
	text = gsub(text, 'erhua([^%]%|])', "[[w:erhua|erhua]]%1")
	return text
end
 
function export.straitdiff(text, pron_ind, tag)
	local conv_text = text
	for i = 1, #text do
		if m_zh_data.MT[sub(text, i, i)] then conv_text = 'y' end
	end
	if tag == 'tag' then
		if conv_text == 'y' then
			tag = {}
			for a, b in pairs(m_zh_data.MT_tag) do tag[a] = b[tonumber(pron_ind)] end
			conv_text = gsub(match(text, '[一-龯㐀-䶵]'), '.', tag)
		else
			conv_text = ''
		end
	elseif pron_ind == 1 or pron_ind == 2 or pron_ind == 3 or pron_ind == 4 then
		reading = {}
		for a, b in pairs(m_zh_data.MT) do
			reading[a] = b[pron_ind]
			if reading[a] then reading[a] = gsub(reading[a], "^([āōēáóéǎǒěàòèaoe])", "'%1") end
		end
		conv_text = gsub(text, '.', reading)
		text = gsub(text, "^'", "")
		text = gsub(text, " '", " ")
		if conv_text == text and tag == 'exist' then return nil end
	end
	conv_text = gsub(conv_text, "^'", "")
	return conv_text
end
 
function export.str_analysis(text, conv_type)
	if type(text) == 'table' then text, conv_type = text.args[1], (text.args[2] or "") end
	local MT = m_zh_data.MT
	text = gsub(text, '=', '—')
	text = gsub(text, ',', '隔')
	text = gsub(text, '隔 ', ', ')
	if conv_type == 'head' then
		if match(text, ', cap—') then
			text = gsub(text, '[一不]', {['一'] = 'Yī', ['不'] = 'Bù'})
		end
		text = gsub(text, '[一不]', {['一'] = 'yī', ['不'] = 'bù'})
	end
	local comp = mw.text.split(text, '隔', true)
	local reading = {}
	local alternative_reading = {}
	local zhuyin = {}
	local param = {'1n', '1na', '1nb', '1nc', '1nd', 'py', 'cap', 'tl', 'tl2', 'tl3', 'a', 'audio', 'er', 'ertl', 'ertl2', 'ertl3', 'era', 'eraudio', '2n', '2na', '2nb', '2nc', '2nd', '2py', '2cap', '2tl', '2tl2', '2tl3', '2a', '2audio', '3n', '3na', '3nb', '3nc', '3nd', '3py', '3cap', '3tl', '3tl2', '3tl3', '3a', '3audio', '4n', '4na', '4nb', '4nc', '4nd', '4py', '4cap', '4tl', '4tl2', '4tl3', '4a', '4audio'}
 
	if conv_type == '' then
		return comp[1]
	elseif conv_type == 'head' then
		for i = 1, #comp do
			if not match(comp[i], '—') then
				if match(comp[i], '[一-龯㐀-䶵]') then
					M, T = {}, {}
					for a, b in pairs(MT) do
						M[a] = b[1]; T[a] = b[2]
						M[a] = gsub(M[a], "^([āōēáóéǎǒěàòèaoe])", "'%1")
						T[a] = gsub(T[a], "^([āōēáóéǎǒěàòèaoe])", "'%1")
					end
					mandarin = gsub(comp[i], '.', M)
					taiwan = gsub(comp[i], '.', T)
					mandarin = gsub(mandarin, "^'", "")
					mandarin = gsub(mandarin, " '", " ")
					taiwan = gsub(taiwan, "^'", "")
					taiwan = gsub(taiwan, " '", " ")
					zhuyin[i] = export.py_zhuyin(mandarin, true) .. ', ' .. export.py_zhuyin(taiwan, true)
					reading[i] = mandarin .. ']], [[' .. taiwan
				else
					zhuyin[i] = export.py_zhuyin(comp[i], true)
					reading[i] = comp[i]
					if len(mw.title.getCurrentTitle().text) == 1 then
						alternative_reading[i] = "[[" .. m_zh.py_transf(reading[i]) .. "|" .. gsub(m_zh.py_transf(reading[i]), '([1-5]*)', '<sup>%1</sup>') .. "]]"
					end
				end
				if reading[i] ~= '' then reading[i] = '[[' .. reading[i] .. ']]' end
			end
		end
		id = m_zh.ts_determ(mw.title.getCurrentTitle().text)
		if id == 'trad' then
			accel = '<span class="form-of lang-cmn pinyin-t-form-of transliteration-' .. m_zh.ts(mw.title.getCurrentTitle().text) .. '">'
		elseif id == 'simp' then
			accel = '<span class="form-of lang-cmn pinyin-s-form-of transliteration-' .. m_zh.st(mw.title.getCurrentTitle().text) .. '">'
		elseif id == 'both' then
			accel = '<span class="form-of pinyin-ts-form-of lang-cmn">'
		end
		local result = "*: (''[[w:Pinyin|Pinyin]]''): <tt>" .. accel .. table.concat(reading, ", ") .. "</span>"
		if alternative_reading[1] then
			result = result .. " (" .. table.concat(alternative_reading, ", ") .. ")"
		end
		result = result .. "</tt>\n*: (''[[w:Zhuyin|Zhuyin]]''): " .. table.concat(zhuyin, ", ")
		return result
 
	elseif conv_type == '2' or conv_type == '3' or conv_type == '4' then
		if not match(text, '隔') or (comp[tonumber(conv_type)] and match(comp[tonumber(conv_type)], '—')) then
			return ''
		else
			return comp[tonumber(conv_type)]
		end
	else
		for i = 1, #comp, 1 do
			target = conv_type .. '—'
			if match(comp[i], target) then
				text = gsub(comp[i], target, '')
				return text
			end
		end
		text = ''
	end
	return text
end
 
function export.homophones(pinyin)
	local text = ''
	if mw.title.getCurrentTitle().nsText == '' then
		local args = hom_data.list[pinyin]
		text = '<div style="visibility:hidden; float:left"><sup><span style="color:#FFF">edit</span></sup></div>'
		i = 1
		while args[i] and args[i] ~= '' do
			if i > 1 then
				text = text .. '<br>'
			end
			text = text .. '<span class="Hani">[[' .. args[i] .. '#Chinese|' .. args[i] .. ']]</span>'
			if args[i+1] and args[i+1] ~= '' then
				text = text .. '<span class="Hani">/[[' .. args[i+1] .. '#Chinese|' .. args[i+1] .. ']]</span>'
			end
			i = i + 2
		end
		text = text .. '[[Category:Chinese terms with homophones]]'
	end
	return text
end
 
function export.make(frame)
	local args = frame:getParent().args
	return export.make_args(args)
end
 
function export.make_args(args)
	local pagename = mw.title.getCurrentTitle().text
	local text = ''
	local reading = {args[1] or '', args[2] or '', args[3] or '', args[4] or ''}
	if reading[1] ~= '' then
		title = export.tag((args["1n"] or ''), (args["1na"] or ''), (args["1nb"] or export.straitdiff(args[1], 1, 'tag')), (args["1nc"] or ''), (args["1nd"] or ''))
		pinyin = export.straitdiff(reading[1], 1, '')
		text = text .. export.make_table(title, pinyin, (args["py"] or ''), (args["cap"] or ''), (args["tl"] or ''), (args["tl2"] or ''), (args["tl3"] or ''), (args["a"] or args["audio"] or ''))
	end
	if args["er"] and args["er"] ~= '' then
		title = export.tag('', '', '', '[[w:erhua|erhua]]-ed')
		if mw.title.new((pagename .. '兒')).exists then
			if mw.title.new((pagename .. '儿')).exists then
				title = title .. ' ([[' .. pagename .. '兒]]/[[' .. pagename .. '儿]])'
			else
				title = title .. ' ([[' .. pagename .. '兒]])'
			end
		else
			if mw.title.new((pagename .. '儿')).exists then
				title = title .. ' ([[' .. pagename .. '儿]])'
			end
		end
		pinyin = (export.straitdiff(reading[1], 1, '') or reading[1]) .. 'r'
		text = text .. export.make_table(title, pinyin, '', (args["cap"] or ''), (args["ertl"] or ''), (args["ertl2"] or ''), (args["ertl3"] or ''), (args["era"] or args["eraudio"] or ''), true)
	end
 
	if reading[2] ~= '' or export.straitdiff(reading[1], 2, 'exist') then
		if args["2nb"] and args["2nb"] ~= '' then tagb = args["2nb"] else tagb = export.straitdiff(args[1], 2, 'tag') end
		title = export.tag((args["2n"] or ''), (args["2na"] or ''), tagb, (args["2nc"] or ''), (args["2nd"] or ''))
		if reading[2] ~= '' then pinyin = reading[2] else pinyin = export.straitdiff(reading[1], 2, '') end
		text = text .. export.make_table(title, pinyin, (args["2py"] or ''), (args["2cap"] or ''), (args["2tl"] or ''), (args["2tl2"] or ''), (args["2tl3"] or ''), (args["2a"] or args["2audio"] or ''), true) .. '[[Category:Mandarin variant pronunciations|' .. (export.straitdiff(args[1], 1, '') or args[1]) .. ']]'
 
		if reading[3] ~= '' or export.straitdiff(reading[1], 3, 'exist') then
			if args["3nb"] and args["3nb"] ~= '' then tagb = args["3nb"] else tagb = export.straitdiff(args[1], 3, 'tag') end
			title = export.tag((args["3n"] or ''), (args["3na"] or ''), tagb, (args["3nc"] or ''), (args["3nd"] or ''))
			if reading[3] ~= '' then pinyin = reading[3] else pinyin = export.straitdiff(reading[1], 3, '') end
			text = text .. export.make_table(title, pinyin, (args["3py"] or ''), (args["3cap"] or ''), (args["3tl"] or ''), (args["3tl2"] or ''), (args["3tl3"] or ''), (args["3a"] or args["3audio"] or ''), true)
 
			if reading[4] ~= '' or export.straitdiff(reading[1], 4, 'exist') then
				if args["4nb"] and args["4nb"] ~= '' then tagb = args["4nb"] else tagb = export.straitdiff(args[1], 4, 'tag') end
				title = export.tag((args["4n"] or ''), (args["4na"] or ''), tagb, (args["4nc"] or ''), (args["4nd"] or ''))
				if reading[4] ~= '' then pinyin = reading[4] else pinyin = export.straitdiff(reading[1], 4, '') end
				text = text .. export.make_table(title, pinyin, (args["4py"] or ''), (args["4cap"] or ''), (args["4tl"] or ''), (args["4tl2"] or ''), (args["4tl3"] or ''), (args["4a"] or args["4audio"] or ''), true)
			end
		end
	end
	if (args["tl"] or '') .. (args["tl2"] or '') .. (args["tl3"] or '') .. (args["2tl"] or '') .. (args["2tl2"] or '') .. (args["2tl3"] or '') ~= '' then
		text = text .. '[[Category:Mandarin words containing toneless variants|' .. export.straitdiff(args[1], 1, '') .. ']]'
	end
	return text
end
 
function export.make_table(title, pinyin, py, cap, tl, tl2, tl3, a, novariety)
	if py == '' then py = nil end;if cap == '' then cap = nil end;if tl == '' then tl = nil end;if tl2 == '' then tl2 = nil end;if tl3 == '' then tl3 = nil end;if a == '' then a = nil end;if novariety == '' then novariety = nil end
	local text = ''
 
	pinyin_simple_fmt = export.py_format(pinyin, false, false, true)
	pinyin_simple_fmt_nolink = export.py_format(pinyin, false, false, true, true)
 
	if not novariety then text = text .. '* [[w:Mandarin Chinese|Mandarin]]' else text = text .. '<br><br>' end
	text = text .. '\n** ' .. title
	if hom_data.list[mw.ustring.lower(pinyin_simple_fmt)] then
		hom_found = true
	else
		hom_found = false
		text = text .. '<sup><small><abbr title="Add Mandarin homophones (click on this if and only if you know what you are doing!)"><span class="plainlinks">[' .. tostring(mw.uri.fullUrl("Module:zh/data/cmn-hom",{["action"]="edit"})) .. ' +]</span></abbr></small></sup>'
	end
	text = text .. "\n*** ''[[w:Pinyin|Pinyin]]'': "
	id = m_zh.ts_determ(mw.title.getCurrentTitle().text)
	if id == 'trad' then
		text = text .. '<span class="form-of lang-cmn pinyin-t-form-of transliteration-' .. m_zh.ts(mw.title.getCurrentTitle().text) .. '">'
	elseif id == 'simp' then
		text = text .. '<span class="form-of lang-cmn pinyin-s-form-of transliteration-' .. m_zh.st(mw.title.getCurrentTitle().text) .. '">'
	elseif id == 'both' then
		text = text .. '<span class="form-of pinyin-ts-form-of lang-cmn">'
	end
	if py then
		text = text .. '<tt>' .. py .. '</tt>'
	else
		text = text .. '<tt>'
		if cap then
			text = text .. export.py_format(pinyin, true, true)
		else
			text = text .. export.py_format(pinyin, false, true)
		end
		if tl or tl2 or tl3 then
			text = text .. ' → '
			if tl then tl_pos = 1 elseif tl2 then tl_pos = 2 elseif tl3 then tl_pos = 3 end
			if cap then
				text = text .. export.make_tl(export.py_format(pinyin, true, false, true, true), tl_pos, true, true)
			else
				text = text .. export.make_tl(pinyin_simple_fmt_nolink, tl_pos, true)
			end
		end
		text = text .. '</tt>'
		if tl then text = text .. ' <small>(toneless final syllable variant)</small>'
			elseif tl2 or tl3 then text = text .. ' <small>(toneless variant)</small>' end
	end
	text = text .. "</span>\n*** ''[[w:Zhuyin|Zhuyin]]'': " .. '<span lang="zh" class="Bopo">' .. export.py_zhuyin(pinyin_simple_fmt, true)
	if tl or tl2 or tl3 then
		text = text .. ' → ' .. export.py_zhuyin(export.make_tl(pinyin_simple_fmt_nolink, tl_pos, false), true)
	end
	text = text .. '</span>'
	if tl then text = text .. ' <small>(toneless final syllable variant)</small>'
		elseif tl2 or tl3 then text = text .. ' <small>(toneless variant)</small>' end
	if len(mw.title.getCurrentTitle().text) == 1 then
		text = text .. "\n*** ''[[w:Wade-Giles|Wade-Giles]]'': <tt>" .. export.py_wg(pinyin_simple_fmt) .. '</tt>'
	end
	text = text .. "\n*** ''[[w:Gwoyeu Romatzyh|Gwoyeu Romatzyh]]'': <tt>"
	if tl or tl2 or tl3 then
		text = text .. export.py_gwoyeu(export.make_tl(pinyin_simple_fmt_nolink, tl_pos, false), pinyin_simple_fmt)
	else
		text = text .. export.py_gwoyeu(pinyin_simple_fmt)
	end
	text = text .. '</tt>'
	text = text .. '\n*** [[Wiktionary:International Phonetic Alphabet|IPA]] <sup>([[Appendix:Mandarin pronunciation|key]])</sup>: <span class="IPA">/' .. export.py_ipa(pinyin)
	if tl or tl2 or tl3 then
		text = text .. '/ → /' .. export.py_ipa(export.make_tl(pinyin_simple_fmt_nolink, tl_pos, false))
	end
	text = text .. '/</span>'
	if a then
		if a == 'y' then a = 'zh-' .. pinyin_simple_fmt .. '.ogg' end
		text = text .. '\n*** ' .. mw.getCurrentFrame():expandTemplate{title = "Template:audio", args = {a, ['lang'] = 'cmn'}}
	end
	if hom_found then
		text = text .. '\n*** Homophones: <table class="wikitable mw-collapsible mw-collapsed" style="width:15em;margin:0; position:left; text-align:center"><tr><th></th></tr><tr><td><sup><div style="float: right; clear: right;"><span class="plainlinks">[' .. tostring(mw.uri.fullUrl("Module:zh/data/cmn-hom", {["action"]="edit"})) .. ' edit]</span></div></sup>' .. export.homophones(mw.ustring.lower(pinyin_simple_fmt)) .. '</td></tr></table>'
	end
	return text
end
 
return export