Module:Eo-headword

local export = {}
local pos_functions = {}
 
local lang = require("Module:languages").getByCode("eo")

local function monosyllabic(word)
	_, n = mw.ustring.gsub(word, "[aeiou]", "")
	return n == 1
end

-- The main entry point
function export.show(frame)
	PAGENAME = mw.title.getCurrentTitle().text
	
	local poscat = frame:getParent().args["pos"] or 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 params = {
		["head"] = {list = true, default = ""},
		["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(frame:getParent().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)
	
	return require("Module:headword").full_headword(data)
end

pos_functions["adjectives"] = {
	params = {
	},
	func = function(args, data)
		table.insert(data.inflections, {label = "accusative singular", accel = "accusative-form-of", PAGENAME .. "n"})
		table.insert(data.inflections, {label = "plural", accel = "plural-form-of", PAGENAME .. "j"})
		table.insert(data.inflections, {label = "accusative plural", accel = "accusative-plural-form-of", 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 = "accusative-form-of", PAGENAME .. "n"})
		table.insert(data.inflections, {label = "plural", accel = "plural-form-of", PAGENAME .. "j"})
		table.insert(data.inflections, {label = "accusative plural", accel = "accusative-plural-form-of", 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 = "uncountable-accusative-form-of", acc})
			table.insert(data.categories, lang:getCanonicalName() .. " uncountable nouns")
			table.insert(data.check, acc)
		else
			table.insert(data.inflections, {label = "accusative singular", accel = "accusative-form-of", acc, acc2})
			table.insert(data.inflections, {label = "plural", accel = "plural-form-of", pl, pl2})
			table.insert(data.inflections, {label = "accusative plural", accel = "accusative-plural-form-of", 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 = "accusative-form-of", acc})
			table.insert(data.inflections, {label = "plural", accel = "plural-form-of", pl})
			table.insert(data.inflections, {label = "accusative plural", accel = "accusative-plural-form-of", 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 = "uncountable-accusative-form-of", 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 = "present-form-of", stem .. "as"})
		table.insert(data.inflections, {label = "past", accel = "past-form-of", stem .. "is"})
		table.insert(data.inflections, {label = "future", accel = "future-form-of", stem .. "os"})
		table.insert(data.inflections, {label = "conditional", accel = "conditional-form-of", stem .. "us"})
		table.insert(data.inflections, {label = "volitive", accel = "volitive-form-of", 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 = "accusative-form-of", 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)
		table.insert(data.categories, 1, "Esperanto nouns")
		table.insert(data.inflections, {label = "accusative", accel = "accusative-form-of", 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, "[aio]n?t[aeo]$")
		if ending:match("[ao]$") then
			table.insert(data.inflections, {label = "accusative singular", accel = ending .. "n-form-of", PAGENAME .. "n"})
			table.insert(data.inflections, {label = "plural", accel = ending .. "j-form-of", PAGENAME .. "j"})
			table.insert(data.inflections, {label = "accusative plural", accel = ending .. "jn-form-of", PAGENAME .. "jn"})
			table.insert(data.check, PAGENAME .. "n")
			table.insert(data.check, PAGENAME .. "j")
			table.insert(data.check, PAGENAME .. "jn")
		end
		if ending:match("a$") then
			data.categories = {"Esperanto adjectival participles"}
		elseif ending:match("e$")  then
			data.categories = {"Esperanto adverbial participles"}
		elseif ending:match("o$") then
			data.categories = {"Esperanto nominal participles"}
		else
			error("This term is not a participle!")
		end
	end
}

function getPOS(word)
	word = mw.ustring.lower(word)
	
	-- delete anything after "de"
	word = mw.ustring.gsub(word, " de .+$", "")
	
	-- 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 "sti"
	if word == "sti" then
		return "verbs"
	end
	
	-- Words with only one vowel are always irregular
	if monosyllabic(word) 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

function check(data)
	local catname = "Missing Esperanto noun forms"
	
	if data.pos_category == "adjectives" or data.pos_category == "determiners" or data.pos_category == "adjectival participles" then
		catname = "Missing Esperanto adjective forms"
	end
	
	for _,entry in ipairs(data.check) do
		local t = mw.title.new(entry)
		if t and not t.exists then
			table.insert(data.categories, catname)
		end
	end
end

return export