Module:wmw-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 export = {}

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

local plural_classes = {
	["1"] = "c2", ["1a"] = "c2", ["3"] = "c4", ["7"] = "c8", ["9"] = "c10", ["9/6"] = "c6",
	["11"] = "c6", ["5"] = "c6", ["11/10"] = "c10"}

local plural_rules = {
		   ["1"] = {[1] = {"mwi",    "we", 4}, [2] = {"mwa",    "wa", 4}, [3] = {"mu",   "wa", 3}, [4] = { "n",   "wa", 2}},
		  ["1a"] = {[1] = {  "",   "wa", 1}},
		   ["3"] = {[1] = {"mwi",    "mi", 4}, [2] = {"mw",    "my", 3}, [3] = {"m",   "mi", 2}, [4] = { "n",   "mi", 2}},
		   ["5"] = {[1] = {"", "ma", 1}},
		   ["7"] = {[1] = {"ki",   "vi", 3}, [2] = {"ch", "vy", 3}},
		   ["9"] = {[1] = {"", "", 1}},
		   ["9/6"] = {[1] = {"", "ma", 1}},
		   ["11"] = {[1] = {  "lu",    "ma", 3}},
		 ["11/10"] = {[1] = {  "",  "ma", 1}},
}

function export.noun(frame)
	local params = {
		[1] = {required=true},
		[2] = {},
		["head"] = {},
		["h"] = {alias_of = "head"}
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {lang = lang, pos_category = "nouns", categories = {}, heads = {args["head"]}, genders = mw.text.split(args[1], "%/"), inflections = {}}
	table.insert(data.categories, lang:getCanonicalName() .. " class " .. data.genders[1] .. " nouns")
	
	local singular = args["head"] and require("Module:links").remove_links(args["head"]) or mw.title.getCurrentTitle().text
	local inflection = nil
	
	if not args[2] then
		inflection = export.generate_plural(singular, args[1])
	else
		if (args[2] ~= "-") then
			inflection = args[2]	
		end
	end
	
	local accel = {form = "p"}
	if (plural_classes[args[1]]) then
		accel.gender = plural_classes[args[1]]
	end
	
	if (inflection) then
		table.insert(data.inflections, {label = "plural", accel = accel, inflection})
	end
	
	return require("Module:headword").full_headword(data)
	
end

function startswith(test_string, start)
	return mw.ustring.sub(mw.ustring.lower(test_string),1,mw.ustring.len(start))==mw.ustring.lower(start)
end

function match_case(string1, string2)
	c1 = mw.ustring.sub(string1, 1, 1)
	c2 = mw.ustring.sub(string2, 1, 1)
	if (mw.ustring.lower(c1) == c1) then
		return mw.ustring.lower(c2) .. mw.ustring.sub(string2, 2)
	end
	return mw.ustring.upper(c2).. mw.ustring.sub(string2, 2)
end

function export.generate_plural(singular, class)
	if (plural_rules[class]) then
		for k,v in ipairs(plural_rules[class]) do
			if startswith(singular, v[1]) then
				return match_case(singular, v[2]..mw.ustring.sub(singular, v[3]))
			end
		end
	end
end

return export