Documentation for this module may be created at မဝ်ဂျူ:kxv-IPA/doc

local export = {}

local lang = require("Module:languages").getByCode("kxv")
local sc = require("Module:scripts").getByCode("Orya")
local m_IPA = require("Module:IPA")
local u = mw.ustring.char
local gsub = mw.ustring.gsub

local consonants = {
	--common
	["କ"]="k", ["ଖ"]="k", ["ଗ"]="ɡ", ["ଘ"]="ɡ", ["ଙ"]="ŋ",
	["ଚ"]="t͡ʃ", ["ଛ"]="t͡ʃ", ["ଜ"]="d͡ʒ", ["ଝ"]="d͡ʒ", ["ଞ"]="n̪",
	["ଟ"]="ʈ", ["ଠ"]="ʈ", ["ଡ"]="ɖ", ["ଢ"]="ɖ", ["ଣ"]="ɳ",
	["ତ"]="t̪", ["ଥ"]="t̪", ["ଦ"]="d̪", ["ଧ"]="d̪", ["ନ"]="n̪",
	["ପ"]="p", ["ଫ"]="p", ["ବ"]="b", ["ଭ"]="b", ["ମ"]="m",
	["ଯ"]="d͡ʒ", ["ୟ"]="j", ["ର"]="ɾ", ["ଲ"]="l", ["ଳ"]="ɭ",
	["ଵ"]="ʋ", ["ୱ"]="w", ["ଶ"]="s", ["ଷ"]="s", ["ସ"]="s", ["ହ"]="h",
	--nuktas
	["କ଼"]="q", ["ଖ଼"]="x", ["ଗ଼"]="ɣ", ["ଜ଼"]="z", ["ଝ଼"]="ʒ",
	["ଡ଼"]="ɽ", ["ଢ଼"]="ɽ", ["ଫ଼"]="f",
}

local vowel_diacritics  = {
	["ା"]="a", ["ା୕"]="aː", ["ି"]="i", ["ୀ"]="iː", ["ୁ"]="u", ["ୂ"]="uː",
	["େ"]="e", ["େ୕"]="eː", ["ୈ"]="ɔi̯", ["ୋ"]="o", ["ୋ୕"]="oː", ["ୌ"]="ɔu̯",
	["ୃ"]="ɾu", ["ୄ"]="ɾu", ["ୢ"]="lu", ["ୣ"]="lu",
	["୍"]="", [""] = "o",
}

local other = {
	["ଅ"]="o", ["ଅ୕"]="oː", ["ଆ"]="a", ["ଆ୕"]="aː", ["ଇ"]="i", ["ଈ"]="iː", ["ଉ"]="u", ["ଊ"]="uː",
	["ଏ"]="e", ["ଏ୕"]="eː", ["ଐ"]="ɔi̯", ["ଓ"]="o", ["ଓ୕"]="oː", ["ଔ"]="ɔu̯",
	["ଋ"]="ɾu", ["ୠ"]="ɾu", ["ଌ"]="lu", ["ୡ"]="lu",
	-- Other symbols
	['ଂ']='m̃', ['ଃ']='ʔ',
}

local adjust1 = {
	-- Assimilate the anusvara
	['m̃([kɡŋ])']='ŋ%1',
	['m̃([td]͡[ʃʒ])']='n̪%1', ['m̃(n̪)']='n̪%1',
	['m̃([ʈɖɳ])']='ɳ%1',
	['m̃([td]̪)']='n̪%1', ['m̃(n)']='n̪%1',
	['m̃([pbm])']='m%1',
	['m̃([%s%p])']='m%1', ['m̃$']='m',
	['([aeiou])୕']='%1ː',
}

local adjust2 = {
	['n̪d͡ʒ']='n̠ʲd͡ʒ', ['n̪t͡ʃ']='n̠ʲt͡ʃ',
}

function export.link(term)
	return require("Module:links").full_link{ term = term, lang = lang, sc = sc }
end

function export.to_IPA(text)

	text = gsub(
		text,
		"([କ-ହୟୱ]଼?)([ା-୍]?)([ଁଂ]?)",
		function(c, d, a)
			return consonants[c] .. vowel_diacritics[d] .. (a ~= "" and u(0x0303) or "")
		end)
	text = gsub(
		text,
		"([ଅ-ଔୠୡ])([ଁଂ]?)",
		function(n, a)
			return other[n] .. (a ~= "" and u(0x0303) or "")
		end)


	for k, v in pairs(adjust1) do
		text = gsub(text, k, v)
	end

	-- If an independent vowel is after another vowel, assume diphthong
	text = gsub(text, "([ɐaeiou]ː?)•", "%1")
	text = gsub(text, '.', other)

	-- Phonetic transcription
	text2 = text
	for k, v in pairs(adjust2) do
		text2 = gsub(text2, k, v)
	end

	return (text == text2 and { text } or { text, text2 })

end

function export.show(frame)

	local args = frame:getParent().args
	local page_title = mw.title.getCurrentTitle().text
	local text = args[1] or page_title
	local qualifier = args['q'] or nil

	local transcriptions = export.to_IPA(text)
	local IPA_text
	if not transcriptions[2] then
		IPA_text = m_IPA.format_IPA_full(
			lang,
			{ { pron = '/' .. transcriptions[1] .. '/' } })
	else
		IPA_text = m_IPA.format_IPA_full(
			lang,
			{ { pron = '/' .. transcriptions[1] .. '/' }, { pron = '[' .. transcriptions[2] .. ']' } })
	end

	return '* ' .. (qualifier and require("Module:qualifier").format_qualifier{qualifier} .. ' ' or '')
		.. IPA_text

end

return export