Jump to content

Module:phl-noun

From Wiktionary, the free dictionary


local export = {}
local lang = require("Module:languages").getByCode("phl")
local m_string_utilities = require("Module:string utilities")
local iut = require("Module:inflection utilities")

local u = require("Module:string/char")
local rsplit = mw.text.split
local rfind = mw.ustring.find
local rmatch = mw.ustring.match
local rgmatch = mw.ustring.gmatch
local rsubn = mw.ustring.gsub
local ulen = mw.ustring.len
local usub = mw.ustring.sub
local uupper = mw.ustring.upper
local ulower = mw.ustring.lower

local function remove_accent(base)
    local accent_map = {
        ["á"] = "a", ["é"] = "e",  ["í"] = "i", ["ó"] = "o", ["ú"] = "u",
        ["Á"] = "A", ["É"] = "E", ["Í"] = "I", ["Ó"] = "O", ["Ú"] = "U"
    }
    base = rsubn(base, ".", function(char)
        return accent_map[char] or char
    end)
    return base
end

local decls = {}
local declprops = {}

-- a-decl (Table 5.10, e.g. [[kráam]])
decls["a"] = function(base, forms)
	local accented = false
	if usub(base, -1) == "ó" then
		accented = true
	end
	stem = base
	stem = rsubn(stem, "u$", "") -- remove final unaccented -u (Table 5.14, e.g. [[tóoru]])
	stem = rsubn(stem, "ó$", "") -- remove final accented -ó (Table 5.15, e.g. [[ghaḍeeró]])
	iut.insert_form(forms, "nom_sg", {form=base})
	iut.insert_form(forms, "obl_sg", {form=stem .. (accented and "á" or "a")})
	iut.insert_form(forms, "nom_pl", {form=stem .. (accented and "á" or "a")})
	iut.insert_form(forms, "obl_pl", {form=stem .. (accented and "ám" or "am")})
end

declprops["a"] = {
	desc = "a-decl",
	cat = "a-declension nouns",
}

-- a-decl with ending -í or -ú. computed automatically from type a (Table 5.12, e.g. [[kilí]])
decls["a-iu"] = function(base, forms)
	stem = remove_accent(base)
	iut.insert_form(forms, "nom_sg", {form=base})
	iut.insert_form(forms, "obl_sg", {form=base})
	iut.insert_form(forms, "nom_pl", {form=stem .. "á"})
	iut.insert_form(forms, "obl_pl", {form=stem .. "óom"})
end

declprops["a-iu"] = {
	desc = "a-decl ending in -í or -ú",
	cat = "a-declension nouns ending in -í or -ú",
}

-- a-decl with ending -ái. computed automatically from type a (Table 5.16, e.g. [[mangái]])
decls["a-ai"] = function(base, forms)
	stem = rsubn(base, "ái$", "")
	iut.insert_form(forms, "nom_sg", {form=base})
	iut.insert_form(forms, "obl_sg", {form=stem .. "ée"})
	iut.insert_form(forms, "obl_sg", {form=stem .. "éea"})
	iut.insert_form(forms, "nom_pl", {form=stem .. "ée"})
	iut.insert_form(forms, "nom_pl", {form=stem .. "éea"})
	iut.insert_form(forms, "obl_pl", {form=stem .. "éem"})
end

declprops["a-ai"] = {
	desc = "a-decl ending in -ái",
	cat = "a-declension nouns ending in -ái",
}

-- a-decl but the accent shifts to the ending (Table 5.11, e.g. [[deés]])
decls["a-shift"] = function(base, forms)
	stem = remove_accent(base)
	iut.insert_form(forms, "nom_sg", {form=base})
	iut.insert_form(forms, "obl_sg", {form=stem .. "á"})
	iut.insert_form(forms, "nom_pl", {form=stem .. "á"})
	iut.insert_form(forms, "obl_pl", {form=stem .. "óom"})
end

declprops["a-shift"] = {
	desc = "a-decl w/ accent shift",
	cat = "a-declension nouns with accent shift",
}

-- a-decl with length alternation (Table 5.13, e.g. [[dáar]])
decls["a-length"] = function(base, forms)
	stem = base
	stem = rsubn(stem, "aá", "a")
	stem = rsubn(stem, "áa", "a")
	iut.insert_form(forms, "nom_sg", {form=base})
	iut.insert_form(forms, "obl_sg", {form=stem .. "á"})
	iut.insert_form(forms, "nom_pl", {form=stem .. "á"})
	iut.insert_form(forms, "obl_pl", {form=stem .. "óom"})
end

declprops["a-length"] = {
	desc = "a-decl w/ length alternation",
	cat = "a-declension nouns with length alternation",
}

-- i-decl (Table 5.17, e.g. [[ḍheér]])
decls["i"] = function(base, forms)
	stem = remove_accent(base)
	stem = rsubn(stem, "u$", "w") -- e.g. [[maṇḍáu]]
	iut.insert_form(forms, "nom_sg", {form=base})
	iut.insert_form(forms, "obl_sg", {form=stem .. "í"})
	iut.insert_form(forms, "nom_pl", {form=stem .. "í"})
	iut.insert_form(forms, "obl_pl", {form=stem .. "íim"})
end

declprops["i"] = {
	desc = "i-decl",
	cat = "i-declension nouns",
}

-- i-decl w/ umlaut (Table 5.18, e.g. [[baát]])
decls["i-uml"] = function(base, forms)
	stem = rsubn(base, "aá", "ee")
	iut.insert_form(forms, "nom_sg", {form=base})
	iut.insert_form(forms, "obl_sg", {form=stem .. "í"})
	iut.insert_form(forms, "nom_pl", {form=stem .. "í"})
	iut.insert_form(forms, "obl_pl", {form=stem .. "íim"})
end

declprops["i-uml"] = {
	desc = "i-decl w/ umlaut",
	cat = "i-declension nouns with umlaut",
}

-- i-decl with length alternation (Table 5.19, e.g. [[ǰhangaár]])
decls["i-length"] = function(base, forms)
	stem = base
	stem = rsubn(stem, "aá", "a")
	stem = rsubn(stem, "áa", "a")
	iut.insert_form(forms, "nom_sg", {form=base})
	iut.insert_form(forms, "obl_sg", {form=stem .. "í"})
	iut.insert_form(forms, "nom_pl", {form=stem .. "í"})
	iut.insert_form(forms, "obl_pl", {form=stem .. "íim"})
end

declprops["i-length"] = {
	desc = "i-decl w/ length alternation",
	cat = "i-declension nouns with length alternation",
}

-- m-decl (Table 5.20, e.g. [[phéepi]])
decls["m"] = function(base, forms)
	stem = base
	iut.insert_form(forms, "nom_sg", {form=base})
	iut.insert_form(forms, "obl_sg", {form=base})
	iut.insert_form(forms, "nom_pl", {form=stem .. "m"})
	iut.insert_form(forms, "obl_pl", {form=stem .. "m"})
end

declprops["m"] = {
	desc = "m-decl",
	cat = "m-declension nouns",
}

-- ee-decl (Table 5.22, e.g. [[qisá]])
decls["ee"] = function(base, forms)
	stem = base
	stem = rsubn(stem, "á$", "") 
	iut.insert_form(forms, "nom_sg", {form=base})
	iut.insert_form(forms, "obl_sg", {form=base})
	iut.insert_form(forms, "nom_pl", {form=stem .. "eé"})
	iut.insert_form(forms, "obl_pl", {form=stem .. "eém"})
end

declprops["ee"] = {
	desc = "ee-decl",
	cat = "ee-declension nouns",
}

-- ee-decl ending in -oó (Table 5.23, e.g. [[qisá]])
decls["ee-oo"] = function(base, forms)
	stem = base
	stem = rsubn(stem, "oó$", "") 
	iut.insert_form(forms, "nom_sg", {form=base})
	iut.insert_form(forms, "obl_sg", {form=stem .. "eé"})
	iut.insert_form(forms, "nom_pl", {form=stem .. "eé"})
	iut.insert_form(forms, "obl_pl", {form=stem .. "eém"})
end

declprops["ee-oo"] = {
	desc = "ee-decl ending in -oó",
	cat = "ee-declension nouns ending in -oó",
}

-- aan-decl (Table 5.24, e.g. [[yaár]])
decls["aan"] = function(base, forms)
	stem = remove_accent(base)
	iut.insert_form(forms, "nom_sg", {form=base})
	iut.insert_form(forms, "obl_sg", {form=stem .. "á"})
	iut.insert_form(forms, "nom_pl", {form=stem .. "aán"})
	iut.insert_form(forms, "nom_pl", {form=stem .. "á"})
	iut.insert_form(forms, "obl_pl", {form=stem .. "oóm"})
	iut.insert_form(forms, "obl_pl", {form=stem .. "aanoóm"})
end

declprops["aan"] = {
	desc = "aan-decl",
	cat = "aan-declension nouns",
}

-- aan-decl with -g- insertion
decls["aan-g"] = function(base, forms)
	stem = remove_accent(base)
	iut.insert_form(forms, "nom_sg", {form=base})
	iut.insert_form(forms, "obl_sg", {form=base})
	iut.insert_form(forms, "nom_pl", {form=stem .. "gaán"})
	iut.insert_form(forms, "obl_pl", {form=stem .. "goóm"})
	iut.insert_form(forms, "obl_pl", {form=stem .. "gaanoóm"})
end

declprops["aan-g"] = {
	desc = "aan-decl w/ -g- insertion",
	cat = "aan-declension nouns with -g- insertion",
}

local function decline_noun(base, decltype)
	local data = {forms = {}}
	
	-- fix decltype
	if decltype == "a" then
		if (usub(base, -2) == "ái") then
			decltype = "a-ai"
		elseif (usub(base, -1) == "ú" or usub(base, -1) == "í") then
			decltype = "a-iu"
		end
	elseif decltype == "ee" then
		if (usub(base, -2) == "oó") then
			decltype = "ee-oo"
		end
	end
	
	decls[decltype](base, data.forms)
    data.category = declprops[decltype].cat
    data.desc = declprops[decltype].desc
	return data
end

local function make_table(data)
	result = [=[
{| class="inflection-table vsSwitcher" data-toggle-category="declension" style="background:var(--wikt-palette-lavender, #f8f8ff); text-align:center; min-width:45em; border: 1px solid #9e9e9e;"
|- style="background: var(--wikt-palette-lightblue, #d9ebff);"
! class="vsToggleElement" style="text-align: left;" colspan="4" | Declension of <i lang="phl">{lemma}</i> (<span style="font-size: smaller;">{desc}</span>)
|- class="vsHide"
! style="background:var(--wikt-palette-lightblue, #d9ebff); width:33%" |
! style="background:var(--wikt-palette-lightblue, #d9ebff)" | singular
! style="background:var(--wikt-palette-lightblue, #d9ebff)" | plural
|- class="vsHide"
! style="background:var(--wikt-palette-lighterblue, #ebf4ff)" | nominative
| {nom_sg}
| {nom_pl}
|- class="vsHide"
! style="background:var(--wikt-palette-lighterblue, #ebf4ff)" | oblique
| {obl_sg}
| {obl_pl}
{notes_clause}
|}]=]

	local notes_template = [===[
|- class="vsHide"
| colspan=3 | 
<div class="hi-footnote-outer-div">
<div class="hi-footnote-inner-div">
{footnote}
</div></div>
]===]
	
	-- footnotes
	data.forms.notes_clause = data.forms.footnote ~= "" and
		m_string_utilities.format(notes_template, data.forms) or ""
	data.forms.desc = data.desc
	data.forms.lemma = data.lemma
	result = m_string_utilities.format(result, data.forms)
		
	return result
end

function export.show(frame)
	local args = frame:getParent().args
	local word = args["head"] or mw.title.getCurrentTitle().text
	local g = args["g"] or args[1] or "m"
	local decltype = args["type"] or args[2] or "a"
	
	-- overrides
	local nom_sg = args["nom_sg"] or word
	local obl_sg = args["obl_sg"] or nil
	local nom_pl = args["nom_pl"] or nil
	local obl_pl = args["obl_pl"] or nil
	
	if word == "Template:phl-noun" then
		word = "kráam"
	end
	
	-- handle irregular declensions separately
	local data = {forms = {}}
	if decltype ~= "irreg" then
		data = decline_noun(word, decltype)
	else
		iut.insert_form(data.forms, "nom_sg", {form=nom_sg})
		iut.insert_form(data.forms, "obl_sg", {form=obl_sg})
		iut.insert_form(data.forms, "nom_pl", {form=nom_pl})
		iut.insert_form(data.forms, "obl_pl", {form=obl_pl})
	    data.category = "irregular nouns"
	    data.desc = "irreg"
	end
	data.lemma = word
	
	local props = {
	  lang = lang,
	  lemmas = {word},
	  slot_list = {{"nom_sg", "nom|sg"}, {"nom_pl", "nom|pl"}, {"obl_sg", "obl|sg"}, {"obl_pl", "obl|pl"}},
	  include_translit = false,
	  create_footnote_obj = nil,
	  canonicalize = nil,
	  preprocess_forms = nil,
	  no_deduplicate_forms = false,
	  combine_metadata_during_dedup = nil,
	  transform_accel_obj = nil,
	  format_forms = nil,
	  generate_link = nil,
	  format_tr = nil,
	  join_spans = nil,
	  allow_footnote_symbols = boolean,
	  footnotes = nil,
	}
	iut.show_forms(data.forms, props)
	
	categories = {"Phalura " .. data.category}
	return make_table(data) .. require("Module:utilities").format_categories(categories, lang)
end

return export