Module:eo-headword

From Wiktionary, the free dictionary
Jump to navigation Jump to search
local export = {}
local pos_functions = {}

local PAGENAME = mw.title.getCurrentTitle().text 
local lang = require("Module:languages").getByCode("eo")
local except = require("Module:eo-headword/exceptions")

-- Verb stem must have at least two letters.
local participle_pattern = "..([aio]n?t[aeo]j?n?)$"

local function monosyllabic(word)
	return select(2, mw.ustring.gsub(word, "[AEIOUaeiou]", "")) == 1
end

local function check(data, tracking_categories)
	local catname = "Esperanto nouns with red links in their headword lines"
	
	if data.pos_category == "adjectives" or data.pos_category == "determiners" or data.pos_category == "adjectival participles" then
		catname = "Esperanto adjectives with red links in their headword lines"
	end
	
	for _, entry in ipairs(data.check) do
		local t = mw.title.new(entry)
		if t and not t.exists then
			table.insert(tracking_categories, catname)
		end
	end
end

-- Determines which function in pos_functions should be used for a word in {{eo-head}}.
local function getPOS(word)
	-- deal with some exceptions
	if except[word] then
		return except[word]
	end
	
	-- delete anything after "de"
	word = mw.ustring.gsub(word, " de .+$", "")
	
	if word:find(" ") then
		if word:find("o%f[%z%s]") and word:find("a%f[%z%s]") then
			return "nouns"
		end
	end
	
	-- deal with letters
	if mw.ustring.match(word,"^[aeiou]$") or mw.ustring.match(word,"^[bcĉdfgĝhĥjĵklmnprsŝtŭvz]o$") then
		return "nouns"
	end
	
	-- deal with ordinals
	if mw.ustring.match(word, "^[0-9]+%-?a$") then
		return "adjectives"
	elseif mw.ustring.match(word, "^[0-9]+%-?aj?n?$") then
		return "adjective forms"
	end
	
	-- Words with only one vowel are always irregular
	if monosyllabic(word) then
		return nil
	elseif mw.ustring.find(word, participle_pattern) then -- detect this firstly
		return "participles"
	end
	
	local ending = word:match("[aeiou][^aeiou]*$")
	
	if ending == "a" then
		return "adjectives"
	elseif ending == "aj" or ending == "an" or ending == "ajn" then
		return "adjective forms"
	elseif ending == "e" or ending == "en" then
		return "adverbs"
	elseif ending == "o" then
		if mw.ustring.find(word, "^%u") then
			return "proper nouns"
		else
			return "nouns"
		end
	elseif ending == "oj" or ending == "on" or ending == "ojn" then
		if mw.ustring.find(word, "^%u") then
			return "proper noun forms"
		else
			return "noun forms"
		end
	elseif ending == "i" then
		return "verbs"
	elseif ending:find("^[iaou]s$") or ending == "u" then
		return "verb forms"
	else
		return nil
	end
end
export.getPOS = getPOS

-- The main entry point
function export.show(frame)
	local tracking_categories = {}
	
	local parent_args = frame:getParent().args
	local poscat = parent_args["pos"] or frame.args["poscat"] or getPOS(PAGENAME)
	
	if not poscat then
		local namespace = mw.title.getCurrentTitle().namespace
		if namespace == 10 or namespace == 828 then
			poscat = "nouns"
		else
			error("Part of speech of \"" .. PAGENAME .. "\" cannot be automatically determined.")
		end
	else
		if poscat == "plurale tantum" then
			poscat = "pluralia tantum"
		elseif not (poscat == "pluralia tantum" or poscat:find("s$")) then
			poscat = poscat .. "s" -- Pluralize part of speech.
		end
	end
	
	local params = {
		["head"] = {list = true},
		["pos"] = {},
		["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(parent_args, params)
	local data = {lang = lang, pos_category = poscat, categories = {}, heads = args["head"], genders = {}, inflections = {}, check = {}}
	
	if pos_functions[poscat] then
		pos_functions[poscat].func(args, data)
	end
	
	check(data, tracking_categories)
	
	return require("Module:headword").full_headword(data) .. require("Module:utilities").format_categories(tracking_categories, lang)
end

pos_functions["adjectives"] = {
	params = {
	},
	func = function(args, data)
		table.insert(data.inflections, {label = "accusative singular", accel = {form = "acc|s"}, PAGENAME .. "n"})
		table.insert(data.inflections, {label = "plural", accel = {form = "p"}, PAGENAME .. "j"})
		table.insert(data.inflections, {label = "accusative plural", accel = {form = "acc|p"}, PAGENAME .. "jn"})
		table.insert(data.check, PAGENAME .. "n")
		table.insert(data.check, PAGENAME .. "j")
		table.insert(data.check, PAGENAME .. "jn")
	end
}

pos_functions["determiners"] = {
	params = {
	},
	func = function(args, data)
		table.insert(data.inflections, {label = "accusative singular", accel = {form = "acc|s"}, PAGENAME .. "n"})
		table.insert(data.inflections, {label = "plural", accel = {form = "p"}, PAGENAME .. "j"})
		table.insert(data.inflections, {label = "accusative plural", accel = {form = "acc|p"}, PAGENAME .. "jn"})
		table.insert(data.check, PAGENAME .. "n")
		table.insert(data.check, PAGENAME .. "j")
		table.insert(data.check, PAGENAME .. "jn")
	end
}

pos_functions["nouns"] = {
	params = {
		[1] = {list = true, allow_holes = true},
	},
	func = function(args, data)
		-- Get the parameters
		local inflected_words_specified = false
		local inflected_words = {}
		
		for i = 1, args[1].maxindex do
			local word = args[1][i]
			
			if word == "+" or word == "-" then
				word = nil
			end
			
			if word then
				inflected_words[word] = true
				inflected_words_specified = true
			end
		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" or pos == "proper nouns") then
				local is_letter = ""
				if mw.ustring.match(word,"^[aeiou]$") or mw.ustring.match(word,"^[bcĉdfgĝhĥjĵklmnprsŝtŭvz]o$")then
					is_letter = "-o"
				end
				table.insert(acc, word .. is_letter .. "n")
				table.insert(pl, word .. is_letter .. "j")
				table.insert(acc_pl, word .. is_letter .. "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, " ")
		
		local acc2, pl2, acc_pl2
		
		if PAGENAME == "sozo" then
			acc2 = "sozo-on"
			pl2 = "sozo-oj"
			acc_pl2 = "sozo-ojn"
		end
		
		if args[1][1] == "-" then
			table.insert(data.inflections, {label = "uncountable"})
			table.insert(data.inflections, {label = "accusative", accel = {form = "acc"}, acc})
			table.insert(data.categories, lang:getCanonicalName() .. " uncountable nouns")
			table.insert(data.check, acc)
		else
			table.insert(data.inflections, {label = "accusative singular", accel = {form = "acc|s"}, acc, acc2})
			table.insert(data.inflections, {label = "plural", accel = {form = "p"}, pl, pl2})
			table.insert(data.inflections, {label = "accusative plural", accel = {form = "acc|p"}, acc_pl, acc_pl2})
			table.insert(data.check, acc)
			table.insert(data.check, pl)
			table.insert(data.check, acc_pl)
		end
	end
}

pos_functions["proper nouns"] = {
	params = {
		[1] = {list = true, allow_holes = true},
	},
	func = function(args, data)
		-- Get the parameters
		local inflected_words_specified = false
		local inflected_words = {}
		
		for i = 1, args[1].maxindex do
			local word = args[1][i]
			
			if word == "+" or word == "-" then
				word = nil
			end
			
			if word then
				inflected_words[word] = true
				inflected_words_specified = true
			end
		end
		
		local acc = {}
		local pl = {}
		local acc_pl = {}
		
		local de = false
		
		-- Split multi-word terms
		for word in mw.text.gsplit(PAGENAME, " ", true) do
			local pos = getPOS(word)
			
			if word == "de" then de = true end
			
			-- Inflect each word separately
			if (not inflected_words_specified or inflected_words[word]) and (pos == "adjectives" or pos == "nouns" or pos == "proper nouns" or mw.ustring.match(word,'[ao]j$') and not monosyllabic(word)) and not de 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][1] == "+" then
			table.insert(data.inflections, {label = "accusative singular", accel = {form = "acc|s"}, acc})
			table.insert(data.inflections, {label = "plural", accel = {form = "p"}, pl})
			table.insert(data.inflections, {label = "accusative plural", accel = {form = "acc|p"}, acc_pl})
			table.insert(data.check, acc)
			table.insert(data.check, pl)
			table.insert(data.check, acc_pl)
		else
			table.insert(data.inflections, {label = "accusative", accel = {form = "acc"}, acc})
			table.insert(data.check, acc)
		end
	end
}

pos_functions["verbs"] = {
	params = {
	},
	func = function(args, data)
		local stem = PAGENAME:sub(1, -2)
		
		table.insert(data.inflections, {label = "present", accel = {form = "pres"}, stem .. "as"})
		table.insert(data.inflections, {label = "past", accel = {form = "past"}, stem .. "is"})
		table.insert(data.inflections, {label = "future", accel = {form = "futr"}, stem .. "os"})
		table.insert(data.inflections, {label = "conditional", accel = {form = "cond"}, stem .. "us"})
		table.insert(data.inflections, {label = "volitive", accel = {form = "voli"}, stem .. "u"})
	end
}

pos_functions["noun forms"] = {
	params = {
		[1] = {},
	},
	func = function(args, data)
		if args[1] == "p" or args[1] == "p+" then
			table.insert(data.genders, "p")
			data.categories = {"Esperanto nouns", "Esperanto pluralia tantum"}
			table.insert(data.inflections, {label = "accusative", accel = {form = "acc"}, PAGENAME .. "n"})
			table.insert(data.check, PAGENAME .. "n")
			if args[1] == "p+" then
				local singular = mw.ustring.gsub(PAGENAME, "j$", "")
				table.insert(data.inflections, {label = "singular", singular})
				table.insert(data.check, singular)
			end
		end
	end
}

pos_functions["pluralia tantum"] = {
	params = {
		[1] = {},
	},
	func = function(args, data)
		data.pos_category = "nouns"
		table.insert(data.categories, 1, "Esperanto pluralia tantum")
		table.insert(data.inflections, {label = "accusative", accel = {form = "acc"}, PAGENAME .. "n"})
		table.insert(data.check, PAGENAME .. "n")
		if args[1] == "+" then
			local singular = mw.ustring.gsub(PAGENAME, "j$", "")
			table.insert(data.inflections, {label = "singular", singular})
			table.insert(data.check, singular)
		end
	end
}

pos_functions["participles"] = {
	params = {
		[1] = {}, [2] = {}, --these will be phased out
	},
	func = function(args, data)
		local ending = mw.ustring.match(PAGENAME, participle_pattern)
		if ending then
			local vowel, consonant = ending:match("([aoe])(j?n?)$")
			if consonant == "" then
				if vowel == "a" or vowel == "o" then
					table.insert(data.inflections, {label = "accusative singular", accel = {form = ending .. "n"}, PAGENAME .. "n"})
					table.insert(data.inflections, {label = "plural", accel = {form = ending .. "j"}, PAGENAME .. "j"})
					table.insert(data.inflections, {label = "accusative plural", accel = {form = ending .. "jn"}, PAGENAME .. "jn"})
					table.insert(data.check, PAGENAME .. "n")
					table.insert(data.check, PAGENAME .. "j")
					table.insert(data.check, PAGENAME .. "jn")
				elseif vowel == "e" then
					data.categories = {"Esperanto adverbial participles"}
				end
			elseif vowel == "e" then
				error("-e" .. consonant .. " is not a valid participle ending!")
			end
			if vowel == "a" then
				data.categories = {"Esperanto adjectival participles"}
			elseif vowel == "o" then
				data.categories = {"Esperanto nominal participles"}
			end
		else
			error("This term is not a participle!")
		end
	end
}

return export