Module:gender and number

From Laenkea
Revision as of 12:02, 27 February 2024 by Maria (talk | contribs)
Jump to navigation Jump to search

Gender and number is the (rather confusingly-named, honestly) module on Laenktionary that generates little snippets like m or f for nouns, or like pf for verbs.

Mostly, you'll see this on higher level templates where you might be able to specify multiple "genders" as a list of parameters. This might look like:

{{lfv-noun|m|or|f}}

to generate a Laefevian noun with the gender m or f.

There are specific genders/aspects/numbers defined, which can be seen in the script below. As in the example above, you can also specify "or" as a special one to create an option between two other "genders".


local export = {}

local available_genders = {
	["n"] = {label = "n", hint = "neuter gender", category = "neuter %", group = "gender"},
	["f"] = {label = "f", hint = "feminine gender", category = "feminine %", group = "gender"},
	["m"] = {label = "m", hint = "masculine gender", category = "masculine %", group = "gender"},
	["c"] = {label = "c", hint = "common gender", category = "common %", group = "gender"},
	["d"] = {label = "du", hint = "duale tantum", category = "dualia tantum", group = "number"},
	["p"] = {label = "pl", hint = "plurale tantum", category = "pluralia tantum", group = "number"},
	["s"] = {label = "sg", hint = "singular number", category = nil, group = "number"},
	["in"] = {label = "inan", hint = "inanimate", category = "inanimate %", group = "animacy"},
	["an"] = {label = "anim", hint = "animate", category = "animate %", group = "animacy"},
	["pr"] = {label = "pers", hint = "personal", category = "personal %", group = "animacy"},
	["np"] = {label = "npers", hint = "nonpersonal", category = "nonpersonal %", group = "animacy"},
	["anml"] = {label = "animal", hint = "animal", category = "animal %", group = "animacy"},
	["nv"] = {label = "nvir", hint = "nonvirile", category = "nonvirile %", group = "virility"},
	["vr"] = {label = "vir", hint = "virile", category = "virile %", group = "virility"},
	["impf"] = {label = "impf", hint = "imperfective aspect", category = "imperfective %", group = "aspect"},
	["pf"] = {label = "pf", hint = "perfective aspect", category = "perfective %", group = "aspect"},
	["?"] = {label = "?", hint = "gender incomplete", category = nil, group = "other"},
	["?!"] = {label = "gender unattested", hint = "gender unattested", category = nil, group = "other"}
}
local combinations = {
	["mf"] = {"m", "or", "f"},
	["van"] = {"in", "or", "an"},
	["biasp"] = {"impf", "pf"}
}
local multiple_group_categories = {
	["gender"] = "% with multiple genders",
	["animacy"] = "% with multiple animacies",
	["aspect"] = "biaspectual %"
}

function export.show_list(frame)
	local args = frame.args
	local lang = require("Module:languages").get_by_code(args["lang"])
	local pos = args["pos"]
	local genders = {}
	local i = 1
	
	while args[i] and args[i] ~= "" do
		table.insert(genders, args[i])
		i = i + 1
	end

	local text, cats = export.format_genders(lang, pos, genders)
	return text .. (cats or "")
end

function export.format_genders(language, pos, genders)
	local parts = {}
	local groups = {}
	local cats = {}
	
	local function pluralize(word)
		local ending = mw.ustring.sub(word, -1)
		if ending == "h" and mw.ustring.sub(word, -2, -1) == "s" then ending = "sh" end
		if ending == "s" or ending == "z" or ending == "x" or ending == "sh" then
			return word .. "es"
		end
		return word .. "s"
	end

	for _, g_code in ipairs(genders) do
		local split_codes = combinations[g_code] or mw.text.split(g_code, "-")
		for _, split_code in ipairs(split_codes) do
			if split_code == "or" then
				table.insert(parts, "or")
			else
				local gender = available_genders[split_code]
				if gender ~= nil then
					table.insert(parts, '<abbr title="' .. gender.hint .. '">' .. gender.label .. "</abbr>")
					if pos ~= nil then
						if groups[gender.group] == nil then
							table.insert(cats, "[[Category:" .. language.name .. " " .. string.gsub(gender.category, "%%", pluralize(pos)) .. "]]")
							groups[gender.group] = 1
						else
							groups[gender.group] = groups[gender.group] + 1
						end
					end
				end
			end
		end
	end
	if pos ~= nil then
		for k, v in pairs(groups) do
			if type(v) == "number" and v > 1 and multiple_group_categories[k] ~= nil then
				table.insert(cats, "[[Category:" .. language.name .. " " .. string.gsub(multiple_group_categories[k], "%%", pos) .. "]]")
			end
		end
	end
	return '<span class="gender">' .. table.concat(parts, " ") .. "</span>", pos and table.concat(cats)
end

return export