Module:eo-headword

Definition from Wiktionary, the free dictionary
Jump to: navigation, search
The following documentation is located at Module:eo-headword/documentation. [edit]
See also: subpages of this module.

This module implements Esperanto headword-line templates.


local export = {}
local pos_functions = {}
 
local lang = require("Module:languages").getByCode("eo")
 
-- The main entry point
function export.show(frame)
	PAGENAME = mw.title.getCurrentTitle().text
	local args = frame:getParent().args
	local head = args["head"]; if head == "" then head = nil end
 
	local poscat = frame.args["poscat"] or getPOS(PAGENAME)
 
	if not poscat then
		if mw.title.getCurrentTitle().nsText == "Template" then
			poscat = "nouns"
		else
			error("Part of speech of \"" .. PAGENAME .. "\" cannot be automatically determined.")
		end
	end
 
	local inflections = {}
	local categories = {lang:getCanonicalName() .. " " .. 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
 
pos_functions["adjectives"] = function(args, inflections, categories)
	table.insert(inflections, {label = "plural", accel = "plural-form-of", PAGENAME .. "j"})
	table.insert(inflections, {label = "accusative singular", accel = "accusative-form-of", PAGENAME .. "n"})
	table.insert(inflections, {label = "accusative plural", accel = "accusative-plural-form-of", PAGENAME .. "jn"})
end
 
pos_functions["nouns"] = function(args, inflections, categories)
	-- Get the parameters
	local inflected_words_specified = false
	local inflected_words = {}
	local i = 1
 
	while args[i] do
		local word = args[i]; if word == "" or word == "+" or word == "-" then word = nil end
 
		if word then
			inflected_words[word] = true
			inflected_words_specified = true
		end
 
		i = i + 1
	end
 
	local pl = {}
	local acc = {}
	local acc_pl = {}
 
	-- Split multi-word terms
	for word in mw.text.gsplit(PAGENAME, " ", true) do
		local pos = getPOS(word)
 
		-- Inflect each word separately
		if (not inflected_words_specified or inflected_words[word]) and (pos == "adjectives" or pos == "nouns") then
			table.insert(acc, word .. "n")
			table.insert(pl, word .. "j")
			table.insert(acc_pl, word .. "jn")
		else
			table.insert(acc, word)
			table.insert(pl, word)
			table.insert(acc_pl, word)
		end
	end
 
	-- Merge back together
	acc = table.concat(acc, " ")
	pl = table.concat(pl, " ")
	acc_pl = table.concat(acc_pl, " ")
 
	if args[1] == "-" then
		table.insert(inflections, {label = "uncountable"})
		table.insert(inflections, {label = "accusative", accel = "uncountable-accusative-form-of", acc})
		table.insert(categories, lang:getCanonicalName() .. " uncountable nouns")
	else
		table.insert(inflections, {label = "accusative singular", accel = "accusative-form-of", acc})
		table.insert(inflections, {label = "plural", accel = "plural-form-of", pl})
		table.insert(inflections, {label = "accusative plural", accel = "accusative-plural-form-of", acc_pl})
	end
end
 
pos_functions["proper nouns"] = function(args, inflections, categories)
	-- Get the parameters
	local inflected_words_specified = false
	local inflected_words = {}
	local i = 1
 
	while args[i] do
		local word = args[i]; if word == "" or word == "+" or word == "-" then word = nil end
 
		if word then
			inflected_words[word] = true
			inflected_words_specified = true
		end
 
		i = i + 1
	end
 
	local acc = {}
	local pl = {}
	local acc_pl = {}
 
	-- Split multi-word terms
	for word in mw.text.gsplit(PAGENAME, " ", true) do
		local pos = getPOS(word)
 
		-- Inflect each word separately
		if (not inflected_words_specified or inflected_words[word]) and (pos == "adjectives" or pos == "nouns") then
			table.insert(acc, word .. "n")
			table.insert(pl, word .. "j")
			table.insert(acc_pl, word .. "jn")
		else
			table.insert(acc, word)
			table.insert(pl, word)
			table.insert(acc_pl, word)
		end
	end
 
	-- Merge back together
	acc = table.concat(acc, " ")
	pl = table.concat(pl, " ")
	acc_pl = table.concat(acc_pl, " ")
 
	if args[1] == "+" then
		table.insert(inflections, {label = "accusative singular", accel = "accusative-form-of", acc})
		table.insert(inflections, {label = "plural", accel = "plural-form-of", pl})
		table.insert(inflections, {label = "accusative plural", accel = "accusative-plural-form-of", acc_pl})
	else
		table.insert(inflections, {label = "accusative", accel = "uncountable-accusative-form-of", acc})
	end
end
 
pos_functions["verbs"] = function(args, inflections, categories)
	local stem = PAGENAME:sub(1, -2)
 
	table.insert(inflections, {label = "present", stem .. "as"})
	table.insert(inflections, {label = "past", stem .. "is"})
	table.insert(inflections, {label = "future", stem .. "os"})
	table.insert(inflections, {label = "conditional", stem .. "us"})
	table.insert(inflections, {label = "volitive", stem .. "u"})
end
 
function getPOS(word)
	word = mw.ustring.lower(word)
	local _, n = word:gsub("[aeiou]", "")
 
	-- Words with only one vowel are always irregular
	if n <= 1 then
		return nil
	elseif word:find("a$") then
		return "adjectives"
	elseif word:find("aj$") or word:find("an$") or word:find("ajn$") then
		return "adjective forms"
	elseif word:find("e$") or word:find("en$") then
		return "adverbs"
	elseif word:find("o$") then
		return "nouns"
	elseif word:find("oj$") or word:find("on$") or word:find("ojn$") then
		return "noun forms"
	elseif word:find("i$") then
		return "verbs"
	elseif word:find("[iaou]s$") or word:find("u$") then
		return "verb forms"
	else
		return nil
	end
end
 
return export