Module:yi-headword

From Wiktionary, the free dictionary
Jump to navigation Jump to search

This module needs documentation.
Please document this module by describing its purpose and usage on the documentation page.

local com = require("Module:yi-common")
local lang = require("Module:languages").getByCode("yi")
local Latn = require("Module:scripts").getByCode("Latn")
local u = mw.ustring

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)
	SUBPAGENAME = mw.title.getCurrentTitle().subpageText

	local args = {}
	for key, val in pairs(frame:getParent().args) do
		if val ~= "" then
			args[key] = val
		end
	end
	
	local poscat = frame.args[1] or error("Part of speech has not been specified. Please pass parameter 1 to the module invocation.")
	
	local data = {lang = lang, pos_category = poscat, categories = {}, heads = {args["head"]}, translits = {args["tr"]}, genders = {}, inflections = {}, sort_key = args["sort"]}
	
	-- Call POS-specific function
	if pos_functions[poscat] then
		pos_functions[poscat](args, data)
	end
	
	return require("Module:headword").full_headword(data)
end

-- if func(form, formtr) is nil or returns nil, then the default get_form will be used
local function get_form_custom(func, args, argname, trname)
	local form = args[argname]
	if form then
		local formtr = args[trname or (argname .. "tr")]
		local ret = func and func(form, formtr)
		if ret == nil then
			ret = com.form(form, args[trname or (argname .. "tr")])
		end
		return ret
	else
		return form
	end
end

local function get_form(args, argname, trname)
	return get_form_custom(func, args, argname, trname)
end

local function get_numbered_forms_custom(func, args, argname, startat, output)
	output = output or {}
	while true do
		local form = get_form_custom(func, args, argname .. startat)
		if form then
			table.insert(output, form)
		else
			break
		end
		startat = startat + 1
	end
	return output
end

local function get_numbered_forms(args, argname, startat, output)
	return get_numbered_forms_custom(nil, args, argname, startat, output)
end

local function add_inflections(inflections, ...)
	for _, inflection in ipairs({...}) do
		if inflection[1] then
			for i, form in ipairs(inflection) do
				if form == "-" then
					inflection[i] = {term = "—", translit = "-", nolink = true, sc = Latn}
				else
					inflection[i] = {term = form.text, translit = form.tr}
				end
			end
			table.insert(inflections, inflection)
		end
	end
end

pos_functions["adjectives"] = function(args, data)
	local comparatives = {get_form(args, 1, "ctr"), label = "comparative", enable_auto_translit = true}
	if comparatives[1] then
		get_numbered_forms(args, "c", 2, comparatives)
	end

	local superlatives = {get_form(args, 2, "str"), label = "superlative", enable_auto_translit = true}
	if superlatives[1] then
		get_numbered_forms(args, "s", 2, superlatives)
	end

	add_inflections(data.inflections, comparatives, superlatives)
end

pos_functions["verbs"] = function(args, data)
	local past_participles = {get_form(args, 1, 2), label = "past participle", enable_auto_translit = true, accel = {form = "past-participle"}}
	if past_participles[1] then
		get_numbered_forms(args, "p", 2, past_participles)
	end

	add_inflections(data.inflections, past_participles)
end

local compound_genders = {
	["mp"] = {"m-p"},
	["np"] = {"n-p"},
	["fp"] = {"f-p"},
	["mn"] = {"m", "n"},
	["mf"] = {"m", "f"},
	["fn"] = {"f", "n"},
	["mnp"] = {"m-p", "n-p"},
	["mfp"] = {"m-p", "f-p"},
	["fnp"] = {"f-p", "n-p"},
	["mfn"] = {"m", "f", "n"},
	["mfnp"] = {"m-p", "f-p", "n-p"},
}

local plural_suffixes = {
	["n"] = "ן",
	["en"] = "ען",
	["s"] = "ס",
	["es"] = "עס",
	["er"] = "ער",
	["ekh"] = "עך",
}

local plural_getter = {}
function plural_getter:__call(f, ftr)
	if f == "+" then
		return com.form(self.head, ftr)
	elseif plural_suffixes[f] then
		return com.form(com.suffix(self.head, plural_suffixes[f]), ftr)
	else
		return nil
	end
end

local dim_suffixes = {
	["l"] = "ל",
	["kl"] = "כל",
	["ele"] = "עלע",
}

local dim_getter = {}
function dim_getter:__call(f, ftr)
	if dim_suffixes[f] then
		return com.form(com.suffix(self.head, dim_suffixes[f]), ftr)
	else
		return nil
	end
end

local gender_endings = {
	["עכץ"]	= "n",
	["עניש"] = "n",
}

local gender_getter = {}
function gender_getter:__call()
	for k, v in pairs(gender_endings) do
		if u.match(self.head, k .. "$") then
			return v
		end
	end
	return nil
end

pos_functions["nouns"] = function(args, data)
	local gs = compound_genders[args["g"]]
	if gs then
		for _, g in ipairs(gs) do
			table.insert(data.genders, g)
		end
	elseif args["g"] then
		table.insert(data.genders, args["g"])
		local i = 2
		while args["g" .. i] do
			table.insert(data.genders, args["g" .. i])
			i = i + 1
		end
	else
		local get_gender = {head = com.form(args["head"] or SUBPAGENAME, args["tr"])}
		setmetatable(get_gender, gender_getter)
		data.genders = {get_gender()}
	end

	local get_plural = {head = com.form(args["head"] or SUBPAGENAME, args["tr"])}
	setmetatable(get_plural, plural_getter)
	local plurals = {get_form_custom(get_plural, args, "pl"), label = "plural", enable_auto_translit = true}
	if plurals[1] then
		get_numbered_forms_custom(get_plural, args, "pl", 2, plurals)
	end

	add_inflections(data.inflections, plurals)

	local get_dim = {head = com.form(args["head"] or SUBPAGENAME, args["tr"])}
	setmetatable(get_dim, dim_getter)
	local dims = {get_form_custom(get_dim, args, "dim"), label = "diminutive", enable_auto_translit = true}
	if dims[1] then
		get_numbered_forms_custom(get_dim, args, "dim", 2, dims)
	end

	add_inflections(data.inflections, dims)
end

pos_functions["prepositions"] = function(args, data)
	local dem_forms = {get_form(args, "dem", "demtr"), label = "contracted ''dem''-form", enable_auto_translit = true}

	if dem_forms[1] == "-" then
		dem_forms.label = "no " .. dem_forms.label
		dem_forms[1] = nil
		table.insert(data.inflections, dem_forms)
	elseif dem_forms[1] then
		get_numbered_forms(args, "dem", 2, dem_forms)
		add_inflections(data.inflections, dem_forms)
	end
end

return export