Jump to content

Module:En-headword

Frae Wikipedia, the free beuk o knawledge
Reveesion aes o 22:49, 25 November 2013 bi AmaryllisGardener (Collogue | contribs) (Created page with "local m_headword = require("Module:headword") local m_utilities = require("Module:utilities") local export = {} local pos_functions = {} -- The main entry point. -- This is ...")
(diff) ← Aulder reveision | see current reveision (diff) | Newer reveision → (diff)
local m_headword = require("Module:headword")
local m_utilities = require("Module:utilities")

local export = {}
local pos_functions = {}

-- 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 args = frame:getParent().args
	local poscat = frame.args[1] or error("Part of speech has not been specified. Please pass parameter 1 to the module invocation.")
	
	local head = args["head"]; if head == "" then head = nil end
	local inflections = {}
	local categories = {"English " .. poscat}
	
	if pos_functions[poscat] then
		pos_functions[poscat](args, inflections, categories)
	end
	
	return
		m_headword.format_headword(head, "en", "Latn") ..
		m_headword.format_inflections(inflections, "en", "Latn") ..
		m_utilities.format_categories(categories, "en")
end

-- This function does the common work between adjectives and adverbs
function make_comparatives(params, inflections, categories)
	local comp_parts = {"[[Appendix:Glossary#comparable|comparative]]"}
	local sup_parts = {"[[Appendix:Glossary#comparable|superlative]]"}
	
	if #params == 0 then
		table.insert(params, {"more"})
	end
	
	-- To form the stem, replace -(e)y with -i and remove a final -e.
	local stem = PAGENAME:gsub("([^aeiou])e?y$", "%1i"):gsub("e$", "")
	
	-- Go over each parameter given and create a comparative and superlative form
	for i, val in ipairs(params) do
		local comp = val[1]
		local sup = val[2]

		if i > 1 then
			table.insert(comp_parts, "or")
			table.insert(sup_parts, "or")
		end
		
		if comp == "more" and PAGENAME ~= "many" and PAGENAME ~= "much" then
			table.insert(comp_parts, "[[more]] " .. PAGENAME)
			table.insert(sup_parts, "[[most]] " .. PAGENAME)
		elseif comp == "further" and PAGENAME ~= "far" then
			table.insert(comp_parts, "[[further]] " .. PAGENAME)
			table.insert(sup_parts, "[[furthest]] " .. PAGENAME)
		elseif comp == "er" then
			table.insert(comp_parts, "<span class=\"form-of comparative-form-of lang-en\">[[" .. stem .. "er]]</span>")
			table.insert(sup_parts, "<span class=\"form-of superlative-form-of lang-en\">[[" .. stem .. "est]]</span>")
		else
			-- If the full comparative was given, but no superlative, then
			-- create it by replacing the ending -er with -est.
			if not sup then
				if comp:find("er$") then
					sup = comp:gsub("er$", "est")
				else
					error("The superlative of \"" .. comp .. "\" cannot be generated automatically. Please provide it with the \"sup" .. (i == 1 and "" or i) .. "=\" parameter.")
				end
			end
			
			-- We should only add [[ ]] here if they are not already present in the parameter.
			table.insert(comp_parts, "<span class=\"form-of comparative-form-of lang-en\">" .. (comp:find("[[", nil, true) and comp or "[[" .. comp .. "]]") .. "</span>")
			table.insert(sup_parts, "<span class=\"form-of superlative-form-of lang-en\">" .. (sup:find("[[", nil, true) and sup or "[[" .. sup .. "]]") .. "</span>")
		end
	end
	
	table.insert(inflections, comp_parts)
	table.insert(inflections, sup_parts)
end

pos_functions["adjectives"] = function(args, inflections, categories)
	local shift = 0
	
	-- If the first parameter is ?, then don't show anything, just return.
	if args[1] == "?" then
		return
	-- If the first parameter is -, then move all parameters up one position.
	elseif args[1] == "-" then
		shift = 1
	end
	
	-- Gather all the comparative and superlative parameters.
	local params = {}
	local i = 1
	
	while true do
		local comp = args[i + shift]; if comp == "" then comp = nil end
		local sup = args["sup" .. (i == 1 and "" or i)]; if sup == "" then sup = nil end
		
		if not comp then
			break
		end
		
		table.insert(params, {comp, sup})
		i = i + 1
	end
	
	if shift == 1 then
		-- If the first parameter is "-" but there are no parameters,
		-- then show "not comparable" only and return. If there are parameters,
		-- then show "not generally comparable" before the forms.
		if #params == 0 then
			table.insert(inflections, {"not [[Appendix:Glossary#comparable|comparable]]"})
			return
		else
			table.insert(inflections, {"not generally [[Appendix:Glossary#comparable|comparable]]"})
		end
	end
	
	-- Process the parameters
	make_comparatives(params, inflections, categories)
end

pos_functions["adverbs"] = function(args, inflections, categories)
	local shift = 0
	
	-- If the first parameter is ?, then don't show anything, just return.
	if args[1] == "?" then
		return
	-- If the first parameter is -, then move all parameters up one position.
	elseif args[1] == "-" then
		shift = 1
	end
	
	-- Gather all the comparative and superlative parameters.
	local params = {}
	local i = 1
	
	while true do
		local comp = args[i + shift]; if comp == "" then comp = nil end
		local sup = args["sup" .. (i == 1 and "" or i)]; if sup == "" then sup = nil end
		
		if not comp then
			break
		end
		
		table.insert(params, {comp, sup})
		i = i + 1
	end
	
	if shift == 1 then
		-- If the first parameter is "-" but there are no parameters,
		-- then show "not comparable" only and return. If there are parameters,
		-- then show "not generally comparable" before the forms.
		if #params == 0 then
			table.insert(inflections, {"not [[Appendix:Glossary#comparable|comparable]]"})
			return
		else
			table.insert(inflections, {"not generally [[Appendix:Glossary#comparable|comparable]]"})
		end
	end
	
	-- Process the parameters
	make_comparatives(params, inflections, categories)
end

pos_functions["nouns"] = function(args, inflections, categories)
	-- Gather all the plural parameters from the numbered parameters.
	local plurals = {}
	local i = 1
	
	while true do
		local pl = args[i]; if pl == "" then pl = nil end
		
		if not pl then
			break
		end
		
		table.insert(plurals, pl)
		i = i + 1
	end
	
	-- Decide what to do next...
	local mode = plurals[1]
	
	-- Plural is unknown
	if mode == "?" then
		table.insert(categories, "English nouns with unknown or uncertain plurals")
		return
	-- Plural is not attested
	elseif mode == "!" then
		table.insert(inflections, {"plural not attested"})
		table.insert(categories, "English nouns with unattested plurals")
		return
	-- Uncountable noun; may occasionally have a plural
	elseif mode == "-" then
		table.insert(categories, "English uncountable nouns")
		table.remove(plurals, 1)  -- Remove the mode parameter
		
		-- If plural forms were given explicitly, then show "usually"
		if #plurals > 0 then
			table.insert(inflections, {"usually [[Appendix:Glossary#uncountable|uncountable]]"})
			table.insert(categories, "English countable nouns")
		else
			table.insert(inflections, {"[[Appendix:Glossary#uncountable|uncountable]]"})
		end
	-- Mixed countable/uncountable noun, always has a plural
	elseif mode == "~" then
		table.insert(inflections, {"[[Appendix:Glossary#countable|countable]] and [[Appendix:Glossary#uncountable|uncountable]]"})
		table.insert(categories, "English uncountable nouns")
		table.insert(categories, "English countable nouns")
		table.remove(plurals, 1)  -- Remove the mode parameter
		
		-- If no plural was given, add a default one now
		if #plurals == 0 then
			plurals = {"s"}
		end
	-- The default, always has a plural
	else
		table.insert(categories, "English countable nouns")
		
		-- If no plural was given, add a default one now
		if #plurals == 0 then
			plurals = {"s"}
		end
	end
	
	-- If there are no plurals to show, return now
	if #plurals == 0 then
		return
	end
	
	-- There are plural forms to show, so show them
	local pl_parts = {"plural"}
	
	local stem = PAGENAME
	
	for i, pl in ipairs(plurals) do
		if i > 1 then
			table.insert(pl_parts, "or")
		end
		
		if pl == "s" then
			table.insert(pl_parts, "<span class=\"form-of plural-form-of lang-en\">[[" .. stem .. "s]]</span>")
		elseif pl == "es" then
			table.insert(pl_parts, "<span class=\"form-of plural-form-of lang-en\">[[" .. stem .. "es]]</span>")
		else
			table.insert(pl_parts, "<span class=\"form-of plural-form-of lang-en\">[[" .. pl .. "]]</span>")
			
			-- Categorise as a plural if it is one
			if pl == PAGENAME then
				table.insert(categories, "English plurals")
			end
		end
	end
	
	table.insert(inflections, pl_parts)
end

return export