Modul:nan-pron

Üdvözlöm, Ön a Modul:nan-pron szó jelentését keresi. A DICTIOUS-ban nem csak a Modul:nan-pron szó összes szótári jelentését megtalálod, hanem megismerheted az etimológiáját, a jellemzőit és azt is, hogyan kell a Modul:nan-pron szót egyes és többes számban mondani. Minden, amit a Modul:nan-pron szóról tudni kell, itt található. A Modul:nan-pron szó meghatározása segít abban, hogy pontosabban és helyesebben fogalmazz, amikor beszélsz vagy írsz. AModul:nan-pron és más szavak definíciójának ismerete gazdagítja a szókincsedet, és több és jobb nyelvi forráshoz juttat.

A modult a Modul:nan-pron/doc lapon tudod dokumentálni

local export = {}

local find = mw.ustring.find
local gsub = mw.ustring.gsub
local sub = mw.ustring.sub
local match = mw.ustring.match
local len = mw.ustring.len
local split = mw.text.split

local psdb_initial = {
	 = "'p",  = "ph",  = "'b",
	 = "'d",  = "'t",
	 = "'k",  = "'q",  = "'g",
	 = "c",  = "z",
	 = "ch",  = "zh",
	 = "s",  = "s",
	 = "j",
	 = "l",  = "'h",
	 = "m",  = "n",  = "ng",
	 = "'"
}

local function psdb_final(text)
	local basic_psdb = {
		--single vowel tone 12357
		 = "af",  = "ar",  = "ax",  = "aa",  = "a",
		 = "y",  = "ie",  = "ix",  = "ii",  = "i",
		 = "w",  = "uo",  = "ux",  = "uu",  = "u",
		 = "ef",  = "ea",  = "ex",  = "ee",  = "e",
		 = "of",  = "or",  = "ox",  = "oo",  = "o",
		 = "oy",  = "oir",  = "oix",  = "ooi",  = "oi",
		 = "'ngf",  = "'ngr",  = "'ngx",  = "'ngg",  = "'ng",
		 = "'mf",  = "'mr",  = "'mx",  = "'mm",  = "'m",
		--double vowel tone 12357
		 = "ay",  = "ae",  = "aix",  = "aai",  = "ai",
		 = "aw",  = "ao",  = "aux",  = "aau",  = "au",
		 = "iaf",  = "iar",  = "iax",  = "iaa",  = "ia",
		 = "iaw",  = "iao",  = "iaux",  = "iaau",  = "iau",
		 = "ioy",  = "ioir",  = "ioix",  = "iooi",  = "ioi",
		 = "iw",  = "iuo",  = "iux",  = "iuu",  = "iu",
		 = "oaf",  = "oar",  = "oax",  = "oaa",  = "oa",
		 = "oay",  = "oae",  = "oaix",  = "oaai",  = "oai",
		 = "oef",  = "oea",  = "oex",  = "oee",  = "oe",
		 = "uy",  = "uie",  = "uix",  = "uii",  = "ui",
		--nasal vowel tone 12357
		--nasal ending tone 12357
		 = "iefn",  = "iern",  = "iexn",  = "ieen",  = "ien",
		 = "iofng",  = "iorng",  = "ioxng",  = "ioong",  = "iong",
		--stopped single vowel tone 48
		 = "ob",  = "op",
		 = "od",  = "ot",
		 = "og",  = "ok",
		--stopped double vowel tone 48
		 = "iob",  = "iop",
		 = "iod",  = "iot",
		 = "iog",  = "iok",
	}
	text = gsub(text, "", { = "1",  = "2",  = "3",  = "4",  = "5",  = "6",  = "7",  = "8"})
	if find(text, "ⁿ$") then
		local basic = gsub(text, "ⁿ", "")
		basic = gsub(basic, "^o()$", "oo%1")
		if basic_psdb then
			return "v" .. basic_psdb
		end
	elseif find(text, ".g?$") and not find(text, "^ian$") and not find(text, "^iong$") then
		local basic = gsub(text, "g?()$", "%1")
		local ending = match(text, "(g?)$")
		basic = gsub(basic, "^o()$", "oo%1")
		if basic_psdb then
			return basic_psdb .. ending
		end
	elseif find(text, "ⁿ?4$") and not find(text, "^i?o4$") then
		local basic = gsub(text, "(ⁿ?)4$", "%1") .. "7"
		local ending = match(text, "()ⁿ?4$")
		ending = gsub(ending, "",{p = "b", t = "d", k = "g", h = "q"})
		if find(basic, "ⁿ") then
			basic = gsub(basic, "ⁿ", "")
			basic = gsub(basic, "^o()$", "oo%1")
			if basic_psdb then
				return "v" .. basic_psdb .. ending
			end
		else
			if basic_psdb then
				return basic_psdb .. ending
			end
		end
	elseif find(text, "ⁿ?8$") and not find(text, "^i?o8$") then
		local basic = gsub(text, "(ⁿ?)8$", "%1") .. "7"
		local ending = match(text, "()ⁿ?8$")
		if find(basic, "ⁿ") then
			basic = gsub(basic, "ⁿ", "")
			basic = gsub(basic, "^o()$", "oo%1")
			if basic_psdb then
				return "v" .. basic_psdb .. ending
			end
		else
			if basic_psdb then
				return basic_psdb .. ending
			end
		end
	else
		return basic_psdb
	end
end

function export.poj_check_invalid(text)
	if not text then
		return nil
	end
	local correct = mw.ustring.toNFD(text) .. "-"
	local accent = ""
	local switch = "%1%3%2%4"
	local title = mw.title.getCurrentTitle().text
	correct = gsub(correct, "()()(" .. gsub(accent, "̍", "") .. ")()", switch)
	correct = gsub(correct, "()(" .. accent .. ")()(?ⁿ?)", switch)
	correct = gsub(correct, "(a)(i)(" .. accent .. ")(h?ⁿ?)", switch)
	correct = gsub(correct, "()()(" .. accent .. ")(h?ⁿ?)", switch)
	correct = gsub(correct, "()(" .. accent .. ")()(u??g?ⁿ?)", switch)
	correct = gsub(correct, "(a)(u)(" .. accent .. ")(h?ⁿ?)", switch)
	correct = gsub(correct, "()(i)(" .. accent .. ")(?)", switch)
	correct = gsub(correct, "()(e)(" .. accent .. ")(h?ⁿ?)", switch)
	correct = gsub(correct, "()(re)(" .. gsub(accent, "̍", "") .. ")()", switch)
	-- correct = gsub(correct, "(" .. accent .. ")", "%1͘")
	if find(title, "") and title ~= "明仔早" then
		correct = gsub(correct, "%-" .. mw.ustring.toNFD("á") .. "()", "-仔%1")
	end
	correct = mw.ustring.toNFC(gsub(correct, "-$", ""))
	if text ~= correct then
		error("Invalid POJ input \"" .. text .. "\": please change it to \"" .. correct .. "\"")
	end
	return text
end

function export.poj_to_tl_conv(text)
	if type(text) == "table" then text = text.args end
	local accent = "?"
	local conv = {
		 = "i",  = "I",  = "u",  = "U"
	}
	local function convert(a, b)
		return conv .. b
	end
	text = gsub(text, "#", "")
	text = mw.ustring.toNFD(text)
	text = gsub(text, "仔", "á")
	text = gsub(text, "%((+)%)", "%1-%1-%1")
	text = gsub(text, "()(̍?k)", convert)
	text = gsub(text, "()(" .. accent .. "ng)", convert)
	text = gsub(text, "()(" .. accent .. ")", convert)
	text = gsub(text, "()(" .. accent .. ")()", "%1%3%2")
	text = gsub(text, "()(" .. accent .. ")(re)", "%1%3%2")
	text = gsub(text, "(" .. accent .. ")͘", "%1o")
	text = gsub(text, "(h?)ⁿ", "nn%1")
	text = gsub(text, "h", { = "ts",  = "Ts"})
	text = gsub(text,'/()',' / %1')
	return mw.ustring.toNFC(text)
end

function export.poj_check_syllable(initial, final, loc)
	local validInitials = {
		 = 1,  = 1,  = 1,  = 1, 
		 = 1,  = 1,  = 1,  = 1, 
		 = 1,  = 1, 
		 = 1,  = 1,  = 1,  = 1,  = 1,  = 1,
	}
	local moreValidInitials = { }
	moreValidInitials = {  = 1, }
	moreValidInitials = {  = 1,  = 1, }
	moreValidInitials = {  = 1, }
	moreValidInitials = {  = 1, }
	moreValidInitials = {  = 1, }
	moreValidInitials = {  = 1, }
	moreValidInitials = {  = 1,  = 1 }
	moreValidInitials = {  = 1, }
	moreValidInitials = {  = 1,  = 1, }
	moreValidInitials = {  = 1, }
	moreValidInitials = {  = 1,  = 1 }
	moreValidInitials = {
			 = 1,
			 = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,
	}
	moreValidInitials = {  = 1, }
	
	local validFinals = {
		 = 1,  = 1,  = 1,  = 1,  = 1, 
		 = 1,  = 1,  = 1,  = 1,  = 1,
		 = 1,  = 1,  = 1,  = 1,  = 1, 
		 = 1,  = 1,  = 1,  = 1,  = 1,
		 = 1,  = 1,  = 1,  = 1,  = 1, 
		 = 1,  = 1,  = 1,  = 1,  = 1, 
		 = 1,  = 1,  = 1,  = 1,  = 1, 
		 = 1,  = 1,  = 1,  = 1,  = 1,
		 = 1,  = 1,  = 1,  = 1,  = 1,
		 = 1,  = 1,  = 1,  = 1, 
		 = 1,  = 1,  = 1,  = 1,  = 1,
		 = 1,  = 1,  = 1,  = 1,  = 1,
		 = 1,  = 1, 
	}
	local moreValidFinals = { }
	moreValidFinals = {
			 = 1,  = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1, 
	}
	moreValidFinals = {
			 = 1,  = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1, 
	}
	moreValidFinals = {
			 = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1, 
	}
	moreValidFinals = {
			 = 1,  = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1, 
	}
	moreValidFinals = {
			 = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,
	}
	moreValidFinals = {
			 = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,
	}
	moreValidFinals = {
			 = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,
	}
	moreValidFinals = {
			 = 1,  = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,  = 1,  = 1,  = 1, 
			 = 1,
	}
	moreValidFinals = {
			 = 1,  = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1, 
	}
	moreValidFinals = {
			 = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1, 
	}
	moreValidFinals = {
			 = 1,  = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1,  = 1, 
	}
	moreValidFinals = {
			 = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,
	}
	moreValidFinals = moreValidFinals
	
	local loc_code = {
		 = "x",
		 = "a",
		 = "d",
		 = "q",
		 = "c",
		 = "h",
		 = "z",
		 = "t",
		 = "k",
		 = "j",
		 = "s",
		 = "p",
		 = "f",
	}
	if not ((validInitials or moreValidInitials) and (validFinals or moreValidFinals)) then
		--error("The syllable " .. initial .. "+" .. final .. " does not appear to be a valid " .. loc .. " POJ syllable.")
		return " .. "]]"
	end
	return nil
end

function export.generate_all(text)
	local nan_pronunc, loc
	if type(text) == "table" then
		text, nan_pronunc, loc = text.args, text.args, text.args
	end
	
	local location_list = {
		 = "Anxi",
		 = "Changtai",
		 = "Hsinchu",
		 = "Jinjiang",
		 = "Kaohsiung",
		 = "Kinmen",
		 = "Lukang",
		 = "Medan",
		 = "Magong",
		 = "Mainland",
		 = "Philippines",
		 = "Penang",
		 = "Quanzhou",
		 = "Quanzhou-d",
		 = "Singapore",
		 = "Sanxia",
		 = "Tong'an",
		 = "Taichung",
		 = "Tainan",
		 = "Taipei",
		 = "Taitung",
		 = "Wanhua",
		 = "Wuqi",
		 = "Xiamen",
		 = "Xiamen-d",
		 = "Yilan",
		 = "Zhangpu",
		 = "Zhangzhou",
		 = "Taiwan",
		 = "Taiwan-t",
		 = "Taiwan-k",
		 = "Taiwan-v",
		 = "Taiwan-vt",
		 = "Taiwan-vk",
		 = "Taiwan-d",
		 = "Taiwan-dt",
		 = "Taiwan-dk",
		 = "Taiwan-r",
		 = "Taiwan-rt",
		 = "Taiwan-rk",
		 = "Taiwan-Q",
		 = "Taiwan-Z",
	}
	
	local location_alias = {
		 = "hc",  = "hc",  = "hc",
		 = "kh",
		 = "km",  = "km",  = "km",
		 = "lk",
		 = "mg",
		 = "qj",
		 = "sx",  = "sx",
		 = "tc",  = "tc",
		 = "tn",
		 = "xm",  = "xm",  = "xm",
		 = "yl",
		 = "zz",
		 = "zz (Zhangzhou) or qz (Quanzhou)",
		 = "zz (Zhangzhou) or qz (Quanzhou)"
	}

	local location_link = {
		 = "'']''",
		 = "'']''",
		 = "'']''",
		 = "'']''",
		 = "'']''",
		 = "'']''",
		 = "'']''",
		 = "'']''",
		 = "'']''",
		 = "'']'', '']'', '']''",
		 = "'']''",
		 = "'']''",
		 = "'']''",
		 = "''dated in ]''",
		 = "'']''",
		 = "'']''",
		 = "'']''",
		 = "'']''",
		 = "'']''",
		 = "'']''",
		 = "'']''",
		 = "'']''",
		 = "'']''",
		 = "'']''",
		 = "''dated in ]''",
		 = "'']''",
		 = "'']''",
		 = "'']''",
		 = "''General ]''",
		 = "''General ]''",
		 = "''General ]''",
		 = "''variant in ]''",
		 = "''variant in ]''",
		 = "''variant in ]''",
		 = "''dated in ]''",
		 = "''dated in ]''",
		 = "''dated in ]''",
		 = "''rare in ]''",
		 = "''rare in ]''",
		 = "''rare in ]''",
		 = "'']-like accent in ]''",
		 = "'']-like accent in ]''",
	}
	
	local IPA_available = {
		 = true,
		 = true,
		 = true,
		 = true,
		 = true,
		 = true,
		 = true,
		 = true,
		 = true,
		 = true,
		 = true,
		 = true,
	}
	
	local ast = (not nan_pronunc or nan_pronunc == "") and "*" or ""
	
	local formatting = {
		LV_two = {
			leading = "\n" .. ast .. "* <small>(\'\']\'\'",
			trailing = ")</small>",
		},
		POJ = {
			leading = "\n" .. ast .. "** <small>'']''</small>: <span style=\"font-family: Consolas, monospace;\">",
			trailing = "</span>",
		},
		TL = {
			leading = "\n" .. ast .. "** <small>'']''</small>: <span style=\"font-family: Consolas, monospace;\">",
			trailing = "</span>",
		},
		PSDB = {
			leading = "\n" .. ast .. "** <small>'']''</small>: <span style=\"font-family: Consolas, monospace;\">",
			trailing = "</span>",
		},
		IPA = {
			leading = "\n" .. ast .. "** <small>] (",
			trailing = ")</small>: ",
		}
	}
	
	local IPA_available_list = { "Xiamen", "Quanzhou", "Zhangzhou", "Taiwan" }
	
	export.poj_check_invalid(text)
	local all_readings, locations, output_text = {}, {}, {}
	
	for i, reading in ipairs(split(text, "/")) do
		if find(reading, ":") then
			local reading_part = split(reading, ":")
			locations = {}
			all_readings = reading_part
			for location_abbrev in mw.text.gsplit(reading_part, ",") do
				if location_alias then
					error("Invalid Min Nan location code: " .. location_abbrev .. ", maybe you meant: " .. location_alias)
				end
				if not location_list then
					error("The region label '" .. location_abbrev .. "' cannot be found. Please see ].")
				end
				table.insert(locations, location_list)
			end
		else
			locations = IPA_available_list
			all_readings = reading
		end
	end
	
	if not find(text, ":") then
		table.insert(output_text, formatting.LV_two.leading .. formatting.LV_two.trailing ..
			formatting.POJ.leading .. export.poj_display(text) .. formatting.POJ.trailing ..
			formatting.TL.leading .. export.poj_to_tl_conv(text) .. formatting.TL.trailing)
		
		if not find(text, "%-%-") then
			local psdb_hash = export.poj_to_psdb_conv(text)
			if not find(psdb_hash, "error") then
				table.insert(output_text, formatting.PSDB.leading .. psdb_hash .. formatting.PSDB.trailing)
			end
			for _, IPA_location in ipairs(IPA_available_list) do
				IPA_location = IPA_location == "Taiwan" and { "Taipei", "Kaohsiung" } or { IPA_location }
				for _, location in ipairs(IPA_location) do
					table.insert(output_text, formatting.IPA.leading .. location_link .. formatting.IPA.trailing)
					local reading_IPA_hash = {}
					for poj_reading in mw.text.gsplit(text, "/") do
						table.insert(reading_IPA_hash, export.generate_IPA(poj_reading, location))
					end
					table.insert(output_text, table.concat(reading_IPA_hash, ", "))
					if #reading_IPA_hash > 1 then
						table.insert(output_text, string.format("]", #reading_IPA_hash))
					end
				end
			end
		end
	else
		for i, poj_reading in ipairs(all_readings) do
			table.insert(output_text, formatting.LV_two.leading)
			
			local location_hash = {}
			for _, location_name in ipairs(locations) do
				table.insert(location_hash, location_link)
			end
			table.insert(output_text, ": " .. table.concat(location_hash, ", ") .. formatting.LV_two.trailing)
			
			table.insert(output_text, formatting.POJ.leading .. export.poj_display(poj_reading) .. formatting.POJ.trailing ..
				formatting.TL.leading .. export.poj_to_tl_conv(poj_reading) .. formatting.TL.trailing)
			
			if not find(poj_reading, "%-%-") then
				local psdb_hash = export.poj_to_psdb_conv(poj_reading)
				if not find(psdb_hash, "error") then
					table.insert(output_text, formatting.PSDB.leading .. psdb_hash .. formatting.PSDB.trailing)
				end
				
				local IPA_readings = {}
				for j, location_name in ipairs(locations) do
					location_name = gsub(location_name, '^Taiwan%-?(?)$', {  = 'Taipei',  = 'Kaohsiung',  = 'Taiwan' })
					loc = {
						 = { 'Taipei', 'Kaohsiung' },
						 = { 'Xiamen-d' },
						 = { 'Xiamen', 'Quanzhou', 'Zhangzhou' }
					}
					location_name = loc or { gsub(location_name, '%-d$', '') }
					for k, location in ipairs(location_name) do
						local loc = gsub(location, '%-d$', '')
						if IPA_available then
							local poj_to_ipa = export.generate_IPA(poj_reading, location)
							if IPA_readings then
								table.insert(IPA_readings, location_link)
							else
								IPA_readings = { j + (k/10), { location_link } }
							end
						end
					end
				end
				for reading, reading_info in pairs(IPA_readings) do
					table.insert(output_text, formatting.IPA.leading .. table.concat(reading_info, ", ") ..
						formatting.IPA.trailing .. reading)
				end
			end
		end
	end
	return table.concat(output_text)
end

function export.generate_IPA(text, location)
	-- (Wyang) I can't seem to find an example where 'triple' is used.. The code is below: 
	
	--if match(p, "%(") then
	--	p = gsub(p, "", "")
	--	triple = true
	--end
	--if triple then
	--	if tone == "一" then
	--		ipa = (initial .. final .. "一至七 " .. initial .. final .. "一至七 " .. initial .. final .. (i == #tone and "一" or "一至七"))
	--	elseif tone == "二" then
	--		ipa = (initial .. final .. "二至一 " .. initial .. final .. "二至一 " .. initial .. final .. (i == #tone and "二" or "二至一"))
	--	elseif tone == "三" then
	--		ipa = (initial .. final .. "三至二 " .. initial .. final .. "三至二 " .. initial .. final .. (i == #tone and "三" or "三至二"))
	--	elseif tone == "四A" then
	--		ipa = (initial .. final .. "四至八 " .. initial .. final .. "四至八 " .. initial .. final .. (i == #tone and "四" or "四至八"))
	--	elseif tone == "四B" then
	--		final = gsub(final, "ʔ", "(ʔ)")
	--		ipa = (initial .. final .. "四至二 " .. initial .. final .. "四至二 " .. initial .. final .. (i == #tone and "四" or "四至二"))
	--	elseif tone == "五" then
	--		if loc == "Quanzhou" or loc == "Taipei" then
	--			ipa = (initial .. final .. "五 " .. initial .. final .. "五至三 " .. initial .. final .. (i == #tone and "五" or "五至三"))
	--		else
	--			ipa = (initial .. final .. "五 " .. initial .. final .. "五至七 " .. initial .. final .. (i == #tone and "五" or "五至七"))
	--		end
	--	elseif tone == "七" then
	--		ipa = (initial .. final .. "七至一 " .. initial .. final .. "七至三 " .. initial .. final .. (i == #tone and "七" or "七至三"))
	--	elseif tone == "八A" then
	--		ipa = (initial .. final .. "八至四 " .. initial .. final .. "八至四 " .. initial .. final .. (i == #tone and "八" or "八至四"))
	--	elseif tone == "八B" then
	--		final = gsub(final, "ʔ", "(ʔ)")
	--		ipa = (initial .. final .. "八至五 " .. initial .. final .. "八至三 " .. initial .. final .. (i == #tone and "八" or "八至三"))
	--	end
	--end

	if type(text) == "table" then text, location = text.args, text.args end
	
	local tone_from_mark = {
		 = "1", 
		 = "2",
		 = "3",
		 = "4A",  = "4A",  = "4A",
		 = "4B",
		 = "5",
		 = "6",
		 = "7",
		 = "8A",  = "8A",  = "8A",
		 = "8B",
		 = "9",
	}
	
	local initial_ipa = {
		 = "p",  = "pʰ",  = "m",  = "b",  = "f",
		 = "t",  = "tʰ",  = "n",  = "l",  = "d",
		 = "t͡s",  = "t͡sʰ",  = "d͡z",  = "s",   = "ʃ",
		 = "k",  = "kʰ",  = "ŋ",  = "ɡ", 
		 = "h",  = "ɹ",  = "w",  = "j",  = "",
		
		 = "z",
	}
	
	local palatal = {  = "ɕ",  = "ʑ" }
	
	local final_ipa = {
		 = "a",  = "aʔ",  = "ãʔ",
		 = "ai",  = "aiʔ",  = "ãi",
		 = "ak̚",  = "am",  = "an",  = "ã", 
		 = "aŋ",  = "ap̚",  = "at̚",
		 = "au",  = "auʔ",  = "ãuʔ",  = "ãu",
		
		 = "e",  = "ɛ",  = "ɛʔ", 
		 = "ɛk̚",  = "ɛŋ",
		 = "eʔ",  = "ẽʔ",  = "ei",  = "iɪk̚", 
		 = "ɛm",  = "ɛn",  = "ẽ", 
		 = "iɪŋ",  = "ɵy",  = "ə",
		 = "əʔ",  = "əm",  = "ən",
		 = "ət",  = "ɛt",
		
		 = "i",  = "ia",  = "iaʔ",
		 = "iãʔ",  = "iak̚", 
		 = "iam",  = "iɛn",  = "iã",
		 = "iaŋ",  = "iap̚",  = "iɛt̚",
		 = "iau",  = "iauʔ",  = "iãuʔ",  = "iãu",
		 = "iʔ",  = "ĩʔ",
		 = "im",  = "in",  = "ĩ",  = "iŋ",
		 = "io",  = "ioʔ",  = "iɔʔ",  = "iɔ",
		 = "iɔk̚",  = "iɔ̃",  = "iɔŋ",
		 = "ip̚",  = "ɯ",  = "ɯʔ",  = "it̚",
		 = "iu",  = "iuʔ",  = "iũ",
		
		 = "m̩",  = "m̩ʔ",
		 = "ŋ̍",  = "ŋ̍ʔ",
		
		 = "o",  = "ɔ",  = "ua",  = "uaʔ",  = "uai",
		 = "uaiʔ",  = "uãiʔ",  = "uãi",  = "uan",  = "uã", 
		 = "uaŋ",  = "uat̚",
		 = "ue",  = "ueʔ",  = "uẽʔ",  = "uẽ", 
		 = "oʔ",  = "ɔʔ",  = "ɔ̃ʔ",  = "ɔi",  = "ɔ̃i",
		 = "ɔk̚",  = "ɔm",  = "ɔ̃",  = "ɔŋ",  = "ɔp̚",
		 = "ɔt̚",  = "ou",
		
		 = "u",  = "uʔ",  = "ui",
		 = "uiʔ",  = "uĩʔ",  = "uĩ",
		 = "ok̚",  = "om",  = "ū",
		 = "un",  = "oŋ",  = "ut̚",
		
		 = "y",  = "yn",
		
		 = "iei",
		 = "ɛ̃",  = "ɛ̃",  = "ɛ̃",
		 = "ɛ̃ʔ",  = "ɛ̃ʔ",
		 = "ɤ",  = "iɤ",
		 = "ɤʔ",  = "iɤʔ",
		 = "uɛ",
		 = "uɛ̃",
		 = "uɛʔ",
		 = "ɛŋ",  = "ɛk̚",
		 = "eŋ",  = "ek̚",
		 = "eŋ",  = "ek̚",  = "ik̚",
		 = "ɔu",  = "ɔ̃u",
	}
	
	local tone_sandhi = { }
		-- (Wyang) I'm not sure about the 'Xd' ones, when tone X is followed by the diminutive 仔.
	tone_sandhi = {
		 = "7",  = "1",  = "2",  = "8A",  = "2",
		 = "7",  = "3",  = "4A",  = "3",
	}
	tone_sandhi = { -- 2 and 4 are special cases
		 = "7",  = "10",
		 = "9",  = "9",  = "11",  = "11",
	}
	tone_sandhi = {
		 = "1",  = "5",  = "2",  = "8A",  = "4B",
		 = "6",  = "6",  = "6",  = "6",  = "6",
	}
	tone_sandhi = {
		 = "1",  = "5",  = "2",  = "8A",  = "4B",
		 = "S",  = "S",  = "S",  = "S",  = "S",
	}
	tone_sandhi = {
		 = "7",  = "1",  = "2",  = "4A",  = "4B",
		 = "7",  = "3",  = "7",  = "7",
	}
	tone_sandhi = {
		 = "7",  = "1",  = "2",  = "S",  = "2", 
		 = "7",  = "3",  = "3",  = "3",
		 = "1",  = "7",
	}
	tone_sandhi = {
		 = "7",  = "1",  = "2",  = "8A",  = "2", 
		 = "3",  = "3",  = "4A",  = "3",  = "9",
		 = "1",  = "1",  = "7",  = "7",  = "7",
	}
	tone_sandhi = {
		 = "7",  = "1",  = "2",  = "8A",  = "2",
		 = "7",  = "3",  = "4A",  = "3",  = "9",
		 = "1",  = "1",  = "7",  = "7",  = "7",
	}
	tone_sandhi = { -- 3 and 4B are special cases
		 = "7",  = "5",  = "8A",
		 = "3",  = "3",  = "4A",  = "3"
	}
	tone_sandhi = { --Xiamen/Zhangzhou-like
		 = "7",  = "1",  = "2",  = "8As",  = "2",
		 = "3",  = "3",  = "3",  = "3"
	}
	tone_sandhi = {
		 = "7",  = "1",  = "1",  = "8A",  = "8B", 
		 = "7",  = "6",  = "3",  = "4A",  = "4B",  = "9"
	}
	tone_sandhi = tone_sandhi

	local tone_value = { }
	tone_value = {
		 = "44",  = "53",  = "21",  = "32",  = "32", 
		 = "24",  = "22",  = "4",  = "4",
	}
	tone_value = {
		 = "44",  = "31",  = "112",  = "32",  = "32", 
		 = "24",  = "22",  = "53",  = "53",
		 = "11",  = "42",  = "1",  = "4" --sandhi-only tones
	}
	tone_value = {
		 = "33",  = "554",  = "41",  = "5",  = "5",
		 = "24",  = "22",  = "41",  = "24",  = "24",
	}
	tone_value = {
		 = "33",  = "554",  = "41",  = "5",  = "5",
		 = "24",  = "33",  = "41",  = "24",  = "24",
		 = "22", --sandhi-only
	}
	tone_value = {
		 = "55",  = "53",  = "11",  = "32",  = "32",
		 = "213",  = "33",  = "14",  = "14",
	}
	tone_value = {
		 = "44",  = "53",  = "21",  = "32",  = "32",
		 = "13",  = "22",  = "121",  = "121",
		 = "5", --sandhi-only
	}
	tone_value = {
		 = "44",  = "53",  = "11",  = "32",  = "32", 
		 = "24",  = "33",  = "4",  = "4",  = "35"
	}
	tone_value = {
		 = "44",  = "41",  = "21",  = "32",  = "32", 
		 = "23",  = "33",  = "4",  = "4",  = "35"
	}
	tone_value = {
		 = "44",  = "53",  = "12",  = "32",  = "32", 
		 = "24",  = "22",  = "54",  = "54"
	}
	tone_value = { --Xiamen/Zhangzhou-like
			 = "44",  = "42",  = "21",  = "32",  = "32", 
			 = "24",  = "22",  = "43",  = "43",  = "4"
	}
	tone_value = {
		 = "33",  = "445",  = "21",  = "3",  = "3", 
		 = "23",  = "55",  = "21",  = "4",  = "4",  = "5"
	}
	tone_value = tone_value
	
	local function get_sandhi_from_post(location, current, post)
		if post then
			if location == "Tong'an" then
				if current == "2" then
					if find(post, "^$") or find(post, "^8$") then
						return "7"
					else
						return "5"
					end
				elseif find(current, "^4$") then
					if post == "2" then
						return "10"
					else
						return "12"
					end
				end
			elseif location == "Kinmen" then
				if current == "3" or current == "4B" then
					if find(post, "^$") or find(post, "^4$") then
						return "1"
					else
						return "2"
					end
				end
			end
		end
	end
	
	local function get_tone(text)
		local tone = gsub(text, "^+(?)*(?)ⁿ?", function(tone_symbol, coda)
			return tone_from_mark end)
		return tone
	end
	
	local function nasalize(final)
		if find(final, "^mh?$") or find(final, "^ngh?$") then return final end
		if find(final, "o͘h?$") then
			final = gsub(final, "͘", "")
		elseif find(final, "oh?$") then
			error("Invalid POJ: nasal initial cannot go with -" .. final)
		elseif find(final, "eeh?$") then
			final = gsub(final, "ee", "e")
		end
		return final .. "ⁿ"
	end
	
	local formatting = {
		leading = "<span class=\"IPA\">/",
		trailing = "/</span>"
	}
	
	local tone_superscript = {  = "¹",  = "²",  = "³",  = "⁴",  = "⁵",  = "⁻" }
	local word_result = {}
	local attention = {}
	
	text = gsub(text, " ", "-")
	text = gsub(text, ",", "#")
	text = gsub(text, "%-?%.%.%.%-?", "#")
	text = gsub(text, "#$", "")
	text = gsub(text, "#%-?", " ")
	text = mw.ustring.toNFD(mw.ustring.lower(text))
	for word in mw.text.gsplit(text, " ") do
		local initial, final, tone, diminutive, sandhi, result = {}, {}, {}, {}, {}, {}
		local syllables = split(word, "-")
		syllables.length = #syllables
		for index, syllable in ipairs(syllables) do
			if syllable == "仔" then
				syllable = "a".."́"
				diminutive = true
			end
			local original_syllable = syllable
			syllable = gsub(syllable, "", "")
			if not find(syllable, "") then
				final = match(syllable, "^?h?h?(ngh?)$") or match(syllable, "^h?(mh?)$")
				initial = syllable ~= final and sub(syllable, 1, len(syllable) - len(final)) or "" --original code: "ʔ"
			else
				initial = match(syllable, "^??h?")
				final = sub(syllable, len(initial) + 1, -1)
			end
			tone = get_tone(sub(original_syllable, len(initial) + 1, -1))
			location = gsub(location, '%-d$', '')
			local nasal_initial = match(initial, "^g?$")
			if nasal_initial then
				if find(final, "ⁿ") then
					error("Too much nasality in POJ. " .. original_syllable .. " should be " .. gsub(original_syllable, "ⁿ", ""))
				end
				if location ~= "Penang" and location ~= "Philippines" and location ~= "Singapore" then --exception for Penang, Philippines and Singapore
					final = nasalize(final)
				end
			end
			local nasal_final = match(final, "^") or match(final, "ⁿ")
			local not_nasal_initial = match(initial, "^$")
			if ((nasal_initial and not nasal_final) or (not_nasal_initial and nasal_final)) and (location ~= "Penang" and location ~= "Philippines" and location ~= "Singapore") then --exception for Penang, Philippines and Singapore
				error("POJ error: nasality of initial and final not synchronized.")
			end
			
			table.insert(attention, export.poj_check_syllable(initial, final, location))
			
			initial = initial_ipa] or initial_ipa]
			final = final_ipa] or final_ipa]
				or error("Cannot recognise " .. final .. ".")
			if find(initial, "ʰ?") and find(final, "^") then
				initial = gsub(initial, "", palatal)
			end
			if index < syllables.length then
				final = gsub(final, "ʔ", "(ʔ)")
			end
		end
		
		for index = 1, syllables.length do
			sandhi = tone_value]
			local sandhi_hash = get_sandhi_from_post(location, tone, tone)
				or tone_sandhi..(diminutive and "d" or "")]
				or tone_sandhi]
			if index < syllables.length and tone_value ~= tone_value] then
				sandhi = sandhi .. "-" .. tone_value
			end
			table.insert(result, initial .. final .. sandhi)
		end
		table.insert(word_result, table.concat(result, " "))
	end
	return (gsub(formatting.leading .. table.concat(word_result, " ") ..
		formatting.trailing, "", tone_superscript)) .. table.concat(attention)
end

function export.poj_to_psdb_conv(text)
	if type(text) == "table" then text = text.args end
	
	local readings = split(mw.ustring.lower(text), "/", true)
	
	for i = 1, #readings do
		-- will ignore # boundary marker
		local parts = split(gsub(readings, "#", ""), " ", true)
		for j = 1, #parts do
			local initial = {}
			local final = {}
			local psdb = {}
			local tone = {}
			local tonesandhi = {}
			local neutral = {}
			parts = gsub(parts, "%-%-", "-0")
			local p = split(parts, "-",true)
			local ar = {}
			local triple = {}
			for i, item in ipairs(p) do
				if find(item, "仔") then
					item = gsub(item, "仔", "á")
					ar = true
				end
				if find(item, "%(") then
					item = gsub(item, "", "")
					triple = true
				end
				if find(item, "^0") then
					item = gsub(item, "0", "")
					neutral = true
				end
				item = gsub(item, "ớ", "óo")
				item = gsub(item, "ờ", "òo")
				item = gsub(item, "ơ̂", "ôo")
				item = gsub(item, "ơ̄", "ōo")
				item = gsub(item, "ơ̍", "o̍o")
				item = gsub(item, "ơ", "oo")
				item = gsub(item, "͘", "o")
				item = gsub(item, "",{ = "捌",  = "伍",  = "柒",  = "叁"})
				if find(item, "?") or find(item, "?g?") then
					if find(item, "捌") then
						tone = "八"
					else
						tone = "四"
					end
				elseif find(item, "") then
					tone = "二"
				elseif find(item, "") then
					tone = "三"
				elseif find(item, "") then
					tone = "五"
				elseif find(item, "") then
					tone = "七"
				else
					tone = "一"
				end
				item = gsub(item, "",{ = "a",  = "i",  = "u",  = "e",  = "o",  = "m",  = "n",  = "",  = "a",  = "i",  = "u",  = "e",  = "o",  = "n",  = "",  = "a",  = "i",  = "u",  = "e",  = "o",  = "",  = "a",  = "i",  = "u",  = "e",  = "o",  = "",  = ""})
				if sub(item,1,3) == "chh" then
					initial = "chh"
					final = sub(item,4,-1)
				elseif sub(item,1,1) == "m" then
					if sub(item,2,2) == "h" then
						initial = ""
						final = "mh"
					elseif sub(item,2,2) == "" then
						initial = ""
						final = "m"
					else
						initial = "m"
						final = sub(item,2,-1)
					end
				elseif sub(item,1,2) == "ng" then
					if sub(item,3,3) == "h" then
						initial = ""
						final = "ngh"
					elseif sub(item,3,3) == "" then
						initial = ""
						final = "ng"
					else
						initial = "ng"
						final = sub(item,3,-1)
					end
				elseif find(item, "^h") then
					initial = sub(item,1,2)
					final = sub(item,3,-1)
				elseif find(item, "^") then
					initial = sub(item,1,1)
					final = sub(item,2,-1)
				else
					initial = ""
					final = item
				end
				if find(initial, "^chh?$") or initial == "s" then
					if find(final, "^i") then
						initial = initial .. "i"
					end
				end
				p = item
			end
			for i = 1, #p do
				if tone == "一" then
					tonesandhi = "七"
				elseif tone == "二" then
					tonesandhi = "一"
				elseif tone == "三" then
					tonesandhi = ar and "一" or "二"
				elseif tone == "四" then
					tonesandhi = "八"
				elseif tone == "五" then
					tonesandhi = "七"
				elseif tone == "七" then
					tonesandhi = ar and "七" or "三"
				elseif tone == "八" then
					tonesandhi = "四"
				end
				if triple then
					local tonesandhi1 = nil
					if tone == "五" then
						tonesandhi1 = "五"
					elseif tone == "七" then
						tonesandhi1 = "一"
					end
					psdb = (psdb_initial] or "error")
							..(psdb_final(final..(tonesandhi1 or tonesandhi)) or "error")
							..psdb_initial]
							..psdb_final(final..tonesandhi)
							..psdb_initial]
							..psdb_final(final..(i == #tone and tone or tonesandhi))
				else
					psdb = (psdb_initial] or "error")
							..(psdb_final(final..(i == #tone and tone or tonesandhi)) or "error")
				end
				if neutral then
					psdb = "~" .. (psdb_initial] or "error")
							..(psdb_final(final.."七") or "error")
				end --psdb = p
			end
			parts = table.concat(psdb, "")
		end
		readings = table.concat(parts, " ")
		readings = gsub(readings, "'+", "'")
		readings = gsub(readings, "^'", "")
		readings = gsub(readings, "()'", "%1")
		readings = gsub(readings, "()'()", "%1%2")
		readings = gsub(readings, "()'()", "%1%2")
		readings = gsub(readings, "()'g", "%1g")
		readings = gsub(readings, "()'h", "%1h")
	end
	
	return (gsub(table.concat(readings, ", "),'/()',' / %1'))
end

function export.poj_display(text)
	if type(text) == "table" then text = text.args end
	text = gsub(text, "仔", "á")
	local readings = split(text, "/")
	for i = 1, #readings do
		readings = gsub(readings, "+:", "")
		readings = gsub(readings, "#", "")
		readings = gsub(readings, "%((+)%)", "%1-%1-%1")
		if not find(readings, " ") then
			readings = " .. "#Min Nan|" .. readings .. "]]"
		end
	end
	text = table.concat(readings, " / ")
	return text
end

function export.pengim_check_syllable(initial, final, loc)
	local validInitials = {
		 = 1,  = 1,  = 1,  = 1, 
		 = 1,  = 1,  = 1,  = 1, 
		 = 1,  = 1,  = 1,  = 1,  = 1,
		 = 1,  = 1,  = 1,  = 1,  = 1,
	}
	local validFinals = {
		 = 1,  = 1,  = 1,  = 1,  = 1, 
		 = 1,  = 1,  = 1,  = 1,  = 1, 
		 = 1,  = 1,  = 1,  = 1, 
		 = 1,  = 1,  = 1,  = 1,  = 1,  = 1, 
		 = 1,  = 1,  = 1,  = 1, 
		 = 1,  = 1,  = 1, 
		 = 1,  = 1,  = 1,  = 1, 
		 = 1,  = 1,  = 1,  = 1, 
		 = 1,  = 1,  = 1, 
		 = 1,  = 1,  = 1, 
		 = 1,  = 1,  = 1, 
		 = 1,  = 1,  = 1,  = 1,  = 1,  = 1, 
		 = 1,  = 1, 
	}
	local moreValidFinals = {
		 = {
			 = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1, 
		},
		 = {
			 = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1, 
			 = 1,  = 1, 
		},
		 = {
			 = 1,  = 1,  = 1,  = 1,
			 = 1,  = 1, 
			 = 1,  = 1, 
			 = 1,  = 1, 
		},
		 = {
			 = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1, 
			 = 1,  = 1, 
		},
		 = {
			 = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1, 
		},
		 = {
			 = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1, 
			 = 1,  = 1,  = 1,  = 1, 
			 = 1, 
		},
	}
	if not (validInitials and (validFinals or moreValidFinals)) then
		return "]"
	end
	return nil
end

local pengim_to_ipa_two_letters_above = {
	 = "ɡ",  = "β",  = "ŋ",
	 = "au",
}

local pengim_to_ipa_one_letter = {
	--initials
	 = "m",  = "n",
	 = "p",  = "t",  = "k",
	 = "pʰ",  = "tʰ",  = "kʰ",
	 = "s",  = "h",
	 = "d͡z",
	 = "t͡s",
	 = "t͡sʰ",
	 = "l",
	--vowels
	 = "a",
	 = "e",
	 = "ɯ",
	 = "i",
	 = "o",
	 = "u",
	--tones
	 = "³³⁻²³",
	 = "⁵²⁻³⁵",
	 = "⁵²⁻²¹",
	 = "²¹³⁻⁵⁵",
	 = "²⁻⁴",
	 = "⁵⁵⁻¹¹",
	 = "³⁵⁻¹¹",
	 = "¹¹",
	 = "⁴⁻²",
}

local pengim_tone_value = {}
pengim_tone_value = {
	 = "33",  = "53",  = "213",  = "2",
	 = "55",  = "35",  = "11",  = "5"
}
pengim_tone_value = pengim_tone_value
pengim_tone_value = pengim_tone_value
pengim_tone_value = pengim_tone_value
pengim_tone_value = {
	 = "33",  = "53",  = "31",  = "2",
	 = "55",  = "313",  = "11",  = "5"
}

local pengim_tone_sandhi = {}
pengim_tone_sandhi = {
	 = "23",
	 = "23",  = "35",
	 = "31",  = "53",
	 = "3",  = "5",
	 = "11",  = "21",  = "",  = "2"
}
pengim_tone_sandhi = {
	 = "23",
	 = "35",  = "35",
	 = "55",  = "55",
	 = "5",  = "5",
	 = "11",  = "21",  = "",  = "2"
}
pengim_tone_sandhi = pengim_tone_value
pengim_tone_sandhi = pengim_tone_value
pengim_tone_sandhi = {
	 = "23",
	 = "31",  = "31",
	 = "55",  = "55",
	 = "5",  = "5",
	 = "11",  = "33",  = "33",  = "2"
}

local pengim_to_ipa_fix = {
	 = "b",
	+)"] = "p̚%1",
	+)"] = "k̚%1",
	+)"] = "ʔ%1",
}

local pengim_to_ipa_nasal = {
	 = "ã",
	 = "ẽ", -- ê
	 = "ɯ̃", -- e
	 = "ĩ",
	 = "õ",
	 = "ũ",
	 = "",
}

function export.pengim_to_ipa_conv(text)
	local result
	for key, val in pairs(pengim_to_ipa_two_letters_above) do
		text = gsub(text, key, val)
	end
	text = gsub(text, "() (+)2$", "%1 %22")
	local function verbose_function(char) return pengim_to_ipa_one_letter or char end
	-- This should work, but it doesn't convert the tone number in "diên1":
	-- result = gsub(text, ".", pengim_to_ipa_one_letter)
	result = gsub(text, ".", verbose_function)
	result = result .. "/"
	for key, val in pairs(pengim_to_ipa_fix) do
		result = gsub(result, key, val)
	end
	result = gsub(result, "+nʔ?+", function (a)
		return gsub(a, ".", pengim_to_ipa_nasal)
	end)
	result = gsub(result, "(⁻+)/", function(a) return (a ~= "⁻²¹" and "/" or a .. "/") end)
	result = gsub(result, "/$", "")
	result = gsub(result, "/", "/, /")
	return "/" .. result .. "/"
end

function export.pengim_display(text)
	text = gsub(text, "()/", "%1 / ")
	text = gsub(text, "()", "<sup>%1</sup>") -- note: originally (+) but it seems like websites have the final tone within parentheses, if at all
	return text
end

function export.pengim_to_pojlike_conv(text)
	-- kind of based on MTR (http://www.ispeakmin.com/bbs/viewthread.php?tid=2784)
	local words = split(text, '/')
			
	local tone_marks = {
		 = '', 
		 = '́',
		 = '̀',
		 = '',
		 = '̂',
		 = '̆', -- this is a breve; MTR: breve; current hokkien dialect convention: hacek; missionary: tilde or breve??
		 = '̄',
		 = '̍'
	}
	
	local function get_tone_mark(a, num) return tone_marks .. a end
	
	local function convert_final(x,c,t) -- convert final -g and -b (but not -ng)
		if c=='b' then c='p'
		elseif c=='g' then c='k' end
		return x..c..t
	end
	
	local cons_correspondences = { ='b', ='g', ='p', ='t',
		='k', ='ph', ='th', ='kh', ='ts', ='tsh',
		='j' }
	
	local function nasalization(n,h,t) return h..'ⁿ'..t end
	
	for i, word in ipairs(words) do
		local syllables = split(word, ' ')
		for i, syllable in ipairs(syllables) do
			syllable = gsub(syllable, '^h?', cons_correspondences)
			syllable = gsub(syllable, '()()(%d)', convert_final)

			syllable = gsub(syllable, '', { ='e', ='ṳ' } )
			syllable = gsub(syllable, 'ao', 'au' )

			syllable = gsub(syllable, '(n)(h?)(%d)', nasalization)

			if find(syllable, 'uai') then
				syllable = gsub(syllable, 'uai', 'ua符i')
			elseif find(syllable, '') then
				syllable = gsub(syllable, '()i', '%1符i') -- ?i
				syllable = gsub(syllable, 'i()', 'i%1符') -- i?
				syllable = gsub(syllable, '()()', '%1符%2') -- ?u
				syllable = gsub(syllable, '()()', '%1%2符') -- u?
			elseif find(syllable, '') or find(syllable, '^') then
				syllable = gsub(syllable, '()', '%1符')
			elseif find(syllable, 'ngh?%d') then
				syllable = gsub(syllable, 'ng(h?)(%d)', 'n符g%1%2')
			elseif find(syllable, 'h?%d') then
				syllable = gsub(syllable, '()(h?)(%d)', '%1符%2%3')
			end
			
			syllable = gsub(syllable, '符(.*)(%d)', get_tone_mark)

			syllables = syllable
		end
		words = table.concat(syllables, ' ')
	end

	return mw.ustring.toNFC(table.concat(words, ' / '))
end

return export