Module:st-headword

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("st")

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

local plural_rules = {
		   ["1"] = {[1] = {"mo",    "ba", 3}, [2] = {"ngw",   "b", 4}, [3] = {"ngo", "b", 4}, [4] = { "mm",   "bab", 3}, [5] = {"ʼm", "bab", 3}},
		  ["1a"] = {[1] = {  "",   "bo", 1, "bo-"}},
		["1/6"] = {[1] = {  "mo",  "ma", 3}},
		   ["3"] = {[1] = {"mo",  "me", 3}, [2] = {"mm", "meb", 3}, [3] = {"ʼm", "meb", 3}},
		   ["5"] = {[1] = {"le", "ma", 3}, [2] = {"", "ma", 1}},
		   ["5/10"] = {[1] = {"le", "di", 3, "li"}},
		   ["7"] = {[1] = {"se",   "di", 3, "li"}, [2] = {"s", "di", 2, "li"}, [3] = {"", "di", 1, "li"}},
		   ["9"] = {[1] = {  "",    "di", 1, "li"}},
		 ["9/6"] = {[1] = {  "",  "ma", 1}},
		["14"] = {[1] = { "bo",  "ma", 3}},
		["1/4"] = {[1] = {"mo", "me", 3}}
}

function export.noun(frame)
	local params = {
		[1] = {},
		[2] = {},
		[3] = {list = true}
	}
	
	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
	
	assert(args[1], "head required")
	
	local class = args[2] or "?"
	
	local data = {lang = lang, pos_category = "nouns", categories = categories, heads = {head or args[1]}, genders = {"c" .. mw.text.split(class, "%/")[1]}, inflections = {}}
	table.insert(data.categories, lang:getCanonicalName() .. " class " .. data.genders[1] .. " nouns")
	
	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][1] 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
		
		assert(#args[3] > 0, "No plurals!")
		
		if #args[3] == 1 then
			table.insert(infl_plural, {term = args[3][1], genders = {plural_classes[args[2]]}})
		elseif #args[3] == 2 then
			table.insert(infl_plural, {term = args[3][1], genders = {plural_classes[args[2]]}, qualifiers = {"South Africa"}})
			table.insert(infl_plural, {term = args[3][2], genders = {plural_classes[args[2]]}, qualifiers = {"Lesotho"}})
		end
		
		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
		for k, v in ipairs(plural_rules[class]) do
			if mw.ustring.find(mw.ustring.lower(singular), "^" .. v[1]) then
				if v[4] then
					return {match_case(singular, v[2] .. mw.ustring.sub(singular, v[3])),
					     	match_case(singular, v[4] .. mw.ustring.sub(singular, v[3]))}
				else
					return {match_case(singular, v[2] .. mw.ustring.sub(singular, v[3]))}
				end
			end
		end
	end
end

return export