Module:User:Mårtensås/sv-nouns-obs

local m_utilities = require("Module:utilities") local m_links = require("Module:links")

local export = {}

local lang = require("Module:languages").getByCode("sv")

local current_title = mw.title.getCurrentTitle local namespace = current_title.nsText

-- Functions that do the actual inflecting by creating the forms of a basic term. local inflections = {}

-- Shifts down all integer arguments, removing argument 1, even when there are -- gaps. For instance, { 1, nil, 3 } will become { nil, 3 }. -- An improved version of table.remove(args, 1). local function remove_arg_1(args) local new_args = {} for k, v in pairs(args) do		if require("Module:table").isPositiveInteger(k) then if k > 1 then new_args[k - 1] = v			end else new_args[k] = v		end end return new_args end

-- The main entry point. -- This is the only function that can be invoked from a template. function export.show(frame) local infl_type = frame.args[1] or error("Inflection type has not been specified. Please pass parameter 1 to the module invocation") local args if current_title.text == "Module:User:Mårtensås/sv-nouns-obs" or current_title.text == "Module:User:Mårtensås/sv-nouns-obs/documentation" then args = frame:getParent.args else args = remove_arg_1(frame.args) end if args["base"] then require("Module:debug").track("sv-nouns/base") end if args["betydelser"] then require("Module:debug").track("sv-nouns/betydelser") end if args["definitions"] then require("Module:debug").track("sv-nouns/definitions") end if args["gender"] then require("Module:debug").track("sv-nouns/gender") end if args["genitive"] then require("Module:debug").track("sv-nouns/genitive") end if not inflections[infl_type] then error("Unknown inflection type '" .. infl_type .. "'") end local data = {forms = {}, title = nil, categories = {}} -- Generate the forms inflections[infl_type](args, data) -- Postprocess postprocess(args, data) return make_table(data) .. m_utilities.format_categories(data.categories, lang) end

--[=[	Inflection functions ]=]--

inflections["c-ar"] = function(args, data) local par1 = args[1]; if par1 == "" then par1 = nil end local par2 = args[2]; if par2 == "" then par2 = nil end local fv = args["fv"] or "" par1 = par1 or current_title.text par2 = (par2 or par1) .. fv	local s = "s" if mw.ustring.find(par1, "[xs]$") then s = "" end data.forms["indf_nom_sg"] = {par1} data.forms["indf_gen_sg"] = {par1 .. s} data.forms["defn_nom_sg"] = {par2 .. "en"} data.forms["defn_gen_sg"] = {par2 .. "ens"} data.forms["indf_nom_pl"] = {par2 .. "ar"} data.forms["indf_gen_pl"] = {par2 .. "ars"} data.forms["defn_nom_pl"] = {par2 .. "arna"} data.forms["defn_gen_pl"] = {par2 .. "arnas"} end

inflections["c-er"] = function(args, data) local par1 = args[1]; if par1 == "" then par1 = nil end local par2 = args[2]; if par2 == "" then par2 = nil end local fv = args["fv"] or "" local ien = (args["ien"] or "") ~= "" local sg_nom_def = args["sg-nom-def"]; if sg_nom_def == "" then sg_nom_def = nil end local sg_gen_def = args["sg-gen-def"]; if sg_gen_def == "" then sg_gen_def = nil end if args["sg-nom-def"] or args["sg-gen-def"] then require("Module:debug").track("sv-nouns/manual") end par2 = (par2 or par1) .. fv	local s = "s" if mw.ustring.find(par1, "[xs]$") then s = "" end data.forms["indf_nom_sg"] = {par1} data.forms["indf_gen_sg"] = {par1 .. s} data.forms["defn_nom_sg"] = {sg_nom_def or par2 .. "en"} data.forms["defn_gen_sg"] = {sg_gen_def or par2 .. "ens"} data.forms["indf_nom_pl"] = {par2 .. "er"} data.forms["indf_gen_pl"] = {par2 .. "ers"} data.forms["defn_nom_pl"] = {par2 .. "erna"} data.forms["defn_gen_pl"] = {par2 .. "ernas"} if ien then table.insert(data.forms["defn_nom_sg"], 1, sg_nom_def or par2 .. "n") table.insert(data.forms["defn_gen_sg"], 1, sg_gen_def or par2 .. "ns") end end

inflections["c-or"] = function(args, data) local par1 = args[1]; if par1 == "" then par1 = nil end local obs = args["obs"]; if obs == "" then obs = nil end data["obs"] = obs if not par1 then if namespace == "Template" or namespace == "Module" then par1 = "" else error("parameter 1 is required") end end data.forms["indf_nom_sg"] = {par1 .. "a"} data.forms["indf_gen_sg"] = {par1 .. "as"} data.forms["defn_nom_sg"] = {par1 .. "an"} data.forms["defn_gen_sg"] = {par1 .. "ans"} data.forms["indf_nom_pl"] = {par1 .. "or"} data.forms["indf_gen_pl"] = {par1 .. "ors"} data.forms["defn_nom_pl"] = {par1 .. "orna"} data.forms["defn_gen_pl"] = {par1 .. "ornas"} if obs then --Obsolete forms data.forms["indf_gen_sg_obs"] = {par1 .. "os"} data.forms["indf_acc_sg"] = {par1 .. "o"} data.forms["indf_dat_sg"] = {par1 .. "o"} data.forms["defn_gen_sg_obs"] = {par1 .. "ones"} data.forms["defn_acc_sg"] = {par1 .. "ona"} data.forms["defn_dat_sg"] = {par1 .. "one"} data.forms["indf_acc_pl"] = {par1 .. "or"} data.forms["indf_dat_pl"] = {par1 .. "om"} data.forms["defn_acc_pl"] = {par1 .. "orna"} data.forms["defn_dat_pl"] = {par1 .. "omen"} end end

inflections["c-r"] = function(args, data) local par1 = args[1]; if par1 == "" then par1 = nil end local par2 = args[2]; if par2 == "" then par2 = nil end local par3 = args[3]; if par3 == "" then par3 = nil end par1 = par1 or current_title.text par2 = par2 or par1 par3 = par3 or par2 or par1 local s = "s" if mw.ustring.find(par1, "[xs]$") then s = "" end data.forms["indf_nom_sg"] = {par1} data.forms["indf_gen_sg"] = {par1 .. s} data.forms["defn_nom_sg"] = {par2 .. "n"} data.forms["defn_gen_sg"] = {par2 .. "ns"} data.forms["indf_nom_pl"] = {par3 .. "r"} data.forms["indf_gen_pl"] = {par3 .. "rs"} data.forms["defn_nom_pl"] = {par3 .. "rna"} data.forms["defn_gen_pl"] = {par3 .. "rnas"} end

inflections["c-zero"] = function(args, data) local stem = args["stem"]; if stem == "" then stem = nil end local pluralstem = args["pluralstem"]; if pluralstem == "" then pluralstem = nil end if not args["stem"] then require("Module:debug").track("sv-nouns/no stem") elseif stem .. "e" ~= current_title.text then require("Module:debug").track("sv-nouns/stem different") end if args["pluralstem"] then require("Module:debug").track("sv-nouns/pluralstem") end local s = "s" if mw.ustring.find(current_title.text, "[xs]$") then s = "" end data.forms["indf_nom_sg"] = {current_title.text} data.forms["indf_gen_sg"] = {current_title.text .. s} data.forms["defn_nom_sg"] = {(stem or current_title.text) .. "en"} data.forms["defn_gen_sg"] = {(stem or current_title.text) .. "ens"} data.forms["indf_nom_pl"] = {current_title.text} data.forms["indf_gen_pl"] = {current_title.text .. s} data.forms["defn_nom_pl"] = {(pluralstem or stem or current_title.text) .. "na"} data.forms["defn_gen_pl"] = {(pluralstem or stem or current_title.text) .. "nas"} end

inflections["c-are"] = function(args, data) if not mw.ustring.find(current_title.text, "are$") then error("The noun must end in -are.") end data.forms["indf_nom_sg"] = {current_title.text} data.forms["indf_gen_sg"] = {current_title.text .. "s"} data.forms["defn_nom_sg"] = {current_title.text .. "n"} data.forms["defn_gen_sg"] = {current_title.text .. "ns"} data.forms["indf_nom_pl"] = {current_title.text} data.forms["indf_gen_pl"] = {current_title.text .. "s"} data.forms["defn_nom_pl"] = {mw.ustring.sub(current_title.text, 1, -2) .. "na"} data.forms["defn_gen_pl"] = {mw.ustring.sub(current_title.text, 1, -2) .. "nas"} end

inflections["c-nde"] = function(args, data) if not mw.ustring.find(current_title.text, "nde$") then error("The noun must end in -nde.") end data.forms["indf_nom_sg"] = {current_title.text} data.forms["indf_gen_sg"] = {current_title.text .. "s"} data.forms["defn_nom_sg"] = {current_title.text .. "n"} data.forms["defn_gen_sg"] = {current_title.text .. "ns"} data.forms["indf_nom_pl"] = {current_title.text} data.forms["indf_gen_pl"] = {current_title.text .. "s"} data.forms["defn_nom_pl"] = {current_title.text .. "na"} data.forms["defn_gen_pl"] = {current_title.text .. "nas"} end

inflections["n-n"] = function(args, data) local par1 = args[1]; if par1 == "" then par1 = nil end local par2 = args[2]; if par2 == "" then par2 = nil end local par3 = args[3]; if par3 == "" then par3 = nil end local obs = args["obs"]; if obs == "" then obs = nil end data["obs"] = obs par1 = par1 or current_title.text par2 = par2 or par1 par3 = par3 or par2 or par1 local s = "s" if mw.ustring.find(par1, "[xs]$") then s = "" end data.forms["indf_nom_sg"] = {par1} data.forms["indf_gen_sg"] = {par1 .. s} data.forms["defn_nom_sg"] = {par2 .. "t"} data.forms["defn_gen_sg"] = {par2 .. "ts"} data.forms["indf_nom_pl"] = {par3 .. "n"} data.forms["indf_gen_pl"] = {par3 .. "ns"} data.forms["defn_nom_pl"] = {par3 .. "na"} data.forms["defn_gen_pl"] = {par3 .. "nas"} if obs then --Obsolete forms data.forms["indf_dat_sg"] = {par1} data.forms["defn_gen_sg_obs"] = {par1 .. s .. "ens"} data.forms["defn_dat_sg"] = {par2 .. "no"} data.forms["indf_dat_pl"] = {par1 .. "om"} data.forms["defn_dat_pl"] = {par1 .. "omen"} end end

inflections["n-r"] = function(args, data) local par1 = args[1]; if par1 == "" then par1 = nil end local par2 = args[2]; if par2 == "" then par2 = nil end par1 = par1 or current_title.text par2 = par2 or par1 local s = "s" if mw.ustring.find(par1, "[xs]$") then s = "" end data.forms["indf_nom_sg"] = {par1} data.forms["indf_gen_sg"] = {par1 .. s} data.forms["defn_nom_sg"] = {par2 .. "t"} data.forms["defn_gen_sg"] = {par2 .. "ts"} data.forms["indf_nom_pl"] = {par2 .. "r"} data.forms["indf_gen_pl"] = {par2 .. "rs"} data.forms["defn_nom_pl"] = {par2 .. "rna"} data.forms["defn_gen_pl"] = {par2 .. "rnas"} end

inflections["n-zero"] = function(args, data) local stem = args["stem"]; if stem == "" then stem = nil end local s = "s" if mw.ustring.find(current_title.text, "[xs]$") then s = "" end data.forms["indf_nom_sg"] = {current_title.text} data.forms["indf_gen_sg"] = {current_title.text .. s} data.forms["defn_nom_sg"] = {(stem or current_title.text) .. "et"} data.forms["defn_gen_sg"] = {(stem or current_title.text) .. "ets"} data.forms["indf_nom_pl"] = {current_title.text} data.forms["indf_gen_pl"] = {current_title.text .. s} data.forms["defn_nom_pl"] = {(stem or current_title.text) .. "en"} data.forms["defn_gen_pl"] = {(stem or current_title.text) .. "ens"} end

-- Helper functions

function postprocess(args, data) local n = args["n"]; if n == "" then n = nil end if n == "pl" then table.insert(data.categories, lang:getCanonicalName .. " pluralia tantum") end if n == "sg" then table.insert(data.categories, lang:getCanonicalName .. " uncountable nouns") end for key, form in pairs(data.forms) do		-- Do not show singular or plural forms for nominals that don't have them if (n == "pl" and key:find("_sg$")) or (n == "sg" and key:find("_pl$")) then form = nil end data.forms[key] = form end -- Check if the lemma form matches the page name if lang:makeEntryName(data.forms[n == "pl" and "indf_nom_pl" or "indf_nom_sg"][1]) ~= current_title.text then table.insert(data.categories, lang:getCanonicalName .. " entries with inflection not matching pagename") end end

-- Make the table function make_table(data) local function show_form(form) if not form then return "&mdash;" elseif type(form) ~= "table" then error("a non-table value was given in the list of inflected forms.") elseif #form == 0 then return "&mdash;" end local ret = {} for key, subform in ipairs(form) do			table.insert(ret, m_links.full_link({lang = lang, term = subform})) end return table.concat(ret, " ") end local function repl(param) if param == "lemma" then return m_links.full_link({lang = lang, alt = current_title.text}, "term") elseif param == "info" then return data.title and " (" .. data.title .. ")" or "" elseif param:find "_obs$" then -- There shouldn't be multiple obsolete forms, because it'd look -- weird to have multiple lines with a dagger on the last line only. if data.forms[param] then assert(#data.forms[param] <= 1) return ", " .. show_form(data.forms[param]) .. "†"			else return "" end else return show_form(data.forms[param]) end end local wikicode if data["obs"] then --Showing obsolete forms wikicode = [=[ else --Not showing obsolete forms wikicode = [=[ end return mw.ustring.gsub(wikicode, "", repl) end

return export