Module:mg-pron

Revision as of 21:10, 25 August 2021 by Sware (talk | contribs)


local sub = mw.ustring.sub
local find = mw.ustring.find
local match = mw.ustring.match
local gmatch = mw.ustring.gmatch
local gsub = mw.ustring.gsub
local u = mw.ustring.char
local split = mw.text.split
local gsplit = mw.text.gsplit

local NASAL = u(0x0303) -- COMBINING TILDE, ̃
local NONSYLL = (0x032F) -- COMBINING INVERTED BREVE BELOW, ̯

local velar = "[kɡɫw]"
local consonants = "[bkdhjlmnɲprɾstʃθβðɡɣzʧɫʎ]"
local vowels = "[aɑɐeɪɛiɔʊouw]"
local voiced = "[bdhʎjmnɲβðɡɣz]"

local function same(foo, bar)
	foo, bar = mw.ustring.toNFD(foo), mw.ustring.toNFD(bar) -- decompose diacritics
	foo, bar = match(foo, "^."), match(bar, "^.") -- sort out the letter
	return foo == bar and true or false
end

local export = {}

local prelims = {
	{"r", "ɾ"}, {"ch", "ʧ"}, {"qu", "k"}, {"il", "ʎ"}, {"ñ", "ɲ"}, {"g", "ɡ"}, {"c", "k"}, {"v", "b"}, 
	{"z", "θ"}, {"x", "ʃ"}, {"(" .. vowels .. ")i(" .. vowels .. ")", "%1j%2"}, {"(" .. vowels .. ")u", "%1w"}, 
}

local function syllabify(term, pos)
	term = gsub(term, "(" .. consonants .. "*)(" .. vowels .. "*)", "%1%2·")
	term = gsub(term, "··", "·"); term = gsub(term, "·$", "")
	term = gsub(term, "·(" .. consonants .. ")(" .. consonants .. ")(" .. vowels .. "*)", "%1·%2%3")
	term = gsub(term, "·(" .. consonants .. ")$", "%1")
	term = gsub(term, "·(" .. consonants .. ")·", "%1·")
	
	local syll = split(term, "·")
	
	local monosyll = {["n"] = "ˈ", ["pron"] = "", ["particle"] = "(ˈ)", ["prep"] = "(ˈ)", ["conj"] = "(ˈ)"}
	if #syll == 1 then
		if not pos then error('Part of speech needed to determine stress') end
		syll[1] = monosyll[pos] .. (pos ~= "n" and gsub(syll[1], "([aá])", "%1ː") or syll[1])
	else
		syll[#syll - 1] = "ˈ" .. syll[#syll - 1]
		if match(syll[#syll - 1], "[aá]") then gsub(syll[#syll - 1], "([aá])", "%1ː") end
	end

	return table.concat(syll, "·")
end

local rules = {
	{"([ɾs])·([ɾs])", function(s1, s2) return same(s1, s2) and "·ʰ" .. s1 or s1 .. s2 end}, -- rr and ss clusters, preaspirated
	{".$", {["a"] = "ɐ", ["e"] = "ɪ", ["o"] = "ʊ", ["n"] = NASAL}},
	{"l·", "ɫ·"}, {"l$", "ɫ"}, -- velarized [l]
	{"a(" .. velar .. ")", "ɑ%1"}, {"(" .. velar .. ")a", "%1ɑ"}, -- [a] backens next to velar consonants
	{"s·(" .. voiced .. ")", "z·%1"}, {"(" .. vowels .. ")·s(" .. vowels .. ")", "%1·z%2"}, -- [s]-voicing
	{"im$", "y" .. NASAL}, {"m$", "u" .. NASAL}, {"n·", NASAL .. "·"}, -- [m] and [n] behave as nasalizers in codas and word-finally
	
	{"e(" .. consonants .. ")·(" .. consonants .. ")", "ɛ%1·%2"}, {"o(" .. consonants .. ")·(" .. consonants .. ")", "ɔ%1·%2"}, 
	{"e(" .. consonants .. ")?·(" .. consonants .. ")a", "ɛ%1·%2a"}, {"o(" .. consonants .. ")?·(" .. consonants .. ")a", "ɔ%1·%2a"},
	{"(" .. vowels .. "i)", "%1̯"}, {"w", "u̯"}, 
	
	{"ʧ", "(t)ʃ"}, {"·ˈ", "ˈ"}, {"·", "."}, 
}

function export.crux(term, pos)
	term = mw.ustring.lower(term)
	
	for _, repl in ipairs(prelims) do
		term = gsub(term, repl[1], repl[2])
	end
	
	term = syllabify(term, pos)
	
	for _, rule in ipairs(rules) do
		term = gsub(term, rule[1], rule[2])
	end
	
	return term
end

function IPA_span(items)
	local bits = {}
	for _, item in ipairs(items) do
		local bit = "<span style=\"font-size:110%;font-family:'Gentium','DejaVu Sans','Segoe UI',sans-serif>" .. item.pron .. "</span>"
		table.insert(bits, bit)
	end
	return table.concat(bits)
end

function format_IPA(items)
	return "[[w:IPA chart|IPA]]<sup>([[IPA for Modern Gallaecian|key]])</sup>:&#32;" .. IPA_span(items)
end

function line_format(pronunciation, register)
	local full_pronunciations = {}
	local IPA_args = {{pron = '[' .. pronunciation .. ']'}}
	table.insert(full_pronunciations, format_IPA(IPA_args))
	return "(''" .. table.concat(register, ", ") .. "'')" .. ' ' .. table.concat(full_pronunciations, ' or ')
end

function separate_word(term, pos)
	local result = {}
	
	for word in gsplit(term, " ") do
		table.insert(result, export.crux(word , pos))
	end
	
	return table.concat(result, " ")
end

function export.show(frame)
	local params = {
		[1] = { default = mw.title.getCurrentTitle().text }, -- PAGENAME
		[2] = {},
	}
	local args = require("Module:parameters").process(frame:getParent().args, params)
	local term = frame.args[1] or mw.title.getCurrentTitle().nsText == 'Template' and "gueizuñe" or args[1]
	local pos = frame.args[2] or args[2]
	
	local ipa = "* "
	ipa = ipa .. line_format(separate_word(term, pos), {'Calá'})
	
	return ipa
end

return export