Module:qay-pron

From Linguifex
Revision as of 13:56, 24 June 2022 by Sware (talk | contribs)
Jump to navigation Jump to search


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

local lang = require("Module:languages").getByCode("qay")
local m_table = require("Module:table")
local m_IPA = require("Module:IPA")

local export = {}

local consonants = "[pbmvstdnrɾlkɡŋhxçʤʧj]"
local front = "ie"
local back = "ou"
local vowels = "[a" .. front .. back .. "ː]"

local function laxen(v)
	local otc = {}
	local switch = {["e"] = "ɛ", ["i"] = "ɪ", ["o"] = "ɔ", ["u"] = "ʊ", ["a"] = "a", ["ː"] = "ː"}
		 
	for vc in gmatch(v, ".") do
		vc = gsub(vc, vc, switch[vc])
		table.insert(otc, vc)
	end
	return table.concat(otc)
end

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 shared_rules = {
	{"n(·?)([kg])", "ŋ%1%2"}, {"ŋg", "ŋ"}, {"c", "ʧ"}, {"j", "ʤ"}, {"y", "j"}, {"g", "ɡ"}, 
	-- Long vowels
	{"ā", "aː"},  {"ē", "eː"}, {"ī", "iː"}, {"ō", "oː"}, {"ū", "uː"},
	-- Diphthongs
	{"(" .. vowels .. ")y", function(v) return laxen(v) .. "ɪ" end}, {"au", "aʊ"},
}

local phonemic_rules = {
	{"·ˈ", "ˈ"}, {"([^nŋ][·ˈ])([tkdɡ])j", "%1%2ʲ"}, 
	{"(" .. consonants .. ")(·?ˈ?)(" .. consonants .. ")", function(c1, st, c2) return same(c1,c2) and st .. c1 or c1 .. st .. c2 end},
	{"jj", "j"}, 
}

local phonetic_rules = {
	{"·ˈ", "ˈ"}, {"([^nŋ]·)[tk]j", "%1ʧ"}, {"([^nŋ]·)[dɡ]j", "%1ʤ"}, {"r", "ɾ"},
	{"h([" .. front .. "])", "ç%1"}, {"h([" .. back .. "])", "x%1"}, 
	{"([·ˈ])(" .. consonants .. "?)(" .. vowels .. "*)(" .. consonants .. ")", function(st,c1,v,c2) return st .. c1 .. laxen(v) .. c2 end},
	{"^(" .. consonants .. "?)(" .. vowels .. "*)(" .. consonants .. ")$", function(c1,v,c2) return c1 .. laxen(v) .. c2 end},
	{"(" .. consonants .. ")(·?ˈ?)(" .. consonants .. ")", function(c1, st, c2) return same(c1,c2) and st .. c1 or c1 .. st .. c2 end},
	{"jj", "j"}, {"(·" .. consonants .. ")e$", "%1ə"},
	
	
}

local function syllabify(term)
	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 noa = {}
	
	if #syll ~= 1 then
		if match(term, "[áéíóúĄ]") then
			for _, s in ipairs(syll) do
				s = remove_acute(s, match(s, "[áéíóúĄ]") and true or false)
				table.insert(noa, s)
			end
		else
			syll[#syll - 1] = "ˈ" .. syll[#syll - 1]
		end
	end

	return table.concat(#noa > 1 and noa or syll, "·")
end

function export.phonemic(term)
	term = mw.ustring.lower(term)
	
	for _, rule in ipairs(shared_rules) do
		term = gsub(term, rule[1], rule[2])
	end
	
	term = syllabify(term)
	
	for _, micrule in ipairs(phonemic_rules) do
		term = gsub(term, micrule[1], micrule[2])
	end
	
	term = gsub(term, "·", ".")
	
	return term
end

function export.phonetic(term)
	term = mw.ustring.lower(term)
	
	for _, rule in ipairs(shared_rules) do
		term = gsub(term, rule[1], rule[2])
	end
	
	term = syllabify(term)
	
	for _, ticrule in ipairs(phonetic_rules) do
		term = gsub(term, ticrule[1], ticrule[2])	
	end
	
	term = gsub(term, "a", "ä")
	term = gsub(term, "·", ".")
	
	return term
end

function export.show(frame)
	local parent_args = frame:getParent().args
	local params = {
		[1] = { default = mw.title.getCurrentTitle().nsText == 'Template' and "ankyu" or mw.title.getCurrentTitle().text },
	}
	local args = require("Module:parameters").process(parent_args, params)
	local term = args[1]

	local IPA_args = {}
	local phonemic = export.phonemic(term)
	local phonetic = export.phonetic(term)
	table.insert(IPA_args, {pron = '/' .. phonemic .. '/'})
	if phonemic ~= phonetic then
		table.insert(IPA_args, {pron = '[' .. phonetic .. ']'})
	end

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

return export