Romanian pronunciation module, used by {{ro-IPA}}.


local export = {}

local m_IPA = require("Module:IPA")
local lang = require("Module:languages").getByCode("ro")
local rsub = mw.ustring.gsub
local rlower = mw.ustring.lower
local rlast = require("Module:string/replace last").replace_last
local rfind = mw.ustring.find

local V = "ʲ[aeiouəɨ]"
local C = "[bkdfɡhʒlmnprstvzʃjw]"
local X = "ʲ[bkdfɡhʒlmnprstvzʃjwaəeiɨou]"
local U = "ʲ?[aeəɨiou]"

local phon = {
	["a"]="a",	["b"]="b",	["c"]="k",	["d"]="d",	["e"]="e",
	["f"]="f",	["g"]="ɡ",	["h"]="h",	["i"]="i",	["j"]="ʒ",	
	["k"]="k",	["l"]="l",	["m"]="m",	["n"]="n",	["o"]="o",	
	["p"]="p",	["q"]="k",	["r"]="r",	["s"]="s",	["t"]="t",	
	["u"]="u",	["v"]="v",	["w"]="v",	["x"]="ks",	["y"]="ʲ",
	["z"]="z",	["ă"]="ə",	["â"]="ɨ",	["î"]="ɨ",	["ș"]="ʃ",
	["ț"]="ts",
}

local function phonetic(text)
    text = rlower(text)
    text = rsub(text, "ce", "tʃe")
    text = rsub(text, "ci", "tʃi")
    text = rsub(text, "ge", "dʒe")
    text = rsub(text, "gi", "dʒi")
    
    -- general phonology
    text = rsub(text, ".", phon)
    
    -- Triphthongs
    local triphthongs = {
        ["eai"] = "e̯aj", ["eau"] = "e̯aw", ["eoa"] = "e̯o̯a",
        ["iai"] = "jaj", ["iau"] = "jaw", ["iei"] = "jej",
        ["eu"]  = "jew", ["ioi"] = "joj", ["iou"] = "jow",
        ["oai"] = "o̯aj", ["uai"] = "waj", ["uau"] = "waw",
        ["uăi"] = "wəj", ["ioa"] = "jo̯a"
    }

    for original, replacement in pairs(triphthongs) do
        text = rsub(text, original, replacement)
    end

    -- If no triphthong was found, try diphthongs
    if text == rsub(text, ".", phon) then
        local diphthongs = {
            ["ai"] = "aj", ["au"] = "aw", ["ei"] = "ej",
            ["eu"] = "ew", ["ii"] = "ij", ["iu"] = "iw",
            ["oi"] = "oj", ["ou"] = "ow", ["ui"] = "uj",
            ["uu"] = "uw", ["ăi"] = "əj", ["ău"] = "əw",
            ["âi"] = "ɨj", ["âu"] = "ɨw",
            ["ea"] = "e̯a", ["eo"] = "e̯o", ["eu"] = "e̯u",
            ["ia"] = "ja", ["ie"] = "je", ["io"] = "jo",
            ["iu"] = "ju", ["oa"] = "o̯a", ["ue"] = "we",
            ["ua"] = "wa", ["uă"] = "wə", ["uâ"] = "wɨ"
        }

        for original, replacement in pairs(diphthongs) do
            text = rsub(text, original, replacement)
        end
    end
    
    -- initial glide (borrowings only)
    text = rsub(text, "^ʲ", "j")
    text = rsub(text, "(" .. V .. ")ʲ", "%1j")
    -- stress (1)
    text = rsub(text, "(" .. V .. ")(" .. X .. ")$", "ˈ%1%2")
    if rfind(text, "ˈ") == nil then
        text = rlast(text, "(" .. V .. ")(" .. X .. ")(" .. C .. ")", "ˈ%1%2%3", 1)
    end
    if rfind(text, "ˈ") == nil then
        text = rlast(text, "(" .. U .. ")", "ˈ%1", 1)
    end
    -- schwa
    text = rsub(text, "(" .. C .. ")([eo])$", "%1ə")
    -- stress (2)
    text = rsub(text, "(" .. V .. ")ˈ(" .. V .. ")", "ˈ%1%2")
    text = rsub(text, "(" .. V .. ")(" .. C .. ")ˈ", "%1ˈ%2")
    text = rsub(text, "(" .. V .. ")(" .. C .. ")(" .. C .. ")ˈ", "%1%2ˈ%3")
    text = rsub(text, "(" .. V .. ")(" .. C .. C .. ")(" .. C .. ")ˈ", "%1%2ˈ%3")
    text = rsub(text, "^(" .. C .. ")ˈ", "ˈ%1")
    text = rsub(text, "^(" .. C .. C .. ")ˈ", "ˈ%1")
    if rfind(text, "^-") ~= nil then
        text = rsub(text, "ˈ", "")
    end
    return 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 = "[" .. phonetic(word) .. "]" })
	end
	
	return m_IPA.format_IPA_full(lang, IPA_results)
end

return export