Module:tg-IPA

From Wiktionary, the free dictionary
Jump to navigation Jump to search

local export = {}

local rsubn = mw.ustring.gsub

local U = mw.ustring.char

local m_a = require("Module:accent_qualifier")
local m_IPA = require("Module:IPA")
local ulower = mw.ustring.lower

local lang = require("Module:languages").getByCode("tg")

local pitchaccent = U(0x301)
local dental = U(0x32A)
local dtack = U(0x31E)

local t_consonants = {
    ['g'] = 'ɡ',
    ['j'] = 'd͡ʒ',
    ['ġ'] = 'ʁ',
    ['y'] = 'j',
    ['š'] = 'ʃ',
    ['č'] = 't͡ʃ',
    ['x'] = 'χ',
    ['ž'] = 'ʒ',
    ['ʾ'] = 'ʔ'
}

local t_vowels = {
    ['a'] = 'ä',
    ['ü'] = 'ɵ',
    ['o'] = 'ɔ',
}

local vowels = "aiuoeü"
local vowel = "[" .. vowels .. "]"
local consonant = "[^" .. vowels .. ". -]"

local function con_assimilation(text) --DONT USE THIS ON CLASSICAL
	-- assimilation/placement of certain consonants
	text = rsubn(text, "([nl])((%.?)[td])", "%1" .. dental .. "%2")
	text = rsubn(text, "([td])", "%1" .. dental .. "")
	text = rsubn(text, "n((%.?)[kg])", "ŋ%1")
	text = rsubn(text, "n((%.?)[bp])", "m%1")
	text = rsubn(text, "n((%.?)[q])", "ɴ%1")
	text = rsubn(text, "([nm])((%.?)[fv])", "ɱ%2")
	text = rsubn(text, "([äiuoeɵ](%" .. dtack .. "?)(%" .. pitchaccent .. "?)(%ː?)(%.?))([h])", "%1ɦ")
	text = rsubn(text, "r([tdszšlž])", "ɹ%1")
	-- formally, f only assimiates in the same syllable
	text = rsubn(text, "f([bjdžğ])", "v%1")
	text = rsubn(text, "ɾ(%.?)ɾ", "#r%1r")
	text = rsubn(text, "ä(" .. pitchaccent .. ")", "æ%1")
	text = rsubn(text, "([ɦ])#", "ʱ#")
	text = rsubn(text, "([h])#", "ʰ#")
	return text
end

function export.to_IPA(text)
	text = lang:transliterate(text)

    text = ulower(text)

	text = rsubn(text, "`", "ˈ")
	
    text = rsubn(text, "[-.,]", " ")
	-- add final stress marker
	text = rsubn(text, "(" .. consonant .. ")ī", "ˈ%1i")
    -- Replace vowels
    text = rsubn(text, ".", t_vowels)
    -- Replace consonants
    text = rsubn(text, ".", t_consonants)

    return text
end

function export.to_IPA_phonetic(text)
	text = lang:transliterate(text)

    text = ulower(text)
	
	-- add final stress marker
	text = rsubn(text, "(" .. consonant .. ")ī", "ˈ%1i")
	
	text = rsubn(text, "ˈ", "`")
	text = rsubn(text, " | ", "# | #")
	text = "##" .. rsubn(text, " ", "# #") .. "##"
	text = rsubn(text, "%-i#", "i#")
	text = rsubn(text, "[-]", ".")

	--pitch accent mark
	text = rsubn(text, "`(".. consonant .. ")(" .. vowel .. ")", "%1%2" .. pitchaccent .. "")

	-- Replace jj with dj
	text = rsubn(text, "jj", "dj")
	-- Replace čč with tč
	text = rsubn(text, "čč", "tč")
	text = rsubn(text, "w(" .. vowel .. ")", "v%1")
	-- universal aspiration
	text = rsubn(text, "([ptkč])(" .. vowel .. ")", "%1ʰ%2")

	--aspiration
	text = rsubn(text, "([ptkč](%" .. dental .. "?))(" .. vowel .. ")", "%1ʰ%3")

	-- Replace vowels
	text = rsubn(text, ".", t_vowels)
	--allophones
	text = con_assimilation(text)
	-- Replace consonants
	text = rsubn(text, ".", t_consonants)

	text = rsubn(text, "#", "")

	text = mw.ustring.toNFC(text)

	return text
end

function export.show(frame)
    local args = frame:getParent().args

    local p, results = {}, {}

    if args[1] then
        for index, item in ipairs(args) do
            table.insert(p, (item ~= "") and item or nil)
        end
    else
        if mw.title.getCurrentTitle().nsText == "Template" then
            p = {"чаҳоршанбе"}
        else 
        	p = {mw.title.getCurrentTitle().text}
        end
    end

    for _, word in ipairs(p) do
        table.insert(results, {pron = "/" .. export.to_IPA(word) .. "/"})
        table.insert(results, {pron = "[" .. export.to_IPA_phonetic(word) .. "]"})
    end

    return "* " .. m_IPA.format_IPA_full(lang, results)
end

return export