-- This module is for 東莞話 (Dongguan Yue/Cantonese), a lect of Yue Chinese
-- Representative dialect: 莞城
-- Romanisation: Jyutping++ (referred to as jpp below)
-- References:
-- 1. 東莞方言詞典 (1997) by 詹伯慧 and 陳曉錦
-- 2. 珠江三角洲方言字音對照 (1987)
-- Also see [[Category:Dongguan Cantonese]]

local export = {}

-- l merges into ng
local initials = {
	b = "p",  p = "pʰ",  m = "m",  f = "f",
	d = "t",  t = "tʰ",  n = "n",
	z = "t͡s", c = "t͡sʰ", s = "s",
	g = "k",  k = "kʰ",  ng = "ŋ", h = "h",
	gw = "kʷ", kw = "kʰʷ",
	j = "z",  w = "v",
	[""] = "", -- glottal stop?
}

local finals = {
	aa="a",aai="ai",aau="au",                 aang="aŋ",aah="aʔ",         aak="ak̚",
	       ai="ɐi",          am="ɐm", an="ɐn",           ap="ɐp̚", at="ɐt̚", ak="ɐk̚",
	e="ɛ",                                     eng="ɛŋ",                   ek="ɛk̚",
	                                          eang="əŋ",                  eak="ək̚",
	oe="ø",                          oen="øn",oeng="øŋ",         oet="øt̚",oek="øk̚",
	o="ɔ", oi="ɔi", ou="ɔu",                   ong="ɔŋ",                   ok="ɔk̚",
	i="i",         iu="iu",       ["in"]="in",                    it="it̚", ik="ɪk̚",
	u="u", ui="ui",                   un="un", ung="ʊŋ",          ut="ut̚", uk="ʊk̚",
	yu="yu",
	m="m̩",
}

-- "6" merges into "3"
local tones = {
	["1"] = "²¹³",--陰平
	["2"] = "³⁵", --陰上
	["3"] = "³²", --去
	["4"] = "²¹", --陽平
	["5"] = "¹³", --陽上
	["7"] = "⁴⁴", --陰入
	["8"] = "²²", --陽入
	["9"] = "²⁴", --變入
	["0"] = "⁵⁵", --(變調)
}

local function validate(text)
	text = text:gsub(","," ")
	if text:match("%d%d") then
		error("Dongguan: Please use a hyphen to indicate a changed tone.")
	end
	if text:sub(1,1) == " " or text:sub(-1,-1) == " " or text:match("  ") then
		error("Dongguan: Empty syllable detected.")
	end
end

function export.jpp_to_ipa(text)
	validate(text)
	text = text:gsub("[^ ,]+",function(syllable)
		local a,b,c,d = syllable:match("^([bpmfdtnzcsgkhjw]?[gw]?)([aeiouym]%l*)([12345789])%-?([02]?)$")
		if not a then
			error("Dongguan: Invalid syllable: " .. syllable)
		end
		return (initials[a] or error("Dongguan: Unrecognised initial: " .. a))
			.. (finals[b] or error("Dongguan: Unrecognised final: " .. b))
			.. tones[c]
			.. (d ~= "" and ("⁻" .. tones[d]) or "")
	end)
		:gsub(",", "/, /")
	return "/" .. text .. "/"
end

return export
"https://si.wiktionary.org/w/index.php?title=Module:yue-pron/Dongguan&oldid=185445" වෙතින් සම්ප්‍රවේශනය කෙරිණි