Module:rw-headword


local export = {}

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

local MACRON = mw.ustring.char(0x0304)
local ACUTE = mw.ustring.char(0x0301)
local CIRCUMFLEX = mw.ustring.char(0x0302)

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 export.verb(frame)
	local params = {
		[1] = {},
		[2] = {list = true},
		["inf"] = {}
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local categories = {}
	local inflections = {}
	local inf_forms = {label = "infinitive"}
	
	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() .. " verb entries")
	else
		head = "-" .. args[1]
	end
	
	-- Generate infinitive form
	local inf_prefix = "ku"
	local root = mw.ustring.toNFD(args[1])
	local infinitive
	
	if args["inf"] then
		table.insert(inf_forms, args["inf"])
	elseif mw.ustring.find(root, "^[cfhkpstCFHKPST]") then
		inf_prefix = "gu"
		infinitive = mw.ustring.toNFC(inf_prefix .. root)
		table.insert(inf_forms, infinitive)
	elseif mw.ustring.find(root, "^[aeiouAEIOU]") then
		inf_prefix = "kw"
		local vowel = mw.ustring.sub(root, 1, 1)
		if mw.ustring.sub(root, 2, 2) == ACUTE then
			infinitive = mw.ustring.toNFC(inf_prefix .. mw.ustring.gsub(root, ACUTE, CIRCUMFLEX, 1))
		elseif mw.ustring.sub(root, 2, 2) == CIRCUMFLEX or mw.ustring.sub(root, 2, 2) == MACRON then
			infinitive = mw.ustring.toNFC(inf_prefix .. root)
		else
			infinitive = mw.ustring.toNFC(inf_prefix .. vowel .. MACRON .. mw.ustring.sub(root, 2))
		end
		
		if mw.ustring.lower(vowel) == "o" or mw.ustring.lower(vowel) == "u" then
			table.insert(inf_forms, {term = mw.ustring.gsub(infinitive, "^kw", "k"), q = {"Rwanda"}})
			table.insert(inf_forms, {term = infinitive, q = {"Rundi"}})
		else
			table.insert(inf_forms, infinitive)
		end
	else
		infinitive = mw.ustring.toNFC(inf_prefix .. root)
		table.insert(inf_forms, infinitive)
	end
	
	table.insert(inflections, inf_forms)
	
	-- perfective stem
	local perf_stem = nil
	if args[2][1] then
		local perf_forms = {label = "perfective"}
		for _, form in ipairs(args[2]) do
			perf_stem = "-" .. form
			table.insert(perf_forms, {term = perf_stem, nolink = true})
		end
		table.insert(inflections, perf_forms)
	else -- generate perfective stem if possible
		local perf_forms = {label = "perfective"}
		root = mw.ustring.gsub(root, "a$", "") -- remove final vowel -a
		local is_generated = false
		if mw.ustring.find(root, "[aeiou]") then -- exclude -CV- roots
			is_generated = true
			-- regular changes
			if mw.ustring.find(root, "c$") then
				perf_stem = "-" .. mw.ustring.gsub(root, "c$", "she")
				table.insert(perf_forms, {term = perf_stem, nolink = true})
			elseif mw.ustring.find(root, "d$") then
				perf_stem = "-" .. mw.ustring.gsub(root, "d$", "ze")
				table.insert(perf_forms, {term = perf_stem, nolink = true})
			elseif mw.ustring.find(root, "g$") then
				perf_stem = "-" .. mw.ustring.gsub(root, "g$", "ze")
				table.insert(perf_forms, {term = perf_stem, nolink = true})
			elseif mw.ustring.find(root, "nk$") then
				perf_stem = "-" .. mw.ustring.gsub(root, "nk$", "nse")
				table.insert(perf_forms, {term = perf_stem, nolink = true})
			elseif mw.ustring.find(root, "k$") then
				perf_stem = "-" .. mw.ustring.gsub(root, "k$", "tse")
				table.insert(perf_forms, {term = perf_stem, nolink = true})
			elseif root == "fat" then -- one irregular case (gufata)
				perf_stem = "-fashe"
				table.insert(perf_forms, {term = perf_stem, nolink = true})
			elseif mw.ustring.find(root, "t$") then
				perf_stem = "-" .. mw.ustring.gsub(root, "t$", "se")
				table.insert(perf_forms, {term = perf_stem, nolink = true})
			elseif mw.ustring.find(root, "[^n]y$") then
				perf_stem = "-" .. root .. "e"
				table.insert(perf_forms, {term = perf_stem, nolink = true})
			elseif mw.ustring.find(root, "m$") then
				perf_stem = "-" .. root .. "ye"
				table.insert(perf_forms, {term = perf_stem, nolink = true})
			elseif mw.ustring.find(root, "b$") then
				local perf_stem_rw = "-" .. root .. "ye"
				table.insert(perf_forms, {term = perf_stem_rw, q = {"Rwanda"}, nolink = true})
				local perf_stem_rn = "-" .. mw.ustring.gsub(root, "b$", "vye")
				table.insert(perf_forms, {term = perf_stem_rn, q = {"Rundi"}, nolink = true})
			elseif mw.ustring.find(root, "[^s]h$") then
				local perf_stem_rw = "-" .. mw.ustring.gsub(root, "h$", "shye")
				table.insert(perf_forms, {term = perf_stem_rw, q = {"Rwanda"}, nolink = true})
				local perf_stem_rn = "-" .. mw.ustring.gsub(root, "h$", "she")
				table.insert(perf_forms, {term = perf_stem_rn, q = {"Rundi"}, nolink = true})
			else
				is_generated = false
			end
		end
		
		if not is_generated then
			table.insert(perf_forms, {term = nil, nolink = true})
		end
		
		table.insert(inflections, perf_forms)
	end
	
	local data = {lang = lang, pos_category = "verbs", categories = categories, heads = {head}, inflections = inflections}
	
	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