local export = {}
local pos_functions = {}

local m_str_utils = require("Module:string utilities")
local m_table = require("Module:table")

local legal_gender = {
	["m"] = true,
	["f"] = true,
	["n"] = true,
	["?"] = true,
	["?!"] = true,
}

local declension_to_english = {
	["1"] = "第一",
	["2"] = "第二",
	["3"] = "第三",
	["4"] = "第四",
	["5"] = "第五",
}

local gender_names = {
	["m"] = "陽性",
	["f"] = "陰性",
	["n"] = "中性",
	["?"] = "未知性別",
	["?!"] = "文獻未記錄性別",
}

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

local u = m_str_utils.char
local rfind = m_str_utils.find
local rmatch = m_str_utils.match
local rsplit = m_str_utils.split
local rsubn = m_str_utils.gsub
local ulower = m_str_utils.lower

local MACRON = u(0x0304)
local BREVE = u(0x0306)
local DIAER = u(0x0308)
local DOUBLE_INV_BREVE = u(0x0361)
local accents = MACRON .. BREVE .. DIAER .. DOUBLE_INV_BREVE

-- version of rsubn() that discards all but the first return value
local function rsub(term, foo, bar)
	local retval = rsubn(term, foo, bar)
	return retval
end

local function track(page)
	require("Module:debug").track("la-headword/" .. page)
	return true
end

local function format(array, concatenater)
	if #array == 0 then
		return ""
	else
		local concatenated = table.concat(array, concatenater)
		if concatenated == "" then
			return ""
		elseif rfind(concatenated, "'$") then
			concatenated = concatenated .. " "
		end
		return ";" .. concatenated .. ""
	end
end

local function glossary_link(anchor, text)
	text = text or anchor
	return "[[Appendix:Glossary#" .. anchor .. "|" .. text .. "]]"
end

-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
	local NAMESPACE = mw.title.getCurrentTitle().nsText
	local PAGENAME = mw.loadData("Module:headword/data").pagename

	local iparams = {
		[1] = {required = true},
		["def"] = {},
		["suff_type"] = {},
	}
	local iargs = require("Module:parameters").process(frame.args, iparams)
	local args = frame:getParent().args
	local poscat = iargs[1]
	local def = iargs.def
	local suff_type = iargs.suff_type
	local postype = nil
	if suff_type then
		postype = poscat .. suff_type
	else
		postype = poscat
	end

	local data = {lang = lang, categories = {}, heads = {}, genders = {}, inflections = {}}
	local infl_classes = {}
	local appendix = {}
	local postscript = {}

	if poscat == "後綴" then
		table.insert(data.categories, "構成" .. suff_type .. "的拉丁語後綴")
	end

	if pos_functions[postype] then
		local new_poscat = pos_functions[postype](def, args, data, infl_classes, appendix, postscript)
		if new_poscat then
			poscat = new_poscat
		end
	end

	if (NAMESPACE == "Reconstruction") then
		data.pos_category = "重構" .. poscat
		data.nolink = true
	else
		data.pos_category = poscat
	end

	data.pos_category = (NAMESPACE == "Reconstruction" and "重構" or "") .. poscat

	postscript = table.concat(postscript, ", ")

	return
		require("Module:headword").full_headword(data)
		.. format(infl_classes, "/")
		.. format(appendix, ", ")
		.. (postscript ~= "" and " (" .. postscript .. ")" or "")
end

local function process_num_type(numtype, categories)
	if numtype == "card" then
		table.insert(categories, "拉丁語基數詞")
	elseif numtype == "ord" then
		table.insert(categories, "拉丁語序數詞")
	elseif numtype == "dist" then
		-- FIXME, should be named 'Latin distributive numbers'
		table.insert(categories, "la:Distributive numbers")
	elseif numtype == "mul" then
		-- FIXME, should be named 'Latin multiplicative numbers'
		table.insert(categories, "la:Multiplicative numbers")
	elseif numtype == "coll" then
		-- FIXME, should be named 'Latin collective numbers'
		table.insert(categories, "la:Collective numbers")
	elseif numtype then
		error("Unrecognized numeral type '" .. numtype .. "'")
	end
end

local function nouns(pos, def, args, data, infl_classes, appendix)
	local NAMESPACE = mw.title.getCurrentTitle().nsText
	local is_num = pos == "數詞" or pos == "numerals"
	local is_pn = false
	if pos == "proper nouns" or pos == "專有名詞" then
		is_pn = true
		pos = "名詞"
	end
	local decldata = require("Module:la-nominal").do_generate_noun_forms(
	  args, pos, true, def, is_num)
	local lemma = decldata.overriding_lemma
	local lemma_num = decldata.num == "pl" and "pl" or "sg"
	if not lemma or #lemma == 0 then
		lemma = decldata.forms["linked_nom_" .. lemma_num]
		if decldata.unattested["nom_" .. lemma_num] then
			lemma[1] = '*' .. lemma[1]
		end
	end

	data.heads = lemma
	-- Since we always set data.heads to the lemma and specification of the lemma is mandatory in {{la-noun}}, there aren't
	-- really any redundant heads.
	data.no_redundant_head_cat = true
	data.id = decldata.id
	
	local genders = decldata.overriding_genders
	if #genders == 0 then
		if decldata.gender then
			genders = {ulower(decldata.gender)}
		elseif not is_num then
			error("No gender explicitly specified in headword template using g=, and can't infer gender from lemma spec")
		end
	end

	if is_num then
		process_num_type(decldata.num_type, data.categories)
	end

	if decldata.indecl then
		table.insert(data.inflections, {label = "无变格"})
		table.insert(data.categories, "拉丁語無屈折" .. decldata.pos)

		for _, g in ipairs(genders) do
			local gender, number = rmatch(g, "^(.)%-([sp])$")
			if not gender then
				gender = g
			end
			if not legal_gender[gender] then
				error("Gender “" .. gender .. "” is not a valid Latin gender.")
			end
			table.insert(data.genders, g)
			local gender_name = gender_names[gender]
			table.insert(data.categories, "拉丁語" .. gender_name .. "無屈折" .. decldata.pos)
		end
	else
		local is_irreg = false
		local is_indecl = false
		local is_decl = false
		local has_multiple_decls = false
		local has_multiple_variants = false
		-- flatten declension specs
		local decls = {}

		for _, g in ipairs(genders) do
			if not legal_gender[g] then
				error("Gender “" .. g .. "” is not a valid Latin gender.")
			end
			local gender_name = gender_names[g]
			if decldata.num == "pl" then
				g = g .. "-p"
			elseif decldata.num == "sg" then
				g = g .. "-s"
			end
			table.insert(data.genders, g)
		end

		local function process_decl(decl_list, decl)
			-- skip adjectival declensions
			if not rfind(decl, "%+$") then
				local irreg_decl_spec = rmatch(decl, "^irreg/(.*)$")
				if irreg_decl_spec then
					is_irreg = true
					local irreg_decls = rsplit(irreg_decl_spec, ",")
					if #irreg_decls > 1 then
						has_multiple_decls = true
					end
					for _, d in ipairs(irreg_decls) do
						if d == "indecl" or decl == "0" then
							is_indecl = true
						else
							is_decl = true
						end
						m_table.insertIfNot(decl_list, d)
					end
				else
					if decl == "indecl" or decl == "0" then
						is_indecl = true
					else
						is_decl = true
					end
					m_table.insertIfNot(decl_list, decl)
				end
			end
		end

		for _, props in ipairs(decldata.propses) do
			if props.headword_decl then
				process_decl(decls, props.headword_decl)
			else
				local alternant_decls = {}
				for _, alternant in ipairs(props) do
					for _, single_props in ipairs(alternant) do
						process_decl(alternant_decls, single_props.headword_decl)
					end
				end
				if #alternant_decls > 1 then
					has_multiple_decls = true
				elseif #decls > 1 then
					has_multiple_variants = true
				end
				for _, d in ipairs(alternant_decls) do
					m_table.insertIfNot(decls, d)
				end
			end
		end

		if is_indecl and is_decl then
			has_multiple_decls = true
		end
		if has_multiple_decls then
			table.insert(data.categories, "有多種變格的拉丁語" .. decldata.pos)
		end
		if has_multiple_variants then
			table.insert(data.categories, "一種變位有多個變種的拉丁語" .. decldata.pos)
		end
		if is_irreg then
			table.insert(data.inflections, {label = glossary_link("不规则")})
			table.insert(data.categories, "拉丁語不規則" .. decldata.pos)
			for _, g in ipairs(genders) do
				table.insert(data.categories, "拉丁語" .. gender_names[g] ..  "不規則" .. decldata.pos)
			end
		end

		if is_indecl then
			if is_decl then
				table.insert(appendix, glossary_link("無屈折"))
			else
				table.insert(data.inflections, {label = glossary_link("無屈折")})
			end
			table.insert(data.categories, "拉丁語無屈折" .. decldata.pos)
			for _, g in ipairs(genders) do
				table.insert(data.categories, "拉丁語" .. gender_names[g] ..  "無屈折" .. decldata.pos)
			end
		end

		if #decls > 1 then
			table.insert(data.inflections, {label = '多种变格'})
			--This causes multipart nouns composed of two nouns of different declensions
			--to go into the category. The above code only triggers if a given term has
			--multiple declensions.
			--table.insert(data.categories, "Latin " .. decldata.pos .. " with multiple declensions")
		end

		for _, decl in ipairs(decls) do
			if decl ~= "irreg" and decl ~= "indecl" and decl ~= "0" then
				local decl_class = declension_to_english[decl]
				if not decl_class then
					error("Something wrong with declension '" .. decl .. "', don't recognize it")
				end
				table.insert(appendix, "[[附录:拉丁语".. decl_class .."类变格|".. decl_class .."类变格]]")
				table.insert(data.categories, "拉丁語" .. decl_class .. "類變格" .. decldata.pos)

				for _, g in ipairs(genders) do
					table.insert(data.categories, "拉丁語" .. decl_class .. "類變格".. gender_names[g] ..  decldata.pos)
				end
			end
		end

		if NAMESPACE == 'Reconstruction' then
			-- For reconstructed nouns:
			if data.genders[1] == 'n' and lemma_num == 'sg' then
				-- singular neuter nouns give a plural
				local pl = decldata.forms["nom_pl"]
				if pl and pl ~= "" and #pl > 0 then
					pl.label = "複數"
					table.insert(data.inflections, pl)
				end
			else
				-- all others give an oblique
				local obl = decldata.forms["acc_" .. lemma_num]
				if obl and obl ~= "" and #obl > 0 then
					obl.label = "斜格"
					table.insert(data.inflections, obl)
				end
			end
		else
			local gen = decldata.forms["gen_" .. lemma_num]
			if (decldata.unattested["gen_" .. lemma_num]) then
				gen[1] = '*' .. gen[1]
				data.nolink = true
			end
			if gen and gen ~= "" and gen ~= "—" and #gen > 0 then
				if is_decl then
					-- Skip displaying the genitive for nouns that are only
					-- indeclinable. But we do display it for nouns like Abrahām
					-- and Ādām that can be either indeclinable or declined.
					gen.label = "屬格"
					table.insert(data.inflections, gen)
				end
			else
				table.insert(data.inflections, {label = "無屬格"})
				table.insert(data.categories, "沒有屬格單數的拉丁語" .. decldata.pos)
			end
		end
	end

	if #decldata.m > 0 then
		decldata.m.label = "阳性"
		table.insert(data.inflections, decldata.m)
	end

	if #decldata.f > 0 then
		decldata.f.label = "阴性"
		table.insert(data.inflections, decldata.f)
	end

	for _, cat in ipairs(decldata.categories) do
		m_table.insertIfNot(data.categories, cat)
	end

	for _, cat in ipairs(decldata.cat) do
		m_table.insertIfNot(data.categories, "拉丁語" .. cat)
	end
	
	return is_pn and decldata.pos == "名詞" and "專有名詞" or decldata.pos
end

pos_functions["nouns"] = function(def, args, data, infl_classes, appendix)
	return nouns("名詞", def, args, data, infl_classes, appendix)
end

pos_functions["proper nouns"] = function(def, args, data, infl_classes, appendix)
	return nouns("專有名詞", def, args, data, infl_classes, appendix)
end

pos_functions["suffixes-noun"] = function(def, args, data, infl_classes, appendix)
	return nouns("後綴", def, args, data, infl_classes, appendix)
end

pos_functions["numerals-noun"] = function(def, args, data, infl_classes, appendix)
	return nouns("數詞", def, args, data, infl_classes, appendix)
end

export.allowed_subtypes = {
	["impers"] = true,
	["3only"] = true,
	["depon"] = true,
	["semidepon"] = true,
	["optsemidepon"] = true,
	["nopass"] = true,
	["pass3only"] = true,
	["passimpers"] = true,
	["perfaspres"] = true,
	["noperf"] = true,
	["nopasvperf"] = true,
	["nosup"] = true,
	["supfutractvonly"] = true,
	["noimp"] = true,
	["nofut"] = true,
	["p3inf"] = true,
	["poetsyncperf"] = true,
	["optsyncperf"] = true,
	["alwayssyncperf"] = true,
	["m"] = true,
	["f"] = true,
	["n"] = true,
	["mp"] = true,
	["fp"] = true,
	["np"] = true,
	-- can be specified manually in the headword to display "highly defective"
	-- in the title (e.g. aveō)
	["highlydef"] = true,
}

pos_functions["verbs"] = function(def, args, data, infl_classes, appendix)
	local m_la_verb = require("Module:la-verb")
	local NAMESPACE = mw.title.getCurrentTitle().nsText
	local def1, def2
	if def then
		def1, def2 = rmatch(def, "^(.*):(.*)$")
	end
	local conjdata, typeinfo = m_la_verb.make_data(args, true, def1, def2)
	local lemma_forms = conjdata.overriding_lemma
	if not lemma_forms or #lemma_forms == 0 then
		lemma_forms = m_la_verb.get_lemma_forms(conjdata, true)
	end
	local first_lemma = ""
	if #lemma_forms > 0 then
		first_lemma = require("Module:links").remove_links(lemma_forms[1])
	end
	data.heads = lemma_forms
	-- Since we always set data.heads to the lemma and specification of the lemma is mandatory in {{la-verb}}, there aren't
	-- really any redundant heads.
	data.no_redundant_head_cat = true
	data.id = conjdata.id
	local conj = typeinfo.conj_type
	local subconj = typeinfo.conj_subtype
	local subtypes = typeinfo.subtypes
	local perf_only = false

	local function insert_inflection(infl, label)
		for _, form in ipairs(infl) do
			if rsub(form, "^[*%[%]|%-a-zA-ZĀāĒēĪīŌōŪūȲȳÄäËëÏïÖöÜüŸÿĂăĔĕĬĭŎŏŬŭ " .. accents .. "]+$", "") ~= "" then
				table.insert(data.categories, "la-verb invalid parameters")
			end
		end
		infl.label = label
		table.insert(data.inflections, infl)
	end

	local inf = m_la_verb.get_valid_forms(conjdata.forms["pres_actv_inf"])
	if #inf > 0 then
		insert_inflection(inf, "现在时不定式")
	else
		inf = m_la_verb.get_valid_forms(conjdata.forms["perf_actv_inf"])
		if #inf > 0 then
			perf_only = true
			insert_inflection(inf, "完全不定式")
		end
	end

	local depon = typeinfo.subtypes.depon or typeinfo.subtypes.semidepon
	if not perf_only then
		local perf
		if depon then
			local sup = m_la_verb.get_valid_forms(conjdata.forms["sup_acc"])
			perf = {}
			for _, form in ipairs(sup) do
				if typeinfo.subtypes.impers then
					form = rsub(form, "^(.*)m$", "[[%1s|%1m]] est")
				elseif typeinfo.subtypes["3only"] then
					form = rsub(form, "^(.*)m$", "[[%1s]] est")
				else
					form = rsub(form, "^(.*)m$", "[[%1s]] sum")
				end
				table.insert(perf, form)
			end
		else
			perf = m_la_verb.get_valid_forms(conjdata.forms["1s_perf_actv_indc"])
			if #perf == 0 then
				perf = m_la_verb.get_valid_forms(conjdata.forms["3s_perf_actv_indc"])
			end
		end
		if #perf > 0 then
			insert_inflection(perf, "完成时主动式")
		end
	end

	if not depon then
		local sup = m_la_verb.get_valid_forms(conjdata.forms["sup_acc"])
		if #sup > 0 then
			insert_inflection(sup, "目的动名词")
		else
			local fap = m_la_verb.get_valid_forms(conjdata.forms["futr_actv_ptc"])
			if #fap > 0 then
				insert_inflection(fap, "将来分词")
			end
		end
	end

	if conj == "1st" or subconj == "1st" then
		table.insert(appendix, "[[附录:拉丁语第一类变位|第一类变位]]")
	elseif conj == "2nd" or subconj == "2nd" then
		table.insert(appendix, "[[附录:拉丁语第二类变位|第二类变位]]")
	elseif conj == "3rd" or subconj == "3rd" then
		table.insert(appendix, "[[附录:拉丁语第三类变位|第三类变位]]")
	elseif conj == "3rd-io" or subconj == "3rd-io" then
		table.insert(appendix, "[[附录:拉丁语第三类变位|第三类变位]] iō-变体")
	elseif conj == "4th" or subconj == "4th" then
		table.insert(appendix, "[[附录:拉丁语第四类变位|第四类变位]]")
	elseif conj == "irreg" then --sum
		table.insert(appendix, "[[附录:拉丁语不规则动词|不规则变位]]")
	else
		if NAMESPACE == "Template" then
			table.insert(appendix, "? 變位")
		else
			table.insert(data.categories, "詞頭中無變格形的拉丁文動詞")
		end
	end

	if conj == "irreg" and subconj == "irreg" or subtypes.irreg then
		--sum, volō, ferō etc.
		table.insert(appendix, "不规则")
	end
	if subtypes.highlydef then
		-- āiō, inquam
		table.insert(appendix, "高度不完全变化")
	end
	if subtypes.perfaspres then
		-- ōdī, meminī, commeminī
		table.insert(appendix, "有现在时意义的完成式")
	end
	if subtypes.impers then
		-- decet
		-- advesperāscit (also nopass)
		table.insert(appendix, "无人称")
	end
	if subtypes.nopass then
		--coacēscō
		table.insert(appendix, "无被动形")
	end
	if subtypes.depon then
		-- dēmōlior
		-- calvor (also noperf)
		table.insert(appendix, "异态")
	end
	if subtypes.semidepon then
		-- fīdō, gaudeō
		table.insert(appendix, "半异态")
	end
	if subtypes.optsemidepon then
		-- audeō, placeō, soleō, pudeō
		table.insert(appendix, "可选半异态")
	end
	if subtypes.nosup and subtypes.noperf then
		-- many verbs
		table.insert(appendix, "无完全或目的动名词词干")
	elseif subtypes.noperf then
		-- īnsolēscō etc.
		table.insert(appendix, "无完成式词干")
	elseif subtypes.nosup then
		-- deeō etc.
		table.insert(appendix, "无目的动名词词干")
	elseif subtypes.supfutractvonly then
		-- sum, dēpereō, etc.
		table.insert(appendix, "无目的动名词词干(除将来主动分词)")
	end
	if typeinfo.subtypes.nopasvperf and not typeinfo.subtypes.nosup and
			not typeinfo.subtypes.supfutractvonly then
		table.insert(appendix, "无被动完全形式")
	end
	if subtypes.pass3only then
		--praefundō
		table.insert(appendix, "有限被动式")
	end
	if subtypes.passimpers then
		--abambulō
		table.insert(appendix, "被动式无人称")
	end
	if rfind(first_lemma, "faciō$") then
		--faciō
		table.insert(appendix, "不规则被动语态")
	end
	if subtypes["3only"] then
		--decet
		table.insert(appendix,"仅第三人称")
	end
	if subtypes.noimp then
		--volō
		table.insert(appendix, "无命令式")
	end
	if (conj == "3rd" or conj == "irreg") and rfind(first_lemma, "d[īū]cō$") then
		--dīcō
		table.insert(appendix, "不规则短命令式")
	end
	if subtypes.nofut then
		--soleō
		table.insert(appendix, "无将来时")
	end
end

pos_functions["suffixes-verb"] = pos_functions["verbs"]

local function adjectives(pos, def, args, data, infl_classes, appendix)
	local is_num = pos == "數詞" or pos == "numerals"
	local decldata = require("Module:la-nominal").do_generate_adj_forms(
	  args, pos, true, def, is_num)
	local lemma = decldata.overriding_lemma
	local lemma_num = decldata.num == "pl" and "pl" or "sg"
	if not lemma or #lemma == 0 then
		lemma = decldata.forms["linked_nom_" .. lemma_num .. "_m"]
		if decldata.unattested["nom_" .. lemma_num .. "_m"] then
			lemma[1] = '*' .. lemma[1]
		end
	end

	data.heads = lemma
	-- Since we always set data.heads to the lemma and specification of the lemma is mandatory in {{la-noun}}, there aren't
	-- really any redundant heads.
	data.no_redundant_head_cat = true
	data.id = decldata.id

	if is_num then
		process_num_type(decldata.num_type, data.categories)
	end

	if decldata.num == "pl" then
		table.insert(data.categories, "拉丁语仅复数" .. decldata.pos)
	end

	if decldata.indecl then
		table.insert(data.inflections, {label = glossary_link("無屈折")})

		if decldata.pos == "participles" then
			if rfind(lemma[1], "[stxu]um$") then
				table.insert(data.categories, "拉丁語完成分詞")
			end
		end
	else

		local function attested_form(index)
			local form
			if (decldata.unattested[index]) then
				form = { { term = '*' .. decldata.forms[index][1], nolink = true } }
			else
				form = decldata.forms[index]
			end
			return form
		end

		local masc = decldata.forms["nom_" .. lemma_num .. "_m"]
		local fem = attested_form("nom_" .. lemma_num .. "_f")
		local neut = attested_form("nom_" .. lemma_num .. "_n")
		local gen = attested_form("gen_" .. lemma_num .. "_m")
	
		if decldata.pos == "participles" then
			if rfind(masc[1], "ūrus$") then
				table.insert(data.categories, "拉丁語將來分詞")
			elseif rfind(masc[1], "ndus$") then
				-- FIXME, should rename to "Latin gerundives")
				table.insert(data.categories, "拉丁語將來被動分詞")
			elseif rfind(masc[1], "[stxu]us$") then
				table.insert(data.categories, "拉丁語完成分詞")
			elseif rfind(masc[1], "ns$") then
				table.insert(data.categories, "拉丁語現在分詞")
			else
				error("Unrecognized participle ending: " .. masc[1])
			end
		end
	
		local function is_missing(form)
			return not form or form == "" or form == "—" or #form == 0
		end
		-- We display the inflections in three different ways to mimic the
		-- old way of doing things:
		--
		-- 1. If masc and fem are different, show masc, fem and neut.
		-- 2. Otherwise, if masc and neut are different, show masc and neut.
		-- 3. Otherwise, show masc nominative and masc genitive.
		if not is_missing(fem) and not m_table.deepEquals(masc, fem) then
			fem.label = "阴性"
			table.insert(data.inflections, fem)
			if not is_missing(neut) then
				neut.label = "中性"
				table.insert(data.inflections, neut)
			end
		elseif not is_missing(neut) and not m_table.deepEquals(masc, neut) then
			neut.label = "中性"
			table.insert(data.inflections, neut)
		elseif not is_missing(gen) then
			gen.label = "属格"
			table.insert(data.inflections, gen)
		end

		table.insert(infl_classes, decldata.title)
	end

	if #decldata.comp > 0 then
		decldata.comp.label = "比较级"
		table.insert(data.inflections, decldata.comp)
	end
	if #decldata.sup > 0 then
		decldata.sup.label = "最高级"
		table.insert(data.inflections, decldata.sup)
	end
	if #decldata.adv > 0 then
		decldata.adv.label = "副词"
		table.insert(data.inflections, decldata.adv)
	end

	for _, cat in ipairs(decldata.categories) do
		m_table.insertIfNot(data.categories, cat)
	end

	for _, cat in ipairs(decldata.cat) do
		m_table.insertIfNot(data.categories, "拉丁語" .. cat)
	end
	
	return decldata.pos
end

local function adjectives_comp(pos, def, args, data, infl_classes, appendix)
	local params = {
		[1] = {alias_of = 'head'},
		[2] = {alias_of = 'pos'},
		["head"] = {list = true},
		["pos"] = {list = true},
		["is_lemma"] = {type = "boolean"},
		["id"] = {},
	}
	local args = require("Module:parameters").process(args, params)
	data.no_redundant_head_cat = #args.head == 0
	-- Set default manually so we can tell whether the user specified head=.
	if #args.head == 0 then
		args.head = {mw.loadData("Module:headword/data").pagename}
	end
	data.heads = args.head
	data.id = args.id
	if args.is_lemma then
		-- See below. This happens automatically by virtue of the default POS
		-- unless we override it, which we do when is_lemma.
		table.insert(data.categories, "拉丁語" .. pos .. "比較級")
	end
	table.insert(infl_classes, "[[Appendix:拉丁語第三類變格|第三類變格]]")

	local n = {label = "中性"}
	for _, head in ipairs(args.head) do
		local neuter = rsub(head, "or$", "us")
		table.insert(n, neuter)
	end

	table.insert(data.inflections, n)

	if #args.pos > 0 then
		args.pos.label = "肯定"
		table.insert(data.inflections, args.pos)
	end
	if args.is_lemma then
		-- If is_lemma, we're a comparative adjective without positive form,
		-- so we're treated as a lemma. In that case, we return "adjectives" as
		-- the part of speech, which will automatically categorize into
		-- "Latin adjectives" and "Latin lemmas", otherwise we don't return
		-- anything, which defaults to the passed-in POS (usually
		-- "comparative adjectives"), which will automatically categorize into
		-- that POS (e.g. "Latin comparative adjectives") and into
		-- "Latin non-lemma forms".
		return pos
	end
end

local function adjectives_sup(pos, def, args, data, infl_classes, appendix)
	local params = {
		[1] = {alias_of = 'head'},
		[2] = {alias_of = 'pos'},
		["head"] = {list = true},
		["pos"] = {list = true},
		["is_lemma"] = {type = "boolean"},
		["id"] = {},
	}
	local args = require("Module:parameters").process(args, params)
	data.no_redundant_head_cat = #args.head == 0
	-- Set default manually so we can tell whether the user specified head=.
	if #args.head == 0 then
		args.head = {mw.loadData("Module:headword/data").pagename}
	end
	data.heads = args.head
	data.id = args.id

	if args.is_lemma then
		-- See below. This happens automatically by virtue of the default POS
		-- unless we overrride it, which we do when is_lemma.
		table.insert(data.categories, "拉丁語" .. pos .. "最高級")
	end
	table.insert(infl_classes, "[[Appendix:拉丁语第一类变格|第一類]]")
	table.insert(infl_classes, "[[Appendix:拉丁语第二类变格|第二類]]")

	local f, n = {label = "阴性"}, {label = "中性"}
	for _, head in ipairs(args.head) do
		local stem = rsub(head, "us$", "")
		table.insert(f, stem .. "a")
		table.insert(n, stem .. "um")
	end

	table.insert(data.inflections, f)
	table.insert(data.inflections, n)

	if #args.pos > 0 then
		args.pos.label = "肯定"
		table.insert(data.inflections, args.pos)
	end
	if args.is_lemma then
		-- If is_lemma, we're a superlative adjective without positive form,
		-- so we're treated as a lemma. In that case, we return "adjectives" as
		-- the part of speech, which will automatically categorize into
		-- "Latin adjectives" and "Latin lemmas", otherwise we don't return
		-- anything, which defaults to the passed-in POS (usually
		-- "superlative adjectives"), which will automatically categorize into
		-- that POS (e.g. "Latin superlative adjectives") and into
		-- "Latin non-lemma forms".
		return pos
	end
end

pos_functions["adjectives"] = function(def, args, data, infl_classes, appendix)
	return adjectives("形容詞", def, args, data, infl_classes, appendix)
end

pos_functions["comparative adjectives"] = function(def, args, data, infl_classes, appendix)
	return adjectives_comp("adjectives", def, args, data, infl_classes, appendix)
end

pos_functions["superlative adjectives"] = function(def, args, data, infl_classes, appendix)
	return adjectives_sup("adjectives", def, args, data, infl_classes, appendix)
end

pos_functions["participles"] = function(def, args, data, infl_classes, appendix)
	return adjectives("分詞", def, args, data, infl_classes, appendix)
end

pos_functions["determiners"] = function(def, args, data, infl_classes, appendix)
	return adjectives("限定詞", def, args, data, infl_classes, appendix)
end

pos_functions["pronouns"] = function(def, args, data, infl_classes, appendix)
	return adjectives("代詞", def, args, data, infl_classes, appendix)
end

pos_functions["suffixes-adjective"] = function(def, args, data, infl_classes, appendix)
	return adjectives("後綴", def, args, data, infl_classes, appendix)
end

pos_functions["numerals-adjective"] = function(def, args, data, infl_classes, appendix)
	return adjectives("數詞", def, args, data, infl_classes, appendix)
end

pos_functions["adverbs"] = function(def, args, data, infl_classes, appendix)
	local params = {
		[1] = {alias_of = 'head'},
		[2] = {alias_of = 'comp'},
		[3] = {alias_of = 'sup'},
		["head"] = {list = true, required = true},
		["comp"] = {list = true},
		["sup"] = {list = true},
		["id"] = {},
	}

	local args = require("Module:parameters").process(args, params)
	data.heads = args.head
	data.no_redundant_head_cat = true -- since head= is required.
	data.id = args.id
	local comp, sup
	local irreg = false

	if args.comp[1] == "-" then
		comp = "-"
	elseif #args.comp > 0 then
		args.comp.label = glossary_link("比較級")
		comp = args.comp
		irreg = true
	end
	if args.comp[1] == "-" or args.sup[1] == "-" then
		sup = "-"
	elseif #args.sup > 0 then
		args.sup.label = glossary_link("最高級")
		sup = args.sup
		irreg = true
	end
	if irreg then
		table.insert(data.categories, "拉丁語不規則副詞")
	end

	if not comp or not sup then
		local default_comp = {label = glossary_link("比較級")}
		local default_sup = {label = glossary_link("最高級")}
		for _, head in ipairs(args.head) do
			local stem = nil
			for _, suff in ipairs({"iter", "nter", "ter", "er", "iē", "ē", "im", "ō"}) do
				stem = rmatch(head, "(.*)" .. suff .. "$")
				if stem ~= nil then
					if suff == "nter" then
						stem = stem .. "nt"
						suff = "er"
					end
					table.insert(default_comp, stem .. "ius")
					table.insert(default_sup, stem .. "issimē")
					break
				end
			end
			if not stem then
				error("Unrecognized adverb type, recognized types are “-ē”, “-er”, “-ter”, “-iter”, “-im”, or “-ō” or specify irregular forms or “-” if incomparable.")
			end
		end
		comp = comp or default_comp
		sup = sup or default_sup
	end

	if comp == "-" then
		table.insert(data.inflections, {label = "无比较级"})
		table.insert(data.categories, "拉丁語無比較級副詞")
	else
		table.insert(data.inflections, comp)
	end
	if sup == "-" then
		if comp ~= "-" then
			table.insert(data.inflections, {label = "无最高级"})
		end
	else
		table.insert(data.inflections, sup)
	end
end

pos_functions["suffixes-adverb"] = pos_functions["adverbs"]

local prepositional_cases = {
	genitive = true, accusative = true, ablative = true,
}

pos_functions["prepositions"] = function(def, args, data, infl_classes, appendix, postscript)
	local params = {
		[1] = {list = true, required = true}, -- headword or cases
		["head"] = {list = true},
		["id"] = {},
	}

	local args = require("Module:parameters").process(args, params)

	-- Case names are supplied in numbered arguments, optionally preceded by
	-- headwords.
	local cases = {}
	while prepositional_cases[args[1][#args[1]]] do
		table.insert(cases, 1, table.remove(args[1]))
	end

	for i = 1, #cases do
		for j = i + 1, #cases do
			if cases[i] == cases[j] then
				error("Duplicate case")
			end
		end
		local case = cases[i]
		local appendix_link = glossary_link(case)
		if i == 1 then
			appendix_link = "+ " .. appendix_link
		end
		table.insert(postscript, appendix_link)
		table.insert(data.categories, "拉丁語" .. case .. "介詞")
	end

	for _, v in ipairs(args[1]) do
		table.insert(args.head, 1, v)
	end

	data.heads = args.head
	data.no_redundant_head_cat = true -- since 1= is required and goes into data.heads
	data.id = args.id
end

pos_functions["gerunds"] = function(def, args, data, infl_classes, appendix, postscript)
	local params = {
		[1] = {required = true, default = "labōrandum"}, -- headword
		[2] = {}, -- gerundive
	}

	local args = require("Module:parameters").process(args, params)

	data.heads = {args[1]}
	data.no_redundant_head_cat = true -- since 1= is required and goes into data.heads
	table.insert(data.inflections, {label = "宾格"})
	local stem = rmatch(args[1], "^(.*)um$")
	if not stem then
		error("Unrecognized gerund ending: " .. stem)
	end
	if args[2] == "-" then
		table.insert(data.inflections, {label = "无动名词"})
	else
		table.insert(data.inflections, {[1] = args[2] or stem .. "us", label = "动名词"})
	end
end

local function non_lemma_forms(def, args, data, infl_classes, appendix, postscript)
	local params = {
		[1] = {required = true, default = def}, -- headword or cases
		["head"] = {list = true, require_index = true},
		["g"] = {list = true},
		["id"] = {},
	}

	local args = require("Module:parameters").process(args, params)

	local heads = {args[1]}
	for _, head in ipairs(args.head) do
		table.insert(heads, head)
	end
	data.heads = heads
	data.no_redundant_head_cat = true -- since 1= is required and goes into data.heads
	data.genders = args.g
	data.id = args.id
end

pos_functions["noun forms"] = non_lemma_forms
pos_functions["proper noun forms"] = non_lemma_forms
pos_functions["pronoun forms"] = non_lemma_forms
pos_functions["verb forms"] = non_lemma_forms
pos_functions["gerund forms"] = non_lemma_forms
pos_functions["adjective forms"] = non_lemma_forms
pos_functions["participle forms"] = non_lemma_forms
pos_functions["determiner forms"] = non_lemma_forms
pos_functions["numeral forms"] = non_lemma_forms
pos_functions["suffix forms"] = non_lemma_forms

pos_functions["名词变格形"] = non_lemma_forms
pos_functions["专有名词变格形"] = non_lemma_forms
pos_functions["代词变格形"] = non_lemma_forms
pos_functions["动词变位形式"] = non_lemma_forms
pos_functions["动名词变格形"] = non_lemma_forms
pos_functions["形容词变格形"] = non_lemma_forms
pos_functions["分词变格形"] = non_lemma_forms
pos_functions["限定词变格形"] = non_lemma_forms
pos_functions["数词变格形"] = non_lemma_forms
pos_functions["前缀变格形"] = non_lemma_forms

pos_functions["名詞變格形"] = non_lemma_forms
pos_functions["專有名詞變格形"] = non_lemma_forms
pos_functions["代詞變格形"] = non_lemma_forms
pos_functions["動詞變位形式"] = non_lemma_forms
pos_functions["動名詞變格形"] = non_lemma_forms
pos_functions["形容詞變格形"] = non_lemma_forms
pos_functions["分詞變格形"] = non_lemma_forms
pos_functions["限定詞變格形"] = non_lemma_forms
pos_functions["數詞變格形"] = non_lemma_forms
pos_functions["前綴變格形"] = non_lemma_forms

pos_functions["副詞"] = pos_functions["adverbs"]

pos_functions["名詞"] = pos_functions["nouns"]

pos_functions["動詞"] = pos_functions["verbs"]

pos_functions["形容詞"] = pos_functions["adjectives"]

pos_functions["後綴名詞"] = pos_functions["suffixes-noun"]

pos_functions["動名詞"] = pos_functions["gerunds"]

pos_functions["專有名詞"] = pos_functions["proper nouns"]

pos_functions["數詞名詞"] = pos_functions["numerals-noun"] 

pos_functions["形容詞比較級"] = pos_functions["comparative adjectives"]

pos_functions["形容詞最高級"] = pos_functions["superlative adjectives"]

pos_functions["分詞"] = pos_functions["participles"]

pos_functions["限定詞"] = pos_functions["determiners"]

pos_functions["代詞"] = pos_functions["pronouns"]

pos_functions["後綴形容詞"] = pos_functions["suffixes-adjective"]

pos_functions["數詞形容詞"] = pos_functions["numerals-adjective"]

pos_functions["後綴副詞"] = pos_functions["suffixes-adverb"]

pos_functions["介詞"] = pos_functions["prepositions"]
return export