Module:zh-pron

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

Used by Template:zh-pron


local m_zh = require("Module:zh")
local m_zh_data = mw.loadData("Module:zh/data")
local m_links = require("Module:links")
local lang= require("Module:languages").getByCode("zh")
local gsub = mw.ustring.gsub
 
local export = {}
 
local langname = {
	['cdo']='Min Dong',
	['cmn']='Mandarin',
	['cjy']='Jin',
	['gan']='Gan',
	['hak']='Hakka',
	['hsn']='Xiang',
	['nan']='Min Nan',
	['wuu']='Wu',
	['yue']='Cantonese'
}
 
local pos_aliases_cat={
	['n']='nouns', ['noun']='nouns', ['pn']='proper nouns', ['propn']='proper nouns', ['proper']='proper nouns', ['proper noun']='proper nouns', ['pron']='pronouns', ['pronoun']='pronouns', --things related to nouns
	['v']='verbs', ['verb']='verbs', ['a']='adjectives', ['adj']='adjectives', ['adjective']='adjectives', ['adv']='adverbs', ['adverb']='adverbs', --other main POSes
	['prep']='prepositions', ['pre']='prepositions', ['postp']='postpositions', ['post']='postpositions', ['con']='conjunctions', ['conj']='conjunctions', ['conjunction']='conjunctions', --sentence modifiers
	['part']='particles', ['particle']='particles', ['pref']='prefixes', ['suf']='suffixes', ['suffix']='suffixes', ['interfix']='interfixes', --word modifiers
	['prov']='proverbs', ['proverb']='proverbs', ['id']='idioms', ['idiom']='idioms', ['ph']='phrases', ['phrase']='phrases', ['intj']='interjections', ['interj']='interjections', ['interjection']='interjections', --more than one word
	['cl']='classifiers', ['cls']='classifiers', ['classifier']='classifiers', ['num']='numerals', ['numeral']='numerals', ['abb']='abbreviations', ['abbreviation']='abbreviations', ['det']='determiners', ['deter']='determiners', ['determiner']='determiners' --other
};
 
function count(text)
	if text == "" then
		return 0
	end
	return 1
end
 
local m_pron = require("Module:cmn-pron")
local c_pron = require("Module:yue-pron")
local h_pron = require("Module:hak-pron")
local md_pron = require("Module:cdo-pron")
local mn_pron = require("Module:nan-pron")
local w_pron = require("Module:wuu-pron")
 
function export.make_table(frame)
	local args = frame:getParent().args
	local m_rom = args["m"] or ""
	local c_rom = args["c"] or ""
	local g_rom = args["g"] or ""
	local h_rom = args["h"] or ""
	local j_rom = args["j"] or ""
	local md_rom = args["md"] or ""
	local mn_rom = args["mn"] or ""
	local w_rom = args["w"] or ""
	local x_rom = args["x"] or ""
	local m_audio = args["ma"] or ""
	local m_audio2 = args["ma2"] or ""
	local c_audio = args["ca"] or ""
	local g_audio = args["ga"] or ""
	local h_audio = args["ha"] or ""
	local j_audio = args["ja"] or ""
	local md_audio = args["mda"] or ""
	local mn_audio = args["mna"] or ""
	local w_audio = args["wa"] or ""
	local x_audio = args["xa"] or ""
	local text = ''
	local count = count(m_rom..m_audio) + count(c_rom..c_audio) + count(md_rom..md_audio) + count(mn_rom..mn_audio) + count(w_rom..w_audio) + count(g_rom..g_audio) + count(h_rom..h_audio) + count(j_rom..j_audio) + count(x_rom..x_audio)
	c_rom = c_rom:gsub("%*","-")
	if g_rom ~= '' then g_rom = ipa_format(g_rom) end
	if j_rom ~= '' then j_rom = ipa_format(j_rom) end
	if x_rom ~= '' then x_rom = ipa_format(x_rom) end
	if count > 1 then
		text = text .. '<div class="toccolours mw-collapsible mw-collapsed" style="width:500px; font-size:100%">'
		if m_rom ~= "" then
			text = text .. '\n* [[w:Mandarin Chinese|Mandarin]]\n' .. m_pron.str_analysis(m_rom,'head')
		end
		if c_rom ~= "" then
			local c_processed = c_rom:gsub(',([^ ])',', %1')
			if mw.ustring.len(mw.title.getCurrentTitle().text) == 1 then
				c_processed = c_processed:gsub('([^, ]+)','[[%1]]')
				c_processed = c_processed:gsub('%[%[%[%[','[[')
				c_processed = c_processed:gsub('%]%]%]%]',']]')
				c_processed = gsub(c_processed, '%[%[([^%]]+)%]%]', function(a)
					return '[[' .. a .. '|' .. gsub(a, '([1-9-]*)', '<sup>%1</sup>') .. ']]' end)
			else
				c_processed = gsub(c_processed, '([1-9-]*)', '<sup>%1</sup>')
			end
			text = text .. '\n* [[w:Cantonese|Cantonese]] (\'\'[[w:Jyutping|Jyutping]]\'\'): <tt>' .. c_processed .. '</tt>'
			c_rom = c_rom:gsub('[%[%]]','')
		end
		if g_rom ~= '' then
			text = text .. '\n* [[w:Gan Chinese|Gan]] (\'\'[[w:International Phonetic Alphabet|IPA]]\'\'): <span class="IPA">/' .. g_rom .. '/</span>'
		end
		if mw.ustring.find(h_rom,'^pfs') then
			text = text .. '\n* [[w:Hakka Chinese|Hakka]] (\'\'[[w:Pha̍k-fa-sṳ|Pha̍k-fa-sṳ]]\'\'): <tt>' .. h_pron.rom_display(h_rom,'yes') .. '</tt>'
		end
		if j_rom ~= '' then
			text = text .. '\n* [[w:Jin Chinese|Jin]] (\'\'[[w:International Phonetic Alphabet|IPA]]\'\'): <span class="IPA">/' .. j_rom .. '/</span>'
		end
		if md_rom ~= "" then
			text = text .. '\n* [[w:Min Dong|Min Dong]] (\'\'[[w:Bàng-uâ-cê|BUC]]\'\'): <tt>' .. md_rom .. '</tt>'
		end
		if mn_rom ~= "" then
			text = text .. '\n* [[w:Min Nan|Min Nan]] (\'\'[[w:Pe̍h-ōe-jī|POJ]]\'\'): <tt>' .. mn_pron.poj_display(mn_rom) .. '</tt>'
		end
		if w_rom ~= "" then
			text = text .. '\n* [[w:Wu Chinese|Wu]] (\'\'[[Wiktionary:About Chinese/Wu|WT Romanisation]]\'\'): <tt>' .. w_pron.rom(w_rom) .. '</tt>'
		end
		if x_rom ~= '' then
			text = text .. '\n* [[w:Xiang Chinese|Xiang]] (\'\'[[w:International Phonetic Alphabet|IPA]]\'\'): <span class="IPA">/' .. x_rom .. '/</span>'
		end
		text = text .. '\n<div class="mw-collapsible-content">\n----'
	end
	text = text .. '\n'
	if m_rom .. m_audio ~= "" then
		m_args = {}
		m_args[1] = m_pron.str_analysis(m_rom,'')
		m_args_names = {'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'}
		for _,name in ipairs(m_args_names) do
			m_args[name] = m_pron.str_analysis(m_rom,name)
		end
		m_args[2] = m_pron.str_analysis(m_rom,'2')
		m_args[3] = m_pron.str_analysis(m_rom,'3')
		m_args[4] = m_pron.str_analysis(m_rom,'4')
		m_args['a'] = m_audio
		m_args['a2'] = m_audio2
		local debug = ""
		for code,name in pairs(m_args) do
			debug = debug .. code .. ":" .. name .. ","
		end
		text = text .. m_pron.make_args(m_args)
		if args["m_note"] then text = text .. '\n** Note: <small>' .. args["m_note"] .. '</small>.' end
	end
	if c_rom .. c_audio ~= "" then
		text = text .. '\n* [[w:Cantonese|Cantonese]]'
		local c_hom = mw.loadData("Module:yue-pron/hom")
		local c_hom_exists = false
		for _,c_first in ipairs(c_pron.jyutping_format(c_rom)) do
			if c_hom[c_first] then
				c_hom_exists = c_first
			end
		end
		text = text .. '\n** (\'\'[[w:Standard Cantonese|Standard Cantonese]], [[w:Guangzhou Cantonese|Guangzhou]]\'\')'
		if not c_hom_exists then
			text = text .. '<sup><small><abbr title="Add Cantonese homophones (click on this if and only if you know what you are doing!)"><span class="plainlinks">[' .. tostring(mw.uri.fullUrl("Module:yue-pron/hom",{["action"]="edit"})) .. ' +]</span></abbr></small></sup>'
		end
		c_comma = gsub(c_rom,',([^ ])',', %1')
		text = text .. '\n*** \'\'[[w:Jyutping|Jyutping]]\'\': <tt>' .. gsub(c_comma, '([1-9-]*)', '<sup>%1</sup>') .. '</tt>'
		text = text .. '\n*** \'\'[[w:Yale romanization of Cantonese|Yale]]\'\': <tt>' .. c_pron.jyutping_to_yale(c_rom) .. '</tt>'
		text = text .. '\n*** \'\'[[w:Cantonese Pinyin|Cantonese Pinyin]]\'\': <tt>' .. gsub(c_pron.jyutping_to_cantonese_pinyin(c_rom), '([1-9-]*)', '<sup>%1</sup>') .. '</tt>'
		text = text .. '\n*** [[Wiktionary:International Phonetic Alphabet|IPA]] <sup>([[w:Cantonese phonology|key]])</sup>: <span class="IPA">/' .. c_pron.jyutping_to_ipa(c_rom) .. '/</span>'
		for _,c_first in ipairs(c_pron.jyutping_format(c_rom)) do
			if c_hom_exists == c_first then
				local hom_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:yue-pron/hom",{["action"]="edit"})) .. ' edit]</span></div></sup><div style="visibility:hidden; float:left"><sup><span style="color:#FFF">edit</span></sup></div>'
				for _,hom in ipairs(c_hom[c_first]) do
					hom_text = hom_text .. "[[" .. mw.ustring.gsub(hom,"/","]]/[[") .. "]]<br />"
				end
				hom_text = mw.ustring.gsub(hom_text,"<br />$","")
				hom_text = hom_text .. '</td></tr></table>'
				hom_text = m_links.language_link(hom_text, nil, lang, nil, false)
				text = text .. hom_text .. '[[Category:Chinese terms with homophones#c]]'
			end
		end
		text = add_audio(text,c_audio,'yue')
		if args["c_note"] then text = text .. '\n** Note: <small>' .. args["c_note"] .. '</small>.' end
	end
	if g_rom .. g_audio ~= "" then
		text = text .. '\n* [[w:Gan Chinese|Gan]]'
		text = text .. '\n** (\'\'[[w:Nanchang dialect|Nanchang]]\'\')'
		if g_rom ~= '' then text = text .. '\n*** [[Wiktionary:International Phonetic Alphabet|IPA]] <sup>([[w:Changsha dialect|key]])</sup>: <span class="IPA">/' .. g_rom .. '/</span>' end
		text = add_audio(text,g_audio,'gan')
	end
	if h_rom .. h_audio ~= "" then
		text = text .. '\n* [[Wiktionary:About Chinese/Hakka|Hakka]]'
		if h_rom ~= '' then text = text .. '\n** Romanisations:' .. h_pron.rom_display(h_rom,'') end
		if mw.ustring.find(h_rom, '^pfs') and not mw.ustring.match(h_rom, '[ ;]') and not mw.ustring.match(h_rom, '([^/]*)-([^/]*)-([^/]*)') then
			text = text .. '\n*** [[Wiktionary:International Phonetic Alphabet|IPA]] (\'\'[[w:zh:四縣話|Siyen]]\'\'): <span class="IPA">/'
			i = 0
			for word in mw.text.gsplit(h_rom, '/') do
				i = i + 1
				if i ~= 1 then
					text = text .. '/, /'
				end
				text = text .. h_pron.ipa(word)
			end
			text = text .. '/</span>'
		end
		if h_audio ~= '' then text = add_audio(text,h_audio,'hak') end
	end
	if j_rom .. j_audio ~= "" then
		text = text .. '\n* [[w:Jin Chinese|Jin]]'
		text = text .. '\n** (\'\'[[w:Taiyuan|Taiyuan]]\'\')<sup>[[:w:zh:太原話|+]]</sup>'
		if j_rom ~= '' then text = text .. '\n*** [[Wiktionary:International Phonetic Alphabet|IPA]] <sup>([[w:Changsha dialect|key]])</sup>: <span class="IPA">/' .. j_rom .. '/</span>' end
		text = add_audio(text,j_audio,'cjy')
	end
	if md_rom .. md_audio ~= "" then
		text = text .. '\n* [[w:Min Dong|Min Dong]]'
		text = text .. '\n** (\'\'[[w:Fuzhou dialect|Fuzhou]]\'\')'
		if md_rom ~= '' then
			text = text .. '\n*** \'\'[[w:Bàng-uâ-cê|Bàng-uâ-cê]]\'\': <tt>' .. md_rom .. '</tt>'
			if not mw.ustring.match(md_rom, ' ') and not mw.ustring.match(md_rom, '([^/]*)-([^/]*)-([^/]*)') then
				text = text .. '\n*** [[Wiktionary:International Phonetic Alphabet|IPA]] <sup>([[w:Fuzhou dialect|key]])</sup>: <span class="IPA">/'
				i = 0
				for word in mw.text.gsplit(md_rom, '/') do
					i = i + 1
					if i ~= 1 then
						text = text .. '/, /'
					end
					text = text .. md_pron.ipa(word)
				end
				text = text .. '/</span>'
			end
		end
		text = add_audio(text,md_audio,'cdo')
		if args["md_note"] then text = text .. '\n** Note: <small>' .. args["md_note"] .. '</small>.' end
	end
	if mn_rom .. mn_audio ~= "" then
		text = text .. '\n* [[w:Min Nan|Min Nan]]'
		text = text .. '\n** (\'\'[[w:Hokkien|Hokkien]]\'\')'
		text = text .. '\n*** \'\'[[w:Pe̍h-ōe-jī|Pe̍h-ōe-jī]]\'\': <tt>' .. mn_pron.poj_display(mn_rom) .. '</tt>'
		mn_tailo = mn_pron.poj_to_tl_conv(mn_rom)
		mn_tailo = gsub(mn_tailo,'/([^ ])',' / %1')
		text = text .. '\n*** \'\'[[w:Tâi-lô|Tâi-lô]]\'\': <tt>' .. mn_tailo .. '</tt>'
		mn_space = mw.ustring.find(mn_rom,' ')
		mn_double_hyphen = mw.ustring.find(mn_rom,'--')
		if not mn_space and mn_double_hyphen then
				text = text .. '\n*** [[Wiktionary:International Phonetic Alphabet|IPA]] (\'\'[[w:Taipei|Taipei]]\'\'): <span class="IPA">/' .. mn_pron.poj_to_ipa_conv(mn_rom,'Taipei') .. '/</span>'
				text = text .. '\n*** [[Wiktionary:International Phonetic Alphabet|IPA]] (\'\'[[w:Zhangzhou|Zhangzhou]]\'\'): <span class="IPA">/' .. mn_pron.poj_to_ipa_conv(mn_rom,'Zhangzhou') .. '/</span>'
		end
		text = add_audio(text,mn_audio,'nan')
		if args["mn_note"] then text = text .. '\n** Note: <small>' .. args["mn_note"] .. '</small>.' end
	end
	if w_rom .. w_audio ~= "" then
		text = text .. '\n* [[w:Wu Chinese|Wu]]'
		text = text .. '\n** (\'\'[[w:Shanghainese|Shanghainese]]\'\')'
		text = text .. '\n*** \'\'[[Wiktionary:About Chinese/Wu|WT Romanisation]]\'\': <tt>' .. w_pron.rom(w_rom) .. '</tt>'
		text = text .. '\n*** [[Wiktionary:International Phonetic Alphabet|IPA]] <sup>([[w:Shanghainese|key]])</sup>: <span class="IPA">/' .. w_pron.ipa_conv(w_rom) .. '/</span>'
		if args["w_note"] then text = text .. '\n** Note: <small>' .. args["w_note"] .. '</small>.' end
		text = add_audio(text,w_audio,'wuu')
	end
	if x_rom .. x_audio ~= "" then
		text = text .. '\n* [[w:Xiang Chinese|Xiang]]'
		text = text .. '\n** (\'\'[[w:Changsha dialect|Changsha]]\'\')'
		if x_rom ~= '' then text = text .. '\n*** [[Wiktionary:International Phonetic Alphabet|IPA]] <sup>([[w:Changsha dialect|key]])</sup>: <span class="IPA">/' .. x_rom .. '/</span>' end
		text = add_audio(text,x_audio,'hsn')
	end
	if count > 1 then
		text = text .. '</div></div>'
	end
	if c_rom == "" then text = text .. "[[Category:Kenny's testing category 2]]" end
	return text
end
 
function ipa_format(text)
	local numbers = { [1]='¹',[2]='²',[3]='³',[4]='⁴',[5]='⁵' }
	return gsub(text,'[1-5]',numbers)
end
 
function add_audio(text,audio,lang)
	if audio ~= "" then
		if audio == "y" then audio = lang .. '-' .. mw.title.getCurrentTitle().baseText .. '.ogg' end
		text = text .. '\n*** ' .. mw.getCurrentFrame():expandTemplate{title="Template:audio",args={audio,['lang']=lang}}
		text = text .. '[[Category:'..langname[lang]..' terms with audio links]]'
	end
	return text
end
 
function export.make_cat(frame)
	local args = frame:getParent().args
	local m_rom = args["m"] or ''
	local c_rom = args["c"] or ''
	local g_rom = args["g"] or ''
	local h_rom = args["h"] or ''
	local j_rom = args["j"] or ''
	local md_rom = args["md"] or ''
	local mn_rom = args["mn"] or ''
	local w_rom = args["w"] or ''
	local x_rom = args["x"] or ''
	local g_audio = args["ga"] or ''
	local h_audio = args["ha"] or ''
	local j_audio = args["ja"] or ''
	local md_audio = args["mda"] or ''
	local mn_audio = args["mna"] or ''
	local w_audio = args["wa"] or ''
	local x_audio = args["xa"] or ''
	local text = args["cat"]
	local cat = {}
	local conv_text = ''
	local cat_start = '[[Category:'
	local cat_end = ']]'
	if m_rom == '' then
		m_rom = mw.title.getCurrentTitle().text
	end
	if mw.ustring.match(m_rom,'[一-龯㐀-䶵]') then
		m_rom = gsub(m_rom,'.',m_zh_data.py)
	end
	m_rom = m_pron.py_transform(m_rom,true,true)
	if w_rom ~= '' then
		w_rom = gsub(w_rom, '%d', '')
	end
	if text and text ~= '' and mw.title.getCurrentTitle().nsText == '' then
		cat = mw.text.split(text,',',true)
		table.insert(cat, 1, "lemmas")
		for i = 1, #cat do
			cat[i] = gsub(cat[i], '^ +', '')
			cat[i] = pos_aliases_cat[cat[i]] or cat[i]
			conv_text = conv_text .. cat_start .. 'Chinese ' .. cat[i] .. '|' .. m_rom .. cat_end
			if args["m"] then
				conv_text = conv_text .. cat_start .. 'Mandarin ' .. cat[i] .. '|' .. m_rom .. cat_end
			end
			if c_rom ~= '' then conv_text = conv_text .. cat_start .. 'Cantonese ' .. cat[i] .. '|' .. c_rom:gsub('[%[%]]','') .. cat_end end
			if g_rom ~= '' or g_audio ~= '' then conv_text = conv_text .. cat_start .. 'Gan ' .. cat[i] .. '|' .. m_rom .. cat_end end
			if h_rom ~= '' or h_audio ~= '' then conv_text = conv_text .. cat_start .. 'Hakka ' .. cat[i] .. '|' .. m_rom .. cat_end end
			if j_rom ~= '' or j_audio ~= '' then conv_text = conv_text .. cat_start .. 'Jin ' .. cat[i] .. '|'  .. m_rom .. cat_end end
			if md_rom ~= '' or md_audio ~= '' then conv_text = conv_text .. cat_start .. 'Min Dong ' .. cat[i] .. '|' .. md_rom .. cat_end end
			if mn_rom ~= '' or mn_audio ~= '' then conv_text = conv_text .. cat_start .. 'Min Nan ' .. cat[i] .. '|' .. mn_rom .. cat_end end
			if w_rom ~= '' or w_audio ~= '' then conv_text = conv_text .. cat_start .. 'Wu ' .. cat[i] .. '|' .. w_rom .. cat_end end
			if x_rom ~= '' or x_audio ~= '' then conv_text = conv_text .. cat_start .. 'Xiang ' .. cat[i]  .. '|'  .. m_rom .. cat_end end
		end
	end
	if mw.ustring.len(mw.title.getCurrentTitle().text) == 1 then
		conv_text  = conv_text .. cat_start .. 'Chinese hanzi|' .. m_rom .. cat_end
	end
	return conv_text
end
 
return export