Module:gender and number

Revision as of 15:05, 6 August 2023 by Maria (talk | contribs)

Gender and number is the (rather confusingly-named, honestly) module on Laenktionary that generates little snippets like Lua error at line 50: attempt to concatenate local 'cats' (a nil value). for nouns, or like Lua error at line 50: attempt to concatenate local 'cats' (a nil value). 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 Lua error at line 50: attempt to concatenate local 'cats' (a nil value)..

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 = "dualia tantum", category = "dualia tantum %", group = "number"},
	["p"] = {label = "pl", hint = "pluralia 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_code = args["lang"]
	local pos = args["pos"]
	if lang_code == nil or lang_code == "" then
		error("A language code must be given")
	end
	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_code, pos, genders)
	return text .. cats
end

function export.format_genders(lang_code, pos, genders)
	local language = require("Module:languages").get_by_code(lang_code)
	local parts = {}
	local groups = {}
	local cats = {}
	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, "%%", pos .. "s") .. "]]")
							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