Jump to content

Module:Ca-headword

Frae Wikipedia, the free beuk o knawledge
local m_com = require("Module:ca-common")

local export = {}
local pos_functions = {}

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

-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
	PAGENAME = mw.title.getCurrentTitle().text
	
	local poscat = frame.args[1] or error("Part of speech has not been specified. Please pass parameter 1 to the module invocation.")
	
	local params = {
		["head"] = {list = true, default = ""},
		["suff"] = {type = "boolean"},
	}
	
	if pos_functions[poscat] then
		for key, val in pairs(pos_functions[poscat].params) do
			params[key] = val
		end
	end
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	local data = {lang = lang, pos_category = poscat, categories = {}, heads = args["head"], genders = {}, inflections = {}, categories = {}}
	
	if args["suff"] then
		data.pos_category = "suffixes"
		
		if poscat == "adjectives" then
			table.insert(data.categories, lang:getCanonicalName() .. " adjective-forming suffixes")
		elseif poscat == "adverbs" then
			table.insert(data.categories, lang:getCanonicalName() .. " adverb-forming suffixes")
		elseif poscat == "nouns" then
			table.insert(data.categories, lang:getCanonicalName() .. " noun-forming suffixes")
		elseif poscat == "verbs" then
			table.insert(data.categories, lang:getCanonicalName() .. " verb-forming suffixes")
		else
			error("No category exists for suffixes forming " .. poscat .. ".")
		end
	end
	
	if pos_functions[poscat] then
		pos_functions[poscat].func(args, data)
	end
	
	return require("Module:headword").full_headword(data)
end

-- Display additional inflection information for an adjective
pos_functions["adjectives"] = {
	params = {
		[1] = {},
		["pl"] = {},
		["pl2"] = {},
		},
	func = function(args, data)
		local feminine = args[1]
		local masculine_plural
		local masculine_plural2
		local feminine_plural
		
		-- Invariable adjectives
		if feminine == "inv" then
			table.insert(data.categories, "Catalan invariable adjectives")
			table.insert(data.inflections, {label = "invariable"})
		else
			-- Adjectives with identical forms in the masculine and feminine singular
			if feminine == "mf" then
				feminine = PAGENAME
				table.insert(data.categories, "Catalan epicene adjectives")
				
				-- Adjectives ending in -ç or -x behave as mf-type in the singular, but
				-- regular type in the plural.
				if PAGENAME:find("[çx]$") then
					masculine_plural = make_plural(PAGENAME, "m")
					feminine_plural = make_plural(PAGENAME .. "a", "f")
				else
					masculine_plural = make_plural(PAGENAME, "m")
					feminine_plural = masculine_plural
				end
				
				-- Has anyone provided forms to override the defaults?
				masculine_plural = args["pl"] or masculine_plural
				feminine_plural = args["pl"] or feminine_plural
				masculine_plural2 = args["pl2"] or masculine_plural2
			-- Adjectives with distinct masculine and feminine singular forms
			-- (the majority)
			else
				feminine = feminine or make_feminine(PAGENAME)
				feminine_plural = make_plural(feminine, "f")
				
				-- If the feminine ends in -ssa, assume that the -ss- is also in the
				-- masculine plural form
				if feminine:find("ssa$") then
					masculine_plural = feminine:gsub("a$", "os")
				elseif feminine == PAGENAME .. "na" then
					masculine_plural = PAGENAME .. "ns"
				-- Adjectives in -ig have two masculine plural forms, one derived from
				-- the m.sg. and the other derived from the f.sg.
				elseif PAGENAME:find("ig$") then
					masculine_plural = PAGENAME .. "s"
					masculine_plural2 = feminine:gsub("ja$", "jos")
				else
					masculine_plural, masculine_plural2 = make_plural(PAGENAME, "m")
				end
				
				-- Has anyone provided forms to override the defaults?
				masculine_plural = args["pl"] or masculine_plural
				masculine_plural2 = args["pl2"] or masculine_plural2
			end
			
			-- Display the forms.
			-- If masculine and feminine are identical, show m,f as the gender and
			-- leave out the feminine.
			if feminine ~= PAGENAME then
				table.insert(data.inflections, {label = "feminine", accel = "feminine-singular-form-of", feminine})
			end
			
			-- If the plurals are identical, show only one form as well.
			if masculine_plural == feminine_plural then
				local infl_parts = {label = "masculine and feminine plural", accel = "plural-form-of"}
				table.insert(infl_parts, masculine_plural)
				
				if masculine_plural2 then
					table.insert(infl_parts, masculine_plural2)
				end
				
				table.insert(data.inflections, infl_parts)
			else
				local infl_parts = {label = "masculine plural", accel = "masculine-plural-form-of"}
				table.insert(infl_parts, masculine_plural)
				
				if masculine_plural2 then
					table.insert(infl_parts, masculine_plural2)
				end
				
				table.insert(data.inflections, infl_parts)
				table.insert(data.inflections, {label = "feminine plural", accel = "feminine-plural-form-of", feminine_plural})
			end
			
			-- Check for missing forms
			if feminine and not mw.title.new(feminine).exists then
				table.insert(data.categories, "Catalan adjectives with missing forms")
			elseif masculine_plural and not mw.title.new(masculine_plural).exists then
				table.insert(data.categories, "Catalan adjectives with missing forms")
			elseif masculine_plural2 and not mw.title.new(masculine_plural2).exists then
				table.insert(data.categories, "Catalan adjectives with missing forms")
			elseif feminine_plural and not mw.title.new(feminine_plural).exists then
				table.insert(data.categories, "Catalan adjectives with missing forms")
			end
		end
	end
}

pos_functions["determiners"] = pos_functions["adjectives"]
pos_functions["pronouns"] = pos_functions["adjectives"]

-- Display information for a noun's gender
-- This is separate so that it can also be used for proper nouns
function noun_gender(args, data)
	local gender = args[1]
	
	if gender == "m-p" or gender == "f-p" then
		table.insert(data.categories, "Catalan pluralia tantum")
	end
	
	if gender == "mf" then
		table.insert(data.genders, "m")
		table.insert(data.genders, "f")
	else
		table.insert(data.genders, gender)
	end
	
	if #data.genders == 0 then
		table.insert(data.genders, "?")
	end
	
	-- Is this a noun with an unexpected ending (for its gender)?
	-- Only check if the term is one word (there are no spaces in the term).
	if not PAGENAME:find(" ") then
		if PAGENAME:find("a$") and (data.genders[1] == "m") then
			table.insert(data.categories, "Catalan masculine nouns ending in -a")
		elseif not (PAGENAME:find("a$") or PAGENAME:find("ió$") or PAGENAME:find("tat$") or PAGENAME:find("tud$") or PAGENAME:find("[dt]riu$")) and data.genders[1] == "f" then
			table.insert(data.categories, "Catalan feminine nouns with no feminine ending")
		end
	end
end

pos_functions["proper nouns"] = {
	params = {
		[1] = {},
		},
	func = function(args, data)
		noun_gender(args, data)
	end
}

-- Display additional inflection information for a noun
pos_functions["nouns"] = {
	params = {
		[1] = {},
		[2] = {},
		["pl2"] = {},
		["f"] = {},
		["m"] = {},
		},
	func = function(args, data)
		noun_gender(args, data)
		
		-- Plural
		if data.genders[1] == "m-p" or data.genders[1] == "f-p" then
			table.insert(data.inflections, {label = "[[Appendix:Glossary#plurale tantum|plurale tantum]]"})
		else
			local plural = args[2]
			
			if plural == "-" then
				table.insert(data.inflections, {label = "[[Appendix:Glossary#uncountable|uncountable]]"})
				table.insert(data.categories, "Catalan uncountable nouns")
			else
				local infl_parts = {label = "plural", accel = "plural-form-of"}
				local plural2 = args["pl2"]
				
				if not plural then
					local p, p2 = make_plural(PAGENAME, data.genders[1])
					plural = p
					plural2 = plural2 or p2
				end
				
				table.insert(infl_parts, plural)
				
				if plural2 then
					table.insert(infl_parts, plural2)
					if not mw.title.new(plural2).exists then
						table.insert(data.categories, "Catalan nouns with missing plurals")
					end
				end
				if plural and not mw.title.new(plural).exists then
					table.insert(data.categories, "Catalan nouns with missing plurals")
				end
				table.insert(data.inflections, infl_parts)
			end
		end
		
		-- Gendered forms
		local feminine = args["f"]
		local masculine = args["m"]
		
		if feminine then
			table.insert(data.inflections, {label = "feminine", feminine})
		end
		
		if masculine then
			table.insert(data.inflections, {label = "masculine", masculine})
		end
	end
}

-- Display additional inflection information for a verb
pos_functions["verbs"] = {
	params = {
		["pres_1_sg"] = {},
		["past_part"] = {},
		},
	func = function(args, data)
		-- Does this verb end in a recognised verb ending (possibly reflexive)?
		if not PAGENAME:find(" ") and (PAGENAME:find("re?$") or PAGENAME:find("r%-se$") or PAGENAME:find("re's$")) then
			local base = PAGENAME:gsub("r%-se$", "r"):gsub("re's$", "re")
			local pres_1_sg
			local past_part
			
			-- Generate inflected forms.
			-- The 2nd conjugation is generally irregular
			-- so generate nothing for that, explicit parameters are required.
			
			-- 1st conjugation
			if base:find("ar$") then
				local stem = base:gsub("ar$", "")
				pres_1_sg = stem .. "o"
				past_part = stem .. "at"
			-- 3rd conjugation (except -tenir/-venir)
			elseif base:find("ir$") and not base:find("[tv]enir$") then
				local stem = base:gsub("ir$", "")
				pres_1_sg = stem .. "eixo"
				
				if stem:find("[aeiou]$") and not stem:find("[gq]u$") then
					past_part = stem .. "ït"
				else
					past_part = stem .. "it"
				end
			end
			
			-- Overridden forms
			pres_1_sg = {label = "first-person singular present", request = true, args["pres_1_sg"] or pres_1_sg}
			past_part = {label = "past participle", request = true, args["past_part"] or past_part}
			
			table.insert(data.inflections, pres_1_sg)
			table.insert(data.inflections, past_part)
		end
	end
}

-- Display additional inflection information for a numeral
pos_functions["numerals"] = {
	params = {
		[1] = {},
		[2] = {},
		},
	func = function(args, data)
		local feminine = args[1]
		local noun_form = args[2]
		
		if feminine then
			table.insert(data.genders, "m")
			table.insert(data.inflections, {label = "feminine", feminine})
			
			if noun_form then
				table.insert(data.inflections, {label = "noun form", noun_form})
			end
		else
			table.insert(data.genders, "m")
			table.insert(data.genders, "f")
		end
	end
}

function make_feminine(base)
	-- final vowel -> -a
	if base:find("a$") then return base end
	if base:find("o$") then return (base:gsub("o$", "a")) end	
	if base:find("e$") then return m_com.front_to_back(base:gsub("e$", "")) .. "a" end
	
	-- -u -> -va
	if base:find("[aeiou]u$") then return (base:gsub("u$", "v") .. "a") end
	
	-- accented vowel -> -na
	if mw.ustring.find(base, "[àèéíòóú]$") then return m_com.remove_accents(base) .. "na" end
	
	-- accented vowel + -s -> -sa
	if mw.ustring.find(base, "[àèéíòóú]s$") then return m_com.remove_accents(base) .. "a" end
	
	return base .. "a"
end

function make_plural(base, gender)
	-- a -> es
	if base:find("a$") then return m_com.back_to_front(base:gsub("a$", "")) .. "es", nil end
	
	-- accented vowel -> -ns
	if mw.ustring.find(base, "[àèéíòóú]$") then
		return m_com.remove_accents(base) .. "ns", nil
	end
	
	if gender == "m" then
		if mw.ustring.find(base, "[àèéíòóú]s$") then
			return m_com.remove_accents(base) .. "os", nil
		end
		
		if base:find("s$") or base:find("ç$") or base:find("x$") or base:find("z$") then
			return base .. "os", nil
		end
		
		if base:find("sc$") or base:find("st$") or base:find("xt$") then
			return base .. "s", base .. "os"
		end
	end
	
	if gender == "f" then
		if base:find("s$") then return base end
		if base:find("sc$") or base:find("st$") or base:find("xt$") then return base .. "s", base .. "es" end
	end
	
	return base .. "s", nil
end

return export