Modul:en-headword

Dari Wikikamus bahasa Indonesia, kamus bebas

Dokumentasi untuk modul ini dapat dibuat di Modul:en-headword/doc

local export = {}
local pos_functions = {}

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

-- 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 require("Module:headword").full_headword(lang, nil, head, nil, nil, inflections, categories, nil)
end

-- This function does the common work between adjectives and adverbs
function make_comparatives(params, inflections, categories)
	local comp_parts = {label = "[[Appendix:Glossary#comparable|comparative]]", accel = "comparative-form-of"}
	local sup_parts = {label = "[[Appendix:Glossary#comparable|superlative]]", accel = "superlative-form-of"}
	
	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 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, stem .. "er")
			table.insert(sup_parts, stem .. "est")
		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
			
			table.insert(comp_parts, comp)
			table.insert(sup_parts, sup)
		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, {label = "not [[Appendix:Glossary#comparable|comparable]]"})
			table.insert(categories, "English uncomparable adjectives")
			return
		else
			table.insert(inflections, {label = "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, {label = "not [[Appendix:Glossary#comparable|comparable]]"})
			return
		else
			table.insert(inflections, {label = "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
		
		local qual = args["pl" .. (i == 1 and "" or i) .. "qual"]; if qual == "" then qual = nil end
		
		if qual then
			table.insert(plurals, {term = pl, qualifiers = {qual}})
		else
			table.insert(plurals, pl)
		end
		
		i = i + 1
	end
	
	-- Decide what to do next...
	local mode = nil
	
	if plurals[1] == "?" or plurals[1] == "!" or plurals[1] == "-" or plurals[1] == "~" then
		mode = plurals[1]
		table.remove(plurals, 1)  -- Remove the mode parameter
	end
	
	-- 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, {label = "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")
		
		-- If plural forms were given explicitly, then show "usually"
		if #plurals > 0 then
			table.insert(inflections, {label = "usually [[Appendix:Glossary#uncountable|uncountable]]"})
			table.insert(categories, "English countable nouns")
		else
			table.insert(inflections, {label = "[[Appendix:Glossary#uncountable|uncountable]]"})
		end
	-- Mixed countable/uncountable noun, always has a plural
	elseif mode == "~" then
		table.insert(inflections, {label = "[[Appendix:Glossary#countable|countable]] and [[Appendix:Glossary#uncountable|uncountable]]"})
		table.insert(categories, "English uncountable nouns")
		table.insert(categories, "English countable nouns")
		
		-- 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 = {label = "plural", accel = "plural-form-of"}
	
	local stem = PAGENAME
	
	for i, pl in ipairs(plurals) do
		if pl == "s" then
			table.insert(pl_parts, stem .. "s")
		elseif pl == "es" then
			table.insert(pl_parts, stem .. "es")
		else
			table.insert(pl_parts, pl)
		end
	end
	
	table.insert(inflections, pl_parts)
end

pos_functions["proper 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 = nil
	
	if plurals[1] == "?" or plurals[1] == "!" or plurals[1] == "-" or plurals[1] == "~" then
		mode = plurals[1]
		table.remove(plurals, 1)  -- Remove the mode parameter
	end
	
	-- 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, {label = "plural not attested"})
		table.insert(categories, "English nouns with unattested plurals")
		return
	-- Uncountable noun; may occasionally have a plural
	elseif mode == "-" then
		-- If plural forms were given explicitly, then show "usually"
		if #plurals > 0 then
			table.insert(inflections, {label = "usually [[Appendix:Glossary#uncountable|uncountable]]"})
			table.insert(categories, "English countable proper nouns")
		else
			table.insert(inflections, {label = "[[Appendix:Glossary#uncountable|uncountable]]"})
		end
	-- Mixed countable/uncountable noun, always has a plural
	elseif mode == "~" then
		table.insert(inflections, {label = "[[Appendix:Glossary#countable|countable]] and [[Appendix:Glossary#uncountable|uncountable]]"})
		table.insert(categories, "English countable proper nouns")
		
		-- If no plural was given, add a default one now
		if #plurals == 0 then
			plurals = {"s"}
		end
	elseif #plurals > 0 then
		table.insert(categories, "English countable proper nouns")
	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 = {label = "plural", accel = "plural-form-of"}
	
	local stem = PAGENAME
	
	for i, pl in ipairs(plurals) do
		if pl == "s" then
			table.insert(pl_parts, stem .. "s")
		elseif pl == "es" then
			table.insert(pl_parts, stem .. "es")
		else
			table.insert(pl_parts, pl)
		end
	end
	
	table.insert(inflections, pl_parts)
end

pos_functions["verbs"] = function(args, inflections, categories)
	-- Get parameters
	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 pres_3sg_forms = {label = "third-person singular simple present", accel = "third-person-singular-form-of"}
	local pres_ptc_forms = {label = "present participle", accel = "present-participle-form-of"}
	local past_forms = {label = "simple past", accel = "simple-past-form-of"}
	local pres_3sg_form = par1 or PAGENAME .. "s"
	local pres_ptc_form = par2 or PAGENAME .. "ing"
	local past_form = par3 or PAGENAME .. "ed"
	local pres_3sg_qual = args["pres_3sg_qual"]; if pres_3sg_qual == "" then pres_3sg_qual = nil end
	local pres_ptc_qual = args["pres_ptc_qual"]; if pres_ptc_qual == "" then pres_ptc_qual = nil end
	local past_qual = args["past_qual"]; if past_qual == "" then past_qual = nil end
	
	if par1 and not par2 and not par3 then
		-- This is the "new" format, which uses only the first parameter.
		if par1 == "es" then
			pres_3sg_form = PAGENAME .. "es"
			pres_ptc_form = PAGENAME .. "ing"
			past_form = PAGENAME .. "ed"
		elseif par1 == "ies" then
			if not mw.ustring.find(PAGENAME, "y$") then
				error("The first parameter is \"ies\" but the verb does not end in -y.")
			end
			
			local stem = mw.ustring.gsub(PAGENAME, "y$", "")
			pres_3sg_form = stem .. "ies"
			pres_ptc_form = stem .. "ying"
			past_form = stem .. "ied"
		elseif par1 == "d" then
			pres_3sg_form = PAGENAME .. "s"
			pres_ptc_form = PAGENAME .. "ing"
			past_form = PAGENAME .. "d"
		else
			pres_3sg_form = PAGENAME .. "s"
			pres_ptc_form = par1 .. "ing"
			past_form = par1 .. "ed"
		end
	else
		-- This is the "legacy" format, using the second and third parameters as well.
		-- It is included here for backwards compatibility and to ease the transition.
		if par3 then
			if par3 == "es" then
				require("Module:debug").track("en-headword/es3")
				pres_3sg_form = par1 .. par2 .. "es"
				pres_ptc_form = par1 .. par2 .. "ing"
				past_form = par1 .. par2 .. "ed"
			elseif par3 == "ing" then
				require("Module:debug").track("en-headword/ing3")
				pres_3sg_form = PAGENAME .. "s"
				pres_ptc_form = par1 .. par2 .. "ing"
				
				if par2 == "y" then
					past_form = PAGENAME .. "d"
				else
					past_form = par1 .. par2 .. "ed"
				end
			elseif par3 == "ed" then
				require("Module:debug").track("en-headword/ed3")
				
				if par2 == "i" then
					pres_3sg_form = par1 .. par2 .. "es"
					pres_ptc_form = PAGENAME .. "ing"
				else
					pres_3sg_form = PAGENAME .. "s"
					pres_ptc_form = par1 .. par2 .. "ing"
				end
				
				past_form = par1 .. par2 .. "ed"
			elseif par3 == "d" then
				require("Module:debug").track("en-headword/d3")
				pres_3sg_form = PAGENAME .. "s"
				pres_ptc_form = par1 .. par2 .. "ing"
				past_form = par1 .. par2 .. "d"
			end
		else
			if par2 == "es" then
				require("Module:debug").track("en-headword/es2")
				pres_3sg_form = par1 .. "es"
				pres_ptc_form = par1 .. "ing"
				past_form = par1 .. "ed"
			elseif par2 == "ies" then
				require("Module:debug").track("en-headword/ies2")
				
				if par1 .. "y" ~= PAGENAME then
					require("Module:debug").track("en-headword/ies2/par1 not pagename")
				end
				
				pres_3sg_form = par1 .. "ies"
				pres_ptc_form = par1 .. "ying"
				past_form = par1 .. "ied"
			elseif par2 == "ing" then
				require("Module:debug").track("en-headword/ing2")
				pres_3sg_form = PAGENAME .. "s"
				pres_ptc_form = par1 .. "ing"
				past_form = par1 .. "ed"
			elseif par2 == "ed" then
				require("Module:debug").track("en-headword/ed2")
				pres_3sg_form = PAGENAME .. "s"
				pres_ptc_form = par1 .. "ing"
				past_form = par1 .. "ed"
			elseif par2 == "d" then
				require("Module:debug").track("en-headword/d2")
				
				if par1 ~= PAGENAME then
					require("Module:debug").track("en-headword/d2/par1 not pagename")
				end
				
				pres_3sg_form = PAGENAME .. "s"
				pres_ptc_form = par1 .. "ing"
				past_form = par1 .. "d"
			end
		end
	end
	
	table.insert(pres_ptc_forms, {term = pres_ptc_form, qualifiers = {pres_ptc_qual}})
	table.insert(pres_3sg_forms, {term = pres_3sg_form, qualifiers = {pres_3sg_qual}})
	table.insert(past_forms, {term = past_form, qualifiers = {past_qual}})
	
	-- Present 3rd singular
	local i = 2
	
	while args["pres_3sg" .. i] do
		local form = args["pres_3sg" .. i]; if form == "" then form = nil end
		local qual = args["pres_3sg" .. i .. "_qual"]; if qual == "" then qual = nil end
		
		if form then
			table.insert(pres_3sg_forms, {term = form, qualifiers = {qual}})
		end
		
		i = i + 1
	end
	
	-- Present participle
	local i = 2
	
	while args["pres_ptc" .. i] do
		local form = args["pres_ptc" .. i]; if form == "" then form = nil end
		local qual = args["pres_ptc" .. i .. "_qual"]; if qual == "" then qual = nil end
		
		if form then
			table.insert(pres_ptc_forms, {term = form, qualifiers = {qual}})
		end
		
		i = i + 1
	end
	
	-- Past
	local i = 2
	
	while args["past" .. i] do
		local form = args["past" .. i]; if form == "" then form = nil end
		local qual = args["past" .. i .. "_qual"]; if qual == "" then qual = nil end
		
		if form then
			table.insert(past_forms, {term = form, qualifiers = {qual}})
		end
		
		i = i + 1
	end
	
	-- Past participle
	local past_ptc_forms = {label = "past participle", accel = "past-participle-form-of"}
	
	if par4 then
		local qual = args["past_ptc_qual"]; if qual == "" then qual = nil end
		table.insert(past_ptc_forms, {term = par4, qualifiers = {qual}})
		local i = 2
		
		while args["past_ptc" .. i] do
			local form = args["past_ptc" .. i]; if form == "" then form = nil end
			local qual = args["past_ptc" .. i .. "_qual"]; if qual == "" then qual = nil end
			
			if form then
				table.insert(past_ptc_forms, {term = form, qualifiers = {qual}})
			end
			
			i = i + 1
		end
	end
	
	-- Are the past forms identical to the past participle forms?
	local identical = true
	
	if #past_forms ~= #past_ptc_forms then
		identical = false
	else
		for key, val in ipairs(past_forms) do
			if past_ptc_forms[key].term ~= val.term or past_ptc_forms[key].qual ~= val.qual then
				identical = false
				break
			end
		end
	end
	
	-- Insert the forms
	table.insert(inflections, pres_3sg_forms)
	table.insert(inflections, pres_ptc_forms)
	
	if #past_ptc_forms == 0 or identical then
		past_forms.label = "simple past and past participle"
		past_forms.accel = "simple-past-and-participle-form-of"
		table.insert(inflections, past_forms)
	else
		table.insert(inflections, past_forms)
		table.insert(inflections, past_ptc_forms)
	end
end

return export