Jump to content

Module:txb-nominal

From Wiktionary, the free dictionary

local export = {}

--[=[

Authors: [[User:kc_kennylau]], [[User:Vampyricon]].
Inspired by [[Module:la-nominal]] by Ben Wing <benwing2>.

]=]

local lang = require("Module:languages").getByCode("txb")
local m_links = require("Module:links")

-- Internal substitutions
local encode = {
	["ā"] = "x",
	["ä"] = "z",
	--["ī"] = "f",
	--["ū"] = "q",
	["Ā"] = "X",
	["Ä"] = "Z",
	--["Ī"] = "F",
	--["Ū"] = "Q",
}
local decode = {
	x = "ā",
	z = "ä",
	--f = "ī",
	--q = "ū",
	X = "Ā",
	Z = "Ä",
	--F = "Ī",
	--Q = "Ū",
}
local function encodefunc(c)
	return encode[c]
end
local function decodefunc(c)
	return decode[c]
end

-- stressed variants of vowels
local stressed = {
	a = "x",
	z = "a",
	--i = "f",
	--u = "q",
	A = "X",
	Z = "A",
	--I = "F",
	--U = "Q",
}
local unstressed = {
	x = "a",
	a = "z",
	--f = "i",
	--q = "u",
	X = "A",
	A = "Z",
	--F = "I",
	--Q = "U",
}

local function palatalise(stem)
	stem = stem:gsub("[tks]$",{t="c",k="c",s="ś"})
	return stem
end

local function palatalize(stem)
	local pos = stem:len()
	while true do
		if stem:sub(pos,pos):match("[ntks]") then
			pos = pos-1 -- if character in pos is n/t/k/s, shift pos to the left by 1
		else
			break -- first character from the right that is not n/t/k/s, break
		end
	end
	-- before ..: first characters up to rightmost n/t/k/s chain unchanged
	-- after .. (chain of n/t/k/s from rightmost character): palatalized
	return stem:sub(1,pos)..stem:sub(pos+1,-1):gsub("[ntks]",{n="ñ",t="c",k="ś",s="ś"})
end

-- count the number of syllables in stem
function export.countsyll(stem)
	local count = 0
	stem = stem:gsub("[aeiouxzfq]+",function(vowel)
		count = count + 1
		return vowel
	end)
	return count
end

-- stress the N-th syllable of stem
function export.stressvar(stem,N)
	local count = 0
	stem = stem:gsub("[aeiouxzfq]+",function(vowel)
		local pos = stem:find(vowel)
		count = count + 1
		if count == N then
			return stressed[vowel] or vowel
		end
		if count == N-1 then
			if stem:sub(pos,pos) == "a" and (stem:sub(pos+2,pos+2):match("[aeiouxzfq]+") ~= nil or stem:sub(pos+2,pos+2) == "") and pos ~= 1 and not stem:sub(pos+1,pos+1):match("[iu]") then
				return ""
			elseif stem:sub(pos,pos+1) == "uw" and (stem:sub(pos+2,pos+2):match("[aeiouxzfq]+") ~= "" or stem:sub(pos+2,pos+2) == "") and pos ~= 1 then
				return ""
			-- elseif stem:sub(pos,pos+1) == "iy" and (stem:sub(pos+2,pos+2):match("[aeiouxzfq]+") ~= "" or stem:sub(pos+2,pos+2) == "") and pos ~= 1 then
			--	return "" -- no evidence for or against this deletion, but would make sense based on "uw"
			else
				return unstressed[vowel] or vowel
			end
		end
		return vowel
	end)
	return stem
end

local function unstress(stem)
	--stemcode = string.gsub(stemcode,"^(.+)uw(.+)$","^(.+)w(.+)$")
	--stemcode = string.gsub(stemcode,"^(.+)iy(.+)$","^(.+)y(.+)$")
	local a,b,c = stem:match("^(.+)([ax])(.+)$")
	return a and a..({a="z",x="a"})[b]..c or stem
end
local function stress(stem)
	local a,b,c = stem:match("^(.+)([az])(.+)$")
	return a and a..({a="x",z="a"})[b]..c or stem
end

local function combine(table1,table2)
	for key, value in pairs(table2) do
    	table1[key] = value
	end
	return table1
end

local patterns = {}

-- Class I.1; plurals in -(C)a
-- pikul: "pikul/pikwala<I.1>"
patterns["I.1"] = function(stem)
	local sg_stem, pl_stem = stem:match("^(.+)/(.+)$")
	local stem_syll = export.countsyll(sg_stem)
	if stem_syll == 1 then
		if sg_stem:sub(-1):match("o") then -- dubious since there are no I.1 -o monosyllables
			sg_stem = sg_stem:sub(1,-2)
			local plvr_stem = export.stressvar(pl_stem,2)
			return {
				["nom-sg"] = "*"..sg_stem.."o",
				["gen-sg"] = "*"..sg_stem.."āntse",
				["acc-sg"] = "*"..sg_stem.."a",
				["abl-sg"] = "*"..sg_stem.."āmeṃ",
				["nom-pl"] = "*"..pl_stem,
				["gen-pl"] = "*"..plvr_stem.."ṃts",
				["acc-pl"] = "*"..pl_stem,
				["abl-pl"] = "*"..plvr_stem.."meṃ",
			}
		elseif sg_stem:sub(-1):match("[lr]") then
			local sgvr_stem = export.stressvar(sg_stem,2)
			local plvr_stem = export.stressvar(pl_stem,2)
			return {
				["nom-sg"] = sg_stem,
				["gen-sg"] = "*"..sg_stem.."ntse",
				["acc-sg"] = sg_stem,
				["abl-sg"] = sg_stem.."meṃ",
				["nom-pl"] = pl_stem,
				["gen-pl"] = "*"..pl_stem.."ṃts",
				["acc-pl"] = pl_stem,
				["abl-pl"] = pl_stem.."meṃ",
			}
		else
			local sgvr_stem = export.stressvar(sg_stem,2)
			local plvr_stem = export.stressvar(pl_stem,2)
			return {
				["nom-sg"] = sg_stem,
				["gen-sg"] = "*"..sg_stem.."äntse",
				["acc-sg"] = sg_stem,
				["abl-sg"] = sg_stem.."meṃ",
				["nom-pl"] = pl_stem,
				["gen-pl"] = "*"..pl_stem.."äṃts",
				["acc-pl"] = pl_stem,
				["abl-pl"] = plvr_stem.."meṃ",
			}
		end
	elseif stem_syll == 2 then
		if sg_stem:sub(-1):match("o") then -- true for all vowels?
			sg_stem = sg_stem:sub(1,-2)
			local sgvr_stem = export.stressvar(sg_stem,2)
			local plvr_stem = export.stressvar(pl_stem,2)
			return {
				["nom-sg"] = sg_stem.."o",
				["gen-sg"] = sgvr_stem.."āntse",
				["acc-sg"] = sg_stem.."a",
				["abl-sg"] = sgvr_stem.."āmeṃ",
				["nom-pl"] = pl_stem,
				["gen-pl"] = plvr_stem.."ṃts",
				["acc-pl"] = pl_stem,
				["abl-pl"] = plvr_stem.."meṃ",
			}
		elseif sg_stem:sub(-1):match("[lr]") then
			local sgvr_stem = export.stressvar(sg_stem,2)
			local plvr_stem = export.stressvar(pl_stem,2)
			return {
				["nom-sg"] = sg_stem,
				["gen-sg"] = sgvr_stem.."ntse",
				["acc-sg"] = sg_stem,
				["abl-sg"] = sgvr_stem.."meṃ",
				["nom-pl"] = pl_stem,
				["gen-pl"] = pl_stem.."ṃts",
				["acc-pl"] = pl_stem,
				["abl-pl"] = pl_stem.."meṃ",
			}
		else
			local sgvr_stem = export.stressvar(sg_stem,2)
			local plvr_stem = export.stressvar(pl_stem,2)
			return {
				["nom-sg"] = "*"..sg_stem,
				["gen-sg"] = "*"..sgvr_stem.."äntse",
				["acc-sg"] = "*"..sg_stem,
				["abl-sg"] = "*"..sgvr_stem.."meṃ",
				["nom-pl"] = "*"..pl_stem,
				["gen-pl"] = "*"..plvr_stem.."äṃts",
				["acc-pl"] = "*"..pl_stem,
				["abl-pl"] = "*"..plvr_stem.."meṃ",
			}
		end
	elseif stem_syll == 3 and sg_stem:sub(-1):match("o") then
		sg_stem = sg_stem:sub(1,-2)
		return {
			["nom-sg"] = sg_stem.."o",
			["gen-sg"] = sg_stem.."antse",
			["acc-sg"] = sg_stem.."a",
			["abl-sg"] = sg_stem.."ameṃ",
			["nom-pl"] = pl_stem,
			["gen-pl"] = pl_stem.."ṃts",
			["acc-pl"] = pl_stem,
			["abl-pl"] = pl_stem.."meṃ",
		}
	else
		if sg_stem:sub(-1):match("o") then -- true for all vowels?
			sg_stem = sg_stem:sub(1,-2)
			return {
				["nom-sg"] = sg_stem.."o",
				["gen-sg"] = sg_stem.."äntse",
				["acc-sg"] = sg_stem.."a",
				["abl-sg"] = sg_stem.."ämeṃ",
				["nom-pl"] = pl_stem,
				["gen-pl"] = pl_stem.."äṃts",
				["acc-pl"] = pl_stem,
				["abl-pl"] = pl_stem.."meṃ",
			}
		elseif sg_stem:sub(-1):match("[lr]") then
			return {
				["nom-sg"] = sg_stem,
				["gen-sg"] = sg_stem.."ntse",
				["acc-sg"] = sg_stem,
				["abl-sg"] = sg_stem.."meṃ",
				["nom-pl"] = pl_stem,
				["gen-pl"] = pl_stem.."ṃts",
				["acc-pl"] = pl_stem,
				["abl-pl"] = pl_stem.."meṃ",
			}
		else
			return {
				["nom-sg"] = sg_stem,
				["gen-sg"] = sg_stem.."äntse",
				["acc-sg"] = sg_stem,
				["abl-sg"] = sg_stem.."meṃ",
				["nom-pl"] = pl_stem,
				["gen-pl"] = pl_stem.."äṃts",
				["acc-pl"] = pl_stem,
				["abl-pl"] = pl_stem.."meṃ",
			}
		end
	end
end

-- Class I.2; plurals in -wa
-- ost: "ost<I.2>"
-- EXCEPTIONS: or-ārwa
patterns["I.2"] = function(stem)
	local stem_syll = export.countsyll(stem)
	if stem_syll == 1 then
		if stem:sub(-1):match("n") then
			stem = stem:sub(1,-2)
			local vr_stem = export.stressvar(stem,2)
			return {
				["nom-sg"] = stem.."ṃ",
				["gen-sg"] = "*"..vr_stem.."nantse",
				["acc-sg"] = stem.."ṃ",
				["abl-sg"] = stem.."ṃmeṃ",
				["nom-pl"] = vr_stem.."nuwa",
				["gen-pl"] = "*"..vr_stem.."nwaṃts",
				["acc-pl"] = vr_stem.."nuwa",
				["abl-pl"] = vr_stem.."nuwameṃ",
			}
		else
			local vr_stem = export.stressvar(stem,2)
			return {
				["nom-sg"] = stem,
				["gen-sg"] = vr_stem.."antse",
				["acc-sg"] = stem,
				["abl-sg"] = stem.."meṃ",
				["nom-pl"] = vr_stem.."uwa",
				["gen-pl"] = vr_stem.."waṃts",
				["acc-pl"] = vr_stem.."uwa",
				["abl-pl"] = vr_stem.."uwameṃ",
			}
		end
	elseif stem_syll == 2 then
		if stem:sub(-1):match("n") then
			stem = stem:sub(1,-2)
			local vr_stem = export.stressvar(stem,2)
			return {
				["nom-sg"] = stem.."ṃ",
				["gen-sg"] = vr_stem.."nntse",
				["acc-sg"] = stem.."ṃ",
				["abl-sg"] = vr_stem.."ṃmeṃ",
				["nom-pl"] = vr_stem.."nwa",
				["gen-pl"] = vr_stem.."nwaṃts",
				["acc-pl"] = vr_stem.."nwa",
				["abl-pl"] = vr_stem.."nwameṃ",
			}
		elseif stem:sub(-1):match("[aeiouxzfq]") then
			local vr_stem = export.stressvar(stem,2)
			return {
				["nom-sg"] = stem,
				["gen-sg"] = vr_stem.."ntse",
				["acc-sg"] = stem,
				["abl-sg"] = vr_stem.."meṃ",
				["nom-pl"] = vr_stem:sub(1,-2).."wa",
				["gen-pl"] = vr_stem:sub(1,-2).."waṃts",
				["acc-pl"] = vr_stem:sub(1,-2).."wa",
				["abl-pl"] = vr_stem:sub(1,-2).."wameṃ",
			}
		else
			local vr_stem = export.stressvar(stem,2)
			return {
				["nom-sg"] = stem,
				["gen-sg"] = vr_stem.."ntse",
				["acc-sg"] = stem,
				["abl-sg"] = vr_stem.."meṃ",
				["nom-pl"] = vr_stem.."wa",
				["gen-pl"] = vr_stem.."waṃts",
				["acc-pl"] = vr_stem.."wa",
				["abl-pl"] = vr_stem.."wameṃ",
			}
		end
	else
		if stem:sub(-1):match("n") then
			stem = stem:sub(1,-2)
			return {
				["nom-sg"] = stem.."ṃ",
				["gen-sg"] = stem.."nntse",
				["acc-sg"] = stem.."ṃ",
				["abl-sg"] = stem.."ṃmeṃ",
				["nom-pl"] = stem.."nwa",
				["gen-pl"] = stem.."nwaṃts",
				["acc-pl"] = stem.."nwa",
				["abl-pl"] = stem.."nwameṃ",
			}
		elseif stem:sub(-1):match("[aeiouxzfq]") then
			return {
				["nom-sg"] = stem,
				["gen-sg"] = stem.."ntse",
				["acc-sg"] = stem,
				["abl-sg"] = stem.."meṃ",
				["nom-pl"] = stem:sub(1,-2).."wa",
				["gen-pl"] = stem:sub(1,-2).."waṃts",
				["acc-pl"] = stem:sub(1,-2).."wa",
				["abl-pl"] = stem:sub(1,-2).."wameṃ",
			}
		else
			return {
				["nom-sg"] = stem,
				["gen-sg"] = stem.."ntse",
				["acc-sg"] = stem,
				["abl-sg"] = stem.."meṃ",
				["nom-pl"] = stem.."wa",
				["gen-pl"] = stem.."waṃts",
				["acc-pl"] = stem.."wa",
				["abl-pl"] = stem.."wameṃ",
			}
		end
	end
end

-- Class II.1; plurals in -na
-- ñem: "ñem/ñemna<II.1>"
-- Enter ACC.SG/ACC.PL (= NOM.PL)
-- EXCEPTIONS: kliye, śaumo, stām.GEN.PL, lāntsa.GEN.SG
patterns["II.1"] = function(stem)
	local sg_stem, pl_stem = stem:match("^(.+)/(.+)$")
	local sg_stem_syll = export.countsyll(sg_stem)
	if sg_stem_syll == 1 then
		if sg_stem:sub(-1):match("n") then
			sg_stem = sg_stem:sub(1,-2)
			local vr_stem = export.stressvar(sg_stem,2)
			sg_table = {
				["nom-sg"] = sg_stem,
				["gen-sg"] = vr_stem.."antse",
				["acc-sg"] = sg_stem.."ṃ",
				["abl-sg"] = sg_stem.."ṃmeṃ",
			}
		else
			local vr_stem = export.stressvar(sg_stem,2)
			sg_table = {
				["nom-sg"] = sg_stem,
				["gen-sg"] = vr_stem.."antse",
				["acc-sg"] = sg_stem,
				["abl-sg"] = sg_stem.."meṃ",
			}
		end
	elseif sg_stem_syll == 2 then
		if sg_stem:sub(-1):match("n") then
			sg_stem = sg_stem:sub(1,-2)
			local vr_stem = export.stressvar(sg_stem,2)
			sg_table = {
				["nom-sg"] = sg_stem,
				["gen-sg"] = vr_stem.."ntse",
				["acc-sg"] = sg_stem.."ṃ",
				["abl-sg"] = vr_stem.."ṃmeṃ",
			}
		elseif sg_stem:sub(-1):match("a") then
			sg_stem = sg_stem:sub(1,-2)
			local vr_stem = export.stressvar(sg_stem,2)
			sg_table = {
				["nom-sg"] = sg_stem,
				["gen-sg"] = vr_stem.."ntse",
				["acc-sg"] = sg_stem.."i",
				["abl-sg"] = unstress(sg_stem).."imeṃ",
			}
		elseif sg_stem:sub(-1):match("o") then
			local vr_stem = export.stressvar(sg_stem,2)
			sg_stem = sg_stem:sub(1,-2)
			sg_table = {
				["nom-sg"] = sg_stem.."a",
				["gen-sg"] = vr_stem.."y",
				["acc-sg"] = sg_stem.."o",
				["abl-sg"] = vr_stem.."ymeṃ",
			}
		else
			local vr_stem = export.stressvar(sg_stem,2)
			sg_table = {
				["nom-sg"] = sg_stem,
				["gen-sg"] = vr_stem.."ntse",
				["acc-sg"] = sg_stem,
				["abl-sg"] = vr_stem.."meṃ",
			}
		end
	else
		if sg_stem:sub(-1):match("n") then
			sg_stem = sg_stem:sub(1,-2)
			sg_table = {
				["nom-sg"] = sg_stem,
				["gen-sg"] = sg_stem.."ntse",
				["acc-sg"] = sg_stem.."ṃ",
				["abl-sg"] = sg_stem.."ṃmeṃ",
			}
		elseif sg_stem:sub(-1):match("a") then
			sg_stem = sg_stem:sub(1,-2)
			sg_table = {
				["nom-sg"] = sg_stem,
				["gen-sg"] = sg_stem.."ntse",
				["acc-sg"] = sg_stem.."i",
				["abl-sg"] = sg_stem.."imeṃ",
			}
		elseif sg_stem:sub(-1):match("o") then
			sg_stem = sg_stem:sub(1,-2)
			sg_table = {
				["nom-sg"] = sg_stem.."a",
				["gen-sg"] = sg_stem.."y",
				["acc-sg"] = sg_stem.."o",
				["abl-sg"] = sg_stem.."ymeṃ",
			}
		else
			sg_table = {
				["nom-sg"] = sg_stem,
				["gen-sg"] = sg_stem.."ntse",
				["acc-sg"] = sg_stem,
				["abl-sg"] = sg_stem.."meṃ",
			}
		end
	end
	pl_table = {
		["nom-pl"] = pl_stem,
		["gen-pl"] = pl_stem.."ṃts",
		["acc-pl"] = pl_stem,
		["abl-pl"] = pl_stem.."meṃ",
	}
	return combine(sg_table,pl_table)
end

-- Class II.2; plurals in -nma
-- śaul: "śaul<II.2>"
patterns["II.2"] = function(stem)
	local stem_syll = export.countsyll(stem)
	if stem:sub(-1) == "n" then
		stem = stem:sub(1,-2)
		return {
			["nom-sg"] = stem.."ṃ",
			["gen-sg"] = stem.."ntse",
			["acc-sg"] = stem.."ṃ",
			["nom-pl"] = stem.."nma",
			["gen-pl"] = stem.."nmaṃts",
			["acc-pl"] = stem.."nma",
		}
	elseif stem:sub(-1) == "i" then
		local vr_stem = export.stressvar(stem:sub(1,-2),2)
		return {
			["nom-sg"] = stem,
			["gen-sg"] = vr_stem.."intse",
			["acc-sg"] = stem,
			["nom-pl"] = vr_stem.."anma",
			["gen-pl"] = vr_stem.."anmaṃts",
			["acc-pl"] = vr_stem.."anma",
		}
	elseif string.match(stem:sub(-1),"[aeiouxzfq]") then
		local vr_stem = export.stressvar(stem,2)
		return {
			["nom-sg"] = stem,
			["gen-sg"] = vr_stem.."ntse",
			["acc-sg"] = stem,
			["nom-pl"] = vr_stem.."nma",
			["gen-pl"] = vr_stem.."anmaṃts",
			["acc-pl"] = vr_stem.."nma",
		}
	elseif stem_syll == 1 or stem == "akṣxr" then
		local vr_stem = export.stressvar(stem,stem_syll+1)
		return {
			["nom-sg"] = stem,
			["gen-sg"] = vr_stem.."antse",
			["acc-sg"] = stem,
			["nom-pl"] = vr_stem.."anma",
			["gen-pl"] = vr_stem.."anmaṃts",
			["acc-pl"] = vr_stem.."anma",
		}
	else
		local vr_stem = export.stressvar(stem,2)
		return {
			["nom-sg"] = stem,
			["gen-sg"] = vr_stem.."ntse",
			["acc-sg"] = stem,
			["nom-pl"] = vr_stem.."nma",
			["gen-pl"] = vr_stem.."nmaṃts",
			["acc-pl"] = vr_stem.."nma",
		}
	end
end

-- Class III, plurals in -nta (Weiss separates this into -enta and -nta but afaict there is no difference in inflection?)
-- oko: "oko<III>"
-- EXCEPTIONS: mñcuṣke-mcuṣkanta, pratin-pratinta, pärkāᵤ-pärkāwänta
patterns["III"] = function(stem)
	local stem_syll = export.countsyll(stem)
	if stem_syll == 1 then
		local vr_stem = export.stressvar(stem,2)
		if stem:sub(-1) == "n" then
			stem = stem:sub(1,-2)
			return {
				["nom-sg"] = stem.."ṃ",
				["gen-sg"] = vr_stem.."nantse",
				["acc-sg"] = stem.."ṃ",
				["abl-sg"] = stem.."nmeṃ",
				["nom-pl"] = vr_stem.."nanta",
				["gen-pl"] = vr_stem.."nantaṃts",
				["acc-pl"] = vr_stem.."nanta",
				["abl-pl"] = vr_stem.."nantameṃ",
			}
		elseif string.match(stem:sub(-1),"[aeiouxzfq]") then
			return {
				["nom-sg"] = stem,
				["gen-sg"] = stem.."ntse",
				["acc-sg"] = stem,
				["abl-sg"] = stem.."meṃ",
				["nom-pl"] = stem.."nta",
				["gen-pl"] = stem.."ntaṃts",
				["acc-pl"] = stem.."nta",
				["abl-pl"] = stem.."ntameṃ",
			}
		else
			return {
				["nom-sg"] = stem,
				["gen-sg"] = vr_stem.."antse",
				["acc-sg"] = stem,
				["abl-sg"] = stem.."meṃ",
				["nom-pl"] = vr_stem.."anta",
				["gen-pl"] = vr_stem.."antaṃts",
				["acc-pl"] = vr_stem.."anta",
				["abl-pl"] = vr_stem.."antameṃ",
			}
		end
	elseif stem_syll == 2 then
		if stem:sub(-1) == "n" then
			stem = stem:sub(1,-2)
			return {
				["nom-sg"] = stem.."ṃ",
				["gen-sg"] = stem.."näntse",
				["acc-sg"] = stem.."ṃ",
				["nom-pl"] = stem.."nänta",
				["gen-pl"] = stem.."näntaṃts",
				["acc-pl"] = stem.."nänta",
			}
		elseif string.match(stem:sub(-1),"[aeiouxzfq]") then
			local vr_stem = export.stressvar(stem,2)
			return {
				["nom-sg"] = stem,
				["gen-sg"] = vr_stem.."ntse",
				["acc-sg"] = stem,
				["nom-pl"] = vr_stem.."nta",
				["gen-pl"] = vr_stem.."ntaṃts",
				["acc-pl"] = vr_stem.."nta",
			}
		elseif stem:sub(-1) == "r" then
			return {
				["nom-sg"] = stem,
				["gen-sg"] = stem.."ntse",
				["acc-sg"] = stem,
				["nom-pl"] = stem.."nta",
				["gen-pl"] = stem.."ntaṃts",
				["acc-pl"] = stem.."nta",
			}
		else
			return {
				["nom-sg"] = stem,
				["gen-sg"] = stem.."äntse",
				["acc-sg"] = stem,
				["nom-pl"] = stem.."änta",
				["gen-pl"] = stem.."äntaṃts",
				["acc-pl"] = stem.."änta",
			}
		end
	else
		if string.match(stem:sub(-1),"[aeiouxzfq]") or stem:sub(-1) == "r" then
			return {
				["nom-sg"] = stem,
				["gen-sg"] = stem.."ntse",
				["acc-sg"] = stem,
				["nom-pl"] = stem.."nta",
				["gen-pl"] = stem.."ntaṃts",
				["acc-pl"] = stem.."nta",
			}
		elseif stem:sub(-1) == "n" then
			stem = stem:sub(1,-2)
			return {
				["nom-sg"] = stem.."ṃ",
				["gen-sg"] = stem.."näntse",
				["acc-sg"] = stem.."ṃ",
				["nom-pl"] = stem.."nänta",
				["gen-pl"] = stem.."näntaṃts",
				["acc-pl"] = stem.."nänta",
			}
		else
			return {
				["nom-sg"] = stem,
				["gen-sg"] = stem.."äntse",
				["acc-sg"] = stem,
				["nom-pl"] = stem.."änta",
				["gen-pl"] = stem.."äntaṃts",
				["acc-pl"] = stem.."änta",
			}
		end
	end
end

-- Class IV.1: familial terms
-- pācer: "protär<IV.1>"
-- Enter ACC.SG
patterns["IV.1"] = function(stem)
	stem = stem:sub(1,-3)
	local yd_stem = palatalise(stem)
	local vr_stem = export.stressvar(yd_stem,2)
	return {
		["nom-sg"] = yd_stem.."er",
		["gen-sg"] = stem.."ri",
		["acc-sg"] = stem.."är",
		["nom-pl"] = vr_stem.."era",
		["gen-pl"] = stem.."ärñts",
		["acc-pl"] = vr_stem.."era",
	}
end

-- Class IV.2: pācer, tkācer
-- pācer: "pātär<IV.2>"
-- Enter ACC.SG
patterns["IV.2"] = function(stem)
	stem = stem:sub(1,-3)
	local yd_stem = palatalise(stem)
	local vr_stem = export.stressvar(yd_stem,2)
	return {
		["nom-sg"] = yd_stem.."er",
		["gen-sg"] = stem.."ri",
		["acc-sg"] = stem.."är",
		["nom-pl"] = {vr_stem.."era", stem.."ärñ"},
		["gen-pl"] = stem.."ärnts",
		["acc-pl"] = {vr_stem.."era", stem.."ärñ"},
	}
end

-- Class V.1: nom.pl in -i
-- eṅkwe: "eṅkwen<V.1>"
-- Enter ACC.SG
-- EXCEPTIONS: kertte-kercci
-- no monosyllables?
patterns["V.1"] = function(stem)
	local stem_syll = export.countsyll(stem)
	if stem_syll == 2 then
		local vr_stem = export.stressvar(stem,2)
		if stem:sub(-1) == "n" then
			stem = stem:sub(1,-2)
			return {
				["nom-sg"] = stem,
				["gen-sg"] = vr_stem.."ntse",
				["acc-sg"] = stem.."ṃ",
				["abl-sg"] = stem.."ṃmeṃ",
				["nom-pl"] = stem:sub(1,-2).."i",
				["gen-pl"] = stem.."ṃts",
				["acc-pl"] = stem.."ṃ",
				["abl-pl"] = stem.."ṃmeṃ",
			}
		elseif string.match(stem:sub(-1),"e") then
			return {
				["nom-sg"] = stem,
				["gen-sg"] = vr_stem.."ntse",
				["acc-sg"] = stem,
				["abl-sg"] = vr_stem.."meṃ",
				["nom-pl"] = stem:sub(1,-2).."i",
				["gen-pl"] = stem.."ṃts",
				["acc-pl"] = stem.."ṃ",
				["abl-pl"] = vr_stem.."ṃmeṃ",
			}
		else
			return {}
		end
	else
		if stem:sub(-1) == "n" then
			stem = stem:sub(1,-2)
			return {
				["nom-sg"] = stem,
				["gen-sg"] = stem.."ntse",
				["acc-sg"] = stem.."ṃ",
				["abl-sg"] = stem.."ṃmeṃ",
				["nom-pl"] = stem:sub(1,-2).."i",
				["gen-pl"] = stem.."ṃts",
				["acc-pl"] = stem.."ṃ",
				["abl-pl"] = stem.."ṃmeṃ",
			}
		elseif string.match(stem:sub(-1),"e") then
			return {
				["nom-sg"] = stem,
				["gen-sg"] = stem.."ntse",
				["acc-sg"] = stem,
				["abl-sg"] = stem.."meṃ",
				["nom-pl"] = stem:sub(1,-2).."i",
				["gen-pl"] = stem.."ṃts",
				["acc-pl"] = stem.."ṃ",
				["abl-pl"] = stem.."ṃmeṃ",
			}
		else
			return {}
		end
	end
end

-- Class V.2
-- plāce: "plātän<V.2>"
-- Enter ACC.PL
-- NOTES: GEN unclear but all other cases have palatalized stems
patterns["V.2"] = function(stem)
	local stem_syll = export.countsyll(stem)
	stem = stem:sub(1,-3)
	local yd_stem = palatalize(stem)
	if stem_syll == 2 then
		local vr_stem = export.stressvar(yd_stem,2)
		return {
			["nom-sg"] = yd_stem.."e",
			["gen-sg"] = vr_stem.."antse",
			["acc-sg"] = yd_stem,
			["abl-sg"] = yd_stem.."meṃ",
			["nom-pl"] = yd_stem.."i",
			["gen-pl"] = yd_stem.."aṃts",
			["acc-pl"] = stem.."äṃ",
			["abl-pl"] = vr_stem.."aṃmeṃ",
		}
	else
		return {
			["nom-sg"] = yd_stem.."e",
			["gen-sg"] = yd_stem.."äntse",
			["acc-sg"] = yd_stem,
			["abl-sg"] = yd_stem.."meṃ",
			["nom-pl"] = yd_stem.."i",
			["gen-pl"] = yd_stem.."aṃts",
			["acc-pl"] = stem.."äṃ",
			["abl-pl"] = yd_stem.."äṃmeṃ",
		}
	end
end

-- Class V.3
-- lyak: "lyak<V.3>"
-- EXCEPTIONS: sāṃ.ACC: sanaṃ
patterns["V.3"] = function(stem)
	local stem_syll = export.countsyll(stem)
	if stem_syll == 1 then
		if stem:sub(-1) == "n" then
			local vr_stem = export.stressvar(stem,2)
			local yd_stem = palatalize(vr_stem)
			stem = stem:sub(1,-2)
			vr_stem = vr_stem:sub(1,-2)
			if stem:find("x") then
				return {
					["nom-sg"] = stem.."ṃ",
					["gen-sg"] = vr_stem.."nantse",
					["acc-sg"] = vr_stem.."naṃ",
					["abl-sg"] = vr_stem.."naṃmeṃ",
					["nom-pl"] = yd_stem.."i",
					["gen-pl"] = vr_stem.."naṃts",
					["acc-pl"] = vr_stem.."naṃ",
					["abl-pl"] = vr_stem.."naṃmeṃ",
				}
			else
				return {
					["nom-sg"] = stem.."ṃ",
					["gen-sg"] = vr_stem.."nantse",
					["acc-sg"] = stem.."ṃ",
					["abl-sg"] = vr_stem.."ṃmeṃ",
					["nom-pl"] = yd_stem.."i",
					["gen-pl"] = vr_stem.."naṃts",
					["acc-pl"] = vr_stem.."ṃ",
					["abl-pl"] = vr_stem.."ṃmeṃ",
				}
			end
		else
			local vr_stem = export.stressvar(stem,2)
			local yd_stem = palatalize(vr_stem)
			return {
				["nom-sg"] = stem,
				["gen-sg"] = vr_stem.."antse",
				["acc-sg"] = vr_stem.."aṃ",
				["abl-sg"] = vr_stem.."aṃmeṃ",
				["nom-pl"] = yd_stem.."i",
				["gen-pl"] = vr_stem.."aṃts",
				["acc-pl"] = vr_stem.."aṃ",
				["abl-pl"] = vr_stem.."aṃmeṃ",
			}
		end
	elseif stem_syll == 2 then
		if stem:sub(-1) == "n" then
			local vr_stem = export.stressvar(stem,2)
			local yd_stem = palatalize(vr_stem)
			stem = stem:sub(1,-2)
			vr_stem = vr_stem:sub(1,-2)
			if stem:find("x") then
				return {
					["nom-sg"] = stem.."ṃ",
					["gen-sg"] = vr_stem.."näntse",
					["acc-sg"] = vr_stem.."näṃ",
					["abl-sg"] = vr_stem.."näṃmeṃ",
					["nom-pl"] = yd_stem.."i",
					["gen-pl"] = vr_stem.."näṃts",
					["acc-pl"] = vr_stem.."näṃ",
					["abl-pl"] = vr_stem.."näṃmeṃ",
				}
			else
				return {
					["nom-sg"] = stem.."ṃ",
					["gen-sg"] = vr_stem.."näntse",
					["acc-sg"] = vr_stem.."ṃ",
					["abl-sg"] = vr_stem.."ṃmeṃ",
					["nom-pl"] = yd_stem.."i",
					["gen-pl"] = vr_stem.."naṃts",
					["acc-pl"] = vr_stem.."ṃ",
					["abl-pl"] = vr_stem.."ṃmeṃ",
				}
			end
		else
			local vr_stem = export.stressvar(stem,2)
			local yd_stem = palatalize(vr_stem)
			return {
				["nom-sg"] = stem,
				["gen-sg"] = vr_stem.."äntse",
				["acc-sg"] = vr_stem.."äṃ",
				["abl-sg"] = vr_stem.."äṃmeṃ",
				["nom-pl"] = yd_stem.."i",
				["gen-pl"] = vr_stem.."äṃts",
				["acc-pl"] = vr_stem.."äṃ",
				["abl-pl"] = vr_stem.."äṃmeṃ",
			}
		end
	else
		if stem:sub(-1) == "n" then
			local yd_stem = palatalize(stem)
			stem = stem:sub(1,-2)
			if stem:find("x") then
				return {
					["nom-sg"] = stem.."ṃ",
					["gen-sg"] = stem.."näntse",
					["acc-sg"] = stem.."näṃ",
					["abl-sg"] = stem.."näṃmeṃ",
					["nom-pl"] = yd_stem.."i",
					["gen-pl"] = stem.."näṃts",
					["acc-pl"] = stem.."näṃ",
					["abl-pl"] = stem.."näṃmeṃ",
				}
			else
				return {
					["nom-sg"] = stem.."ṃ",
					["gen-sg"] = stem.."näntse",
					["acc-sg"] = stem.."ṃ",
					["abl-sg"] = stem.."ṃmeṃ",
					["nom-pl"] = yd_stem.."i",
					["gen-pl"] = stem.."naṃts",
					["acc-pl"] = stem.."ṃ",
					["abl-pl"] = stem.."ṃmeṃ",
				}
			end
		else
			local yd_stem = palatalize(stem)
			return {
				["nom-sg"] = stem,
				["gen-sg"] = stem.."äntse",
				["acc-sg"] = stem.."äṃ",
				["abl-sg"] = stem.."äṃmeṃ",
				["nom-pl"] = yd_stem.."i",
				["gen-pl"] = stem.."äṃts",
				["acc-pl"] = stem.."äṃ",
				["abl-pl"] = stem.."äṃmeṃ",
			}
		end
	end
end

-- IRREGULARS
patterns["irreg"] = function(stem)
	local nom_sg,gen_sg,acc_sg,nom_pl,gen_pl,acc_pl = stem:match("([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)")
	return {
		["nom-sg"] = nom_sg,
		["gen-sg"] = gen_sg,
		["acc-sg"] = acc_sg,
		["nom-pl"] = nom_pl,
		["gen-pl"] = gen_pl,
		["acc-pl"] = acc_pl,
	}
end

local function make_links(forms)
	for key,val in pairs(forms) do
		if type(val) == "string" then
			val = {val}
		end
		for i,form in ipairs(val) do
			form = form:gsub("[xzXZ]",decode)
			val[i] = m_links.full_link({lang=lang, term=form})
		end
		forms[key] = table.concat(val," / ")
	end
	return true
end

local function make_table(forms)
	local stylesheet = require("Module:TemplateStyles")("Template:txb-nominal/style.css")
	return stylesheet .. '\n' .. ([=[
{| class="wikitable inflection-table inflection-table-txb"
|-
! class="corner-header" | Case
! class="number-header" | Singular
! class="number-header" | Plural
|-
! class="case-header" | [[nominative case|Nominative]]
| class="form-cell" | {{{nom-sg}}}
| class="form-cell" | {{{nom-pl}}}
|-
! class="case-header" | [[genitive case|Genitive]]
| class="form-cell" | {{{gen-sg}}}
| class="form-cell" | {{{gen-pl}}}
|-
! class="case-header" | [[accusative case|Accusative]]
| class="form-cell" | {{{acc-sg}}}
| class="form-cell" | {{{acc-pl}}}
|-
! class="case-header" | [[ablative case|Ablative]]
| class="form-cell" | {{{abl-sg}}}
| class="form-cell" | {{{abl-pl}}}
|}]=]):gsub("{{{([^{}]+)}}}", forms)
end

function export.show(frame)
	local stem_typ = frame:getParent().args[1]
	stem_typ = stem_typ:gsub("[\192-\223][\128-\191]",encode)
	local stem,typ = stem_typ:match("^(.+)<(.+)>$")
	local forms = patterns[typ](stem)
	make_links(forms)
	return make_table(forms)
end

return export