ထလာ်တိတ်အာနကဵုမာတိကာညိ

မဝ်ဂျူ:sa-decl/data

နူ ဝိက်ရှေန်နရဳ

ထာမ်ပလိက်တဏအ်ဒးနွံကဵုလိက်စၟတ်သမ္တီမချူလဝ်သာဓက။
သ္ပဂုန်တုဲထာမ်ပလိက်တဏအ်ဒးစၟတ်သမ္တီကဵုလိက်မချူလဝ်နူကဵုဓမံက်ထ္ၜးကဵုဗီုပြၚ်ကိစ္စမရန်တၟအ်လဝ် ကဵု ပရေၚ်မရပ်စပ်လ္တူမုက်လိက်နကဵုလိက်စၟတ်သမ္တီမချူလဝ်သာဓက။

local decl_data = {}

local sa_utils = require("Module:sa-utilities")
local SLP_to_IAST = require("Module:sa-utilities/translit/SLP1-to-IAST")
local IAST_to_SLP = require("Module:sa-utilities/translit/IAST-to-SLP1")

local match = string.match -- sufficient for SLP1
local umatch = mw.ustring.match

-- Make a detection function for ARGS that fetches the stem according to MATCH_RE (which is matched against
-- args.lemma and should have the stem in the first capture). ADDL_CONDITION is an optional function of one
-- argument (ARGS) that must return true for the detection to happen.
local function make_detect(match_re, addl_condition)
	return function(args)
		if addl_condition and not addl_condition(args) then
			return false
		end
		local stem = match(args.lemma, match_re)
		if stem then
			args.stem = stem
			return true
		else
			return false
		end
	end
end

-- Construct all or part of a given noun's declension. Each of SG, DU and PL is a table, whose keys are
-- as follows:
--
-- n = nominative
-- a = accusative
-- v = vocative
-- i = instrumental
-- d = dative
-- ab = ablative
-- g = genitive
-- l = locative
--
-- The corresponding value is one of the following:
-- 1. a "copy spec" such as "[ins]", meaning to copy from the instrumental of the same number;
-- 2. a single string (specifying an ending); or
-- 3. a list of specs, where a spec is either a string (an ending) or a table of the form
--    {"ENDING", stem = "STEM", mono = TRUE/FALSE, note = "NOTE"}. The latter format lets you explicitly specify what
--    the stem is, whether the form is monosyllabic, and what the footnote is. All named keys are optional.
--
-- In forms 2 and 3, if the ending begins with +, the stem defaults to args.lemma; otherwise it defaults to args.stem.
local function decline(args, data, sg, du, pl)
	local cases = {n="nom", a="acc", v="voc", i="ins", d="dat", ab="abl", g="gen", l="loc"}
	local function process_number(endings, tag)
		if not endings then
			return
		end
		for case, es in pairs(endings) do
			if type(es) == "string" and es:find("^%[") then
				-- copy from another case; skip and handle later
			else
				if type(es) == "string" then
					es = {es}
				end
				local forms = {}
				for i, e in ipairs(es) do
					local stem, mono, accent_override, note
					if type(e) == "table" then
						stem = e.stem
						mono = e.mono
						accent_override = e.accent_override
						note = e.note
						e = e[1]
					end
					
					-- reduce Vedic forms to zero in case of novedic parameter
					if args.novedic == true and note and (match(note, "[Vv]edic$") or umatch(note, "Br[aā]hma[nṇ]a")) then
						stem = ""; e = ""; note = ""
					elseif args.norigvedic == true and note and match(note, "Rigvedic$") then
						stem = ""; e = ""; note = ""
					else
						if e:find("^%+") then
							if stem then
								error("Internal error: Can't use + in an ending when stem is explicitly given")
							end
							e = e:gsub("^%+", "")
							stem = args.lemma
						elseif not stem then
							stem = args.stem
						end

						forms[i] = sa_utils.internal_sandhi({
							stem = stem, ending = e, has_accent = args.has_accent, recessive = case == "v", no_syncope = no_syncope,
							mono = mono, root = root, ambig_final = args.ambig_final, diaspirate = args.diaspirate,
							accent_override = accent_override, han = args.han, stem_final_n = stem_final_n, voc_svar = args.voc_svar,
						})
						if note and note~= "" then
							forms["note" .. i] = note
						end
					end
				end
				data.forms[cases[case] .. "_" .. tag] = forms
			end
		end

		-- Now handle cases copied from another.
		for case, es in pairs(endings) do
			if type(es) == "string" and es:find("^%[") then
				-- copy from another case; skip and handle later
				local other_case = es:match("^%[(.*)%]$")
				if not other_case then
					error("Internal error: Unrecognized copy case spec " .. es)
				end
				local other_slot = other_case .. "_" .. tag
				local value = data.forms[other_slot]
				if not value then
					error("Internal error: Slot '" .. other_slot .. "' to copy from is empty")
				end
				local this_slot = cases[case] .. "_" .. tag
				if data.forms[this_slot] then
					error("Internal error: A value already exists for slot '" .. this_slot ..
						"' when copying from '" .. other_slot .. "'")
				end
				data.forms[cases[case] .. "_" .. tag] = value
			end
		end
	end

	process_number(sg, "s")
	process_number(du, "d")
	process_number(pl, "p")
end

decl_data["numeral"] = {
	detect = make_detect("(.+)a/?[nz]?$",
		function(args) return args.g == "gneut" end)
}

setmetatable(decl_data["numeral"], {
	__call = function(self, args, data)
		local oxy = match(args.lemma, "(/?)[nz]?$")
		local acc = match(args.lemma, "(/?)[^/]*$")
		
		data.decl_type = "numeral"
		
		if match(args.lemma, "a/?z$") then -- ṣaṣ
			decline(args, data, {}, {}, {
				n="+",
				a="[nom]",
				v="[nom]",
				i="aqBi" .. oxy .. "s",
				d="aqBya" .. oxy .. "s",
				ab="[dat]",
				g="aRRA" .. oxy .. "m",
				l="awsu" .. oxy,
			})
		else -- pañca(n), etc.
			decline(args, data, {}, {}, {
				n="a" .. oxy,
				a="[nom]",
				v="a",
				i={{"a" .. acc .. "Bis", accent_override = true}},
				d={{"a" .. acc .. "Byas", accent_override = true}},
				ab="[dat]",
				g={{"AnA" .. acc .. "m", accent_override = true}},
				l={{"a" .. acc .. "su", accent_override = true}},
			})
		end
		
		-- no categorisation?
	end
})

decl_data["a"] = {
	detect = make_detect("(.+)a" .. sa_utils.accent .. "?$",
		function(args) return args.g == "m" or args.g == "n" end)
}

setmetatable(decl_data["a"], {
	__call = function(self, args, data)
		local oxy = match(args.lemma, "(" .. sa_utils.accent .. "?)$")

		if args.pronominal then
			data.decl_type = "pronominal a-stem"
		else
			data.decl_type = "တံမအရေဝ်(အ)"
		end

		if args.g == "m" then
			decline(args, data, {
				n="a" .. oxy .. "s",
				a="a" .. oxy .. "m",
				v="a"
			}, {
				n={ "O" .. oxy, { "A" .. oxy, note = "Vedic" } },
				v={ "O", { "A", note = "Vedic" } }
			}, {
				a="A" .. oxy .. "n",
			})
			if args.pronominal then
				decline(args, data, {}, {}, {
					n="e" .. oxy,
					v="e"
				})
			else
				decline(args, data, {}, {}, {
					n={"A" .. oxy .. "s", {"A" .. oxy .. "sas", note="Vedic"}},
					v={"As", {"Asas", note="Vedic"}}
				})
			end
		else
			decline(args, data, {
				n="a" .. oxy .. "m",
				a="[nom]",
				v="a"
			}, {
				n="e" .. oxy,
				v="e"
			}, {
				n={"A" .. oxy .. "ni", {"A" .. oxy, note="Vedic"}},
				a="[nom]",
				v={"Ani", {"A", note="Vedic"}}
			})
		end
		
		if args.pronominal then
			decline(args, data, {
				d="a" .. oxy .. "smE",
				ab="a" .. oxy .. "smAt",
				l="a" .. oxy .. "smin"
			}, {}, {
				g="e" .. oxy .. "zAm"
			})
		else
			decline(args, data, {
				d="A" .. oxy .. "ya",
				ab="A" .. oxy .. "t",
				l="e" .. oxy
			}, {}, {
				g="A" .. oxy .. "nAm"
			})
		end

		decline(args, data, {
			-- For nouns on -tva(na), RV has only three instr.sg. with -ena  (1.110.4, 8.18.13, 10.37.9); see Lanman p.335f. for forms on -ā.
			-- This avoidance is phonetically perfectly understandable for -tvanā instead of -tvanena, and with abstract nouns like this
			-- (predominantly used in the singular), there wouldn't be much cause for confusion with the nom./acc. plural.
			-- (detection of oxytone accent to avoid gerundives on -tva)
			i= match(args.lemma, "tva?n?a/$") and {"e" .. oxy .. "na", {"A" .. oxy, note = "Vedic"}} or "e" .. oxy .. "na",
			g="a" .. oxy .. "sya"
		}, {
			a="[nom]",
			i="A" .. oxy .. "ByAm",
			d="[ins]",
			ab="[ins]",
			-- see Wackernagel III, p.98-99, §48a
			g= match(args.stem, ".y$") and {"a" .. oxy .. "yos", {"o" .. oxy .. "s", note = "Vedic"}} or "a" .. oxy .. "yos",
			l="[gen]"
		}, {
			i={ "E" .. oxy .. "s", { "e" .. oxy .. "Bis", note = "Vedic" } },
			d="e" .. oxy .. "Byas", 
			ab="[dat]",
			l="e" .. oxy .. "zu"
		})
	
		if args.adj == true and args.g == "m" then
			-- table.insert(data.categories, "Sanskrit a-stem adjectives")
		else
			-- table.insert(data.categories, "Sanskrit a-stem nouns")
		end
	end
})

decl_data["iu"] = {
	detect = make_detect("(.+)[iu]" .. sa_utils.accent .. "?$")
}

setmetatable(decl_data["iu"], {
	__call = function(self, args, data)
		local vowel, oxy = match(args.lemma, "([iu])(" .. sa_utils.accent .. "?)$")

		data.decl_type = vowel .. "-stem"

		-- for 'sákhi'; in classical possibly regular in compounds (see Wackernagel vol.III p.141), for which use 'decl=i'
		if match(args.lemma, "[sz]a/?Ki/?$") and not (args.decl and match(args.decl, "i")) then
			decline(args, data, {
				n="A" .. oxy,
				a="A" .. oxy .. "yam",
				i={{stem = args.stem .. vowel, "A" .. oxy }},
				d={{stem = args.stem .. vowel, "e" .. oxy }},
				ab={{stem = args.stem .. vowel, "u" .. oxy .. "r" }},
				l={{stem = args.stem .. vowel, "O" .. oxy }}, -- Wackernagel suggests older *sákhayi (ibid.)
				v="e",
			}, {
				n={"A" .. oxy .. "yO", {"A" .. oxy .. "yA", note = "Vedic"}},
				v={"A" .. oxy .. "yO", {"A" .. oxy .. "yA", note = "Vedic"}},
				g={{stem = args.stem .. vowel, "o" .. oxy .. "s" }},
			}, {
				n="A" .. oxy .. "yas",
				v="Ayas",
				a="I" .. oxy .. "n",
			})
		elseif args.g == "m" then
			-- see Wackernagel III, p.138-9 ('flexion forte'); nom./acc.pl. on -as only to be given when attested
			if match(args.lemma, "^ari/$") or match(args.lemma, "^a/vi$")
			or match(args.lemma, "^kra/tu$") or match(args.lemma, "^paSu/$") or match(args.lemma, "^pitu/$")
			or match(args.lemma, "^ma/Du$") or match(args.lemma, "^va/su$") or match(args.lemma, "^Si/Su$") then
				decline(args, data, {
					i= { "+nA", {stem = args.stem .. vowel, "A" .. oxy, note = "Vedic" }},
					d={{stem = args.stem .. sa_utils.up_one_grade[vowel .. oxy], "e" }, {stem = args.stem .. vowel, "e" .. oxy, note = "Vedic"}},
					ab={{stem = args.stem .. sa_utils.up_one_grade[vowel .. oxy], "s"}, {stem = args.stem .. vowel, "a" .. oxy .. "s", note = "Vedic"}},
				}, {}, {})
			else
				decline(args, data, {
					-- see Wackernagel III, p.146-7
					i= (match(args.lemma, "^Urmi/$") or match(args.lemma, "^pavi/$") or match(args.lemma, "paraSu/"))
						and { "+nA", {stem = args.stem .. vowel, "A" .. oxy, note = "Vedic" } } or "+nA",
					d={{stem = args.stem .. sa_utils.up_one_grade[vowel .. oxy], "e" }},
					ab={{stem = args.stem .. sa_utils.up_one_grade[vowel .. oxy], "s"}}, -- code written like this for the case of 'titaü'
				}, {}, {})
			end
			
			if vowel == "i" then
				decline(args, data, {
					l={ "O" .. oxy, { "A" .. oxy, note = "Vedic"}}, -- Whitney §336f
				}, {}, {})
			-- see Macdonell p.296 and Wackernagel III p.154
			elseif match(args.lemma, "^a/nu$") or match(args.lemma, "^da/syu$") or match(args.lemma, "^trasa/dasyu$")
			or match(args.lemma, "^druhyu/$") or match(args.lemma, "^pa/vIru$")	or match(args.lemma, "^vi/zRu$") or match(args.lemma, "^sUnu/$") then
				decline(args, data, {
					l={ "O" .. oxy, {"a" .. oxy .. "vi", note = "Rigvedic"}},
				}, {}, {})
			else
				decline(args, data, {
					l= "O" .. oxy
				}, {}, {})
			end
			
			decline(args, data, {
				n="+s",
				a="+m",
				v={{stem= args.stem .. sa_utils.up_one_grade[vowel], ""}},
			}, {
				n={{stem = args.stem .. sa_utils.lengthen[vowel..oxy], ""}},
				v={{stem = args.stem .. sa_utils.lengthen[vowel], ""}},
				g={{stem = args.stem .. vowel, "o" .. oxy .. "s"}},
			}, {
				n={{stem = args.stem .. sa_utils.up_one_grade[vowel .. oxy], "as"}},
				a={{stem = args.stem ..sa_utils.lengthen[vowel..oxy], "n"}},
				v={{stem = args.stem .. sa_utils.up_one_grade[vowel], "as"}},
			})
		elseif args.g == "f" then
			if vowel == "i" then
				decline(args, data, {
					i={{stem = args.stem .. vowel, "A" .. oxy}, {"I" .. oxy, note = "Vedic"}},  -- Whitney §336c
					l={ "O" .. oxy, {stem = args.stem .. vowel, "A" .. oxy .. "m" }, { "A" .. oxy, note = "Vedic"} },
				}, {}, {})
			else
				decline(args, data, {
					i={{stem = args.stem .. vowel, "A" .. oxy }},
					l={ "O" .. oxy, {stem = args.stem .. vowel, "A" .. oxy .. "m" } },
				}, {}, {})
			end
			decline(args, data, {
				n="+s",
				a="+m",
				-- the Rigvedic dat.sg. on -ī (mostly ūtī́) seems too rare to extrapolate to every stem (see also Macdonell p.282)
				d={{stem = args.stem .. sa_utils.up_one_grade[vowel .. oxy], "e"}, {stem = args.stem .. vowel, "E" .. oxy } }, 
				ab={{stem = args.stem .. sa_utils.up_one_grade[vowel .. oxy], "s"}, {stem = args.stem .. vowel, "A" .. oxy .. "s" }, 
					{stem = args.stem .. vowel, "E" .. oxy, note = "Brāhmaṇas"}	},
				v=sa_utils.up_one_grade[vowel],
			}, {
				n=sa_utils.lengthen[vowel..oxy],
				v=sa_utils.lengthen[vowel],
				g={{stem = args.stem .. vowel, "o" .. oxy .. "s"}},
			}, {
				n={{stem = args.stem .. sa_utils.up_one_grade[vowel .. oxy], "as"}},
				a=sa_utils.lengthen[vowel..oxy] .. "s",
				v={{stem = args.stem .. sa_utils.up_one_grade[vowel], "as"}},
			})
		else -- neuter
			if args.adj == true then -- for difference with nouns see Whitney §344
				-- see Wackernagel III, p.138-9 ('flexion forte'); nom./acc.pl. on -as only to be given when attested
				if match(args.lemma, "^ma/Du$") or match(args.lemma, "^va/su$") then
					decline(args, data, {
						d={ "+ne", {stem = args.stem ..  "a" .. oxy .. "v", "e"}, {stem = args.stem .. "v", "e" .. oxy, note = "Vedic"}},
						ab={ "+nas", {stem = args.stem .. "o" .. oxy, "s"}, {stem = args.stem .. vowel, "a" .. oxy .. "s", note = "Vedic"}},
					}, {}, {})
				else
					decline(args, data, {
						d={ "+ne", {stem = args.stem ..  "a" .. oxy .. "y", "e"}},
						ab={ "+nas", {stem = args.stem .. "e" .. oxy, "s"}},
					}, {}, {})
				end
				if vowel == "i" then
					decline(args, data, {
						l={ "+ni", "O" .. oxy, {"A" .. oxy, note = "Vedic"}}, -- Whitney §336f + §340f
					}, {}, {})
				else decline(args, data, {
						l={ "+ni", "O" .. oxy},
					}, {}, {})
				end
				decline(args, data, {}, {
					g={ "+nos", {stem = args.stem .. vowel, "o" .. oxy .. "s" }},
				}, {})
			else -- nouns
				-- see Wackernagel III, p.138-9 ('flexion forte'); nom./acc.pl. on -as only to be given when attested
				if match(args.lemma, "^pa/Su$") or match(args.lemma, "^ma/Du$") or match(args.lemma, "^va/su$") then
					decline(args, data, {
						d={ "+ne", {stem = args.stem .. "a" .. oxy .. "v", "e", note = "Vedic" },
							{stem = args.stem .. "v", "e" .. oxy, note = "Vedic"}},
						ab={ "+nas", {stem = args.stem .. "o" .. oxy, "s", note = "Vedic"},
							{stem = args.stem .. vowel, "a" .. oxy .. "s", note = "Vedic"}},
					}, {}, {})
				else
					decline(args, data, {
						d={ "+ne", {stem = args.stem ..  "a" .. oxy .. "v", "e", note = "Vedic" }},
						ab={ "+nas", {stem = args.stem .. "o" .. oxy, "s", note = "Vedic"}},
					}, {}, {})
				end
				if vowel == "i" then
					decline(args, data, {
						l={ "+ni", {"O" .. oxy, note = "Vedic"}, {"A" .. oxy, note = "Vedic"}}, -- Whitney §336f + §340f
					}, {}, {})
				else decline(args, data, {
						l={ "+ni", {"O" .. oxy, note = "Vedic"}},
					}, {}, {})
				end
				decline(args, data, {}, {
					g={ "+nos", {stem = args.stem .. vowel, "o" .. oxy .. "s", note = "Vedic" }},
				}, {})
			end
			decline(args, data, {
				n="+",
				a="[nom]",
				-- only 'mádhu' has Vedic instr.sg. on -ā
				i= match(args.lemma, "^ma/Du$") and { "+nA", {"+A", note = "Vedic" }} or "+nA",
				v={ "+", {stem= args.stem .. sa_utils.up_one_grade[vowel], ""}},
			}, {
				n={ "+nI", {stem = args.stem .. vowel, "I" .. oxy, note = "Vedic" }},
				v={ "+nI", {stem = args.stem .. vowel, "I", note = "Vedic" }},
			}, {
				n={{ stem = args.stem .. sa_utils.lengthen[vowel .. oxy], "ni" }, { "+", note = "Vedic"}, 
					{ stem = args.stem .. sa_utils.lengthen[vowel..oxy], "", note = "Vedic"}},
				a="[nom]",
				v={{ stem = args.stem .. sa_utils.lengthen[vowel], "ni" }, { "+", note = "Vedic"},
					{ stem = args.stem .. sa_utils.lengthen[vowel], "", note = "Vedic"}},
			})
		end
		decline(args, data, {
			g="[abl]",
		}, {
			a="[nom]",
			i="+ByAm",
			d="[ins]",
			ab="[ins]",
			l="[gen]",
		}, {
			i="+Bis",
			d="+Byas",
			ab="[dat]",
			g={{ stem = args.stem .. sa_utils.lengthen[vowel], "nA" .. (oxy ~= "" and "/" or "") .. "m"}},
			l="+su",
		})
		
		if args.adj == true and args.g == "m" then
			-- table.insert(data.categories, "Sanskrit " .. vowel .. "-stem adjectives")
		else
			-- table.insert(data.categories, "Sanskrit " .. vowel .. "-stem nouns")
		end
	end
})

decl_data["A"] = {
	detect = function(args)
		if make_detect("(.+)A" .. sa_utils.accent .. "?$")(args) then
			args.true_mono = sa_utils.is_monosyllabic(args.lemma)
			return true
		else
			return false
		end
	end
}

setmetatable(decl_data["A"], {
	__call = function(self, args, data)
		local oxy = match(args.lemma, "(" .. sa_utils.accent .. "?)$")

		if args.g == "n" then
			-- It seems better not to base a neuter declension on the masculine nominatives on -ās accompanying neuter nouns.
			-- See Whitney §367b and Wackernagel vol.III p.129
			error("The neuter of radical stems in -ā ends in -a.")
		end

		if args.pronominal then
			data.decl_type = "pronominal ā-stem"
		else
			data.decl_type = "တံမအရေဝ်(-ာ)"
		end

		if not (args.root or args.true_mono) or (args.true_mono and args.root == false) then -- derived stem
			if args.g == "m" then
				error("Add 'root=1' in case this is a root noun or adjective.")
			else
				-- table.insert(data.categories, "Sanskrit ā-stem nouns")
			end
			if args.pronominal then
				decline(args, data, {
					d="a" .. oxy .. "syE",
					ab={"a" .. oxy .. "syAs", {"a" .. oxy .. "syE", note = "Brāhmaṇas"}},
					l="a" .. oxy .. "syAm"
				}, {}, {
					g="+sAm"
				})
			else
				decline(args, data, {
					d="+yE",
					ab={"+yAs", {"+yE", note = "Brāhmaṇas"}},
					l="+yAm"
				}, {}, {
					g="+nAm"
				})
			end
			decline(args, data, {
				n="+",
				i={ "a" .. oxy .. "yA", { "+", note = "Vedic" } },
				v="e",
			}, {
				n="e" .. oxy,
				g="a" .. oxy .. "yos",
				v="e",
			}, {
				a="+s",
			})
		else
			if args.adj == true and args.g == "m" then
				-- table.insert(data.categories, "Sanskrit root-stem adjectives on -ā")
			else
				-- table.insert(data.categories, "Sanskrit root-stem nouns on -ā")
			end
			if args.g == "m" then
				data.general_notes = {"In practice, this declension only occurs in early Vedic<br> and is later replaced by a-stem declension."}
			else
				data.general_notes = {"In practice, this declension only occurs in early Vedic<br> and is later replaced by regular ā-stem declension."}
			end
			
			decline(args, data, {
				n="+s",
				i="+",
				d="e" .. oxy,
				ab="a" .. oxy .. "s",
				l="i" .. oxy,
				v="+s",
			}, {
				n={ "O" .. oxy, { "A" .. oxy, note = "Vedic" } },
				g="o" .. oxy .. "s",
				v={ "O", { "A", note = "Vedic" } }
			}, {
				a={ "+s", { "a" .. oxy .. "s", note = "Perhaps" } },
				g={ "+nAm", { "+Am", note = "Perhaps" } },
			})
		end
		decline(args, data, {
			a="+m",
			g="[abl]",
		}, {
			a="[nom]",
			i="+ByAm",
			d="[ins]",
			ab="[ins]",
			l="[gen]",
		}, {
			n="+s",
			i="+Bis",
			d="+Byas",
			ab="[dat]",
			l="+su",
			v="+s",
		})
	end
})

decl_data["IU"] = {
	detect = function(args)
		if make_detect("(.+)[IU]" .. sa_utils.accent .. "?$")(args) then
			args.true_mono = sa_utils.is_monosyllabic(args.lemma)
			return true
		else
			return false
		end
	end
}

setmetatable(decl_data["IU"], {
	__call = function(self, args, data)
		local vowel, oxy = match(args.lemma, "([IU])(" .. sa_utils.accent .. "?)$")

		data.decl_type = SLP_to_IAST.tr(vowel) .. "-stem"

		if args.g == "n" then
			-- it seems better to mention the one or two Vedic forms that could be interpreted
			-- as coming from a neuter ī-stem (see Whitney §367b) separately
			error("The neuter of radical stems in -ī/ū ends in -i/u.")
		end

		if not (args.root or args.true_mono) or (args.true_mono and args.root == false) or args.han then -- derived stem
			
			if args.vrki then -- with original vṛkī-declension (Whitney §355 ff.), given as older forms of later classical declension
				if vowel == "U" then
					error()
				elseif args.adj == true and args.g == "m" then
					-- table.insert(data.categories, "Sanskrit ī-stem adjectives with original vṛkī́-declension")
				else
					-- table.insert(data.categories, "Sanskrit ī-stem nouns with original vṛkī́-declension")
				end
				data.general_notes = {"This stem originally had ''vṛkī́''-declension."}
				
				decline(args, data, {
					n={"+", {"+s", note = "Vedic"}},
					a={"+m", {"+am", note = "Vedic"}},
					i={{stem = args.stem .. vowel, "A" .. oxy}, {"+A", note = "Vedic"}},
					d={{stem = args.stem .. vowel, "E" .. oxy}, {"+e", note = "Vedic"}},
					ab={{stem = args.stem .. vowel, "A" .. oxy .. "s"}, {"+as", note = "Vedic"}, 
						{stem = args.stem .. vowel, "E" .. oxy, note = "Brāhmaṇas"}},
					l={{stem = args.stem .. vowel, "A" .. oxy .. "m"}, {"+", note = "Vedic"}}, -- Wackernagel III, §86c, p.170
				}, {
					n={"+O", {"+A", note = "Vedic"}},
					v={"+O", {"+A", note = "Vedic"}},
					g={{stem = args.stem .. vowel, "o" .. oxy .. "s"}, {"+os", note = "Vedic"}},
				}, {
					n="+as",
					v="+as",
					a={"+s", {"+as", note = "Vedic"}},
					g="+nAm",
				})
			elseif vowel == "I" then -- devī-declension
				if args.adj == true and args.g == "m" then
					-- table.insert(data.categories, "Sanskrit ī-stem adjectives")
				else
					-- table.insert(data.categories, "Sanskrit ī-stem nouns")
				end
				decline(args,data, {
					n="+",
					a="+m",
					i={{stem = args.stem .. vowel, "A" .. oxy}},
					d={{stem = args.stem .. vowel, "E" .. oxy}},
					ab={{stem = args.stem .. vowel, "A" .. oxy .. "s"}, {stem = args.stem .. vowel, "E" .. oxy, note = "Brāhmaṇas"}},
					l={{stem = args.stem .. vowel, "A" .. oxy .. "m"}},
				}, {
					n={ "+O", { "+", note = "Vedic" } },
					v={ "+O", { "+", note = "Vedic" } },
					g={{stem = args.stem .. vowel, "o" .. oxy .. "s"}},
				}, {
					n={ "+as", { "+s", note = "Vedic" } },
					v={ "+as", { "+s", note = "Vedic" } },
					a="+s",
					g= oxy == "" and "+nAm" or {"+nAm", { stem = args.stem .. vowel, "nA" .. oxy .. "m", note = "Rigvedic"} }, -- Whitney §319a
				})
			else -- ū-stems, with Whitney's declension at §356a given as older forms of §364, see Wackernagel vol.III p.189
				if args.adj == true and args.g == "m" then
					-- table.insert(data.categories, "Sanskrit ū-stem adjectives") -- probably only bahuvrihis like 'sarvatanū'
				else
					-- table.insert(data.categories, "Sanskrit ū-stem nouns")
				end
				if match(args.stem, "y$") or match(args.stem, "agr$")
				or match(args.stem, "kadr$") or match(args.stem, "bIBats") then -- with Vedic -úv-, see Macdonell p.290 §384a
					decline(args,data, {
						a={"+m", {"+am", note = "Vedic", mono = true}},
						i={{stem = args.stem .. vowel, "A" .. oxy}, {"+A", note = "Vedic", mono = true}},
						d={{stem = args.stem .. vowel, "E" .. oxy}, {"+e", note = "Vedic", mono = true}},
						ab={{stem = args.stem .. vowel, "A" .. oxy .. "s"}, {"+as", note = "Vedic", mono = true},
							{stem = args.stem .. vowel, "E" .. oxy, note = "Brāhmaṇas"}},
						l={{stem = args.stem .. vowel, "A" .. oxy .. "m"}, {"+i", note = "Vedic", mono = true},
							{"+", note = "Vedic"}},
					}, {
						n={"+O", {"+A", note = "Vedic", mono = true}},
						v={"+O", {"+A", note = "Vedic", mono = true}},
						g={{stem = args.stem .. vowel, "o" .. oxy .. "s"}, {"+os", note = "Vedic", mono = true}},
					}, {
						n={"+as", {"+as", note = "Vedic", mono = true}},
						v={"+as", {"+as", note = "Vedic", mono = true}},
						a={"+s", {"+as", note = "Vedic", mono = true}},
					})
				else
					decline(args,data, {
						a={"+m", {"+am", note = "Vedic"}},
						i= oxy == "" and "+A" or {{stem = args.stem .. vowel, "A" .. oxy}, {"+A", note = "Vedic"}},
						d={{stem = args.stem .. vowel, "E" .. oxy}, {"+e", note = "Vedic"}},
						ab={{stem = args.stem .. vowel, "A" .. oxy .. "s"}, {"+as", note = "Vedic"},
							{stem = args.stem .. vowel, "E" .. oxy, note = "Brāhmaṇas"}},
						l={{stem = args.stem .. vowel, "A" .. oxy .. "m"}, {"+i", note = "Vedic"},
							{"+", note = "Vedic"}}, -- Wackernagel III, §97b, p.188
					}, {
						n={"+O", {"+A", note = "Vedic"}}, -- duals on -ū for ū-stems do not occur
						v={"+O", {"+A", note = "Vedic"}},
						g= oxy == "" and "+os" or {{stem = args.stem .. vowel, "o" .. oxy .. "s"}, {"+os", note = "Vedic"}},
					}, {
						n="+as", -- RV 4.41.8 has 'yuvayū́s', but it seems better to simply give this at the relevant lemma
						v="+as",
						a={"+s", {"+as", note = "Vedic"}},
					})
				end
				decline(args,data, {
					n="+s",
				}, {}, {
					g="+nAm",
				})
			end
			decline(args, data, {
				g="[abl]",
				v=sa_utils.shorten[vowel],
			}, {
				a="[nom]",
				i="+ByAm",
				d="[ins]",
				ab="[ins]",
				l="[gen]",
			}, {
				i="+Bis",
				d="+Byas",
				ab="[dat]",
				l="+su",
			})
		else
			if args.adj == true and args.g == "m" then
				-- table.insert(data.categories, "Sanskrit root-stem adjectives on -" .. SLP_to_IAST.tr(vowel))
			else
				-- table.insert(data.categories, "Sanskrit root-stem nouns on -" .. SLP_to_IAST.tr(vowel))
			end
			if args.true_mono then -- rout nouns, Whitney §351
				no_syncope = true
				if args.g == "f" then -- see Wackernagel vol.III p.181
					decline(args, data, {
						d={{ "+e" .. oxy, mono = true }, { "+E" .. oxy, mono = true, note = "Later Sanskrit" } },
						ab={{ "+a" .. oxy .. "s", mono = true }, { "+A" .. oxy .. "s", mono = true, note = "Later Sanskrit" } },
						l={{ "+i" .. oxy, mono = true }, { "+A" .. oxy .. "m", mono = true, note = "Later Sanskrit" } },
					})
				else
					decline(args, data, {
						d={{ "+e" .. oxy, mono = true } },
						ab={{ "+a" .. oxy .. "s", mono = true } },
						l={{ "+i" .. oxy, mono = true } },
					})
				end
				decline(args, data, {
					i={{ "+A" .. oxy, mono = true}},
				}, {
					i={{"+ByA" .. oxy .. "m", mono = true}},
					g={{"+o" .. oxy .. "s", mono = true}},
				}, {
					i={{"+Bi" .. oxy .. "s", mono = true}},
					d={{"+Bya" .. oxy .. "s", mono = true}},
					g={{"+A" .. oxy .. "m", mono = true }, { "+nA" .. oxy .. "m", mono = true } },
					l={{"+su" .. oxy, mono = true}},
				})
			else -- root terms in compounds, Whitney §352; I'd suggest to give two tables when both contracted and non-contracted forms occur
				-- see Wackernagel III §91a (p.179) for ī, and §100a + §100eα (p.192-3) for ū; Macdonell: p.268 + 288
				-- Taking as default that ū-stems with accent (rule of thumb for Vedic terms) are not contracted. To be overrided with 'contract=1'
				if args.contract == false or (args.contract ~= true and (match(args.stem, sa_utils.consonant .. sa_utils.consonant .. "$") 
				or vowel == "U" and (match(args.lemma, sa_utils.accent) or match(args.stem, "B$") or match(args.stem, "jo/?g$"))
				or vowel == "I" and (match(args.stem, "avadyaB$") or (match(args.stem, "D$") and match(args.stem, sa_utils.accent))))) then
					no_syncope = true  -- -Ciy-/-Cuv- when followed by vowel
					
					decline(args, data, {
						l= args.g == "f" and {"+i", {"+Am", note = "Later Sanskrit"}} or "+i",
					})
				else
					decline(args, data, {
						l= args.g == "f" and {{ stem = args.stem .. sa_utils.vowel_to_cons[vowel], "i" .. (oxy ~= "" and "\\" or "") }, {"+Am", note = "Later Sanskrit"}} 
						or {{ stem = args.stem .. sa_utils.vowel_to_cons[vowel], "i" .. (oxy ~= "" and "\\" or "") }},
					})
				end
				if args.g == "f" then -- see Wackernagel vol.III p.181
					decline(args, data, {
						d={ "+e", { "+E", note = "Later Sanskrit" } },
						ab={ "+as", { "+As", note = "Later Sanskrit" } },
					})
				else
					decline(args, data, {
						d= "+e",
						ab= "+as",
					})
				end
				decline(args, data, {
					i= "+A",
				}, {
					i="+ByAm",
					g= "+os",
				}, {
					i="+Bis",
					d="+Byas",
					g= {"+Am", "+nAm"},
					l="+su",
				})
			end
			decline(args, data, {
				n= "+s",
				v= "+s",
				a= "+am",
				g="[abl]",
			}, {
				n= { "+O", { "+A", note = "Vedic"}},
				v= { "+O", { "+A", note = "Vedic"}},
				a="[nom]",
				d="[ins]",
				ab="[ins]",
				l="[gen]",
			}, {
				n= "+as",
				v= "+as",
				a="[nom]",
				ab="[dat]",
			})
		end
	end
})

decl_data["f"] = { -- for nouns in ṛ
	detect = make_detect("(.+)f" .. sa_utils.accent .. "?$")
}

setmetatable(decl_data["f"], { -- for nouns in ṛ
	__call = function(self, args, data)
		if args.g == "f" and args.r_stem_a == "ā" and match(args.stem, "t$") and not match(args.stem, "^s?t$") then
			error("The feminine of adjectives in -tṛ ends in -trī.")
		end
		
		local oxy = match(args.lemma, "(" .. sa_utils.accent .. "?)$")

		data.decl_type = SLP_to_IAST.tr("f") .. "-stem"

		if args.g == "n" then
			decline(args, data, {
				-- for neuter -úr, see Wackernagel, III, p.204, §107b (denies 'sthātar' and 'sthātṝ́n' are neuter sg.)
				-- all cases of -tári given as nom./acc.n.sg. by Macdonell (p.243) are translated as locatives by Jamison & Brereton 
				n={"f" .. oxy, {"u" .. oxy .. "r", note = "Rigvedic"}}, 
				a="[nom]",
				i={"+nA", "rA" .. oxy},
				d={"+ne", "re" .. oxy},
				ab={"+nas", "u" .. oxy .. "r"},
				l={"+ni", "a" .. oxy .. "ri"},
				v={ "f", "ar" },
			}, {
				n="+nI",
				g={"+nos", "ro" .. oxy .. "s"},
				v="+nI",
			}, {
				n="F" .. oxy .. "ni",
				a="[nom]",
				v="Fni",
			})
		else
            if not args.r_stem_a then
            	error('Please specify the length of the accusative singular vowel with r_stem_a = "a" or "ā".')
            else
            	args.r_stem_a = IAST_to_SLP.tr(args.r_stem_a)
            end
			decline(args, data, {
				n="A" .. oxy,
				a=args.r_stem_a .. oxy .. "ram",
				i="rA" .. oxy,
				d="re" .. oxy,
				ab="u" .. oxy .. "r",
				l="a" .. oxy .. "ri",
				v="ar",
			}, {
				n={ args.r_stem_a .. oxy .. "rO", { args.r_stem_a .. oxy .. "rA", note = "Vedic" } },
				g="ro" .. oxy .. "s",
				v={ args.r_stem_a .. "rO", { args.r_stem_a .. "rA", note = "Vedic" } },
			}, {
				n=args.r_stem_a .. oxy .. "ras",
				a=args.g == "f" and "F" .. oxy .. "s" or "F" .. oxy .. "n",
				v=args.r_stem_a .. "ras",
			})
		end
		decline(args, data, {
			g="[abl]",
		}, {
			a="[nom]",
			i="+ByAm",
			d="[ins]",
			ab="[ins]",
			l="[gen]",
		}, {
			i="+Bis",
			d="+Byas",
			ab="[dat]",
			g={{stem = args.stem .. "F", "nA" .. (oxy ~= "" and "/" or "") .. "m"}},
			l="+su",
		})
		
		if args.adj == true and args.g == "m" then
			-- table.insert(data.categories, "Sanskrit " .. SLP_to_IAST.tr("f") .. "-stem adjectives")
		else
			-- table.insert(data.categories, "Sanskrit " .. SLP_to_IAST.tr("f") .. "-stem nouns")
		end
	end
})

decl_data["root"] = {		-- for root nouns ending on -k/g/gh/c/j/ṭ/ḍ/th/d/dh/p/bh/m/r/ṣ/ś/h + -āt/it/ut/ṛt/akt + -ās/os
	detect = function(args)
		-- capture of vowel needed for accent
		if (make_detect("(.*)" .. sa_utils.vowel .. sa_utils.accent .. "?r?[kgGcjwqTdDpBmrzSh]$")(args) 
		or (make_detect("(.*)[Aiuf]" .. sa_utils.accent .. "?t$")(args) and not args.participle == true)
		or make_detect("(.*)a" .. sa_utils.accent .. "?kt$")(args) -- nákt-
		or make_detect("(.*)[Ao]" .. sa_utils.accent .. "?s$")(args)
		or (args.root == true and make_detect("(.*)[ai]" .. sa_utils.accent .. "?[ts]$")(args) )) -- + āśis + numerals like triṃśát
		and args.g ~= "gneut" then
			args.true_mono = sa_utils.is_monosyllabic(args.lemma)
			return true
		else
			return false
		end
	end
}

setmetatable(decl_data["root"], {
	__call = function(self, args, data)
		local vowel, oxy, extra, cons = match(args.lemma, "(" .. sa_utils.vowel .. ")(" .. sa_utils.accent .. "?)(r?)([kgGcjwqtTdDpBmrzShs]t?)$")
		if match(cons, "[jSh]") ~= nil and not args.ambig_final then
			error('Please specify the final consonant in the nominative singular with ambig_final = "k" or "ṭ".')
		elseif args.ambig_final then -- also for cases like sarágh > saráṭ or potentially anuṣṭúbh > anuṣṭúk
			args.ambig_final = IAST_to_SLP.tr(args.ambig_final)
		end
		local nasal = cons:gsub(".", sa_utils.homorganic_nasal)

		data.decl_type = "root-stem"
		root = true
		
		-- neuter nom. plural (Whitney §389c, although saying that these forms with infixed nasal are not attested for nouns)
		if args.g == "n" then
			if match(cons, "[mr]") == nil and extra == "" then
				decline(args, data, nil, nil, {
					n= {{stem = args.stem .. vowel .. oxy .. nasal .. cons, "i"}},
					v= {{stem = args.stem .. vowel .. nasal .. cons, "i"}},
				})
			else
				decline(args, data, nil, nil, {
					n= "+i",
					v= "+i",
				})
			end
		end
		
		if args.true_mono then -- monosyllabic stem
			if args.g ~= "n" then
				decline(args, data, {
					-- nom.sg. -s is an 'invisible' (historical) ending to provoke vowel lengthening in -ir/-ur stems, see Whitney §392
					-- this ending should be removed by internal_sandhi at an early stage
					n="+s",
					a="+am",
					v="+s",
				}, {
					n={ "+O", { "+A", note = "Vedic" } },
					v={ "+O", { "+A", note = "Vedic" } },
				}, {
					n="+as",
					v="+as",
				})
			else
				decline(args, data, {
					n= "+",
					a="[nom]",
					v= "+"
				}, {
					n= {{"+I" .. oxy, mono = true }},
					v= "+I"
				}, { })
			end
			decline(args, data, {
				i= {{ "+A" .. oxy, mono = true }},
				d= {{ "+e" .. oxy, mono = true }},
				ab= {{ "+a" .. oxy .. "s", mono = true }},
				g="[abl]",
				l= {{ "+i" .. oxy, mono = true }},
			}, {
				a= "[nom]",
				i= {{ "+ByA" .. oxy .. "m", mono = true }},
				d="[ins]",
				ab="[ins]",
				g= {{ "+o" .. oxy .. "s", mono = true }},
				l="[gen]",
			}, {
				a= "[nom]",
				i= {{ "+Bi" .. oxy .. "s", mono = true }},
				d= {{ "+Bya" .. oxy .. "s", mono = true }},
				ab="[dat]",
				g= {{ "+A" .. oxy .. "m", mono = true }},
				l= {{ "+su" .. oxy, mono = true }},
			})
		
		else -- polysyllabic stem, no accent on ending		
			if args.g ~= "n" then
				decline(args, data, {
					n="+s",
					a="+am",
					v="+s",
				}, {
					n={ "+O", { "+A", note = "Vedic" } },
					v={ "+O", { "+A", note = "Vedic" } },
				}, {
					n="+as",
					v="+as"
				})
			else
				decline(args, data, {
					n= "+",
					a="[nom]",
					v= "+"
				}, {
					n= "+I",
					v= "+I",
				}, { })
			end
			decline(args, data, {
				i="+A",
				d="+e",
				ab="+as",
				g="[abl]",
				l="+i",
			}, {
				a= "[nom]",
				i= "+ByAm",
				d="[ins]",
				ab="[ins]",
				g="+os",
				l="[gen]",
			}, {
				a= "[nom]",
				i= "+Bis",
				d= "+Byas",
				ab="[dat]",
				g= "+Am",
				l= "+su",
			})
		end
		
		if args.adj == true and args.g == "m" then
			-- table.insert(data.categories, "Sanskrit consonantal root-stem adjectives")
		else
			-- table.insert(data.categories, "Sanskrit consonantal root-stem nouns")
		end
	end
})

decl_data["[aiu]s"] = {
	detect = make_detect("(.+)[aiu]" .. sa_utils.accent .. "?s$")
}

setmetatable(decl_data["[aiu]s"], {
	__call = function(self, args, data)
		local vowel, oxy = match(args.lemma, "([aiu])(" .. sa_utils.accent .. "?)s$")
		
		data.decl_type = vowel .. "တံမအရေဝ်(သ်)"
		
		if (match(args.stem, "[^aAiK/\\]"..sa_utils.accent.."?y$") or match (args.stem, "jyA/?y$"))
		and vowel == "a" then -- comparative
			comp = "1"
		else comp = "0" end
		
		if comp == "1" and args.g ~= "n" then
			if args.g == "m" then
				decline(args, data, {
					n="An",
					a="AMsam",
					v={"an", { "+", note = "Rigvedic"}},
				}, {
					n={"AMsO", {"AMsA", note = "Vedic"}},
					v={"AMsO", {"AMsA", note = "Vedic"}},
				}, {
					n="AMsas",
					a="+as",
					v="AMsas",
				})
			else
				error("The feminine of comparatives in -yas ends in -yasī.")
			end
		elseif args.g == "m" or args.g == "f" then
			if vowel == "a" then
				decline(args, data, {
					n="A" .. oxy .. "s",
					a={ "+am", { "A" .. oxy .. "m", note = "Vedic" } },
					v="+",
				}, nil, {
					n={ "+as", { "A" .. oxy .. "s", note = "Vedic" } },
					v={ "+as", { "A" .. oxy .. "s", note = "Vedic" } },
				})
			else
				decline(args, data, {
					n="+",
					a="+am",
					v={ "+",{ sa_utils.up_one_grade[vowel], note = "Vedic" } }, -- Wackernagel vol.III, p.292, §152c
				}, nil, {
					n="+as",
					v="+as",
				})
			end

			decline(args, data, nil, {
				n={ "+O", { "+A", note = "Vedic" } },
				v={ "+O", { "+A", note = "Vedic" } },
			}, {
				a="[nom]",
			})
		else
			if args.adj == true and vowel == "a" and comp == "0"
			and (match(args.stem, sa_utils.vowel .. ".*" .. sa_utils.vowel) or match(args.stem, "^sv")) then
				-- compounds on -as, see Macdonell, p.226 + Whitney §418 a
				decline(args, data, {
					n={ "+", { "A" .. oxy .. "s", note = "Vedic"} },
				}, {}, {})
			else
				decline(args, data, {
					n="+",
				}, {}, {})
			end
			decline(args, data, {
				a="[nom]",
				v="+",
			}, {
				n="+I",
				v="+I",
			}, {
				n={{stem = args.stem .. sa_utils.lengthen[vowel] .. oxy .. "Ms", "i"}},
				v={{stem = args.stem .. sa_utils.lengthen[vowel] .. "Ms", "i"}},
				a="[nom]",
			})
		end

		decline(args, data, {
			i="+A",
			d="+e",
			ab="+as",
			g="[abl]",
			l="+i",
		}, {
			a="[nom]",
			i= "+ByAm",
			d="[ins]",
			ab="[ins]",
			g="+os",
			l="[gen]",
		}, {
			i= "+Bis",
			d= "+Byas",
			ab="[dat]",
			g="+Am",
			l= {{stem = args.stem .. vowel .. oxy, "Hsu"}, {stem = args.stem .. vowel .. oxy, "ssu"}},
		})
		
		if comp == "1" then
			-- table.insert(data.categories, "Sanskrit yas-stem adjectives")
		elseif args.adj == true and args.g == "m" then
			-- table.insert(data.categories, "Sanskrit " .. vowel .. "s-stem adjectives")
		else
			-- table.insert(data.categories, "Sanskrit " .. vowel .. "s-stem nouns")
		end
	end
})

decl_data["an"] = {
	detect = make_detect("(.+)a" .. sa_utils.accent .. "?n$",
		function(args) return args.g ~= "gneut" end) -- not gender-neutral numerals
}

setmetatable(decl_data["an"], {
	__call = function(self, args, data)
		local oxy = match(args.lemma, "a(" .. sa_utils.accent .. "?)n$")

		data.decl_type = "an-stem"
		stem_final_n = true

		if not match(args.stem, sa_utils.consonant .. "[NYRnmyrlv]$") then
			args.contract = true
		end

		if args.g == "n" then -- Wackernagel vol.III p.238 presupposes neuter pl. '-hāni' for '-han'
			decline(args, data, {
				n=args.han == true and {"a" .. oxy, {"a" .. oxy .. "m", note = "Vedic"}} or "a" .. oxy,
				a="[nom]",
				v={ "+", "a" },
			}, {}, {
				n={ "A" .. oxy .. "ni", {"a" .. oxy, note = "Vedic"},  {"A" .. oxy, note = "Vedic"}},
				a="[nom]",
				v={ "Ani", {"a", note = "Vedic"},  {"A", note = "Vedic"}},
			})
			if args.contract then
				decline(args, data, nil, {
					n={ "nI" .. oxy, "+I" },
					v={ "nI", "+I" },
				}, {})
            else
				decline(args, data, nil, {
					n="+I",
					v="+I",
				}, {})
			end
		else
			decline(args, data, {
				n="A" .. oxy,
				v="+",
			})
			if args.han or match(args.stem, "pUz$") or match(args.stem, "aryam$") then
				decline(args, data, {
					a="a" .. oxy .. "nam",
				}, {
					n={ "a" .. oxy .. "nO", { "a" .. oxy .. "nA", note = "Vedic" } },
					v={ "anO", { "anA", note = "Vedic" } },
				}, {
					n="a" .. oxy .. "nas",
					v="anas",
					a=args.han == true and { "na" .. oxy .. "s", "a" .. oxy .. "nas"} or "na" .. oxy .. "s",
				})
			elseif match(args.stem, "ukz$") or match(args.stem, "yo/?z$") or match(args.stem, "vf/?z$")
			or match(args.stem, "^tm$") then -- Whitney §426b; Wackernagel vol.III p.267
				decline(args, data, {
					a={"A" .. oxy .. "nam", {"a" .. oxy .. "nam", note = "Vedic" }},
				}, {
					n={ "A" .. oxy .. "nO", { "a" .. oxy .. "nO", note = "Vedic" }, { "a" .. oxy .. "nA", note = "Vedic" } },
					v={ "AnO", { "anO", note = "Vedic" }, { "anA", note = "Vedic" } },
				}, {
					n={"A" .. oxy .. "nas", {"a" .. oxy .. "nas", note = "Vedic" }},
					v={"Anas", {"anas", note = "Vedic" }},
					a= args.contract == true and "na" .. oxy .. "s" or "+as",
				})
			else
				decline(args, data, {
					a="A" .. oxy .. "nam",
				}, {
					n={ "A" .. oxy .. "nO", { "A" .. oxy .. "nA", note = "Vedic" } },
					v={ "AnO", { "AnA", note = "Vedic" } },
				}, {
					n="A" .. oxy .. "nas",
					v="Anas",
					a= args.contract == true and "na" .. oxy .. "s" or "+as",
				})
			end
		end
		
		if args.contract then
			decline(args, data, {
				i="nA" .. oxy,
				d="ne" .. oxy,
				ab="na" .. oxy .. "s",
				g="[abl]",
				l={ "ni" .. oxy, "+i", { "+" , note = "Vedic"} }, -- Whitney §425c for Vedic form
			}, {
				g="no" .. oxy .. "s",
			}, {
				g="nA" .. oxy .. "m",
			})
		else
			decline(args, data, {
				i="+A",
				d="+e",
				ab="+as",
				g="[abl]",
				l= { "+i", { "+" , note = "Vedic"} }, -- Whitney §425c for Vedic form
			}, {
				g="+os",
			}, {
				g="+Am",
			})
		end

		decline(args, data, nil, {
			a="[nom]",
			i="a" .. oxy .. "ByAm",
			d="[ins]",
			ab="[ins]",
			l="[gen]",
		}, {
			i="a" .. oxy .. "Bis",
			d="a" .. oxy .. "Byas",
			ab="[dat]",
			l="a" .. oxy .. "su",
		})
		
		if args.adj == true and args.g == "m" then
			-- table.insert(data.categories, "Sanskrit an-stem adjectives")
		else
			-- table.insert(data.categories, "Sanskrit an-stem nouns")
		end
	end
})

decl_data["in"] = {
	detect = make_detect("(.+)i" .. sa_utils.accent .. "?n$",
	function(args) return args.g == "m" or args.g == "n" end)
}

setmetatable(decl_data["in"], {
	__call = function(self, args, data)
		local oxy = match(args.lemma, "i(" .. sa_utils.accent .. "?)n$")

		data.decl_type = "တံမအရေဝ်(ဣန်)"
		stem_final_n = true

		if args.g ~= "n" then
			decline(args, data, {
				n="I" .. oxy,
				a="i" .. oxy .. "nam",
				v="+",
			}, {
				n={ "+O", { "+A", note = "Vedic" } },
				v={ "+O", { "+A", note = "Vedic" } },
			}, {
				n="+as",
				a="[nom]",
				v="+as",
			})
		else
			decline(args, data, {
				n="i" .. oxy,
				a="[nom]",
				v={ "i", "+" },
			}, {
				n="+I",
				v="+I",
			}, {
				n="I" .. oxy .. "ni",
				a="[nom]",
				v="Ini",
			})
		end

		decline(args, data, {
			i="+A",
			d="+e",
			ab="+as",
			g="[abl]",
			l="+i",
		}, {
			a="[nom]",
			i="i" .. oxy .. "ByAm",
			d="[ins]",
			ab="[ins]",
			g="+os",
			l="[gen]",
		}, {
			i="i" .. oxy .. "Bis",
			d="i" .. oxy .. "Byas",
			ab="[dat]",
			g="+Am",
			l="i" .. oxy .. "zu",
		})

		if args.adj == true and args.g == "m" then
			-- table.insert(data.categories, "Sanskrit in-stem adjectives")
		else
			-- table.insert(data.categories, "Sanskrit in-stem nouns")
		end
	end
})

decl_data["at"] = {
	detect = function(args)
		if (make_detect("(.+)a" .. sa_utils.accent .. "?t$")(args) 
		or (make_detect("(.+)A" .. sa_utils.accent .. "?t$")(args) and args.participle == true)) -- participles like 'yā́t'
		and (args.g == "m" or args.g == "n") then
			return true
		else
			return false
		end
	end
}

setmetatable(decl_data["at"], {
	__call = function(self, args, data)
		local cons, vow, oxy = match(args.lemma, "([mv]?)([aA])(" .. sa_utils.accent .. "?)t$")
		
		-- for present participles
		if args.lemma:find("[vm]a/?t$") == nil or args.participle == true then
			data.decl_type = "တံမအရေဝ်(အတ်)"
			
			if args.at_nom_s == true then -- participles like 'júhvat'
				if args.g ~= "n" then
					decline(args, data, {
						n="a" .. oxy .. "t",
						a="a" .. oxy .. "tam",
						v="at",
					}, {
						n={ "a" .. oxy .. "tO", { "a" .. oxy .. "tA", note = "Vedic"} },
						v={ "atO", { "atA", note = "Vedic"} },
					}, {
						n="a" .. oxy .. "tas",
						a="a" .. oxy .. "tas",
						v="atas",
					})
				else
					decline(args, data, {
						n="a" .. oxy .. "t",
						a="[nom]",
						v="at",
					}, {
						n="a" .. oxy .. "tI",
						v="atI",
					}, {
						n="a" .. oxy .. "ti",
						a="[nom]",
						v="ati",
					})
				end
				decline(args, data, {
					i="+A",
					d="+e",
					ab="+as",
					l="+i",
				}, {
					g="+os",
				}, {
					g="+Am",
				})
			else
				if args.g ~= "n" then
					decline(args, data, {
						n= vow .. oxy .. "n",
						a= vow .. oxy .. "ntam",
						v= vow .. "n",
					}, {
						n={ vow .. oxy .. "ntO", { vow .. oxy .. "ntA", note = "Vedic"} },
						v={ vow .. "ntO", { vow .. "ntA", note = "Vedic"} },
					}, {
						n= vow .. oxy .. "ntas",
						a= vow .. "ta" .. oxy .. "s",
						v= vow .. "ntas",
					})
				else
					if not args.participle_n_nom_d and not args.nom_d then
						error('Please indicate nominative dual (identical to feminine stem) with nom_d')
					else
						if args.nom_d then
							local s = mw.text.split(args.nom_d, ",")
							args.participle_n_nom_d = s[1]
							args.participle_n_nom_d2 = s[2]
						end
						-- neuter nom.du. taken from feminine, see Whitney §443a (though maybe artificial: §447b)
						args.participle_n_nom_d = IAST_to_SLP.tr(args.participle_n_nom_d)
						if args.participle_n_nom_d2 then
							args.participle_n_nom_d2 = IAST_to_SLP.tr(args.participle_n_nom_d2)
							decline(args, data, {}, {
								n= {{stem = args.participle_n_nom_d, ""}, {stem = args.participle_n_nom_d2, ""}},
								v= {{stem = args.participle_n_nom_d, ""}, {stem = args.participle_n_nom_d2, ""}},
							}, {})
						else
							decline(args, data, {}, {
								n= {{stem = args.participle_n_nom_d, ""}},
								v= {{stem = args.participle_n_nom_d, ""}},
							}, {})
						end
						if vow == "a" then
							decline(args, data, {}, {}, {
								n= {"a" .. oxy .. "nti", {"A" .. oxy .. "nti", note = "Rigvedic"}}, -- see Wackernagel III p 262
								a="[nom]",
								v= {"anti", {"Anti", note = "Rigvedic"}},
							})
						else
							decline(args, data, {}, {}, {
								n= vow .. oxy .. "nti",
								a="[nom]",
								v= vow .. "nti",
							})
						end
					end
					decline(args, data, {
						n= vow .. oxy .. "t",
						a="[nom]",
						v= vow .. "t",
					}, {}, {})
				end
				decline(args, data, {
					i= vow .. "tA" .. oxy,
					d= vow .. "te" .. oxy,
					ab= vow .. "ta" .. oxy .. "s",
					l= vow .. "ti" .. oxy,
				}, {
					g= vow .. "to" .. oxy .. "s",
				}, {
					g= vow .. "tA" .. oxy .. "m",
				})
			end
			decline(args, data, {
				g="[abl]",
			}, {
				a="[nom]",
				i= vow .. oxy .. "dByAm",
				d="[ins]",
				ab="[ins]",
				l="[gen]",
			}, {
				i= vow .. oxy .. "dBis",
				d= vow .. oxy .. "dByas",
				ab="[dat]",
				l="+su",
			})
			
			if args.adj == true and args.g == "m" then
				-- table.insert(data.categories, "Sanskrit at-stem adjectives")
			else
				-- table.insert(data.categories, "Sanskrit at-stem nouns")
			end
		
		else -- for nouns suffixed with -vat/-mat
			data.decl_type = cons .. "at-stem"
			
			if args.g ~= "n" then
				decline(args, data, {
					n="A" .. oxy .. "n",
					a="a" .. oxy .. "ntam",
					v={ "an", { "as", note = "Rigvedic"} },
				}, {
					n={ "a" .. oxy .. "ntO", { "a" .. oxy .. "ntA", note = "Vedic"} },
					v={ "antO", { "antA", note = "Vedic"} },
				}, {
					n="a" .. oxy .. "ntas",
					a="a" .. oxy .. "tas",
					v="antas",
				})
			else
				decline(args, data, {
					n="a" .. oxy .. "t",
					a="[nom]",
					v="at",
				}, {
					n="+I",
					v="+I",
				}, {
					n={{"a" .. oxy .. "nti"}, {"A" .. oxy .. "nti", note = "Rigvedic"}}, -- see Wackernagel III p 258-9
					a="[nom]",
					v={{"anti"}, {"Anti", note = "Rigvedic"}},
				})
			end
			decline(args, data, {
				i="+A",
				d="+e",
				ab="+as",
				g="[abl]",
				l="+i",
			}, {
				a="[nom]",
				i="a" .. oxy .. "dByAm",
				d="[ins]",
				ab="[ins]",
				g="+os",
				l="[gen]",
			}, {
				i="a" .. oxy .. "dBis",
				d="a" .. oxy .. "dByas",
				ab="[dat]",
				g="+Am",
				l="+su",
			})
			
			if args.adj == true and args.g == "m" then
				-- table.insert(data.categories, "Sanskrit " .. cons .. "at-stem adjectives")
			else
				-- table.insert(data.categories, "Sanskrit " .. cons .. "at-stem nouns")
			end
		end
	end
})

decl_data["añc"] = {
	detect = make_detect("(.+[aA]" .. sa_utils.accent .. "?Yc)$",
		function(args) return args.g == "m" or args.g == "n" end)
}

setmetatable(decl_data["añc"], {
	__call = function(self, args, data)
		local cons, vowel, oxy = match(args.lemma, "([yv]?)([aA])(" .. sa_utils.accent .. "?)Yc$")
		
		data.decl_type = "တံမအရေဝ်(အည်စ)"
		
		-- make stem for 'middle' cases
		args.stem = args.stem:gsub("Yc$", "c")
		
		if args.g == "m" then
            decline(args, data, {
                n= "+",  -- args.lemma
                a="+am",
                v= "+",
			}, {
                n={ "+O", { "+A", note = "Vedic" } },
                v={ "+O", { "+A", note = "Vedic" } },
            }, {
                n="+as",
                v="+as",
            })
        else -- neuter
            decline(args, data, {
                n= "",  -- args.stem
                v= "",
                a="[nom]"
            }, {}, {
                n="+i",
                v="+i",
                a="[nom]"
            })
        end
		decline(args, data, {}, {
            i= "ByAm",
        }, {
            i= "Bis",
            d= "Byas",
            l= "su",
        })
        
		-- changing stem to weakest form
		if vowel == "A" then
            -- do nothing
        elseif match(args.stem, "tirya/?c$") then -- specifically for tiryañc
            args.stem = args.stem:gsub("ya(/?)c$", "a%1Sc")
        elseif cons ~= "" then
        	args.stem = args.stem:gsub("([yv])a([/\\]?)c$", 
        	function(cons, acc) return (cons == "y" and "I" or "U") .. (acc == "" and "" or "/") .. "c" end)
        elseif match(args.stem, "dac$") then
            args.stem = args.stem:gsub("ac$", "Ic")
        else
            error("Not supported")
        end
		
		if vowel == "A" or oxy ~= "/" then
			if args.g == "m" then
				decline(args, data, nil, nil, {
					a="as",
				})
			else -- neuter
				decline(args, data, nil, {
					n="I",
					v="I",
				})
			end
			decline(args, data, {
				i="A",
				d="e",
				ab="as",
				l="i",
			}, {
				g="os",
			}, {
				g="Am",
			})
		else -- oxytone accent, showing two possible accentuations (Whitney §410)
            if args.g == "m" then
                decline(args, data, nil, nil, { a={"as", {"a" .. oxy .. "s", note = "Rigvedic", mono = true}} })
            else
                decline(args, data, nil, { 
                    n= {"I", {"I" .. oxy, note = "Rigvedic", mono = true}},
                    v= "I",
                })
            end
            decline(args, data, {
                i= {"A", {"A" .. oxy, note = "Rigvedic", mono = true}},
                d= {"e", {"e" .. oxy, note = "Rigvedic", mono = true}},
                ab= {"as", {"a" .. oxy .. "s", note = "Rigvedic", mono = true}},
                l= {"i", {"i" .. oxy, note = "Rigvedic", mono = true}},
            }, {
                g= {"os", {"o" .. oxy .. "s", note = "Rigvedic", mono = true}},
            }, {
                g= {"Am", {"A" .. oxy .. "m", note = "Rigvedic", mono = true}},
            })
        end
		
		decline(args, data, {
            g="[abl]"
        }, {
            a="[nom]",
            d="[ins]",
            ab="[ins]",
            l="[gen]",
        }, {
            ab="[dat]",
        })
        
        if args.adj == true and args.g == "m" then
			-- table.insert(data.categories, "Sanskrit añc-stem adjectives")
		end
	end
})

decl_data["o"] = {
	detect = make_detect("(.+)o" .. sa_utils.accent .. "?$",
		function(args) return args.g == "m" or args.g == "f" end)
}

setmetatable(decl_data["o"], {  -- 'gó'
	__call = function(self, args, data)
		local oxy = match(args.lemma, "o(" .. sa_utils.accent .. "?)$")
		data.decl_type = "တံမအရေဝ်(ဩ)"

		decline(args, data, {
			n="O" .. oxy .. "s",
			v="Os",
			a="A" .. oxy .. "m",
			i="+A",
			d="+e",
			ab="o" .. oxy .. "s",
			g="[abl]",
			l="+i",
		}, {
			n={ "A" .. oxy .. "vO", { "A" .. oxy .. "vA", note = "Vedic" } },
			v={ "AvO", { "AvA", note = "Vedic" } },
			a="[nom]",
			i="+ByAm",
			d="[ins]",
			ab="[ins]",
			g= "+os",
			l="[gen]",
		}, {
			n="A" .. oxy .. "vas",
			a={"A" .. oxy .. "s", { "A" .. oxy .. "vas", note = "rare" } },
			v="Avas",
			i="+Bis",
			d="+Byas",
			ab="[dat]",
			g={ "+Am", { "+nAm", note = "Vedic" } },
			l="+su",
		})
		
		-- table.insert(data.categories, "Sanskrit o-stem nouns")
	end
})

decl_data["O"] = {
	detect = make_detect("(.+)O" .. sa_utils.accent .. "?$",
		function(args) return args.g == "m" or args.g == "f" end)
}

setmetatable(decl_data["O"], {
	__call = function(self, args, data)
		local oxy = match(args.lemma, "O(" .. sa_utils.accent .. "?)$")
		data.decl_type = "တံမအရေဝ်(အော)"

		decline(args, data, {
			n="+s",
			v="+s",
			a="+am",
			i= {{ "+A" .. oxy, mono = true }},
			d= {{ "+e" .. oxy, mono = true }},
			ab={{ "+a" .. oxy .. "s", mono = true }},
			g="[abl]",
			l= {{ "+i" .. oxy, mono = true }},
		}, {
			n={ "+O", { "+A", note = "Vedic" } },
			v={ "+O", { "+A", note = "Vedic" } },
			a="[nom]",
			i= {{"+ByA" .. oxy .. "m", mono = true }},
			d="[ins]",
			ab="[ins]",
			g= {{ "+o" .. oxy .. "s", mono = true }},
			l="[gen]",
		}, {
			n="+as",
			v="+as",
			a="[nom]",
			i= {{"+Bi" .. oxy .. "s", mono = true }},
			d= {{"+Bya" .. oxy .. "s", mono = true }},
			ab="[dat]",
			g= {{ "+A" .. oxy .. "m", mono = true }},
			l= {{ "+su" .. oxy, mono = true }},
		})

		-- table.insert(data.categories, "Sanskrit au-stem nouns")
	end
})

decl_data["vāṃs"] = {
	detect = make_detect("(.+)vA" .. sa_utils.accent .. "?Ms$",
		function(args) return args.g == "m" or args.g == "n" end)
}

setmetatable(decl_data["vāṃs"], {
	__call = function(self, args, data)
		local oxy = match(args.lemma, "vA(" .. sa_utils.accent .. "?)Ms$")
		data.decl_type = "တံမအရေဝ်(ဝါံသ်)"
		
		local weak_stem
		if match(args.stem, "baBU$") or match(args.stem, "u$") then
			weak_stem = args.stem .. "v"
		elseif match(args.stem, "U$") then
			weak_stem = args.stem:gsub("U$", "uv")
		elseif match(args.stem, "pIpi$") or match(args.stem, "dI[dD]i$") then
			weak_stem = args.stem
		-- the second 'i' of 'śiśrivāṃs' etc. is part of the root
		elseif match(args.stem, "[aAIuUfFxeEoO]" .. sa_utils.accent .."?M?" .. sa_utils.consonant .. "+i$") or match(args.stem, "vi[Sd]i$") then
			weak_stem = args.stem:gsub("i$", "")
		else
			weak_stem = args.stem
		end
		
		if args.g == "m" then
			decline(args, data, {
				n= "vA" .. oxy .. "n",
				a= "vA" .. oxy .. "Msam",
				v={ "van", { "vas", note = "Rigvedic"} },
			}, {
				n= "vA" .. oxy .. "MsO",
				a="[nom]",
				v= "vAMsO",
			}, {
				n= "vA" .. oxy .. "Msas",
				a={{stem = weak_stem, "u" .. oxy .. "zas"}},
				v= "vAMsas",
			})
		else
			decline(args, data, {
				n= "va" .. oxy .. "t",
				a="[nom]",
				v= "vat"
			}, {
				n={{stem = weak_stem, "u" .. oxy .. "zI"}},
				a="[nom]",
				v={{stem = weak_stem, "uzI"}},
			}, {
				n= "vA" .. oxy .. "MsI",
				a="[nom]",
				v= "vAMsI"
			})
		end
		
		decline(args, data, {
			i={{stem = weak_stem, "u" .. oxy .. "zA"}},
			d={{stem = weak_stem, "u" .. oxy .. "ze"}},
			ab={{stem = weak_stem, "u" .. oxy .. "zas"}},
			g="[abl]",
			l={{stem = weak_stem, "u" .. oxy .. "zi"}},
		}, {
			i= "va" .. oxy .. "dByAm",
			d="[ins]",
			ab="[ins]",
			g={{stem = weak_stem, "u" .. oxy .. "zos"}},
			l="[gen]"
		}, {
			i= "va" .. oxy .. "dBis",
			d= "va" .. oxy .. "dByas", 
			ab="[dat]",
			g={{stem = weak_stem, "u" .. oxy .. "zAm"}},
			l= "va" .. oxy .. "tsu"
		})
		
		if args.adj == true and args.g == "m" then
			-- table.insert(data.categories, "Sanskrit vāṃs-stem adjectives")
		else
			-- table.insert(data.categories, "Sanskrit vāṃs-stem nouns")
		end
	end
})

return decl_data