Turkish pronunciation module, powers {{tr-IPA}}. This is under development and may contain errors; mass deployment on entries is discouraged.


local export = {}

local m_IPA = require("Module:IPA")
local lang = require("Module:languages").getByCode("tr")

local C = "[bçcdfgğhjklmnprsştvyzqxw]" -- consonants
local V = "[aeiıoöuüâû]" -- vowels

local lowerc = {
--Armeno-Turkish
	["Ո"]="ո", ["Ա"]="ա", ["Գ"]="գ", ["Ե"]="ե", ["Զ"]="զ", ["Է"]="է", 
	["Ը"]="ը", ["Թ"]="թ", ["Ժ"]="ժ", ["Ի"]="ի", ["Լ"]="լ", ["Խ"]="խ", 
	["Կ"]="կ", ["Հ"]="հ", ["Ղ"]="ղ", ["Ճ"]="ճ", ["Մ"]="մ", ["Ն"]="ն",
	["Շ"]="շ", ["Չ"]="չ", ["Պ"]="պ", ["Ս"]="ս", ["Վ"]="վ", ["Տ"]="տ", 
	["Ր"]="ր", ["Փ"]="փ", ["Ք"]="ք", ["Օ"]="օ", ["Ֆ"]="ֆ", ["Յ"]="յ",
--Latin
	["A"]="a", ["B"]="b", ["C"]="c", ["Ç"]="ç", ["D"]="d", ["E"]="e",
	["F"]="f", ["G"]="g", ["Ğ"]="ğ", ["H"]="h", ["I"]="ı", ["İ"]="i",
	["J"]="j", ["K"]="k", ["L"]="l", ["M"]="m", ["N"]="n", ["O"]="o",
	["Ö"]="ö", ["P"]="p", ["R"]="r", ["S"]="s", ["Ş"]="ş", ["T"]="t",
	["U"]="u", ["Ü"]="ü", ["V"]="v", ["Y"]="y", ["Z"]="z", ["Â"]="â",
	["Û"]="û", ["Q"]="q", ["X"]="x", ["W"]="w", ["Î"]="î"
}

local monographs = {
	["ա"]="a", ["գ"]="k", ["ե"]="y", ["զ"]="z", ["է"]="e", ["ը"]="ı",
	["թ"]="t", ["ժ"]="j", ["ի"]="i", ["լ"]="l", ["խ"]="ẍ", ["կ"]="g",
	["հ"]="h", ["ղ"]="g", ["ճ"]="c", ["մ"]="m", ["ն"]="n", ["շ"]="ʃ",
	["չ"]="ç", ["պ"]="b", ["ս"]="s", ["վ"]="v", ["տ"]="d", ["ր"]="r",
	["փ"]="p", ["ք"]="k", ["օ"]="o", ["ֆ"]="f"
}

local placeholders = {
    ["ու"] = "ù",
    ["իւ"] = "ì",
    ["էօ"] = "ò",
}

local placeholder_to_latin = {
    ["ù"] = "u",
    ["ì"] = "ü",
    ["ò"] = "ö",
}

local function convert_armeno_turkish_to_latin(text)
    -- Replace digraphs with placeholders
    for digraph, placeholder in pairs(placeholders) do
        text = mw.ustring.gsub(text, digraph, placeholder)
    end
    -- Replace placeholders with Latin script
    text = mw.ustring.gsub(text, '.', placeholder_to_latin)
    -- Replace monographs
    text = mw.ustring.gsub(text, '.', monographs)
    return text
end

local phon = {
	["c"]="d͡ʒ", ["ç"]="t͡ʃ", ["ğ"]="ɣ", ["ş"]="ʃ",
	["b"]="b", ["d"]="d", ["f"]="f", ["g"]="ɡ",
	["h"]="h", ["j"]="ʒ", ["k"]="k", ["l"]="l",
	["m"]="m", ["n"]="n", ["p"]="p", ["r"]="ɾ",
	["s"]="s", ["t"]="t", ["v"]="v", ["y"]="j", ["z"]="z",
	["q"]="k", ["x"]="ks", ["w"]="v", ["î"]="iː",
	["a"]="a", ["â"]="a", ["e"]="e", ["ı"]="ɯ", ["i"]="i",
	["o"]="o", ["ö"]="œ", ["u"]="u", ["û"]="u", ["ü"]="y",
}

local function phonemic(text)
	text = mw.ustring.gsub(text, '.', lowerc)
    text = convert_armeno_turkish_to_latin(text)
    text = mw.ustring.gsub(text, '.', phon)
	text = mw.ustring.gsub(text, "ć", "c")
	text = mw.ustring.gsub(text, "ý", "j")
	text = mw.ustring.gsub(text, "ÿ", "y")
	text = mw.ustring.gsub(text, "g", "ɡ")
	text = mw.ustring.gsub(text, ":", "ː")
	text = mw.ustring.gsub(text, "ẍ", "x")
	text = mw.ustring.gsub(text, "č", "t͡ʃ")
	text = mw.ustring.gsub(text, "ḉ", "ç")
	return text
end

local function phonetic(text)
	text = mw.ustring.gsub(text, '.', lowerc)
    text = convert_armeno_turkish_to_latin(text)
    
	-- Handle k, g palatalization rules
	text = mw.ustring.gsub(text, "k[ɛeiyœöüÿâû]", "ć%1")
	text = mw.ustring.gsub(text, "g[ɛeiyœöüÿâû]", "ɟ%1")
	text = mw.ustring.gsub(text, "ćk", "ć")
	text = mw.ustring.gsub(text, "ɟg", "ɟ")
	text = mw.ustring.gsub(text, "[ɛeiyœöüÿâû]k", "%1ć")
	text = mw.ustring.gsub(text, "[ɛeiyœöüÿâû]g", "%1ɟ")
	text = mw.ustring.gsub(text, "kć", "ć")
	text = mw.ustring.gsub(text, "gɟ", "ɟ")
	
	-- Handle ğ rules
	text = mw.ustring.gsub(text, "([aeiıoöuüÿâû])ğ([bçcdfgğhjklmnprsştvyzqxwý])",
		"%1ː%2")
	text = mw.ustring.gsub(text, "([ɛeiyöüÿ])ğ([ɛeiyöüÿ])", "%1ý%2")
	text = mw.ustring.gsub(text, "ğ$", "ː")
	text = mw.ustring.gsub(text, "([ɛeiyÿöü])ğ", "%1ý")
	text = mw.ustring.gsub(text, "ğ", "")

	-- Handle l rules
	text = mw.ustring.gsub(text, "l","ɫ")
	text = mw.ustring.gsub(text, "ɫ[ɛeiyœöüÿâû]", "l%1")
	text = mw.ustring.gsub(text, "[ɛeiyœöüÿâû]ɫ", "%1l")
	text = mw.ustring.gsub(text, "lɫ", "ɫ")
	text = mw.ustring.gsub(text, "ɫl", "l")

	-- Handle aspirated p, t, c, k
	text = mw.ustring.gsub(text, "^p", "pʰ")
	text = mw.ustring.gsub(text, "^t", "tʰ")
	text = mw.ustring.gsub(text, "^ć", "ćʰ")
	text = mw.ustring.gsub(text, "^k", "kʰ")

	-- Front final /h/
	text = mw.ustring.gsub(text, "[ɛeiyœöüÿâû]h$", "%1ḉ")
	text = mw.ustring.gsub(text, "hḉ", "ḉ")
	text = mw.ustring.gsub(text, "h$", "ẍ")

	-- Handle exceptions for final devoicing
	local exceptions = { 
		["ad"] = true,
		["hac"] = true,
		["İd"] = true,
		["kod"] = true,
		["od"] = true
	}

	-- Only apply devoicing rule if word isn't in exception list
	if not exceptions[text] then
		-- Devoice final /b, d, d͡ʒ, ɡ, ɟ/
		text = mw.ustring.gsub(text, "b$", "p")
		text = mw.ustring.gsub(text, "d$", "t")
		text = mw.ustring.gsub(text, "d͡ʒ$", "č")
		text = mw.ustring.gsub(text, "ɡ$", "k")
		text = mw.ustring.gsub(text,"g$", "k")
		text = mw.ustring.gsub(text, "ɟ$", "ć")
		text = mw.ustring.gsub(text, "c$", "č")
	end

	-- Devoice /ɾ/ and make /ɫ/ and /l/ voiceless in appropriate conditions
	text = mw.ustring.gsub(text, "ɾ$", "ɾ̥")
	text = mw.ustring.gsub(text, "ɾ([ptćkḉsčʃ])", "ɾ̥%1")
	text = mw.ustring.gsub(text, "r$", "ɾ̥")
	text = mw.ustring.gsub(text, "r([ptćkḉsčʃ])", "ɾ̥%1")
	text = mw.ustring.gsub(text, "ɫ$", "ɫ̥")
	text = mw.ustring.gsub(text, "ɫ([ptćkḉsčʃ])", "ɫ̥%1")
	text = mw.ustring.gsub(text, "l$", "l̥")
	text = mw.ustring.gsub(text, "l([ptćkḉsčʃ])", "l̥%1")

	-- Lower /e/ before coda /m, n, l, r/
	text = mw.ustring.gsub(text, "e([mnlrɾɾ̥l̥ɫ̥])", "ɛ%1")

	-- Handle the lowering of specific vowels in word-final position
	text = mw.ustring.gsub(text, "i$", "ɪ")
	text = mw.ustring.gsub(text, "ÿ$", "ʏ")
	text = mw.ustring.gsub(text, "u$", "ʊ")
	text = mw.ustring.gsub(text, "e$", "ɛ")

	return phonemic(text)
end

function export.IPA(frame)
	local words = {}

	for _, word in ipairs(frame:getParent().args) do
		table.insert(words, word)
	end

	if #words == 0 then
		words = {mw.title.getCurrentTitle().text}
	end

	local IPA_results = {}

	for _, word in ipairs(words) do
		table.insert(IPA_results, { pron = "/" .. phonemic(word) .. "/" })
		table.insert(IPA_results, { pron = "[" .. phonetic(word) .. "]" })
	end

	return m_IPA.format_IPA_full(lang, IPA_results)
end

return export