A modult a Modul:rw-headword/doc lapon tudod dokumentálni

local export = {}

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

local plural_classes = {
	["1"] = "c2", ["1a"] = "c2a", ["3"] = "c4", ["7"] = "c8", ["9"] = "c10", ["11"] = "c10", ["11/6"] = "c6", ["11/14"] = "c14",
	["12"] = "c13", ["5"] = "c6", ["5/10"] = "c10", ["9/6"] = "c6", ["9/za"] = "c10", ["14"] = "c6", ["15"] = "c6"}

local plural_rules = {
		   ["1"] = {[1] = {"umw",  "ab", 4}, [2] = {"umu",   "aba", 4}, [3] = {"mu", "ba", 3}, [4] = {"mw", "b", 3}},
		  ["1a"] = {[1] = {  "",   "bā", 1}},
		   ["3"] = {[1] = {"umw",  "imy", 4}, [2] = {"umu",   "imi", 4}, [3] = {"mu", "mi", 3}, [3] = {"mw", "my", 3}},
		   ["5"] = {[1] = {"i", "ama", 2}, [2] = {"iry", "am", 4}, [3] = {"ry", "m", 3}, [4] = {"", "ma", 1}},
		   ["5/10"] = {[1] = {  "",    "", 1}},
		   ["7"] = {[1] = {"iki",   "ibi", 4}, [2] = {"igi", "ibi", 4}, [3] = {"icy", "iby", 4}, [4] = {"ic", "ivy", 3}, [5] = {"ki", "bi", 3}, [6] = {"gi", "bi", 3}, [7] = {"cy", "by", 3}, [8] = {"c", "vy", 2}},
		   ["9"] = {[1] = {  "",    "", 1}},
		 ["9/6"] = {[1] = {  "i",  "ama", 2}, [2] = {"","ama",1}},
		 ["9/za"] = {[1] = {"", "[[zā]] ",1}},
	 	 ["11"] = {[1] = {"uruh", "imp", 5}, [2] = {"urub", "imb", 5}, [3] = {"urup", "imp", 5}, [4] = {"urum", "im", 5}, [5] = {"uruf", "imf", 5}, [6] = {"uruv", "imv", 5}, [7] = {"urw", "inz", 4}, [8] = {"uruy", "inz", 5}, [9] = {"uruny", "iny", 6}, [10] = {"urur", "ind", 5}, [11] = {"urun", "in", 5}, [12] = {"uru", "in", 4}},--fails for urū > inzū
	 	 ["11/6"] = {[1] = {"uru","ama", 4}},
	 	 ["11/14"] = {[1] = {"uru","ubu", 4}, [2] = {"urw", "ubw", 4}},
	 	 ["12"] = {[1] = {"aka", "utu", 4}, [2] = {"aga", "udu", 4}, [3] = {"ak", "utw", 3}, [4] = {"ka", "tu", 3}, [5] = {"ga", "du", 3}, [6] = {"k", "tw", 2}},
		["14"] = {[1] = {  "ubu",  "ama", 4}, [2] = {"ubw", "am", 4}, [3] = {"ub", "am", 3}, [4] = {"bu", "ma", 3}, [5] = {"bw", "m", 3}, [6] = {"b","m", 2}},
		["15"] = {[1] = {"uku", "ama", 4}, [2] = {"ugu", "ama", 4}, [3] = {"ukw", "am", 4}, [5] = {"ugw", "am", 4}, [6] = {"ku", "ma", 3}, [7] = {"gu", "ma", 3}, [8] = {"kw", "m", 3}, [9] = {"gw", "m", 3}}
}

function export.noun(frame)
	local params = {
		[1] = {},
		[2] = {},
		[3] = {}
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local categories = {}
	local head
	if not args[1] then
		args[1] = mw.title.getCurrentTitle().subpageText
		head = mw.title.getCurrentTitle().subpageText:gsub("^-", "") .. "<sup title=\"tones missing\">?</sup>"
		table.insert(categories, "Requests for tone in " .. lang:getCanonicalName() .. " noun entries")
	end
	
	local class
	if args[2] then
		class = mw.text.split(args[2], "%/")[1]
	else
		class = "?"
	end
	
	local data = {lang = lang, pos_category = "nouns", categories = categories, heads = {head or args[1]}, genders = {"c" .. class}, inflections = {}}
	
	if class ~= "?" then
		table.insert(data.categories, lang:getCanonicalName() .. " class " .. class .. " nouns")
	end
	
	if args[3] ~= "-" and plural_classes[args[2]] then
		local infl_plural = {label = "plural", accel = {form = "p", gender = plural_classes[args[2]]}, request = true}
		
		-- If no plural was provided, generate one
		if not args[3] then
			local singular = args[1] and require("Module:links").remove_links(args[1]) or mw.title.getCurrentTitle().text
			args[3] = export.generate_plural(singular, args[2])
		end
		
		table.insert(infl_plural, {term = args[3], genders = {plural_classes[args[2]]}})
		
		table.insert(data.inflections, infl_plural)
	end
	
	return require("Module:headword").full_headword(data)
end


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


function export.generate_plural(singular, class)
	if plural_rules[class] then
		singular = mw.ustring.toNFD(singular)
		for k, v in ipairs(plural_rules[class]) do
			if mw.ustring.find(mw.ustring.lower(singular), "^" .. v[1]) then
				local plural = match_case(singular, v[2] .. mw.ustring.sub(singular, v[3]))
				return mw.ustring.toNFC(plural)
			end
		end
	end
end

return export