Jump to content

Module:tcy-ml-IPA

From Wiktionary, the free dictionary


local export = {}

local lang = require("Module:languages").getByCode("tcy")
local sc = require("Module:scripts").getByCode("Mlym")
local m_IPA = require("Module:IPA")
local gsub = mw.ustring.gsub

local consonants = {
	['ക'] = 'k', ['ഖ'] = 'kʰ', ['ഗ'] = 'ɡ', ['ഘ']  = 'ɡʱ', ['ങ'] = 'ŋ',
	['ച'] = 't͡ʃ', ['ഛ'] = 't͡ʃʰ', ['ജ'] = 'd͡ʒ', ['ഝ'] = 'd͡ʒʱ', ['ഞ'] = 'ɲ',
	['ട'] = 'ʈ', ['ഠ'] = 'ʈʰ', ['ഡ'] = 'ɖ', ['ഢ'] = 'ɖʱ', ['ണ'] = 'ɳ',
	['ത'] = 't̪', ['ഥ'] = 't̪ʰ', ['ദ'] = 'd̪', ['ധ'] = 'd̪ʱ', ['ന'] = 'n̪', 
	['പ'] = 'p', ['ഫ'] = 'f', ['ബ'] = 'b', ['ഭ'] = 'bʱ',  ['മ'] = 'm',
	['യ'] = 'j', ['ര'] = 'ɾ', ['ല'] = 'l', ['വ'] = 'ʋ', 
	['ശ'] = 'ʃ', ['ഷ'] = 'ʂ', ['സ'] = 's', ['ഹ'] = 'h', 
	['ള'] = 'ɭ', ['ഴ'] = 'ɻ', ['റ'] = 'r', ['ഩ'] = 'n', ['ഺ']  = 't',
}

local vowel_diacritics = {
	['ാ'] =  'aː', ['ി'] = 'i', ['ീ'] = 'iː', ['ു'] = 'u', ['ൂ'] = 'uː', ['ൃ'] = 'ri', ['ൄ'] = 'riː', ['ൢ'] = 'li', ['ൣ'] = 'liː',
	['െ'] = 'e', ['േ'] = 'eː', ['ൈ'] = 'ɐi̯', ['ൊ'] = 'o', ['ോ'] = 'oː', ['ൌ'] = 'ɐu̯', ['ൗ'] = 'ɐu̯',
	['്'] = '', ['഻'] = '', ['഼'] = '',	--virama, supresses the inherent vowel "a"
	[''] = 'ɐ'	-- no diacritic
}

local other = {
	-- independent vowels
	['അ'] = 'ɐ', ['ആ'] = 'aː', ['ഇ'] = 'i', ['ഈ'] = 'iː', ['ൟ'] = 'iː', ['ഉ'] = 'u', ['ഊ'] = 'uː', ['ഋ'] = 'ri', ['ൠ'] = 'riː', ['ഌ'] = 'li', ['ൡ'] = 'liː',
	['എ'] = 'e', ['ഏ'] = 'eː', ['ഐ'] = 'ai̯', ['ഒ'] = 'o', ['ഓ'] = 'oː', ['ഔ'] = 'au̯',
	-- Other symbols
	['ർ'] = 'r', ['ൎ'] = 'r', ['ൽ'] = 'l', ['ൾ'] = 'ɭ', ['ൻ'] = 'n', ['ൺ'] = 'ɳ', ['ൿ'] = 'k', ['ൔ'] = 'm', ['ൕ'] = 'j', ['ൖ'] = 'ɻ',
	['ം'] = 'm̃', ['ഀ'] = 'm̃',
	['ഃ'] = 'h',
	['ഁ'] = '̃',		-- Chandrabindu
	['ഽ'] = '',		-- Avagraha
	['ഄ'] = '',		-- Vedic anusvara
}

local adjust1 = {
	-- Assimilate the anusvara
	['m̃([kɡŋ])'] = 'ŋ%1',
	['m̃([td]͡[ʃʒ])'] = 'ɲ%1', ['m̃(ɲ)'] = 'ɲ%1',
	['m̃([ʈɖɳ])'] = 'ɳ%1',
	['m̃([td]̪)'] = 'n̪%1', ['m̃(n̪)'] = 'n̪%1',
	['m̃([tdn])'] = 'n%1',
	['m̃([pbmjɾlʋʃʂshɭɻr])'] = 'm%1',
	['m̃([%s%p])'] = 'm%1', ['m̃$'] = 'm',
	['ɾɾ'] = 'rr', ['rɾ'] = 'rr', ['ɾr'] = 'rr',
	['([pt̪tʈʃkbɖʒmn̪nɳŋʋʂshʰʱ)])ɾ'] = '%1r', 
	['hp'] = 'pp', ['hk'] = 'kk', ['hs'] = 'ss', ['hʃ'] = 'ʃʃ', ['hʂ'] = 'ʂʂ', ['hn'] = 'nn', ['hm'] = 'mm',
	['ː̃']='̃ː', ['n്r'] = 'nt',
	['u്'] = 'ɯ', ['uː്'] = 'ɯː', ['ɐൗ'] =  'ɐu̯',
	['s2'] = 'z', ['ɖ2'] = 'd', ['ʋ2'] = 'w', ['ɐ2'] = 'ɯ', ['aː2'] = 'æː', ['o2'] = 'ɔ', ['oː2'] = 'ɔː', -- English loans
}

local adjust2 = {
	['d͡ʒɲ'] = 'ɟɲ',
	['([pt̪tʈkbd̪dɖgmn̪nɳɲŋjlʋʃʒʂshʰʱ])ʋ'] = '%1ʋʷ',
	['t͡ʃt͡ʃ'] = 't̚t͡ʃ', ['d͡ʒd͡ʒ'] = 'd̚d͡ʒ', ['ɲd͡ʒ'] = 'n̠ʲd͡ʒ',
}

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

function export.to_IPA(text)
	local VIRAMA = '്'
	-- final virama rules
	text = gsub(text, VIRAMA .. "([%,%.%!%?%:%;]?)$", VIRAMA .. "ɯ%1")
	text = gsub(text, VIRAMA .. "([%,%.%!%?%:%;]?) ", VIRAMA .. "ɯ%1 ")

	text = mw.ustring.gsub(text, "ɯ ([%,%.%!%?%:%;]?)([ ɐaeiou])", "‿ %1%2") -- ŭ is elided before vowels
	
	text = gsub(
		text,
		'([ക-ഺ])(഻?)([ാ-്]?)',
		function(c, n, d)
			return ((consonants[c..n] or consonants[c]) or c) .. vowel_diacritics[d]
		end)

	text = gsub(text, '[ം-ൡ]', other)

	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 = lang,
			items = {{ pron = "/" .. transcriptions[1] .. "/" }},
		}
	else
		IPA_text = m_IPA.format_IPA_full {
			lang = lang,
			items = {{ pron = "/" .. transcriptions[1] .. "/" }, { pron = "[" .. transcriptions[2] .. "]" }},
		}
	end

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

return export