Module:siwa-noun

From Linguifex
Jump to navigation Jump to search


local export = {}

local m_u = require('Module:utilities')
local m_data = require('Module:siwa-noun/data')
local m_com = require('Module:siwa-noun/common')
local lang = require('Module:languages').getByCode("siwa")

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 PAGENAME = mw.title.getCurrentTitle().text
local NAMESPACE = mw.title.getCurrentTitle().nsText
local HOOK = u(0x0309) -- COMBINING HOOK ABOVE ̉

local vowels = "[aeiouyůõảẻỉỏủỷęȯởử]"
local consonants = "[mpbvntdsṡʦʨʥŋɲcɟħðrṁṅḥkgġhłƛɬḍ]"

local function dedigraphicize(word)
	for digraph, repl in pairs(m_com.digraphs_to_single) do
		word = gsub(word, digraph, repl)
	end
	return word
end

local function syll_count(word)
	word = dedigraphicize(word)
	
	local pattern = "(" .. consonants .. "?" .. vowels .. "+ː?" .. consonants .. "*)"
	local _, n = gsub(word, pattern, "_")
	
	return n
end

local function detect_quality(word, stressed)
	local n = syll_count(word)
	stressed = dedigraphicize(stressed)
	
	if (match(stressed, vowels .. vowels .. vowels .. "?") or match(mw.ustring.toNFD(stressed), HOOK) or match(stressed, "õu")) and n<3 then
		return "l" -- long nouns 
	elseif (match(stressed, vowels .. vowels .. vowels .. "?") or match(mw.ustring.toNFD(stressed), HOOK) or match(stressed, "õu")) or n>=3 then
		return "w" -- weak nouns
	else return "s" -- strong nouns
	end 
end

local function detect_decl(word, stressed, gender)
	word = dedigraphicize(word)
	if gender == "a" then return "animate"
	elseif word:match("[ảẻỉỏủỷởử]$") or word:match("i[rů]$") or word:match("[aeio]u$") or word:match("[aeiouyůử]l$") then
		return "l"
	elseif match(vowels, sub(word, -1, -1)) then
		return m_com.stressed_vowels[stressed]
	else
		return sub(word, -1, -1)
	end
end

-- The main entry point.
function export.show(frame)
	local parent_args = frame:getParent().args
	
	local numbers = {}
	local decl = {}
	local g = NAMESPACE == "Template" and "i" or parent_args[1]
	local word = NAMESPACE == "Template" and "sivi" or parent_args["word"] or PAGENAME
	local args = {}
	local sv = NAMESPACE == "Template" and "i" or parent_args[2]
	local decl_type = (parent_args["decl"] == "decl" and detect_decl(word, sv, g)) or parent_args["decl"] or detect_decl(word, sv, g)
	if decl_type == "l" then parent_args["nocoal"] = true end
	
	if g ~= "i" and g ~= "a" then error("Unknown gender: it must be either ‘i’ or ‘a’") end
	
	if not m_data[word] then
		
		if not decl_type then
			error("Unknown declension '" .. decl_type .. "'")
		end
		
		args = require("Module:parameters").process(parent_args, m_data[decl_type].params, true)
	
		if numbers then
			for i, number in ipairs(numbers) do
				args[i] = number
			end
		end
	end

	local data = {forms = {}, categories = {}}
	
	data.head = parent_args["head"] or word
	data.proper = parent_args["proper"] and true or false
	data.nocat = parent_args["nocat"] and true or false
	data.sv = sv or error("Parameter 2 must be the word's stressed vowel")
	data.g = g
	data.alt = parent_args["alt"]
	data.decl_type = decl_type
	data.q = parent_args["q"] or detect_quality(data.head, data.sv)
	data.pos = parent_args["pos"] or "nouns"
	
	-- Generate the forms
	if m_data[word] then
		m_data[word](parent_args, data)
	else
		m_data[decl_type](args, data)
	end
	
	local coal_cases = {"m", "ine", "ill", "ela", "ade", "all", "abl"}
	
	if g == "i" or data.pos == "adjectives" then
		if parent_args.m then data.forms["m"][1] = parent_args["m"] end
		-- Diphthong and long vowel coalescence
		if (parent_args.nocoal ~= 1) and (not m_data[word]) then
			for _, case in ipairs(coal_cases) do
				local n = 1
				local v = "(" .. vowels .. ")"
				if data.decl_type ~= "l" then
					while data.forms[case][n] do
						for regex, repl in pairs(m_com.triphthong_coalescence) do
							data.forms[case][n] = gsub(data.forms[case][n], v .. v .. v .. v, "%1%2%3@%4")
							data.forms[case][n] = gsub(data.forms[case][n], regex, repl)
							data.forms[case][n] = gsub(data.forms[case][n], "@", "")
							-- just in case some slipped through
							if not parent_args["notine"] then
								data.forms["ine"][n] = gsub(data.forms["ine"][n], "([aoueů])ia$", "%1įa")
								data.forms["ine"][n] = gsub(data.forms["ine"][n], "ỉia$", "igįia")
							end
						end
					n = n + 1
					end
				end
			end
		end
		--[[]]
	else
		for _, form in ipairs({"m_sg", "u_pl", "m_pl"}) do
			if parent_args[form] then data.forms[form][1] = parent_args[form] end
		end
	end
	
	-- make the table
	return make_table(data)
end

function make_table(data)

	local function show_form(form)
		if not form then
			return "—"
		end
		
		local ret = {}
		
		for _, subform in ipairs(form) do
			table.insert(ret, subform)
		end
			
		return table.concat(ret, ", ")
	end
	
	local function link(term)
		local links = {}
		for alt in gmatch(term, "([^%s,]+)") do
			alt = term == "—" and term or "[[Contionary:" .. alt .. "|" .. alt .. "]]"
			table.insert(links, alt)
		end
		return table.concat(links, ", ")
	end	
	
	local function repl(param)
		if param == "decl_type" then
			return data.decl_type == "irregular" and data.decl_type or "''" .. data.decl_type .. "''-declension"
		elseif param == "title" then
			return NAMESPACE == "Template" and "sivi" or data.alt or PAGENAME
		elseif param == "word" and NAMESPACE == "Template" then
			return "sivi"
		elseif param == "word" then
			return data.head
		elseif param == "gender" then
			return (data.g == "i" and "inanimate" or "animate")
		else
			return show_form(data.forms[param])
		end
	end

	function make_cases(data)
		local cases = {"inessive", "illative", "elative", "adessive", "allative", "ablative"}
		local all = {"u", "m"}
		local numbers = {"singular", "plural"}; local numbers_short = {"sg", "pl"}
		local ret = {}
		
		if data.g == "i" then
			for _, case in ipairs(cases) do
				local case_short = sub(case, 1, 3)
				table.insert(ret, "! " .. case .. "\n")
				table.insert(all, case_short)
			end
			table.insert(ret, "|-\n")
			for _, single in ipairs(all) do
				table.insert(ret, "| " .. link(show_form(data.forms[single])) .. "\n")
			end
		else
			for n, number in ipairs(numbers) do
				table.insert(ret, "|- \n! " .. number .. "\n")
				for _, single in ipairs(all) do
					table.insert(ret, "| " .. link(show_form(data.forms[single .. "_" .. numbers_short[n]])) .. "\n")
				end
			end
		end
		
		return table.concat(ret)
	end

	local navframe = [=[
<div class="mw-collapsible" style="border-collapse: collapse; margin: 0px 0px -1px 0px; padding: 2px; border: 1px solid #aaaaaa; text-align: center; font-size: 95%; overflow: auto; width: 70%;">
<div style="min-height: 1.6em; font-weight:bold; font-size: 100%; text-align: left; background-color:#efefef; padding-left: 10px; background-image: -webkit-gradient(linear, left top, left bottom, from(#EFEFEF), to(#DFDFDF), color-stop(0.6, #E3E3E3)); background-image: -moz-linear-gradient(top, #EFEFEF, #E3E3E3 60%, #DFDFDF);  background-image: -o-linear-gradient(top, #EFEFEF, #E3E3E3 60%, #DFDFDF);">''{{{title}}}'' —]=] .. (data.pos == "adjectives" and " " or " {{{gender}}} ") .. sub(data.pos, 1, -2) .. (data.g == "i" and ", {{{decl_type}}}" or "") .. [=[
</div>
<div class="mw-collapsible-content" style="font-size: 100%;">
	]=]
	
	local inanimate = [=[
	
{| class="greentable" border="1px #aaaaaa solid" margin="1em 1em 1em 0" style="background: #f9f9f9; border-collapse: collapse;" width="100%"
|-
! rowspan=2 | Unmarked<br>''agentive/dative'' !! rowspan=2 | Marked<br>''patientive/genitive'' !! colspan="6" | Locative
|-
]=] .. make_cases(data) .. [=[
|}</div></div>]=]

	local animate = [=[
{| class="greentable" border="1px #aaaaaa solid" margin="1em 1em 1em 0" style="background: #f9f9f9; border-collapse: collapse;" width="100%"
|-
! &nbsp; !! Unmarked<br>''agentive/dative'' !! Marked<br>''patientive/genitive''
|-
]=] .. make_cases(data) .. [=[
|}</div></div>]=]

	local wikicode = navframe .. (data.g == "i" and inanimate or animate)
	
	return gsub(wikicode, "{{{([a-z0-9_]+)}}}", repl) .. (not data.nocat and m_u.format_categories(data.categories, lang) or "")
end

return export