Module:User:Zhnka/hrx-pronunciation

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

This is a private module sandbox of Zhnka, for his own experimentation. Items in this module may be added and removed at Zhnka's discretion; do not rely on this module's stability.


local export = {}

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

local U = mw.ustring.char
local rsub = mw.ustring.gsub
local rlower = mw.ustring.lower
local rfind = mw.ustring.find

local raised = U(0x031D)
local consonants = "bpmwfdtznɲrlkɡhvxjsʋʃs̄s̠ŋ"
local C = "[" .. consonants .. "]"
local vowels = "aeɛiouæøyə"
local V = "[" .. vowels .. "]"

local function track(page)
	require("Module:debug").track("hrx-IPA/" .. page)
	return true
end

local function rsub_repeatedly(term, foo, bar)
	while true do
		local new_term = rsub(term, foo, bar)
		if new_term == term then
			return term
		end
		term = new_term
	end
end
	
local digraphs = {
	["iu"] = "yː",
	["ei"] = "ɛi̯",
	["ie"] = "iə̯",
	["ou"] = "ou̯",
	["uo"] = "uə̯",
	["öu"] = "øy̯",
	["eu"] = "ɛu̯",
	["uu"] = "uː",
	["ck"] = "k",
	["ng"] = "ŋ",
	["qu"] = "kʷ", "kv",
	["sch"] = "ʃ",
	["sk"] = "ʃk",
	["sp"] = "ʃp",
	["st"] = "ʃt",
	["ng"] = "ŋ",
	["tz"] = "t͡s",
}

local function flatmap(items, fun)
	local new = {}
	for _, item in ipairs(items) do
		local results = fun(item)
		mw.logObject(results, "results")
		for _, result in ipairs(results) do
			table.insert(new, result)
		end
		mw.logObject(new, "new")
	end
	return new
end

local phon = {
	["a"] = "a",
	["â"] = "aː",
	["ä"] = "æ",
	["æ"] = "æː",
	["e"] = "e",
	["ë"] = "ɛ",
	["ê"] = "ɛː",
	["i"] = "i",
	["î"] = "iː",
	["o"] = "o",
	["ô"] = "oː",
	["u"] = "u",
	["û"] = "uː",
	["ö"] = "ø",
	["œ"] = "øː",
	["ü"] = "y",
	["b"] = "p",
	["g"] = "k",
	["w"] = "v",
	["z"] = "t͡s",
	["x"] = "ks",
	["r"] = "ɾ",
	[":"] = "ː",
	["?"] = "ʔ"
}

function export.phonemic(text, post)
	text = rlower(text)

	text = rsub(text, " | ", "# | #")
	text = "##" .. rsub(text, " ", "# #") .. "##"

	-- basic phonology
	for digraph, replacement in pairs(digraphs) do
		text = rsub(text, digraph, replacement)
	end
	text = rsub(text, ".", phon)
	
	text = rsub(text, "'", "ˈ")
	if not rfind(text, " ") and not rfind(text, "ˈ") then
	text = rsub(text, text, "ˈ" .. text)
	end
	text = rsub(text, "-", "");
	text = rsub(text, "t͡s#", "t̬͡s̬#")
	text = rsub(text, "ks#", "̬ks̬#")
	text = rsub(text, "[ktfs]#", "%1̬#")
	text = rsub(text, "(c)([aou])", "k%2")
	text = rsub(text, "(c)([ei])", "s%2")
	text = rsub(text, "([aou])(ch)", "%1x")
	text = rsub(text, "([ei])(ch)", "%1ç")
	text = rsub(text, "(e)(" .. C .. ")", "ə%2")
	text = rsub(text, "(e)#", "ə#")
	text = rsub(text, "(ˈ" .. C .. "*)(ə)", "%1e")
	text = rsub(text, "(ˈ##" .. C .. "*)(ə)", "%1e")
	text = rsub(text, "^ˈ%-", "-")

	local variants = {text}

	local function flatmap_and_sub_pre(from, to1, to2)
		variants =
			flatmap(
			variants,
			function(item)
				if rfind(item, from) then
					local retval = {rsub_repeatedly(item, from, to1)}
					if to2 then
						m_table.insertIfNot(retval, rsub_repeatedly(item, from, to2))
					end
					return retval
				else
					return {item}
				end
			end
		)
	end
	
	flatmap_and_sub_pre("#", "")

	return variants
end

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

	args = frame:getParent().args
	local currentTitle = mw.title.getCurrentTitle().text

	for _, term in ipairs(args) do
		if term == currentTitle then track("redundant") end
		table.insert(terms, term)
	end

	if #terms == 0 then
		terms = {currentTitle}
	end

	local results = {}

	for _, term in ipairs(terms) do
		local phonemic = export.phonemic(term)
		for _, variant in ipairs(phonemic) do
			table.insert(results, {pron = "/" .. variant .. "/"})
		end
	end

	return "*" .. m_IPA.format_IPA_full { lang = lang, items = results }

end

return export