Module:izh-decl

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


local export = {}
local m_izh = require("Module:izh")

local function get_stem(word, ending)
	if mw.ustring.match(word, ending .. "$") then
		return word:sub(1, #word - #ending)
	end
	error("Unexpected ending for this inflection type! Wrong type?")
end

local function elongate(stem, condition)
	if condition == nil or condition then
		-- already long?
		if mw.ustring.match(stem, m_izh.vowel .. m_izh.vowel .. "$") then
			for k, v in pairs(m_izh.vowel_sequences) do
				if mw.ustring.find(stem, v .. "$", pos) then
					return stem
				end
			end
		end
		return stem .. mw.ustring.sub(stem, -1)
	else
		return stem
	end
end

local function frontalize(w, vh)
	if vh == "ä" then
		w = mw.ustring.gsub(w, "[aou]", { a = "ä", o = "ö", u = "y" })
	end
	return w
end

local function geminate(c)
	if mw.ustring.match(c, m_izh.consonant .. m_izh.consonant .. "$") then
		return c
	end
	return c .. mw.ustring.sub(c, -1)
end

local function join(...)
	local t = {}
	for _, s in ipairs({...}) do
		if type(s) == "table" then
			for _, v in ipairs(s) do
				table.insert(t, v)
			end
		else
			table.insert(t, s)
		end
	end
	return t
end

local function append(t, x)
	if not x then return t end
	if type(t) == "string" then
		return t .. x
	end
	local r = {}
	for _, v in ipairs(t) do
		table.insert(r, v .. x)
	end
	return r
end

local function elongate_all(t)
	if type(t) == "string" then
		return elongate(t)
	end
	local r = {}
	for _, v in ipairs(t) do
		table.insert(r, elongate(v))
	end
	return r
end

local function make_gradation(s, w)
	if s == w then
		return "no gradation"
	else
		return s .. "-" .. w .. " gradation"
	end
end

local function get_geminated(data, w, fallback)
	if data.geminate == false then return fallback end
	if data.headword then
		local gemprefix = mw.ustring.len(data.title) - mw.ustring.len(data.headword)
		if gemprefix > 0 then
			local wp = mw.ustring.sub(w, 1, gemprefix)
			local wg = m_izh.guess_gemination(mw.ustring.sub(w, gemprefix + 1))
			if not wg then return fallback end
			return wp .. wg
		end
	end
	return m_izh.guess_gemination(w) or fallback
end

local function get_elongation(data, w)
	if data.elongate == false then return false end
	if data.headword then
		local gemprefix = mw.ustring.len(data.title) - mw.ustring.len(data.headword)
		if gemprefix > 0 then
			return m_izh.guess_elongation(mw.ustring.sub(w, gemprefix + 1))
		end
	end
	return m_izh.guess_elongation(w)
end

local function process(data, result, vh)
	local vh = data.vh
	-- genitive singular/plural stem
	local gs = result.stem_gs
	local gp = result.stem_gp
	-- partitive singular/plural stem
	local ps = result.stem_ps
	local pp = result.stem_pp
	-- oblique plural stem
	local op = result.stem_op or pp
	-- illative singular/plural stem
	local is = result.stem_is or gs
	local ip = result.stem_ip or op
	-- allative/inessive singular/plural stem
	local as = result.stem_as or gs
	local ap = result.stem_ap or op
	-- essive singular/plural stem
	local es = result.stem_es or is
	local ep = result.stem_ep or ip
	-- exessive singular/plural stem
	local xs = result.stem_xs or (result.exe_weak and gs or es)
	local xp = result.stem_xp or (result.exe_weak and op or ep)

	if not data.no_singular then
		result["nom_sg"] = { data.title }
		result["gen_sg"] = append(gs, "n")
		result["par_sg"] = append(ps, vh)
		if result.par_short_ok then
			result["par_sg"] = join(result["par_sg"], ps)
		end
		if result.ill_short or result.ill_short_sg then
			result["ill_sg"] = append(is, result.ill_ending or result.ill_ending_sg or nil)
		else
			result["ill_sg"] = append(elongate_all(is), result.ill_ending or result.ill_ending_sg or nil)
		end
		result["ine_sg"] = append(as, "s")
		result["ela_sg"] = append(gs, "st")
		result["all_sg"] = append(gs, "lle")
		result["ade_sg"] = append(as, "l")
		result["abl_sg"] = append(gs, "lt")
		result["tra_sg"] = append(gs, "ks")
		result["ess_sg"] = join(append(es, "nn" .. vh), append(elongate_all(is), "n"))
		result["exe_sg"] = append(xs, "nt")
	end

	if not data.no_plural then
		result["nom_pl"] = append(gs, "t")
		result["gen_pl"] = append(gp, "n")
		result["par_pl"] = append(pp, vh)
		if result.ill_short then
			result["ill_pl"] = append(ip, result.ill_ending or result.ill_ending_pl or nil)
		else
			result["ill_pl"] = append(elongate_all(ip), result.ill_ending or result.ill_ending_pl or nil)
		end
		result["ine_pl"] = append(ap, "s")
		result["ela_pl"] = append(op, "st")
		result["all_pl"] = append(op, "lle")
		result["ade_pl"] = append(ap, "l")
		result["abl_pl"] = append(op, "lt")
		result["tra_pl"] = append(op, "ks")
		result["ess_pl"] = join(append(ep, "nn" .. vh), append(elongate_all(ip), "n"))
		result["exe_pl"] = append(xp, "nt")
	end

	return result
end

local function guess_illative_weight(stem)
	local syllables = m_izh.split_syllables(stem)
	return #syllables % 2 == 1 and mw.ustring.find(stem, m_izh.vowel .. m_izh.vowel .. "$") and mw.ustring.sub(stem, -2, -2) ~= mw.ustring.sub(stem, -1, -1)
end

local function generate_hv_illative_forms(result, data, stems, weights, ending, short)
	local outputs = {}

	for i, stem in ipairs(stems) do
		local weight = weights[i]

		if not short then
			stem = elongate(stem)
		end

		if weight == nil then
			local guess = stem
			if data.headword then
				guess = mw.ustring.sub(guess, 1 + mw.ustring.len(data.title) - mw.ustring.len(data.headword))
			end
			weight = guess_illative_weight(guess)
		end

		if weight then
			table.insert(outputs, stem .. ending)
		else
			table.insert(outputs, stem)
		end
	end

	return outputs
end

-- -hV illative ending is only sometimes evident
local function generate_hv_illative(result, data, stem_s, weight_s, stem_p, weight_p)
	if not data.no_singular and stem_s then
		result["ill_sg"] = generate_hv_illative_forms(result, data, stem_s, weight_s, result.ill_ending_sg or result.ill_ending or "", result.ill_short_sg or result.ill_short)
	end

	if not data.no_plural and stem_p then
		result["ill_pl"] = generate_hv_illative_forms(result, data, stem_p, weight_p, result.ill_ending_pl or result.ill_ending or "", result.ill_short)
	end

	return result
end

-- inflection classes begin
local inflections = {}

inflections["kärpäin"] = function (data)
	local result = { typeno = "1" }
	local word = data.title
	if data.no_singular then -- plural title -> singular
		word = get_stem(word, "iset") .. "in"
	end
	local stem = get_stem(word, "in")
	local sgem = ""
	if get_geminated(data, stem .. "ise", nil) then
		sgem = "s"
	end

	result.stem_gs = stem .. "ise"
	result.stem_ps = stem .. "ist"
	result.stem_as = stem .. "is" .. sgem .. "ee"

	result.stem_gp = stem .. "is" .. sgem .. "ii"
	result.stem_pp = stem .. "is" .. sgem .. "i"
	result.stem_ap = stem .. "is" .. sgem .. "ii"

	result.par_short_ok = true
	result.exe_weak = true

	return process(data, result)
end

inflections["jokahiin"] = function (data)
	local result = { typeno = "1" }
	local word = data.title
	if data.no_singular then -- plural title -> singular
		word = get_stem(word, "iset") .. "iin"
	end
	local stem = get_stem(word, "iin")
	if ((data.geminate == true and mw.ustring.match(stem, "m$")) or (data.geminate ~= false and mw.ustring.match(stem, "mm$"))) then
		stem = get_stem(stem, "m")
	end
	local sgem = ""
	if get_geminated(data, stem .. "ise", nil) then
		sgem = "s"
	end

	result.stem_gs = stem .. "ise"
	result.stem_ps = stem .. "ist"
	result.stem_is = stem .. "is" .. sgem .. "e"
	result.stem_es = stem .. "ise"

	result.stem_gp = stem .. "is" .. sgem .. "ii"
	result.stem_pp = stem .. "is" .. sgem .. "i"
	result.stem_op = stem .. "isi"
	result.stem_ip = stem .. "is" .. sgem .. "i"
	result.stem_ep = stem .. "isi"

	result.par_short_ok = true

	return process(data, result)
end

inflections["kolmas"] = function (data)
	local result = { typeno = "2" }
	local word = data.title
	if data.no_singular then -- plural title -> singular
		word = get_stem(word, "nnet") .. "s"
	end
	local stem = get_stem(word, "s")

	result.stem_gs = stem .. "nne"
	result.stem_ps = stem .. "tt"
	result.stem_is = stem .. "nte"
	result.stem_as = stem .. "nnee"

	result.stem_gp = stem .. "nsii"
	result.stem_pp = stem .. "nsi"
	result.stem_ip = stem .. "nsii"
	result.stem_ap = stem .. "nsii"

	result.exe_weak = true
	
	return process(data, result)
end

inflections["mees"] = function (data)
	local result = { typeno = "2" }
	local word = data.title
	if data.no_singular then -- plural title -> singular
		word = get_stem(word, "het") .. "s"
	end
	local stem = get_stem(word, "s")

	result.stem_gs = stem .. "he"
	result.stem_ps = stem .. "st"
	result.stem_as = stem .. "hee"

	result.stem_gp = stem .. "hii"
	result.stem_pp = stem .. "hi"
	result.stem_ap = stem .. "hii"

	result.par_short_ok = true
	result.exe_weak = true

	return process(data, result)
end

inflections["patsas"] = function (data)
	local result = { typeno = "2" }
	local word = data.title
	local weak = data.args[1] or error("must specify weak grade")
	local strong = data.args[2] or error("must specify strong grade")
	if data.no_singular then -- plural title -> singular
		-- does not have to be perfect, plural forms being right is enough
		word = get_stem(word, "t")
		local final = mw.ustring.sub(word, -1, -1)
		word = get_stem(mw.ustring.sub(word, 1, -3), strong) .. weak .. final .. "s"
	end
	local final = mw.ustring.sub(word, -2, -2)
	if not mw.ustring.match(final, m_izh.vowel) then
		error("invalid penultimate character for tytär-type nominals (should be a vowel)")
	end
	local stem = get_stem(word, weak .. final .. "s")
	local longfinal = final .. final
	local gem = get_geminated(data, stem .. strong .. final, stem .. strong)

	result.stem_gs = gem .. longfinal
	result.stem_ps = stem .. weak .. final .. "st"
	result.stem_is = gem .. longfinal

	result.stem_gp = gem .. final .. "i"
	result.stem_pp = { result.stem_gp .. "t", stem .. strong .. final .. "j" }
	result.stem_op = result.stem_gp

	result.par_short_ok = true
	result.ill_ending = "sse"
	result.exe_weak = true

	result.grade = make_gradation(strong, weak)
	result.geminate = gem ~= stem .. strong
	return process(data, result)
end

inflections["petos"] = function (data)
	local result = { typeno = "2" }
	local word = data.title
	if data.no_singular then -- plural title -> singular
		word = get_stem(word, "kset") .. "s"
	end
	local stem = get_stem(word, "s")

	result.stem_gs = stem .. "kse"
	result.stem_ps = stem .. "st"
	result.stem_as = stem .. "ksee"

	result.stem_gp = stem .. "ksii"
	result.stem_pp = stem .. "ksi"
	result.stem_ap = stem .. "ksii"

	result.par_short_ok = true
	result.exe_weak = true

	return process(data, result)
end

inflections["oikehus"] = function (data)
	local result = { typeno = "2" }
	local word = data.title
	if data.no_singular then -- plural title -> singular
		word = get_stem(word, "et") .. "s"
	end
	local stem = get_stem(word, "s")
	local long = get_elongation(data, stem .. "e")

	result.stem_gs = stem .. "e"
	result.stem_ps = stem .. "tt"
	result.stem_as = elongate(result.stem_gs, long)

	result.stem_gp = stem .. "ksii"
	result.stem_pp = stem .. "ksi"
	result.stem_ap = stem .. "ksii"

	result.exe_weak = true

	return process(data, result)
end

inflections["kana"] = function (data)
	local result = { typeno = "3" }
	local word = data.title
	local strong = data.args[1] or error("must specify strong grade")
	local weak = data.args[2] or error("must specify weak grade")
	local vh = data.vh
	if data.no_singular then -- plural title -> singular
		word = get_stem(word, weak .. vh .. "t") .. strong .. vh
	end
	local stem = get_stem(word, strong .. vh)
	local gem = get_geminated(data, word, stem .. strong)
	local long = get_elongation(data, stem .. weak .. vh)

	result.stem_gs = stem .. weak .. vh
	result.stem_ps = gem .. vh
	result.stem_is = gem .. vh
	result.stem_as = elongate(stem .. weak .. vh, long)
	result.stem_es = stem .. strong .. vh

	result.stem_gp = gem .. frontalize("oi", vh)
	result.stem_pp = stem .. strong .. frontalize("oj", vh)
	result.stem_ip = result.stem_gp
	result.stem_op = stem .. weak .. frontalize("oi", vh)
	result.stem_ep = stem .. strong .. frontalize("oi", vh)

	result.grade = make_gradation(strong, weak)
	result.geminate = gem ~= stem .. strong

	result.ill_ending_pl = "he"
	return generate_hv_illative(process(data, result), data,
								nil, nil,
							    { result.stem_ip }, { nil })
end

inflections["koira"] = function (data)
	local result = { typeno = "3" }
	local word = data.title
	local strong = data.args[1] or error("must specify strong grade")
	local weak = data.args[2] or error("must specify weak grade")
	local vh = data.vh
	if data.no_singular then -- plural title -> singular
		-- does not have to be perfect, plural forms being right is enough
		word = get_stem(word, weak .. vh .. "t") .. strong .. vh
	end
	local stem
	if mw.ustring.match(word, vh .. "$") then
		stem = get_stem(word, strong .. vh)
	else
		stem = get_stem(word, strong)
	end
	local gem = get_geminated(data, word, stem .. strong)
	local long = get_elongation(data, stem .. weak .. vh)

	result.stem_gs = stem .. weak .. vh
	result.stem_ps = gem .. vh
	result.stem_is = result.stem_ps
	result.stem_as = elongate(result.stem_gs, long)
	result.stem_es = stem .. strong .. vh

	result.stem_gp = gem .. "ii"
	result.stem_pp = gem .. "i"
	result.stem_op = stem .. weak .. "i"
	result.stem_ip = result.stem_pp
	result.stem_ap = elongate(stem .. weak .. "i", long)
	result.stem_ep = stem .. strong .. "i"

	result.grade = make_gradation(strong, weak)
	result.geminate = gem ~= stem .. strong
	return process(data, result)
end

inflections["koivu"] = function (data)
	local result = { typeno = "4" }
	local word = data.title
	local strong = data.args[1] or error("must specify strong grade")
	local weak = data.args[2] or error("must specify weak grade")
	local vh = data.vh
	if data.no_singular then -- plural title -> singular
		-- does not have to be perfect, plural forms being right is enough
		word = get_stem(word, "t")
		local final = mw.ustring.sub(word, -1, -1)
		word = get_stem(mw.ustring.sub(word, 1, -2), weak) .. strong .. final
	end
	local final = mw.ustring.sub(word, -1, -1)
	local stem = get_stem(word, strong .. final)
	local gem = get_geminated(data, word, stem .. strong)
	local long = get_elongation(data, stem .. weak .. final)

	result.stem_gs = stem .. weak .. final
	result.stem_ps = gem .. final
	result.stem_is = result.stem_ps
	result.stem_as = elongate(stem .. weak .. final, long)
	result.stem_es = stem .. strong .. final

	result.stem_gp = { gem .. final .. "i", stem .. strong .. final .. frontalize("loi", vh) }
	result.stem_pp = { stem .. strong .. final .. "j", stem .. strong .. final .. frontalize("loj", vh) }
	result.stem_op = { stem .. weak .. final .. "i", stem .. strong .. final .. frontalize("loi", vh) }
	result.stem_ip = result.stem_gp
	result.stem_ap = result.stem_op
	result.stem_ep = { stem .. strong .. final .. "i", stem .. strong .. final .. frontalize("loi", vh) }

	result.ill_ending_pl = "he"

	result.grade = make_gradation(strong, weak)
	result.geminate = gem ~= stem .. strong
	return generate_hv_illative(process(data, result), data,
								nil, nil,
								result.stem_ip, { nil, true })
end

inflections["keeli"] = function (data)
	local result = { typeno = "5" }
	local word = data.title
	local vh = data.vh
	if data.no_singular then -- plural title -> singular
		-- does not have to be perfect, plural forms being right is enough
		word = get_stem(word, "et") .. "i"
	end
	local stem, final
	if mw.ustring.match(word, "i$") then
		stem, final = mw.ustring.match(word, "(.-)(" .. m_izh.consonant .. "+)i$")
	elseif mw.ustring.match(word, m_izh.consonant .. "$") then
		stem, final = mw.ustring.match(word, "(.-)(" .. m_izh.consonant .. "+)$")
	else
		error("keeli-type nominals may only end in i or a consonant!")
	end
	if not final then
		error("keeli-type, but has no consonant?")
	end
	local base = stem .. final
	local gem = get_geminated(data, base .. "e", base)
	local long = get_elongation(data, base .. "e")

	result.stem_gs = stem .. final .. "e"
	if mw.ustring.match(final, "[kpt]s") then
		result.stem_ps = stem .. "st"
	elseif final == "m" then
		result.stem_ps = stem .. "nt"
	else
		result.stem_ps = base .. "t"
	end
	result.stem_is = gem .. "e"
	result.stem_as = long and gem .. "ee" or base .. "e"
	result.stem_es = base .. "e"

	result.stem_gp = { gem .. "ii", base .. frontalize("iloi", vh) }
	result.stem_pp = { gem .. "i", base .. frontalize("iloj", vh) }
	result.stem_op = { base .. "i", base .. frontalize("iloi", vh) }
	result.stem_ip = { gem .. "i", base .. frontalize("iloi", vh) }
	result.stem_ap = { long and gem .. "ii" or base .. "i", base .. frontalize("iloi", vh) }
	result.stem_ep = { base .. "i", base .. frontalize("iloi", vh) }

	result.exe_weak = true
	result.par_short_ok = true
	
	result.ill_ending_pl = "he"

	result.geminate = gem ~= base
	return generate_hv_illative(process(data, result), data,
								nil, nil,
								result.stem_ip, { nil, true })
end

local vesi_strong = {["s"] = "t", ["ls"] = "lt", ["ns"] = "nt", ["rs"] = "rt"}
local vesi_weak = {["s"] = "", ["ls"] = "ll", ["ns"] = "nn", ["rs"] = "rr"}
inflections["vesi"] = function (data)
	local result = { typeno = "5" }
	local word = data.title
	local vh = data.vh
	if data.no_singular then -- plural title -> singular
		-- does not have to be perfect, plural forms being right is enough
		word = get_stem(word, "et")
		if mw.ustring.match(word, "rr$") then
			word = mw.ustring.sub(word, 1, -3) .. "rsi"
		elseif mw.ustring.match(word, "nn$") then
			word = mw.ustring.sub(word, 1, -3) .. "nsi"
		elseif mw.ustring.match(word, "ll$") then
			word = mw.ustring.sub(word, 1, -3) .. "lsi"
		elseif mw.ustring.match(word, "[ouöy]vv$") then
			word = mw.ustring.sub(word, 1, -3) .. "si"
		elseif mw.ustring.match(word, "vv$") then
			word = mw.ustring.sub(word, 1, -3) .. frontalize("u", vh) .. "si"
		elseif mw.ustring.match(word, "ij$") then
			word = mw.ustring.sub(word, 1, -3) .. "isi"
		else
			word = word .. "si"
		end
	end
	local stem, prefinal, final, strong, weak
	if mw.ustring.match(word, "si$") then
		stem, prefinal, final = mw.ustring.match(word, "(.-)(" .. m_izh.vowel .. "+)(" .. m_izh.consonant .. "+)i$")
	elseif mw.ustring.match(word, "s$") then
		stem, prefinal, final = mw.ustring.match(word, "(.-)(" .. m_izh.vowel .. "+)(" .. m_izh.consonant .. "+)$")
	end
	if not final then
		error("invalid vesi-type nominal")
	end
	local strong = vesi_strong[final] or error("final consonant not supported")
	local weak = vesi_weak[final] or error("final consonant not supported")
	local glide = prefinal
	if weak == "" then
		if mw.ustring.match(prefinal, "[oö][oö]$") or mw.ustring.match(prefinal, "[uy][uy]$") then
			weak = "vv"
		elseif mw.ustring.match(prefinal, m_izh.vowel .. "[uy]$") then
			weak = "vv"
			glide = mw.ustring.sub(glide, 1, -2)
		elseif mw.ustring.match(prefinal, m_izh.vowel .. "i$") then
			weak = "j"
		end
	end

	local raw_stem = stem .. prefinal
	local raw_stem_syllables = m_izh.split_syllables(raw_stem)
	local long_essive = not m_izh.is_heavy_syllable(raw_stem_syllables[#raw_stem_syllables])
	local weak_stem = stem .. glide .. weak
	local strong_stem = stem .. prefinal .. strong
	local plain_stem = stem .. prefinal .. final
	local plain_gem_stem = get_geminated(data, plain_stem .. "e", plain_stem)
	local strong_gem_stem = get_geminated(data, strong_stem .. "e", strong_stem)

	local long = get_elongation(data, stem .. prefinal .. weak .. "e")

	result.stem_gs = weak_stem .. "e"
	result.stem_ps = strong_stem .. "t"
	result.stem_is = strong_gem_stem .. "e"
	result.stem_as = elongate(weak_stem .. "e", long)
	result.stem_es = long_essive and strong_gem_stem .. "ee" or strong_stem .. "e"
	result.stem_xs = strong_gem_stem .. "e"

	result.stem_gp = { plain_gem_stem .. "ii", plain_stem .. frontalize("iloi", vh) }
	result.stem_pp = { plain_gem_stem .. "i", plain_stem .. frontalize("iloj", vh) }
	result.stem_op = { plain_stem .. "i", plain_stem .. frontalize("iloi", vh) }
	result.stem_ip = { plain_gem_stem .. "i", plain_stem .. frontalize("iloi", vh) }
	result.stem_ap = { elongate(plain_stem .. "i", long), plain_stem .. frontalize("iloi", vh) }
	result.stem_ep = { long_essive and plain_gem_stem .. "ii" or plain_stem .. "i", plain_stem .. frontalize("iloi", vh) }
	result.stem_xp = { plain_gem_stem .. "i", plain_stem .. frontalize("iloi", vh) }

	if weak == "vv" then
		result.grade = make_gradation(prefinal .. strong, glide .. weak)
	elseif weak == "j" then
		result.grade = make_gradation("i" .. strong, "i" .. weak)
	else
		result.grade = make_gradation(strong, weak)
	end
	
	result.ill_ending_pl = "he"
	return generate_hv_illative(process(data, result), data,
								nil, nil,
							    result.stem_ip, { nil, true })
end

inflections["lehti"] = function (data)
	local result = { typeno = "5" }
	local word = data.title
	local strong = data.args[1] or error("must specify strong grade")
	local weak = data.args[2] or error("must specify weak grade")
	if data.no_singular then -- plural title -> singular
		-- does not have to be perfect, plural forms being right is enough
		word = get_stem(word, weak .. "et") .. strong .. "i"
	end
	local vh = data.vh
	local stem
	if mw.ustring.match(word, "i$") then
		stem = get_stem(word, strong .. "i")
	else
		stem = get_stem(word, strong)
	end
	local gem = get_geminated(data, word, stem .. strong)
	local long = get_elongation(data, stem .. weak .. "e")

	result.stem_gs = stem .. weak .. "e"
	result.stem_ps = gem .. "i"
	result.stem_is = gem .. "ee"
	result.stem_as = elongate(stem .. weak .. "e", long)
	result.stem_es = stem .. strong .. "e"

	result.stem_gp = { gem .. "ii", stem .. strong .. frontalize("iloi", vh) }
	result.stem_pp = { stem .. strong .. "ij", stem .. strong .. frontalize("iloj", vh) }
	result.stem_op = { stem .. weak .. "i", stem .. strong .. frontalize("iloi", vh) }
	result.stem_ip = { gem .. "ii", stem .. strong .. frontalize("iloi", vh) }
	result.stem_ap = { elongate(stem .. weak .. "i", long), stem .. strong .. frontalize("iloi", vh)}
	result.stem_ep = { stem .. strong .. "i", stem .. strong .. frontalize("iloi", vh) }

	result.ill_ending = ""
	result.ill_ending_pl = "he"
	result.ill_short_sg = true

	result.grade = make_gradation(strong, weak)
	result.geminate = gem ~= stem .. strong
	return generate_hv_illative(process(data, result), data,
								nil, nil,
							    result.stem_ip, { nil, true })
end

inflections["vahti"] = function (data)
	local result = { typeno = "5" }
	local word = data.title
	local strong = data.args[1] or error("must specify strong grade")
	local weak = data.args[2] or error("must specify weak grade")
	if data.no_singular then -- plural title -> singular
		-- does not have to be perfect, plural forms being right is enough
		word = get_stem(word, weak .. "it") .. strong .. "i"
	end
	local vh = data.vh
	local stem
	if mw.ustring.match(word, "i$") then
		stem = get_stem(word, strong .. "i")
	else
		stem = get_stem(word, strong)
	end
	local gem = get_geminated(data, word, stem .. strong)
	local long = get_elongation(data, stem .. weak .. "i")

	result.stem_gs = stem .. weak .. "i"
	result.stem_ps = gem .. "i"
	result.stem_is = gem .. "i"
	result.stem_as = elongate(stem .. weak .. "i", long)
	result.stem_es = stem .. strong .. "i"

	result.stem_gp = { gem .. "ii", stem .. strong .. frontalize("iloi", vh) }
	result.stem_pp = { stem .. strong .. "ij", stem .. strong .. frontalize("iloj", vh) }
	result.stem_op = { stem .. strong .. "ii", stem .. strong .. frontalize("iloi", vh) }
	result.stem_ip = result.stem_gp
	result.stem_ap = result.stem_op
	result.stem_ep = { stem .. strong .. "ii", stem .. strong .. frontalize("iloi", vh) }

	result.ill_ending_pl = "he"

	result.grade = make_gradation(strong, weak)
	result.geminate = gem ~= stem .. strong
	return generate_hv_illative(process(data, result), data,
								nil, nil,
								result.stem_ip, { nil, true })
end

inflections["lähe"] = function (data)
	local result = { typeno = "6" }
	local word = data.title
	local weak = data.args[1] or error("must specify weak grade")
	local strong = data.args[2] or error("must specify strong grade")
	local stem, gem
	if data.no_singular then -- plural title -> singular
		-- does not have to be perfect, plural forms being right is enough
		gem = get_stem(word, "eet")
		stem = gem
	elseif mw.ustring.match(word, "e$") then
		stem = get_stem(word, weak .. "e")
		gem = get_geminated(data, stem .. strong .. "e", stem .. strong)
	else
		stem = get_stem(word, weak)
		gem = get_geminated(data, stem .. strong, stem .. strong)
	end

	result.stem_gs = gem .. "ee"
	result.stem_ps = stem .. weak .. "ett"

	result.stem_gp = gem .. "ei"
	result.stem_pp = result.stem_gp .. "t"
	result.stem_op = result.stem_gp

	result.ill_ending = "sse"
	
	result.grade = make_gradation(strong, weak)
	result.geminate = gem ~= stem .. strong
	return process(data, result)
end

inflections["kevät"] = function (data)
	local result = { typeno = "7" }
	local word = data.title
	local weak = data.args[1] or error("must specify weak grade")
	local strong = data.args[2] or error("must specify strong grade")
	local final, stem, gem
	if data.no_singular then -- plural title -> singular
		-- does not have to be perfect, plural forms being right is enough
		word = get_stem(word, "t")
		final = mw.ustring.sub(word, -2, -2)
		gem = mw.ustring.sub(word, 1, -3)
		stem = gem
	else
		final = mw.ustring.sub(word, -2, -2)
		stem = get_stem(word, weak .. final .. "t")
		gem = get_geminated(data, stem .. strong .. final, stem .. strong)
	end

	result.stem_gs = gem .. final .. "e"
	result.stem_ps = stem .. weak .. final .. "tt"
	result.stem_as = result.stem_gs .. "e"

	result.stem_gp = gem .. final .. "ei"
	result.stem_pp = result.stem_gp .. "t"
	result.stem_op = result.stem_gp

	result.ill_short_sg = true
	result.ill_ending = "sse"
	
	result.grade = make_gradation(strong, weak)
	result.geminate = gem ~= stem .. strong
	return process(data, result)
end

inflections["maa"] = function (data)
	local result = { typeno = "8" }
	local word = data.title
	if data.no_singular then -- plural title -> singular
		word = get_stem(word, "t")
	end
	local stem = mw.ustring.sub(word, 1, -2)

	result.stem_gs = word
	result.stem_ps = word .. "t"

	result.stem_gp = stem .. "ije"
	result.stem_pp = stem .. "it"
	result.stem_op = stem .. "i"

	result.ill_ending_sg = "h" .. mw.ustring.sub(word, -1, -1)
	if result.ill_ending_sg == "hi" then result.ill_ending_sg = "he" end
	result.ill_ending_pl = "he"
	
	return generate_hv_illative(process(data, result), data,
								{ result.stem_gs }, { true },
								{ result.stem_op }, { true })
end

inflections["diikkoi"] = function (data)
	local result = { typeno = "8" }
	local word = data.title
	if data.no_singular then -- plural title -> singular
		word = get_stem(word, "t")
	end
	local stem = mw.ustring.sub(word, 1, -2)

	result.stem_gs = word
	result.stem_ps = word .. "t"

	result.stem_gp = stem .. "i"
	result.stem_pp = stem .. "it"
	result.stem_op = stem .. "i"

	result.ill_ending_sg = "h" .. mw.ustring.sub(word, -1, -1)
	if result.ill_ending_sg == "hi" then result.ill_ending_sg = "he" end
	result.ill_ending_pl = "he"
	
	return generate_hv_illative(process(data, result), data,
								{ result.stem_gs }, { true },
								{ result.stem_op }, { true })
end

inflections["yks"] = function (data)
	local result = { typeno = "9" }
	local word = data.title
	if data.no_singular then -- plural title -> singular
		word = get_stem(word, vh .. "het") .. "ks"
	end
	local stem
	if mw.ustring.match(word, "i$") then
		stem = get_stem(word, "ksi")
	else
		stem = get_stem(word, "ks")
	end

	result.stem_gs = stem .. "he"
	result.stem_ps = stem .. "ht"
	result.stem_is = stem .. "hte"

	result.stem_gp = stem .. "ksii"
	result.stem_pp = stem .. "ksi"
	result.stem_op = stem .. "ksi"
	result.stem_ap = stem .. "ksii"

	return process(data, result)
end

inflections["kolt"] = function (data)
	local result = { typeno = "10" }
	local word = data.title
	if data.no_singular then -- plural title -> singular
		word = get_stem(word, vh .. "met") .. "t"
	end
	local stem = get_stem(word, "t")

	result.stem_gs = stem .. "me"
	result.stem_ps = stem .. "mi"
	result.stem_as = result.stem_gs .. "e"

	result.stem_gp = stem .. "mii"
	result.stem_pp = stem .. "mi"
	result.stem_ap = result.stem_pp .. "i"

	return process(data, result)
end

inflections["kaheksan"] = function (data)
	local result = { typeno = "11" }
	local word = data.title
	local final = data.args[1]
	if data.no_singular then -- plural title -> singular
		word = get_stem(word, vh .. "t")
		if mw.ustring.sub(word, -1, -1) == "m" then
			word = mw.ustring.sub(word, 1, -2) .. "n"
		elseif mw.ustring.match(word, "[s]$") then
			word = word .. vh .. "n"
		end
	end
	local vh = data.vh
	local stem
	if mw.ustring.match(word, vh .. "n$") then
		stem = get_stem(word, vh .. "n")
		final = final or ""
	else
		stem = get_stem(word, "n")
		final = final or "n"
	end

	result.stem_gs = stem .. final .. vh
	result.stem_ps = result.stem_gs
	result.stem_as = elongate(result.stem_gs)

	result.stem_gp = stem .. final .. "ii"
	result.stem_pp = stem .. final .. "i"
	result.stem_ap = elongate(result.stem_pp)

	return process(data, result)
end

inflections["tytär"] = function (data)
	local result = { typeno = "12" }
	local word = data.title
	local weak = data.args[1] or error("must specify weak grade")
	local strong = data.args[2] or error("must specify strong grade")
	if data.no_singular then -- plural title -> singular
		-- does not have to be perfect, plural forms being right is enough
		word = get_stem(word, "et")
		local coda = mw.ustring.sub(word, -1, -1)
		local final = mw.ustring.sub(word, -2, -2)
		word = get_stem(word, strong .. final .. coda) .. weak .. final .. coda
	end
	local final = mw.ustring.sub(word, -2, -2)
	local coda = mw.ustring.sub(word, -1, -1)
	if coda ~= "l" and coda ~= "r" and coda ~= "n" then
		error("invalid final character for tytär-type nominals")
	end
	if not mw.ustring.match(final, m_izh.vowel) then
		error("invalid penultimate character for tytär-type nominals (should be a vowel)")
	end
	local stem = get_stem(word, weak .. final .. coda)

	result.stem_gs = stem .. strong .. final .. coda .. "e"
	result.stem_ps = stem .. weak .. final .. coda .. "t"
	result.stem_as = elongate(result.stem_gs)

	result.stem_gp = stem .. strong .. final .. coda .. "ii"
	result.stem_pp = stem .. strong .. final .. coda .. "i"
	result.stem_ap = elongate(result.stem_pp)

	result.grade = make_gradation(strong, weak)
	return process(data, result)
end

inflections["harmaa"] = function (data)
	local result = { typeno = "13" }
	local word = data.title
	if data.no_singular then -- plural title -> singular
		word = get_stem(word, vh .. "t")
	end
	local stem = mw.ustring.sub(word, 1, -2)

	result.stem_gs = word
	result.stem_ps = word .. "t"
	result.stem_is = word

	result.stem_gp = stem .. "i"
	result.stem_pp = stem .. "it"
	result.stem_op = stem .. "i"
	
	result.ill_ending = "sse"
	result.exe_weak = true
	
	return process(data, result)
end

inflections["olt"] = function (data)
	local result = { typeno = "14" }
	local word = data.title
	if mw.ustring.sub(word, -2, -2) == "u" or mw.ustring.sub(word, -2, -2) == "y" then
		word = mw.ustring.sub(word, 1, -3) .. "t"
	end
	local long = data.args[1] or ""
	if long == "+" then
		long = mw.ustring.sub(word, -2, -2)
		word = mw.ustring.sub(word, 1, -2) .. long .. "t"
	end
	local vh = data.vh
	if data.no_singular then --plural title -> singular
		word = get_stem(word, "eet") .. "t"
		if long ~= "+" then
			word = mw.ustring.sub(word, 1, -3) .. "t"
		end
	end
	local stem = get_stem(word, "t")
	local gem = get_geminated(data, stem .. "u", stem)

	result.stem_gs = gem .. "ee"
	result.stem_ps = stem .. frontalize("u", vh) .. "tt"
	result.stem_is = result.stem_gs
	result.stem_es = result.stem_gs

	result.stem_gp = gem .. "ei"
	result.stem_pp = gem .. "eit"
	result.stem_ip = result.stem_gp
	result.stem_op = result.stem_gp
	result.stem_ep = result.stem_gp

	result.ill_ending = "sse"
	result.geminate = gem ~= stem
	return process(data, result)
end

inflections["syän"] = function (data)
	local result = { typeno = "15" }
	local word = data.title
	local weak = data.args[1] or error("must specify weak grade")
	local strong = data.args[2] or error("must specify strong grade")
	local final = mw.ustring.sub(word, -2, -2)
	if data.no_singular then -- plural title -> singular
		final = mw.ustring.sub(word, -4, -4)
		word = get_stem(word, strong .. final .. "met") .. weak .. final .. "n"
	end
	if not mw.ustring.match(final, m_izh.vowel) then
		error("invalid penultimate character for avvain-type nominals (should be a vowel)")
	end
	local stem = get_stem(word, weak .. final .. "n")
	local long = get_elongation(data, stem .. strong .. final .. "me")
	local gem
	if m_izh.guess_gemination(stem .. strong .. final .. "me") then
		gem = "m"
	else gem = "" end

	result.stem_gs = stem .. strong .. final .. "me"
	result.stem_ps = stem .. weak .. final .. "nt"
	result.stem_is = stem .. weak .. final .. gem .. "me"
	result.stem_os = result.stem_gs
	result.stem_as = elongate(result.stem_gs, long)
	result.stem_es = result.stem_gs

	result.stem_gp = stem .. strong .. final .. gem .. "mii"
	result.stem_pp = stem .. strong .. final .. gem .. "mi"
	result.stem_ip = stem .. strong .. final .. gem .. "mi"
	result.stem_ep = stem .. strong .. final .. "mi"
	result.stem_op = result.stem_ep
	result.stem_ap = elongate(result.stem_ep, long)

	result.par_short_ok = true

	result.grade = make_gradation(strong, weak)

	return process(data, result)
end

inflections["koitoin"] = function (data)	
	local result = { typeno = "15" }
	local word = data.title
	local weak = data.args[1] or error("must specify weak grade")
	local strong = data.args[2] or error("must specify strong grade")
	local final = mw.ustring.sub(word, -3, -3)
	local vh = data.vh
	if not mw.ustring.match(final, m_izh.vowel) then
		error("invalid antepenultimate character for koitoin-type nominals (should be a vowel)")
	end
	if data.no_singular then -- plural title -> singular
		word = get_stem(word, strong .. final .. "mat") .. weak .. final .. "in"
	end
	local stem = get_stem(word, weak .. final .. "in")
	local gem = get_geminated(data, stem .. strong .. final .. "ma", stem .. strong .. final .. "m")
	local long = get_elongation(data, stem .. strong .. final .. "ma")

	result.stem_gs = stem .. strong .. final .. "m" .. frontalize("a", vh)
	result.stem_ps = stem .. weak .. final .. "int"
	result.stem_is = gem .. frontalize("a", vh)
	result.stem_as = elongate(result.stem_gs, long)
	result.stem_es = result.stem_gs

	result.stem_gp = gem .. "ii"
	result.stem_pp = gem .. "i"
	result.stem_ip = result.stem_pp
	result.stem_op = stem .. strong .. final .. "m" .. "i"
	result.stem_ap = elongate(result.stem_op, long)
	result.stem_ep = result.stem_op
	
	result.par_short_ok = true

	result.grade = make_gradation(strong, weak)

	return process(data, result)
end

inflections["hapan"] = function (data)	
	local result = { typeno = "15" }
	local word = data.title
	local weak = data.args[1] or error("must specify weak grade")
	local strong = data.args[2] or error("must specify strong grade")
	local final = mw.ustring.sub(word, -2, -2)
	local vh = data.vh
	if not mw.ustring.match(final, m_izh.vowel) then
		error("invalid antepenultimate character for koitoin-type nominals (should be a vowel)")
	end
	if data.no_singular then -- plural title -> singular
		word = get_stem(word, strong .. final .. "m" .. frontalize("a", vh) .. "t") .. weak .. final .. "n"
	end
	local stem = get_stem(word, weak .. final .. "n")
	local long = get_elongation(data, stem .. strong .. final .. "ma")

	result.stem_gs = stem .. strong .. final .. "m" .. frontalize("a", vh)
	result.stem_ps = stem .. weak .. final .. "nt"
	result.stem_is = result.stem_gs
	result.stem_as = elongate(result.stem_gs, long)

	result.stem_gp = stem .. strong .. final .. "mii"
	result.stem_pp = stem .. strong .. final .. "mi"
	result.stem_ip = result.stem_pp
	result.stem_ap = elongate(result.stem_pp, long)
	
	result.par_short_ok = true

	result.grade = make_gradation(strong, weak)

	return process(data, result)
end

-- inflection classes end

local infl_table = [=[{| class="inflection-table vsSwitcher izh-decl" data-toggle-category="declension"
|-
!colspan=3 class="vsToggleElement izh-decl-header-row"|Declension of {{{title}}} (<span class="izh-decl-type">{{{type}}}</span>)
|- class="vsHide"
! class="izh-decl-case-column" |
! class="izh-decl-form-column" | singular
! class="izh-decl-form-column" | plural
|-
! class="izh-decl-case-column" | nominative
| class="izh-decl-form-column" |  {{{nom_sg}}}
| class="izh-decl-form-column" |  {{{nom_pl}}}
|-
! genitive
|{{{gen_sg}}}
|{{{gen_pl}}}
|- 
! partitive
|{{{par_sg}}}
|{{{par_pl}}}
|-
! illative
|{{{ill_sg}}}
|{{{ill_pl}}}
|- class="vsHide" |
! inessive
|{{{ine_sg}}}
|{{{ine_pl}}}
|- class="vsHide" |
!  elative
|{{{ela_sg}}}
|{{{ela_pl}}}
|- class="vsHide" |
! allative
|{{{all_sg}}}
|{{{all_pl}}}
|- class="vsHide" |
! adessive
|{{{ade_sg}}}
|{{{ade_pl}}}
|- class="vsHide" |
! ablative
|{{{abl_sg}}}
|{{{abl_pl}}}
|- class="vsHide" |
! translative
|{{{tra_sg}}}
|{{{tra_pl}}}
|- class="vsHide" |
! essive
|{{{ess_sg}}}
|{{{ess_pl}}}
|- class="vsHide" |
! exessive<sup>1)</sup>
|{{{exe_sg}}}
|{{{exe_pl}}}
|- class="vsHide" |
| colspan="3" class="izh-decl-notes" | <sup>1)</sup> obsolete <br /> <sup>*)</sup> the '''accusative''' corresponds with either the '''genitive''' (<span class="gender"><abbr title="singular number">sg</abbr></span>) or '''nominative''' (<span class="gender"><abbr title="plural number">pl</abbr></span>)<br /> <sup>**)</sup> the '''comitative''' is formed by adding the suffix {{m|izh|-ka}}<sup><span style="cursor:help;" title="in back-vocalic stems">?</span></sup> or {{m|izh|-kä}}<sup><span style="cursor:help;" title="in front-vocalic stems">?</span></sup> to the '''genitive'''.
|}]=]

local function link(text)
	return require("Module:links").full_link{ term = text, lang = m_izh.lang }
end

local function mention(text)
	return require("Module:links").full_link({ term = text, lang = m_izh.lang }, "term")
end

function export.show(frame)
	local infl_type = frame.args[1] or error("inflection class not specified")
	local infl = inflections[infl_type] or error("unsupported inflection type")
	local args = frame:getParent().args
	local title = args["title"] or mw.title.getCurrentTitle().text

	local geminate, elongate, vh, headword
	if args["g"] == "1" then
		geminate = true
	elseif args["g"] == "0" or args["g"] == "-" then
		geminate = false
	else
		headword = args["g"]
		vh = m_izh.guess_vowel_harmony(headword or title)
	end
	
	if args["e"] == "1" then
		elongate = true
	elseif args["e"] == "0" or args["e"] == "-" then
		elongate = false
	else
		elongate = nil
	end
	
	if args["v"] then
		vh = args["v"]
		if vh ~= "a" and vh ~= "ä" then
			error("Invalid vowel harmony specification")
		end
	elseif not vh then
		vh = m_izh.guess_vowel_harmony(title)
	end

	local data = { title = title, headword = headword, geminate = geminate, elongate = elongate, vh = vh, args = args }

	if args["n"] then
		if args["n"] == "s" or args["n"] == "sg" then
			data.no_plural = true
		elseif args["n"] == "p" or args["n"] == "pl" then
			data.no_singular = true
		end
	end

	local forms = infl(data)

	local function repl(form)
		if form == "title" then
			return "'''" .. title .. "'''"
		elseif form == "type" then
			if forms.irregular then
				return "irregular"
			end
			local s = "type " .. forms.typeno .. "/" .. mention(infl_type)
			if forms.grade then
				s = s .. ", " .. forms.grade
			else
				s = s .. ", " .. make_gradation(nil, nil)
			end
			if forms.geminate then
				s = s .. ", gemination"
			end
			return s
		else
			local value = forms[form]
			if not value then
				return "&mdash;"
			elseif type(value) == "table" then
				local result = {}
				for _, f in ipairs(value) do
					table.insert(result, link(f))
				end
				return table.concat(result, ", ")
			else
				return link(value)
			end
		end
	end

	local result = mw.ustring.gsub(infl_table, "{{{([a-z0-9_:]+)}}}", repl)
	result = mw.ustring.gsub(result, "{{m|izh|([^}]-)}}", mention)
	return result
		.. require("Module:TemplateStyles")("Module:izh-decl/style.css")

end

return export