local export = {}

local hsv_initial = {
	["b"] = "p", ["p"] = "pʰ", ["m"] = "ᵐb", ["f"] = "f", ["v"] = "v",
	["d"] = "t", ["t"] = "tʰ", ["n"] = "ⁿd", ["l"] = "l", ["lh"] = "ɬ",
	["g"] = "k", ["k"] = "kʰ", ["ng"] = "ᵑɡ", 
	["z"] = "t͡s", ["c"] = "t͡sʰ",
	["y"] = "j", ["s"] = "s", ["h"] = "h", [""] = ""
}

local hsv_final = {
	["a"] = "a", ["ai"] = "ai", ["au"] = "au", ["am"] = "am",
	["an"] = "an", ["ang"] = "aŋ", ["ap"] = "ap̚", ["at"] = "at̚",
	["ak"] = "ak̚", 

	["i"] = "i", ["iu"] = "iu", ["im"] = "im", ["in"] = "in", 
	["ip"] = "ip̚", ["it"] = "it̚",

	["ie"] = "iɛ", ["iau"] = "iau", ["iam"] = "iam", ["iang"] = "iaŋ", 
	["iap"] = "iap̚", ["iak"] = "iak̚",

	["u"] = "u", ["ui"] = "ui", ["un"] = "un", ["ut"] = "ut̚", 

	["e"] = "ə", ["ei"] = "ei", ["eu"] = "eu", ["em"] = "em", ["en"] = "en", 
	["uung"] = "ɵŋ", ["ep"] = "ep̚", ["et"] = "et̚", ["uuk"] = "ɵk̚", ["uut"] = "ɵt̚",

	["o"] = "ᵘɔ", ["oi"] = "ᵘɔi", ["on"] = "ᵘɔn", ["ong"] = "ɔŋ", 
	["ot"] = "ᵘɔt̚", ["ok"] = "ɔk̚", 
	
	["m"] = "m̩"
}

local hsv_tone = { ["1"]="³³", ["2"]="⁵⁵", ["3"]="²²", ["4"]="²¹", ["5"]="³²" }

function export.hoisanva_to_ipa(text)
	if text:match("2%*") or text:match("([1-5])%-%1") then
		error("Invalid Taishanese tone.")
	end
	
	text = text:gsub("[^ ,]+",function(syllable)
		local initial, final, tone, tone_ch = syllable:match("^([^aeiou]*)([^1-5]*)([1-5])([%*%-]?[1-5]?%*?)$")
		if final == "" then initial, final = "", initial end
		initial, final, tone = hsv_initial[initial], hsv_final[final], hsv_tone[tone]
		if not initial or not final then
			error("Invalid Taishanese syllable: " .. syllable)
		end
		local tone2 = false
		if tone_ch == "*" then
			tone2 = tone .. "⁵"
		elseif tone_ch:match("^%-[1-5]%*?$") then
			tone2 = hsv_tone[tone_ch:sub(2,2)]
				.. (tone_ch:sub(3,3) == "*" and "⁵" or "")
		elseif tone_ch ~= "" then
			error("Invalid Taishanese syllable: " .. syllable)
		end
		return initial..final..tone..(tone2 and ("⁻"..tone2) or "")
	end)
		:gsub(",","/, /")
	return "/" .. text .. "/"
end

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