Mòdul:la-nom

De Viccionari
Icona de documentació de mòdul Documentació del mòdul[mostra] [modifica] [refresca]

A continuació es mostra la documentació transclosa de la subpàgina /ús. [salta a la caixa de codi]


Mòdul de suport per les plantilles {{la-nom-decl}} i {{la-nom propi-decl}}. Genera una taula de declinació d’un nom comú o propi en llatí. Vegeu la documentació de les plantilles per a la seva utilització.

local p = {}
local m_link = require("Mòdul:enllaç")
local m_utilities = require("Mòdul:utilitats")
local lang = {code = "la", name = "llatí", sc = "Latn"}

local function notEmpty(var)
	if var == nil or (type(var) == 'string' and mw.text.trim(var) == '') then
		return nil
	end
	return var
end

local function firstDecl(nominative, genitive, has_loc, sing)
	local decl = {}
	decl.title = "[[Viccionari:Primera declinació en llatí|1a declinació]] -a, -ae"
	local root = nominative:gsub("ae?$", "")
	if not nominative:find("ae$") then
		decl.noms = nominative
		decl.vocs = nominative
		decl.acs = root .. "am"
		decl.gens = genitive
		decl.dats = root .. "ae"
		decl.abls = root .. "ā"
		if has_loc then decl.locs = root .. "ae" end
	end
	if not sing then
		decl.nomp = root .. "ae"
		decl.vocp = root .. "ae"
		decl.acp = root .. "ās"
		decl.genp = root .. "ārum"
		decl.datp = root .. "īs"
		decl.ablp = root .. "īs"
		if has_loc then decl.locp = root .. "īs" end
	end
	return decl
end

local function firstDeclGreek(nominative, genitive, has_loc, sing)
	local decl = {}
	local noms_as = (mw.ustring.sub(nominative, -2) == "ās")
	if mw.ustring.sub(genitive, -2) == "ēs" then
		decl.title = "[[Viccionari:Primera declinació en llatí|1a declinació]] -ē, -ēs (grega)"
	elseif noms_as then
		decl.title = "[[Viccionari:Primera declinació en llatí|1a declinació]] -ās, -ae (grega)"
	else
		decl.title = "[[Viccionari:Primera declinació en llatí|1a declinació]] -ēs, -ae (grega)"
	end
	local root = mw.ustring.sub(genitive, 1, -3)
	decl.noms = nominative
	decl.vocs = root .. (noms_as and "ā" or "ē")
	decl.acs = root .. (noms_as and "ān" or "ēn")
	decl.gens = genitive
	decl.dats = root .. "ae"
	decl.abls = root .. (noms_as and "ā" or "ē")
	if has_loc then decl.locs = root .. "ae" end
	if not sing then
		decl.nomp = root .. "ae"
		decl.vocp = root .. "ae"
		decl.acp = root .. "ās"
		decl.genp = root .. "ārum"
		decl.datp = root .. "īs"
		decl.ablp = root .. "īs"
		if has_loc then decl.locp = root .. "īs" end
	end
	return decl
end

local function secondDecl_us(nominative, genitive, has_loc, sing)
	local decl = {}
	decl.title = "[[Viccionari:Segona declinació en llatí|2a declinació]] -us, -ī"
	local root = genitive:gsub("ī$", ""):gsub("ōrum$", "")
	local ius = nominative:find("ius$")
	local greek = nominative:find("os$")
	local root_i = genitive:gsub("iī$", "ī")
	if not genitive:find("ōrum$") then
		decl.noms = nominative
		decl.vocs = ius and root_i or (root .. "e")
		if greek then
			decl.acs = root .. "on"
			decl.acs2 = root .. "um"
		else
			decl.acs = root .. "um"
		end
		decl.gens = genitive
		if ius then
			decl.gens2 = root_i
		end
		decl.dats = root .. "ō"
		decl.abls = root .. "ō"
		if has_loc then decl.locs = root .. "ī" end
	end
	if not sing then
		decl.nomp = root .. "ī"
		decl.vocp = root .. "ī"
		decl.acp = root .. "ōs"
		decl.genp = root .. "ōrum"
		decl.datp = root .. "īs"
		decl.ablp = root .. "īs"
		if has_loc then decl.locp = root .. "īs" end
	end
	return decl
end

local function secondDecl_er(nominative, genitive, has_loc, sing)
	local decl = {}
	decl.title = "[[Viccionari:Segona declinació en llatí|2a declinació]] (e)r-, -ī"
	local root = genitive:gsub("ī$", ""):gsub("ōrum$", "")
	if not genitive:find("ōrum$") then
		decl.noms = nominative
		decl.vocs = nominative
		decl.acs = root .. "um"
		decl.gens = genitive
		decl.dats = root .. "ō"
		decl.abls = root .. "ō"
		if has_loc then decl.locs = root .. "ī" end
	end
	if not sing then
		decl.nomp = root .. "ī"
		decl.vocp = root .. "ī"
		decl.acp = root .. "ōs"
		decl.genp = root .. "ōrum"
		decl.datp = root .. "īs"
		decl.ablp = root .. "īs"
		if has_loc then decl.locp = root .. "īs" end
	end
	return decl
end

local function secondDecl_um(nominative, genitive, has_loc, sing)
	local decl = {}
	decl.title = "[[Viccionari:Segona declinació en llatí|2a declinació]] -um, -ī"
	local root = genitive:gsub("ī$", ""):gsub("ōrum$", "")
	--local ium = nominative:find("ium$")
	local root_i = genitive:gsub("iī$", "ī")
	if not genitive:find("ōrum$") then
		decl.noms = nominative
		decl.vocs = nominative
		decl.acs = nominative
		decl.gens = genitive
		--if ium then
		--	decl.gens2 = root_i
		--end
		decl.dats = root .. "ō"
		decl.abls = root .. "ō"
		if has_loc then decl.locs = root .. "ī" end
	end
	if not sing then
		decl.nomp = root .. "a"
		decl.vocp = root .. "a"
		decl.acp = root .. "a"
		decl.genp = root .. "ōrum"
		decl.datp = root .. "īs"
		decl.ablp = root .. "īs"
		if has_loc then decl.locp = root .. "īs" end
	end
	return decl
end

local function thirdDecl_mf(nominative, genitive, has_loc, sing, theme)
	local decl = {}
	decl.title = "[[Viccionari:Tercera declinació en llatí|3a declinació]] -, -is (tema " .. (theme == "i" and "i" or "cons.") .. ")"
	local root = genitive:gsub("[io]s$", ""):gsub("i?um$", "")
	if not genitive:find("um$") then
		decl.noms = nominative
		decl.vocs = nominative
		decl.acs = root .. "em"
		decl.gens = genitive
		decl.dats = root .. "ī"
		decl.abls = root .. "e"
		if has_loc then decl.locs = root .. "e" end
	end
	if not sing then
		decl.nomp = root .. "ēs"
		decl.vocp = root .. "ēs"
		decl.acp = root .. "ēs"
		decl.genp = root .. (theme == "i" and "i" or "") .. "um"
		decl.datp = root .. "ibus"
		decl.ablp = root .. "ibus"
		if has_loc then decl.locp = root .. "ibus" end
	end
	return decl
end

local function thirdDecl_n(nominative, genitive, has_loc, sing, theme)
	local decl = {}
	decl.title = "[[Viccionari:Tercera declinació en llatí|3a declinació]] -, -is (tema " .. (theme == "i" and "i" or "cons.") .. ")"
	local root = genitive:gsub("is$", ""):gsub("i?um$", "")
	local root_i = root .. (theme == "i" and "i" or "")
	if not genitive:find("um$") then
		decl.noms = nominative
		decl.vocs = nominative
		decl.acs = nominative
		decl.gens = genitive
		decl.dats = root .. "ī"
		decl.abls = root .. (theme == "i" and "ī" or "e")
		if has_loc then decl.locs = root .. "ī" end
	end
	if not sing then
		decl.nomp = root_i .. "a"
		decl.vocp = root_i .. "a"
		decl.acp = root_i .. "a"
		decl.genp = root_i .. "um"
		decl.datp = root .. "ibus"
		decl.ablp = root .. "ibus"
		if has_loc then decl.locp = root .. "ibus" end
	end
	return decl
end

local function fourthDecl(nominative, genitive, has_loc, sing)
	local decl = {}
	local neuter = nominative:find("ū$")
	decl.title = "[[Viccionari:Quarta declinació en llatí|4a declinació]] -" .. (neuter and "ū" or "us") .. ", -ūs"
	local root = genitive:gsub("ūs$", ""):gsub("uum$", "")
	if not nominative:find("ūs$") then
		decl.noms = nominative
		decl.vocs = nominative
		decl.acs = neuter and nominative or root .. "um"
		decl.gens = genitive
		decl.dats = neuter and nominative or root .. "uī"
		decl.abls = root .. "ū"
		if has_loc then decl.locs = root .. "ū" end
	end
	if not sing then
		decl.nomp = root .. (neuter and "ua" or "ūs")
		decl.vocp = root .. (neuter and "ua" or "ūs")
		decl.acp = root .. (neuter and "ua" or "ūs")
		decl.genp = root .. "uum"
		decl.datp = root .. "ibus"
		decl.ablp = root .. "ibus"
		if has_loc then decl.locp = root .. "ibus" end
	end
	return decl
end

local function fifthDecl(nominative, genitive, has_loc, sing)
	local decl = {}
	decl.title = "[[Viccionari:Cinquena declinació en llatí|5a declinació]] -es, -eī"
	local root = mw.ustring.gsub(genitive, "[eē]ī$", "")
	decl.noms = nominative
	decl.vocs = nominative
	decl.acs = root .. "em"
	decl.gens = genitive
	decl.dats = genitive
	decl.abls = root .. "ē"
	if has_loc then decl.locs = root .. "ē" end
	if not sing then
		decl.nomp = root .. "ēs"
		decl.vocp = root .. "ēs"
		decl.acp = root .. "ēs"
		if nominative == "diēs" or nominative == "rēs" then
			decl.genp = root .. "ērum"
			decl.datp = root .. "ēbus"
			decl.ablp = root .. "ēbus"
			if has_loc then decl.locp = root .. "ēbus" end
		end
	end
	return decl
end

local function makeDecl(nominative, genitive, gender, has_loc, sing)
	local decl = {}
	if (nominative:find("a$") and genitive:find("ae$"))
		or (nominative:find("ae$") and mw.ustring.find(genitive, "ārum$"))
		then
		decl = firstDecl(nominative, genitive, has_loc, sing)
	elseif (nominative:find("ē$") and genitive:find("ēs$"))
		or (nominative:find("[āē]s$") and genitive:find("ae$"))
		then
		decl = firstDeclGreek(nominative, genitive, has_loc, sing)
	elseif (nominative:find("er$") and genitive:find("ī$"))
		or (nominative:find("rī$") and genitive:find("rōrum$"))
		or (nominative:find("vir$") and genitive:find("virī$"))
		then
		decl = secondDecl_er(nominative, genitive, has_loc, sing)
	elseif (nominative:find("[uo]s$") and genitive:find("ī$") and gender ~= "n")
		or (nominative:find("ī$") and genitive:find("ōrum$"))
		then
		decl = secondDecl_us(nominative, genitive, has_loc, sing)
	elseif (nominative:find("us$") and genitive:find("ī$") and gender == "n")
		or ((nominative:find("um$") or nominative:find("on$")) and genitive:find("ī$"))
		or (nominative:find("a$") and genitive:find("ōrum$"))
		then
		decl = secondDecl_um(nominative, genitive, has_loc, sing)
	elseif genitive:find("[io]s$") or genitive:find("um$") then
		-- 3rd declension
		local theme_i = m_utilities.llista({"cos", "dos", "lis", "imber", "linter", "venter", "uter"})
		if gender ~= "n" then
			if genitive:find("ium$") then
				decl = thirdDecl_mf(nominative, genitive, has_loc, sing, "i")
			elseif nominative == "canis" or nominative == "juvenis" or genitive:find("[^i]um$") then
				decl = thirdDecl_mf(nominative, genitive, has_loc, sing, "c")
			elseif (nominative:find("[eēi]s$") and genitive == mw.ustring.gsub(nominative, "[eēi]s$", "is"))
				or (nominative:find("[sx]$") and genitive:find("[^aāeēiīoōuū][^aāeēiīoōuū]is$"))
				or theme_i[nominative]
				then
				decl = thirdDecl_mf(nominative, genitive, has_loc, sing, "i")
			else
				decl = thirdDecl_mf(nominative, genitive, has_loc, sing, "c")
			end
		else
			if nominative:find("e$") or nominative:find("a[lr]$") then
				decl = thirdDecl_n(nominative, genitive, has_loc, sing, "i")
			else
				decl = thirdDecl_n(nominative, genitive, has_loc, sing, "c")
			end
		end
	elseif mw.ustring.find(genitive, "ūs$") then
		decl = fourthDecl(nominative, genitive, has_loc, sing)
	elseif mw.ustring.find(genitive, "[eē]ī$") then
		decl = fifthDecl(nominative, genitive, has_loc, sing)
	--else unknown declension
	end
	
	return decl
end

local function makeLink(declension, case)
	local ret = '-'
	if notEmpty(declension[case]) then
		ret = m_link.full_link({term = declension[case], lang = lang}, nil, false)
		if notEmpty(declension[case .. "2"]) then
			ret = ret .. ", " .. m_link.full_link({term = declension[case .. "2"], lang = lang}, nil, false)
		end
	end
	return ret
end

function makeTable(declension)
	local ret = ""
	if declension.title then
		local has_loc = declension.locs or declension.locp
		ret = [=[ 
{| class="wikitable" style="margin-left: 0.5em; float: left; font-size: 90%;"
|+ style="font-weight: normal; white-space: nowrap;" | ]=] .. declension.title .. [=[ 
|-
| style="background:#EEEEEE; font-style:italic; text-align:center;" | Cas
| style="background:#EEEEEE; font-style:italic; text-align:center;" | Singular
| style="background:#EEEEEE; font-style:italic; text-align:center;" | Plural
|-
| style="background:#EEEEEE; font-style:italic;" | Nominatiu
| ]=] .. makeLink(declension, "noms") .. [=[ 
| ]=] .. makeLink(declension, "nomp") .. [=[ 
|-
| style="background:#EEEEEE; font-style:italic;" | Vocatiu
| ]=] .. makeLink(declension, "vocs") .. [=[ 
| ]=] .. makeLink(declension, "vocp") .. [=[ 
|-
| style="background:#EEEEEE; font-style:italic;" | Acusatiu
| ]=] .. makeLink(declension, "acs") .. [=[ 
| ]=] .. makeLink(declension, "acp") .. [=[ 
|-
| style="background:#EEEEEE; font-style:italic;" | Genitiu
| ]=] .. makeLink(declension, "gens") .. [=[ 
| ]=] .. makeLink(declension, "genp") .. [=[ 
|-
| style="background:#EEEEEE; font-style:italic;" | Datiu
| ]=] .. makeLink(declension, "dats") .. [=[ 
| ]=] .. makeLink(declension, "datp") .. [=[ 
|-
| style="background:#EEEEEE; font-style:italic;" | Ablatiu
| ]=] .. makeLink(declension, "abls") .. [=[ 
| ]=] .. makeLink(declension, "ablp") .. [=[ 
]=] .. (has_loc and '|-' .. [=[ 
| style="background:#EEEEEE; font-style:italic;" | Locatiu
| ]=] .. makeLink(declension, "locs") .. [=[ 
| ]=] .. makeLink(declension, "locp") or "") ..  [=[ 
|}
<br clear="all>]=]
	end
	return ret
end

function p.show(frame)
	local args = frame:getParent().args
	local pagename = notEmpty(args.pagename) or mw.title.getCurrentTitle().subpageText
	local nominative = notEmpty(args[1]) or pagename
	local genitive = notEmpty(args[2])
	local gender = notEmpty(args[3])
	if genitive == nil or gender == nil then
		return
	end
	local has_loc = args.loc
	local sing = frame.args.sing -- true by default in proper nouns
	if args.sing then
		sing = args.sing ~= "no" or false
	end
	
	-- Make declension
	local declension = makeDecl(nominative, genitive, gender, has_loc, sing)
	
	-- parameters provided
	local cases = {"nom", "voc", "ac", "gen", "dat", "abl", "loc"}
	for _, case in ipairs(cases) do
		declension[case .. "s"] = args[case .. "s"] or declension[case .. "s"]
		declension[case .. "s2"] = args[case .. "s2"] or declension[case .. "s2"]
		declension[case .. "p"] = args[case .. "p"] or declension[case .. "p"]
		declension[case .. "p2"] = args[case .. "p2"] or declension[case .. "p2"]
	end
	declension.title = args.decl or declension.title
	
	-- Redlinks?
	local categories = {}
	for case, form in pairs(declension) do
		if case ~= "title" and notEmpty(form) and not mw.title.new(m_link.sense_diacritics(lang.code, form)).exists then
			table.insert(categories, "Termes en llatí amb flexions a crear")
			break
		end
	end
	
	local ret = ""
	if args.bot then
		if declension.title then
			for key, form in pairs(declension) do
				if key ~= "title" then
					ret = ret .. "* " .. key .. "=" .. m_link.sense_diacritics(lang.code, form) .. "(" .. form .. ")\n"
				end
			end
		end
	else
		ret = makeTable(declension)
		if #categories > 0 then
			ret = ret .. m_utilities.format_categories(categories, lang)
		end
	end
	
	return ret
end

return p