跳转到内容

模組:Sv-adjectives

維基詞典,自由的多語言詞典


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

local export = {}

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

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

-- 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 = frame:getParent().args
	
	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["reg"] = 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 alt = args["alt"]; if alt == "" then alt = nil end
	local n = args["n"]; if n == "" then n = nil end
	
	data.forms["pos_indf_c_sg"] = {par1 or mw.title.getCurrentTitle().text}
	data.forms["pos_indf_n_sg"] = {par2 or n or (par1 or mw.title.getCurrentTitle().text) .. "t"}
	data.forms["pos_indf_pl"]   = {(par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "a"}
	data.forms["pos_defn_m_sg"] = {(par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "e"}
	data.forms["pos_defn_all"]  = {(par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "a"}
	
	data.forms["comp_indf_c_sg"] = {(par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "are"}
	data.forms["comp_indf_n_sg"] = {(par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "are"}
	data.forms["comp_indf_pl"]   = {(par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "are"}
	data.forms["comp_defn_m_sg"] = {(par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "are"}
	data.forms["comp_defn_all"]  = {(par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "are"}
	
	data.forms["sup_indf_c_sg"] = {(par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "ast"}
	data.forms["sup_indf_n_sg"] = {(par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "ast"}
	data.forms["sup_indf_pl"]   = {(par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "ast"}
	data.forms["sup_defn_m_sg"] = {(par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "aste"}
	data.forms["sup_defn_all"]  = {(par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "aste"}
end

inflections["abs"] = 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 alt = args["alt"]; if alt == "" then alt = nil end
	local n = args["n"]; if n == "" then n = nil end
	
	data.forms["pos_indf_c_sg"] = {par1 or mw.title.getCurrentTitle().text}
	data.forms["pos_indf_n_sg"] = {par2 or n or (par1 or mw.title.getCurrentTitle().text) .. "t"}
	data.forms["pos_indf_pl"]   = {(par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "a"}
	data.forms["pos_defn_m_sg"] = {(par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "e"}
	data.forms["pos_defn_all"]  = {(par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "a"}
end

inflections["peri"] = 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 alt = args["alt"]; if alt == "" then alt = nil end
	local n = args["n"]; if n == "" then n = nil end
	
	data.forms["pos_indf_c_sg"] = {par1 or mw.title.getCurrentTitle().text}
	data.forms["pos_indf_n_sg"] = {par2 or n or (par1 or mw.title.getCurrentTitle().text) .. "t"}
	data.forms["pos_indf_pl"]   = {(par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "a"}
	data.forms["pos_defn_m_sg"] = {(par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "e"}
	data.forms["pos_defn_all"]  = {(par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "a"}
	
	data.forms["comp_indf_c_sg"] = {"[[mer]] " .. (par1 or mw.title.getCurrentTitle().text)}
	data.forms["comp_indf_n_sg"] = {"[[mer]] " .. (par2 or n or (par1 or mw.title.getCurrentTitle().text) .. "t")}
	data.forms["comp_indf_pl"]   = {"[[mer]] " .. ((par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "a")}
	data.forms["comp_defn_m_sg"] = {"[[mer]] " .. ((par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "e")}
	data.forms["comp_defn_all"]  = {"[[mer]] " .. ((par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "a")}
	
	data.forms["sup_indf_c_sg"] = {"[[mest]] " .. (par1 or mw.title.getCurrentTitle().text)}
	data.forms["sup_indf_n_sg"] = {"[[mest]] " .. (par2 or n or (par1 or mw.title.getCurrentTitle().text) .. "t")}
	data.forms["sup_indf_pl"]   = {"[[mest]] " .. ((par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "a")}
	data.forms["sup_defn_m_sg"] = {"[[mest]] " .. ((par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "e")}
	data.forms["sup_defn_all"]  = {"[[mest]] " .. ((par3 or alt or par1 or mw.title.getCurrentTitle().text) .. "a")}
end

inflections["inv"] = function(args, data)
	local par1 = args[1]; if par1 == "" then par1 = nil end
	
	data.forms["pos_indf_c_sg"] = {par1 or mw.title.getCurrentTitle().text}
	data.forms["pos_indf_n_sg"] = {par1 or mw.title.getCurrentTitle().text}
	data.forms["pos_indf_pl"]   = {par1 or mw.title.getCurrentTitle().text}
	data.forms["pos_defn_m_sg"] = {par1 or mw.title.getCurrentTitle().text}
	data.forms["pos_defn_all"]  = {par1 or mw.title.getCurrentTitle().text}
	
	data.forms["comp_indf_c_sg"] = {"[[mer]] " .. (par1 or mw.title.getCurrentTitle().text)}
	data.forms["comp_indf_n_sg"] = {"[[mer]] " .. (par1 or mw.title.getCurrentTitle().text)}
	data.forms["comp_indf_pl"]   = {"[[mer]] " .. (par1 or mw.title.getCurrentTitle().text)}
	data.forms["comp_defn_m_sg"] = {"[[mer]] " .. (par1 or mw.title.getCurrentTitle().text)}
	data.forms["comp_defn_all"]  = {"[[mer]] " .. (par1 or mw.title.getCurrentTitle().text)}
	
	data.forms["sup_indf_c_sg"] = {"[[mest]] " .. (par1 or mw.title.getCurrentTitle().text)}
	data.forms["sup_indf_n_sg"] = {"[[mest]] " .. (par1 or mw.title.getCurrentTitle().text)}
	data.forms["sup_indf_pl"]   = {"[[mest]] " .. (par1 or mw.title.getCurrentTitle().text)}
	data.forms["sup_defn_m_sg"] = {"[[mest]] " .. (par1 or mw.title.getCurrentTitle().text)}
	data.forms["sup_defn_all"]  = {"[[mest]] " .. (par1 or mw.title.getCurrentTitle().text)}
end

inflections["irreg"] = 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 par4 = args[4]; if par4 == "" then par4 = nil end
	local par5 = args[5]; if par5 == "" then par5 = nil end
	local par6 = args[6]; if par6 == "" then par6 = nil end
	local par7 = args[7]; if par7 == "" then par7 = nil end
	local par8 = args[8]; if par8 == "" then par8 = nil end
	local par9 = args[9]; if par9 == "" then par9 = nil end
	local par10 = args[10]; if par10 == "" then par10 = nil end
	
	if par9 ~= par8 then
		require("Module:debug").track("sv-adjectives/98")
	end
	
	data.forms["pos_indf_c_sg"] = {par1 or mw.title.getCurrentTitle().text}
	data.forms["pos_indf_n_sg"] = {par2}
	data.forms["pos_indf_pl"]   = {par5}
	data.forms["pos_defn_m_sg"] = {par3}
	data.forms["pos_defn_all"]  = {par4}
	
	data.forms["comp_indf_c_sg"] = {par6}
	data.forms["comp_indf_n_sg"] = {par6}
	data.forms["comp_indf_pl"]   = {par6}
	data.forms["comp_defn_m_sg"] = {par6}
	data.forms["comp_defn_all"]  = {par6}
	
	data.forms["sup_indf_c_sg"] = {par10}
	data.forms["sup_indf_n_sg"] = {par10}
	data.forms["sup_indf_pl"]   = {par10}
	data.forms["sup_defn_m_sg"] = {par7}
	data.forms["sup_defn_all"]  = {par8}
end

inflections["prespart"] = function(args, data)
	local par1 = args[1]; if par1 == "" then par1 = nil end
	
	data.forms["pos_indf_c_sg"] = {par1 or mw.title.getCurrentTitle().text}
	data.forms["pos_indf_n_sg"] = {par1 or mw.title.getCurrentTitle().text}
	data.forms["pos_indf_pl"]   = {par1 or mw.title.getCurrentTitle().text}
	data.forms["pos_defn_m_sg"] = {par1 or mw.title.getCurrentTitle().text}
	data.forms["pos_defn_all"]  = {par1 or mw.title.getCurrentTitle().text}
end

inflections["pastpart"] = 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 par4 = args[4]; if par4 == "" then par4 = nil end
	local par5 = args[5]; if par5 == "" then par5 = nil end
	
	local enstem = args["en-stem"]; if enstem == "" then enstem = nil end
	
	data.forms["pos_indf_c_sg"] = {(enstem and enstem .. "en") or par1 or mw.title.getCurrentTitle().text}
	data.forms["pos_indf_n_sg"] = {(enstem and enstem .. "et") or par2}
	data.forms["pos_indf_pl"]   = {(enstem and enstem .. "na") or par5 or par4 or par3 or ((par1 or mw.title.getCurrentTitle().text) .. "e")}
	data.forms["pos_defn_m_sg"] = {(enstem and enstem .. "ne") or par3 or ((par1 or mw.title.getCurrentTitle().text) .. "e")}
	data.forms["pos_defn_all"]  = {(enstem and enstem .. "na") or par4 or par3 or ((par1 or mw.title.getCurrentTitle().text) .. "e")}
end


-- Helper functions

function postprocess(args, data)
	-- Check if the lemma form matches the page name
	if not appendix and lang:makeEntryName(data.forms["pos_indf_c_sg"][1]) ~= mw.title.getCurrentTitle().text then
		table.insert(data.categories, "Swedish 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 "—"
		elseif type(form) ~= "table" then
			error("a non-table value was given in the list of inflected forms.")
		elseif #form == 0 then
			return "—"
		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, "<br/>")
	end
	
	local function repl(param)
		if param == "lemma" then
			return m_links.full_link({lang = lang, alt = mw.title.getCurrentTitle().text}, "term")
		elseif param == "info" then
			return data.title and " (" .. data.title .. ")" or ""
		else
			return show_form(data.forms[param])
		end
	end
	
	local wikicode = [=[
{| class="inflection-table vsSwitcher vsToggleCategory-inflection" style="border: solid 1px #CCCCFF; text-align:left;" cellspacing="1" cellpadding="2"
|- style="background: #CCCCFF; vertical-align: top;"
! class="vsToggleElement" colspan="4" | {{{lemma}}}{{{info}}}的變格
|- class="vsHide" style="background: #CCCCFF;"
! style="min-width: 12em;" | 不定
! style="min-width: 12em;" | 肯定
! style="min-width: 12em;" | 比较级
! style="min-width: 12em;" | 最高级<sup>2</sup>
|- class="vsHide" style="background: #F2F2FF;"
! style="background: #E6E6FF;" | 通性單數
| {{{pos_indf_c_sg}}}
| {{{comp_indf_c_sg}}}
| {{{sup_indf_c_sg}}}
|- class="vsHide" style="background: #F2F2FF;"
! style="background: #E6E6FF;" | 中性單數
| {{{pos_indf_n_sg}}}
| {{{comp_indf_n_sg}}}
| {{{sup_indf_n_sg}}}
|- class="vsHide" style="background: #F2F2FF;"
! style="background: #E6E6FF;" | 复数
| {{{pos_indf_pl}}}
| {{{comp_indf_pl}}}
| {{{sup_indf_pl}}}
|- class="vsHide" style="background: #CCCCFF;"
! 定
! 肯定
! 比较级
! 最高级
|- class="vsHide" style="background: #F2F2FF;"
! style="background: #E6E6FF;" | 陽性單數<sup>1</sup>
| {{{pos_defn_m_sg}}}
| {{{comp_defn_m_sg}}}
| {{{sup_defn_m_sg}}}
|- class="vsHide" style="background: #F2F2FF;"
! style="background: #E6E6FF;" | 其他
| {{{pos_defn_all}}}
| {{{comp_defn_all}}}
| {{{sup_defn_all}}}
|- class="vsHide" style="background: #E6E6FF;"
| style="font-size: smaller;" colspan="4" | 1) 只用於指陽性生物。<br/>2) 不定最高級僅用於表語。
|}]=]
	return mw.ustring.gsub(wikicode, "{{{([a-z0-9_:]+)}}}", repl)
end

return export