Mòdul:Ca-flexió

De Viquipèdia
Salta a la navegació Salta a la cerca
Icona de documentació de mòdul Documentació del mòdul [ mostra ] [ modifica el codi ] [ mostra l'historial ] [ refresca ]

Mòdul Ca-flexió (codi · ús · discussió · tests · casos prova | subpàgines · enllaços)

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


local p = {}

-- Forma femenina d'un mot
local function forma_fem(mot, exc_fem)
	local exc = exc_fem[mot]
	if exc then return exc end
	
	local fem = mot
	fem = mw.ustring.gsub(fem, "à$", "ana")
	if string.find(mot, "e$") and not string.find(mot, "ble$") then
		fem = string.gsub(fem, "gue$", "ga")
		fem = string.gsub(fem, "e$", "a")
	end
	fem = mw.ustring.gsub(fem, "[èé]$", "ena")
	fem = mw.ustring.gsub(fem, "([^gq][aeou])í$", "%1ïna")
	fem = mw.ustring.gsub(fem, "í$", "ina")
	fem = mw.ustring.gsub(fem, "a(%l)i$", "à%1ia")
	fem = mw.ustring.gsub(fem, "i(%l)i$", "í%1ia")
	fem = string.gsub(fem, "o$", "a")
	fem = mw.ustring.gsub(fem, "[òó]$", "ona")
	fem = string.gsub(fem, "([aio])u$", "%1va")
	fem = string.gsub(fem, "eu$", "ea")
	fem = mw.ustring.gsub(fem, "ú$", "una")
	fem = string.gsub(fem, "leg$", "loga")
	fem = string.gsub(fem, "oig$", "oja")
	fem = string.gsub(fem, "ogen$", "ògena")
	fem = mw.ustring.gsub(fem, "[aà]s$", "assa")
	fem = mw.ustring.gsub(fem, "ès$", "esa")
	fem = mw.ustring.gsub(fem, "és$", "essa")
	fem = mw.ustring.gsub(fem, "ís$", "issa")
	fem = mw.ustring.gsub(fem, "[oò]s$", "ossa")
	fem = mw.ustring.gsub(fem, "ós$", "osa")
	fem = mw.ustring.gsub(fem, "ús$", "usa")
	fem = mw.ustring.gsub(fem, "([aiïu])t$", "%1da")
    if fem == mot then
		local no_fem = false
		if string.find(mot, "a$") then
			no_fem = true
		elseif string.find(mot, "ble$") then
			no_fem = true
		elseif string.find(mot, "ei$") then
			no_fem = true
			if mot == "rei" or mot == "virrei" then
				fem = mot .. "na"
			end
		elseif string.find(mot, "[cinsv][ai]l$")  and mot ~= "col·legial" and mot ~= "oficial" then
			no_fem = true
		elseif string.find(mot, "del$") then
			no_fem = true
		elseif string.find(mot, "mim$") then
			no_fem = true
		elseif string.find(mot, "ar$") then
			no_fem = true
		elseif string.find(mot, "es$") then
			no_fem = true
		elseif string.find(mot, "%a[ceptx]ant$") and mot ~= "bastant" and mot ~= "marxant" then
			no_fem = true
		elseif string.find(mot, "gent$") and mot ~= "sergent" then
			no_fem = true
		elseif string.find(mot, "ilot$") then
			no_fem = true
		end
		if no_fem == false then
			fem = mot .. "a"
		end
    end
    return fem
end

-- Forma femenina d'una locució
function p.fem(lema)
	if type(lema) == "table" then lema = lema.args[1] end -- des de plantilles via invoke o des de mòduls via require
	local exc_fem = mw.loadData("Mòdul:Ca-flexió/femení")
	if lema:find("[^ ]+ [^ ]+") then
		local exc = exc_fem[lema]
		if exc then return exc end
		local preposicions = {" a ", " al ", " amb ", " contra ", " de ", " d'", " del ", " dels ", " des ", " dintre ",
				" en ", " endavant", " enrere", " entre ",
				" fora ", " per ", " pel ", " pels ", " que ", " sense ", " sobre ", " sota "}
		for i, prep in ipairs(preposicions) do
			offset = mw.ustring.find(lema, prep, 1, true)
			if offset then
				local part1 = mw.ustring.sub(lema, 1, offset-1)
				local part2 = mw.ustring.sub(lema, offset)
				return p.fem(part1) .. part2 -- expressament recursiu per a locucions complexes
			end
		end
		local femenins = {}
		for part in mw.text.gsplit(lema, " ", true) do
			if string.find(part, "al$") and #femenins > 0 then -- adjectius en -al invariables
				table.insert(femenins, part)
			else
				table.insert(femenins, forma_fem(part, exc_fem))
			end
		end
		return table.concat(femenins, " ")
	else
		return forma_fem(lema, exc_fem)
	end
end

-- Plural d'un mot
local function forma_plural(mot, exc_plural)
	local exc = exc_plural[mot]
	if exc then return exc end
	
	local plural = mot
	-- a -> es, amb canvis morfològics
	if string.find(mot, "a$") then
		plural = string.gsub(plural, "ca$", "ques")
		plural = string.gsub(plural, "ga$", "gues")
		plural = mw.ustring.gsub(plural, "ça$", "ces")
		plural = string.gsub(plural, "ja$", "ges")
		plural = mw.ustring.gsub(plural, "([gq])ua$", "%1ües")
		if plural == mot then plural = string.gsub(mot, "a$", "es") end
	-- vocal accentuada (+n) -> sense accent + ns
	elseif mw.ustring.find(mot, "[àéèíóòú]n?$") then
		plural = mw.ustring.gsub(plural, "à$", "ans")
		plural = mw.ustring.gsub(plural, "[èé]n?$", "ens")
		plural = mw.ustring.gsub(plural, "([^g][aeou])í$", "%1ïns")
		plural = mw.ustring.gsub(plural, "ín?$", "ins")
		plural = mw.ustring.gsub(plural, "[òó]n?$", "ons")
		plural = mw.ustring.gsub(plural, "ú$", "uns")
		if plural == mot then plural = mot .. "s" end
	-- s -> s(s)os
	elseif string.find(mot, "s$") then
		plural = mw.ustring.gsub(plural, "[oò]s$", "ossos")
		plural = mw.ustring.gsub(plural, "[aà]s$", "assos")
		plural = mw.ustring.gsub(plural, "és$", "essos")
		plural = mw.ustring.gsub(plural, "ès$", "esos")
		plural = mw.ustring.gsub(plural, "oís$", "oïssos")
		plural = mw.ustring.gsub(plural, "([^gq][au])ís$", "%1ïsos")
		plural = mw.ustring.gsub(plural, "ís$", "issos")
		plural = mw.ustring.gsub(plural, "ós$", "osos")
		plural = mw.ustring.gsub(plural, "ús$", "usos")
		if plural == mot and not string.find(mot, "es$") then plural = mot .. "os" end
	else
		plural = mw.ustring.gsub(plural, "([çxz])$", "%1os")
		if plural == mot then
			plural = mot .. "s"
		end
	end
	return plural
end

-- Forma plural d'una locució
function p.plural(lema)
	if type(lema) == "table" then lema = lema.args[1] end -- des de plantilles via invoke o des de mòduls via require
	local exc_plural = mw.loadData("Mòdul:Ca-flexió/plural")
	if lema:find("[^ ]+ [^ ]+") then
		local exc = exc_plural[lema]
		if exc then return exc end
		local preposicions = {" a ", " al ", " amb ", " contra ", " de ", " d'", " del ", " dels ", " des ", " dintre ",
				" en ", " endavant", " enrere", " entre ",
				" fora ", " per ", " pel ", " pels ", " sense ", " sobre ", " sota "}
		for i, prep in ipairs(preposicions) do
			offset = mw.ustring.find(lema, prep, 1, true)
			if offset then
				local part1 = mw.ustring.sub(lema, 1, offset-1)
				local part2 = mw.ustring.sub(lema, offset)
				return p.plural(part1) .. part2
			end
		end
		local plurals = {}
		for part in mw.text.gsplit(lema, " ", true) do
			table.insert(plurals, forma_plural(part, exc_plural))
		end
		return table.concat(plurals, " ")
	else
		return forma_plural(lema, exc_plural)
	end
end

-- Funció auxiliar per l’apostrofació de l’article masculí, retorna true o false
-- Limitacions: vegeu plantilla:deod'
local function sapostrofa(text)
	local apostrofa = {
		["her "]=false, ["his "]=false, -- paraules amb angles que provoquen falsos apostrofs 
		["hakk"]=false, -- h consonant (hakka)
    	["hawa"]=false, -- h consonant (hawaià)
    	["hia"]=false, ["hie"]=false, ["hio"]=false, ["hui"]=false, -- vocal consonant
    	["uix"]=true, -- excepció per u vocal
    	["ha"]=true, ["he"]=true, ["hi"]=true, ["hí"]=true, ["ho"]=true, ["hu"]=true, ["hy"]=true, -- excepte anteriors
    	["ia"]=false, ["ià"]=false, ["ie"]=false, ["io"]=false, ["iu"]=false, -- i consonant
    	["ua"]=false, ["ue"]=false, ["ui"]=false, ["uí"]=false, ["uï"]=false, ["uo"]=false, -- u consonant
    	["ya"]=false, ["ye"]=false, ["yi"]=false, ["yo"]=false, ["yu"]=false, -- y consonant
    	["a"]=true, ["à"]=true, ["e"]=true, ["è"]=true, ["é"]=true,
    	["i"]=true, ["í"]=true, ["ï"]=true, ["y"]=true,
    	["o"]=true, ["ò"]=true, ["ó"]=true, ["u"]=true, ["ú"]=true, ["ü"]=true, -- excepte anteriors
    	["1 "]=true, ["11 "]=true -- dates amb "1 de" o "11 de "
    	}

    local elText = mw.ustring.lower(require('Mòdul:Delink')._delink({text}))
    for i = 4, 1, -1 do
        local lletres = mw.ustring.sub(elText, 1, i)
        local apo = apostrofa[lletres]
        if apo ~= nil then
            return apo
        end
    end
    return false
end

function p.apostrofar(frame)
	args = frame:getParent().args
	local text = args[2] or ""
	local ret = (args[1] or "") .. " " .. text
	if sapostrofa(text) then
		local article = mw.language.getContentLanguage():lcfirst(args[1])
		if article == "el" then
			ret = "l'" .. text
		elseif article == "al" then
			ret = "a l'" .. text
		elseif article == "del" then
			ret = "de l'" .. text
		elseif article == "pel" then
			ret = "per l'" .. text
		elseif article == "cal" then
			ret = "ca l'" .. text
		elseif article == "can" then
			ret = "ca n'" .. text
		end
		if article ~= args[1] then
			ret = mw.language.getContentLanguage():ucfirst(ret)
		end
	end
	return ret
end

function p.ordinal(number, feminine)
	local suffix = {["1"] = 'r', ["2"] = 'n', ["3"] = 'r', ["4"] = 't'}
	if feminine then
		return number .. 'a'
	elseif suffix[number] then
		return number .. suffix[number]
	else
		return number .. 'è'
	end
end

return p