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

local export = {}

local consonants = {
	['க']='k', ['ங']='ŋ', ['ச']='t͡ɕ', ['ஞ']='ɲ', ['ட']='ʈ', ['ண']='ɳ', ['த']='t̪',
	['ந']='n̪', ['ப']='p', ['ம']='m', ['ய']='j', ['ர']='ɾ̪', ['ல']='l̪', ['வ']='ʋ',
	['ழ']='ɻ', ['ள']='ɭ', ['ற']='r', ['ன']='n', ['ஶ']='ɕ', ['ஜ']='d͡ʑ', ['ஷ']='ʂ',
	['ஸ']='s', ['ஹ']='h', ['ஃப']='f', ['ஃஜ']='z', ['ஃஸ']='x',
	['ஃ']='',
    ['க3']='ɡ', ['ட3']='ɖ', ['ப3']='b', ['த3']='d̪'
}

local numbers_sa = {
	['1']='1', ['2']='2', ['3']='3', ['4']='4', 
	['¹']='1', ['²']='2', ['³']='3', ['⁴']='4',
	['₁']='1', ['₂']='2', ['₃']='3', ['₄']='4'
}

local diacritics = {
	['ா']= 'aː', ['ி']='i', ['ீ']='iː', ['ு']='u', ['ூ']='uː',  ['ெ']='e',
	['ே']='eː', ['ை']='aɪ̯', ['ொ']='o', ['ோ']='oː', ['ௌ']='aʊ̯',
	['்']='',	--halant, supresses the inherent vowel "a"
	-- no diacritic
	[''] = 'a'
}

local nonconsonants = {
	-- independent vowels
	['அ']='•a', ['ஆ']='•aː', ['இ']='•i', ['ஈ']='•iː', ['உ']='•u', ['ஊ']='•uː',
	['எ']='•e', ['ஏ']='•eː', ['ஐ']='•aɪ̯', ['ஒ']='•o', ['ஓ']='•oː', ['ஔ']='•aʊ̯',
	-- other symbols
--	['ஃ']='',
}

local adjust1 = {
	['ŋk']='ŋɡ', ['ɲt͡ɕ']='ɲd͡ʑ', ['ɳʈ']='ɳɖ', ['n̪t̪']='n̪d̪', ['mp']='mb',
	['ŋr']='ŋɡɾ', ['ɲr']='ɲd͡ʑɾ', ['ɳr']='ɳɖɾ', ['n̪r']='n̪d̪ɾ', ['mr']='mbɾ', ['rr']='ʈʈɾ',
	['([aeiou]ː?)k([aeiou])']='%1ɡ%2', ['([aeiou]ː?)t͡ɕ([aeiou])']='%1s%2',
	['([aeiou]ː?)ʈ([aeiou])']='%1ɖ%2', ['([aeiou]ː?)t̪([aeiou])']='%1d̪%2', ['([aeiou]ː?)p([aeiou])']='%1b%2',
}

local adjust2 = {
	['t͡ɕ'] = 's',
	['a([^ː])']='ə%1', ['a$']='ə',
	['aː']='ɑː',
	['i([^ː])']='ɪ%1', ['i$']='ɪ',
	['u([^ː%s%p])']='ʊ%1', ['u([%s%p])']='ɯ%1', ['u$']='ɯ',
	['e([^ː])']='ɛ%1', ['e$']='ɛ',
}

function export.to_IPA(text)

	text = mw.ustring.gsub(
		text,
		'(ஃ?)([க-ஹ])([ா-்]?)([1234¹²³⁴₁₂₃₄]?)',
		function(h, c, d, n)
			n = numbers_sa[n] or n
			return ((consonants[h..c..n] or consonants[h..c]) or (consonants[h] .. (consonants[c..n] or consonant[c] or c))) .. diacritics[d]
		end)

	text = mw.ustring.gsub(text, '[அ-ஔ]', nonconsonants)

	for k, v in pairs(adjust1) do
		text = mw.ustring.gsub(text, k, v)
		text = mw.ustring.gsub(text, k, v) --twice
	end

	--convert consonant gemination to triangular colon
	text = mw.ustring.gsub(text, "([kŋɲʈɳpmjʋɻɭrnɕʂshfzx])%1", "%1ː")
	text = mw.ustring.gsub(text, "([tnɾl]̪)%1", "%1ː")
	text = mw.ustring.gsub(text, "([td]͡[ɕʑ])%1", "%1ː")

	--if an independent vowel is after another vowel, assume diphthong
	text = mw.ustring.gsub(text, "([aeiou]ː?)•", "%1")

	--otherwise, add ʔ
	text = mw.ustring.gsub(text, "•", "ʔ")

	text2 = text --phonetic

	for k, v in pairs(adjust2) do
		text2 = mw.ustring.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 = require('Module:IPA').format_IPA_full(
			require('Module:languages').getByCode('ta'),
			{ { pron = '/' .. transcriptions[1] .. '/' } })
	else
		IPA_text = require('Module:IPA').format_IPA_full(
			require('Module:languages').getByCode('ta'),
			{ { pron = '/' .. transcriptions[1] .. '/' }, { pron = '[' .. transcriptions[2] .. ']' } })
	end

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

end

return export