Module:ny-derivations

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

For use in {{ny-derivations}}.


local m_links = require("Module:links")
local lang = require("Module:languages").getByCode("ny")
local match = mw.ustring.match
local gsub = mw.ustring.gsub

PAGENAME = mw.title.getCurrentTitle().text or ''

local export = {}

local classes = {
	"appl",
	"caus",
	"conv",
	"pass",
	"recip",
	"redup",
	"stat",
}

local classes_names = {
	appl = "Applicative",
	caus = "Causative",
	conv = "Conversive",
	pass = "Passive",
	recip = "Reciprocal",
	redup = "Reduplicative",
	stat = "Stative",
}

local function join_E_suffix(stem, suffix)
	if match(stem, "[aiu][^aeiou]*$") then
		return stem .. gsub(suffix, "E", "i")
	elseif match(stem, "[oe][^aeiou]*$") then
		return stem .. gsub(suffix, "E", "e")
	else -- Default to an 'e'. Perhaps throw an error instead?
		return stem .. gsub(suffix, "E", "e")
	end
end

local function join_O_suffix(stem, suffix)
	if match(stem, "[aiu][^aeiou]*$") then
		return stem .. gsub(suffix, "O", "u")
	elseif match(stem, "[oe][^aeiou]*$") then
		return stem .. gsub(suffix, "O", "o")
	else -- Default to an 'o'. Perhaps throw an error instead?
		return stem .. gsub(suffix, "O", "o")
	end
end

local auto_deriv = {}

auto_deriv.appl = function(lemma)
	return join_E_suffix(gsub(lemma, "a$", ""), "Era")
end
auto_deriv.caus = function(lemma)
	if match(lemma, "ka$") then
		return gsub(lemma, "ka$", "tsa")
	else
		return join_E_suffix(gsub(lemma, "a$", ""), "Etsa")
	end
	return lemma
end
auto_deriv.conv = function(lemma)
	return join_O_suffix(gsub(lemma, "a$", ""), "Ola")
end
auto_deriv.pass = function(lemma)
	return join_E_suffix(gsub(lemma, "a$", ""), "Edwa")
end
auto_deriv.recip = function(lemma)
	return lemma .. "na"
end
auto_deriv.redup = function(lemma)
	return lemma .. lemma
end
auto_deriv.stat = function(lemma)
	if match(lemma, "la$") then
		return gsub(lemma, "la$", "ka")
	else
		return join_E_suffix(gsub(lemma, "a$", ""), "Eka")
	end
	return lemmma
end

local function pop_contains(arr, val)
	for i, v in pairs(arr) do
		if val == v then
			table.remove(arr, i)
			return true
		end
	end
	return false
end

local function get_remaining(arr)
	local out = {}
	for _, v in ipairs(arr) do
		table.insert(out, v)	
	end
	return out
end

local function make_verb_row(lemma, code, autogen, forms, gloss)
	if autogen and auto_deriv[code] then
		local autoval = auto_deriv[code](lemma)
		table.insert(forms, 1, autoval)
	end
	
	local row = {}
	
	for i, term in pairs(forms) do
		table.insert(row, m_links.full_link({ lang = lang, term = term, alt = '-' .. term, gloss = gloss[i] }))
	end
	return table.concat{"** ''", classes_names[code], "'': ", table.concat(row, ", ")}
end

local function make_other_verb_row(forms, gloss)
	local row = {}
	
	for i, term in pairs(forms) do
		table.insert(row, m_links.full_link({ lang = lang, term = term, alt = '-' .. term, gloss = gloss[i] }))
	end
	table.sort(row)
	return table.concat{"** ''Other formations'': ", table.concat(row, ", ")}
end

local function make_noun_row(term, gloss)
	return table.concat{
		"** ",
		m_links.full_link({ lang = lang, term = term, gloss = gloss })
	}
end

local function make_output(args)
	local output = {}
	local autogen = nil
	local verb_output = {}
	for _, code in ipairs(classes) do
		autogen = pop_contains(args[1], code)
		if autogen or #args[code] > 0 then
			table.insert(verb_output, make_verb_row(args.lemma, code, autogen, args[code], args[code .. "-g"]))
		end
	end
	if #args.vrb > 0 then
		table.insert(verb_output, make_other_verb_row(args.vrb, args["vrb-g"]))
	end
	
	if #args[1] > 0 then
		error('Unrecognized Chichewa derivation type(s) "' .. table.concat(get_remaining(args[1]), '", "') .. '".')
	end
	
	if #verb_output > 0 then
		table.insert(verb_output, 1, "* '''[[Appendix:Chichewa verbal derivation|Verbal derivations]]''':")
		table.insert(output, table.concat(verb_output, "\n"))
	end
	
	local noun_output = {}
	for i, noun in pairs(args.nom) do
		table.insert(noun_output, make_noun_row(noun, args["nom-g"][i]))
	end
	
	if #noun_output > 0 then
		table.sort(noun_output)
		table.insert(noun_output, 1, "* '''[[Appendix:Chichewa verbal derivation#Nouns|Nominal derivations]]''':")
		table.insert(output, table.concat(noun_output, "\n"))
	end
	
	return table.concat(output, "\n\n")
end

function export.show(frame)
	local params = {
		--verbal derivatives
		[1] = {list = true},
		["appl"] = {list = true},
		["appl-g"] = {list = true, allow_holes=true},
		["caus"] = {list = true},
		["caus-g"] = {list = true, allow_holes=true},
		["conv"] = {list = true},
		["conv-g"] = {list = true, allow_holes=true},
		["pass"] = {list = true},
		["pass-g"] = {list = true, allow_holes=true},
		["recip"] = {list = true},
		["recip-g"] = {list = true, allow_holes=true},
		["redup"] = {list = true},
		["redup-g"] = {list = true, allow_holes=true},
		["stat"] = {list = true},
		["stat-g"] = {list = true, allow_holes=true},
		-- other verbal formations
		["vrb"] = {list = true},
		["vrb-g"] = {list = true, allow_holes=true},
		-- nominal derivatives
		["nom"] = {list = true},
		["nom-g"] = {list = true, allow_holes=true},
		-- other
		["lemma"] = {default = PAGENAME},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	return make_output(args)
end

return export