Module:niv-IPA

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


local export = {}

--[=[

Author: saph (User:Saph668)

Implements {{niv-IPA}}.

]=]--

local m_IPA = require("Module:IPA")

local lang = require("Module:languages").getByCode("niv")
local sc = require("Module:scripts").getByCode("Cyrl")
local m_para = require("Module:parameters")
local m_a = require("Module:accent qualifier")
local utilities = require("Module:niv-utilities")

local gsub = mw.ustring.gsub
local rlower = mw.ustring.lower

local correspondences = {
    ["а"]='a', ["в"]='v', ["е"]='je',
    ["ё"]='jo', ["г"]='ɡ', ["д"]='d',
    ["и"]='i', ["й"]='j',
    ["к"]='k', ["л"]='l',
    ["м"]='m', ["н"]='n',
    ["о"]='o', ["п"]='p', ["р"]='r',
    ["с"]='s', ["т"]='t',
    ["у"]='u', ["ф"]='f', ["х"]='x',
    ["ы"]='ɤ', ["э"]='e', ["ю"]='ju',
    ["я"]='ja', ["з"]='z', ["б"]='b',
}

local decode = {
	["G"] = "ɣ", ["R"] = "ʁ", ["Q"] = "ɢ",
	["N"] = "ŋ", ["X"] = "χ", ["c"] = "cʰ",
	["r"] = "r̥", ["h"] = "h",
	["L"] = "q", ["w"] = "w"
}

local vowel = "[aeiouɤ]"
local C = "[mnŋptkqbdɡPTKQfsxχhvzɣʁlwRrl]"
local fv = "[fsxχhvzɣʁaeiouɤ]"
local pv = "[ptckqPTCKQaeiouɤ]"

local function pron(text)
	text = rlower(text)
    text = utilities.encipher(text)
    text = text:gsub("P", "pʰ")
    	:gsub("T", "tʰ")
    	:gsub("K", "kʰ")
    	:gsub("q", "qʰ")
    text = gsub(text, ".", decode)
    text = gsub(text, "ни", "ɲi")
    text = gsub(text, "ди", "ɟi")
    text = gsub(text, "ти", "ci")
    text = gsub(text, "р̌", "r̥")
    text = gsub(text, ".", correspondences)
    text = text:gsub("(" .. C .. ")([ʰ]?)je", "%1%2e")
    text = gsub(text, "nj", "ɲ")
    text = gsub(text, "dj", "ɟ")
    text = gsub(text, "tj", "c")
    return text
end

local function toIPA(text)
	text = gsub(text, "\'", "")
	text = gsub(text, "—", "")
	text = gsub(text, ",", "‿")
	text = gsub(text, "ⁿ", "")
	return text
end

local function phon(text)
	-- universal phonetic pronunciation
	local mutation_u = {
		["p"] = 'v', ["t"] = 'r', ["c"] = 'z',
		["k"] = 'ɣ', ["q"] = 'ʁ'
	}
	local mutation_a = {
		["pʰ"] = 'f', ["tʰ"] = 'r̥', ["cʰ"] = 's',
		["kʰ"] = 'x', ["qʰ"] = 'χ'
	}
	local voicing = {
		["p"] = 'b', ["t"] = 'd', ["c"] = 'ɟ', 
		["k"] = 'ɡ', ["q"] = 'ɢ'
	}
	local spirantisation = {
		["k"] = 'x', ["q"] = 'χ'
	}
	local boundary = ","
	text = pron(text)
	text = utilities.encipher(text)
	text = gsub(text, ".", correspondences)
	text = gsub(text, "(" .. vowel .. ")f(" .. vowel .. ")", "%1v%2")
	text = gsub(text, "(" .. vowel .. ")s(" .. vowel .. ")", "%1z%2")
	text = gsub(text, "\'", "ˈ")
	text = gsub(text, "—", "\203\144")
	for u,v in pairs(voicing) do
		text = gsub(text, "([mnɲŋⁿ])" .. boundary .. u, "%1" .. boundary .. v)
	end
    for u,v in pairs(spirantisation) do
    	text = gsub(text, u .. "([xχʁ])", v .. "%1")
    end
    for u,v in pairs(mutation_a) do
		text = gsub(text, "(" .. pv .. ")" .. boundary .. u, "%1" .. boundary .. "" .. v)
		text = gsub(text, "(" .. fv .. ")" .. boundary .. v, "%1" .. boundary .. "" .. u)
    end
    for u,v in pairs(mutation_u) do
		text = gsub(text, "(" .. pv .. ")" .. boundary .. u, "%1" .. boundary .. "" .. v)
		text = gsub(text, "(" .. fv .. ")" .. boundary .. v, "%1" .. boundary .. "" .. u)
    end
	text = text:gsub("", "")
	text = gsub(text, ",", "\226\128\191\203\136")
	text = gsub(text, "a\203\144", "i")
	text = gsub(text, "r\204\165", "r\204\165\203\148")
	return text
end

local function yphon(text)
	-- phonetic pronunciation among younger speakers
	text = phon(text)
	text = text:gsub("pʰ", "P")
		:gsub("tʰ", "T")
		:gsub("kʰ", "K")
		:gsub("qʰ", "Q")
		:gsub("r\204\165\203\148", "R")
	for i in text:gmatch(C) do mw.logObject(i) end
	text = gsub(text, "^(" .. C .. ")([ʼ’]?)([ie])(" .. C .. "?)(" .. C .. "?)(" .. C .. "?)$", "%1\202\178%2%3%4%5")
	text = text:gsub("P", "pʰ")
		:gsub("T", "tʰ")
		:gsub("K", "kʰ")
		:gsub("Q", "qʰ")
		:gsub("R", "r\204\165\203\148")
	return text
end

local function ophon(text)
	-- phonetic pronunciation among older speakers
	-- empty for now
	text = phon(text)
	return text
end

local function fphon(text)
	-- phonetic pronunciation in fast speech
	text = phon(text)
	local boundary = "\226\128\191\203\136"
	local voicing = {
		["f"] = "v",
		["s"] = "z"
	}
	local spirantisation = {
		["q" .. boundary .. "x"] = "χ" .. boundary .. "x",
		["q" .. boundary .. "ʁ"] = "χ" .. boundary .. "ʁ",
		["q" .. boundary .. "χ"] = "χ" .. boundary .. "χ"
	}
	text = text:gsub("r\204\165\203\148", "R")
	text = gsub(text, "(" .. vowel .. ")" .. "([ɣrʁ])" .. "([rl])", "%1\203\144" .. "" .. "%3")
	text = text:gsub("R", "r\204\165\203\148")
	for u,v in pairs(spirantisation) do
		text = gsub(text, u, v)
	end
	for u,v in pairs(voicing) do
		text = gsub(text, u .. boundary .. "([mnŋvzɣʁljwrl])", v .. boundary .. "%1")
	end
	return text
end

function export.make(frame)
	local args = m_para.process(frame:getParent().args, {
	    [1] = { list = true, default = mw.loadData("Module:headword/data").pagename },
	})
    local results = {}
    local results_y = {}
    local results_o = {}
    local results_f = {}
    for i, term in ipairs(args[1]) do
        local phonemic = {}
        local phonetic = {}
        local yphonetic = {}
        local ophonetic = {}
        local fphonetic = {}
        local words = mw.text.split(term, ".", true)
        phonemic[i] = pron(words[i])
        phonetic[i] = phon(words[i])
        yphonetic[i] = yphon(words[i])
        ophonetic[i] = ophon(words[i])
        fphonetic[i] = fphon(words[i])
        phonemic = table.concat(phonemic, " ")
        phonetic = table.concat(phonetic, " ")
        yphonetic = table.concat(yphonetic, " ")
        ophonetic = table.concat(ophonetic, " ")
        fphonetic = table.concat(fphonetic, " ")
        phonemic = toIPA(phonemic)
        phonetic = toIPA(phonetic)
        yphonetic = toIPA(yphonetic)
        ophonetic = toIPA(ophonetic)
        fphonetic = toIPA(fphonetic)
        table.insert(results, { pron = "/" .. phonemic .. "/" })
        if phonetic ~= phonemic and gsub(phonetic, "ˈ", "") ~= phonemic then
        	if not phonetic:match("ˈ") then
        		phonetic = gsub(phonetic, "^", "ˈ")
        	end
        	if not yphonetic:match("ˈ") then
        		yphonetic = gsub(yphonetic, "^", "ˈ")
	        end
	        if not ophonetic:match("ˈ") then
	        	ophonetic = gsub(ophonetic, "^", "ˈ")
	        end
	        if not fphonetic:match("ˈ") then
	        	fphonetic = gsub(fphonetic, "^", "ˈ")
	        end
        	table.insert(results, { pron = "[" .. phonetic .. "]" })
        end
        if yphonetic ~= phonetic and gsub(yphonetic, "ˈ", "") ~= phonemic then
        	table.insert(results_y, { pron = "[" .. yphonetic .. "]" })
        end
        if ophonetic ~= phonetic and gsub(ophonetic, "ˈ", "") ~= phonemic then
        	table.insert(results_o, { pron = "[" .. ophonetic .. "]" })
        end
        if fphonetic ~= phonetic and gsub(fphonetic, "ˈ", "") ~= phonemic then
        	table.insert(results_f, { pron = "[" .. fphonetic .. "]" })
        end
    end
    
	local show = 
		"*" .. m_IPA.format_IPA_full { lang = lang, items = results }
    
	local hide = show .. "\n"

	local sections = {}
	
	if #results_y > 0 then
	    table.insert(sections, m_a.format_qualifiers(lang, {"younger speakers"}) .. " " .. m_IPA.format_IPA_full { lang = lang, items = results_y })
	end
	
	if #results_o > 0 then
	    table.insert(sections, m_a.format_qualifiers(lang, {"older speakers"}) .. " " .. m_IPA.format_IPA_full { lang = lang, items = results_o })
	end
	
	if #results_f > 0 then
	    table.insert(sections, m_a.format_qualifiers(lang, {"in faster speech"}) .. " " .. m_IPA.format_IPA_full { lang = lang, items = results_f })
	end
	
	if #sections > 0 then
	    hide = hide .. "**" .. table.concat(sections, "\n**")
	end
		
	if hide == show .. "\n" then
	    return show
	else
	    return '\n<div class="vsSwitcher" data-toggle-category="pronunciations">'
	        .. '<span class="vsToggleElement"></span>'
	        .. '<div class="vsShow">\n'
	        .. show
	        .. '\n</div><div class="vsHide">\n'
	        .. hide
	        .. '</div></div>\n<span></span>'
end
end

return export