Module:User:AryamanA/mr-decl

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

This is a private module sandbox of AryamanA, for his own experimentation. Items in this module may be added and removed at AryamanA's discretion; do not rely on this module's stability.


local export = {}
local m_translit = require("Module:mr-translit")

local gsub = mw.ustring.gsub
local sub = mw.ustring.sub
local match = mw.ustring.match
local len = mw.ustring.len

local genders = {
	['m'] = 'masculine', ['f'] = 'feminine', ['f-e'] = 'feminine (exception)', ['n'] = 'neuter',
}

local function wordify(text)
	local words, translits = {}, {}
	for word in mw.text.gsplit(text, ",") do
		table.insert(words, mr_format(word))
		table.insert(translits, m_translit.tr(word, "mr", "Deva"))
	end
	text = table.concat(words, ", ") .. "<br><small style=\"color:#888\">" ..
		table.concat(translits, ", ") .. "</small>"
	return text
end

function mr_format(text)
	text = ('<span lang="mr" class="Deva">[[%s]]</span>'):format(text)
	return text
end

local function stem(word)
	return sub(word, 1, -2)
end

local function stem_2(word)
	return sub(word, 1, -3)
end

local function stem_ending(word)
	return sub(word, -1)
end

local function generate(word, stem, g, num)
	paradigms = {
		["m"] = { "रा", "[^र]ा", "ी", "ू", "[क-ह]" },
		["f"] = { "ा", "री", "[^र]ी", "ू", "[क-ह]" },
		["f-e"] = { "." },
		["n"] = { "ी", "ू", "[क-ह]", "[ें]" },
	}

	-- plural, obl. sg, obl. pl (if needed)
	
	data = {
		["m"] = {
			["रा"] = { stem .. "े", stem_2(word) .. "ऱ्या" },
			["[^र]ा"] = { stem .. "े", stem .. "्या" },
			["ी"] = { word, word },
			["ू"] = { word, stem .. "वा" },
			["[क-ह]"] = { word, stem .. "ा" },
		},
		["f"] = {
			["री"] = { stem_2(word) .. "ऱ्या", stem .. "ी", stem_2(word) .. "ऱ्यां" },
			["ा"] = { word, stem .. "े", word .. "ं" },
			["[^र]ी"] = { word, stem .. "्या" },
			["ू"] = { word, stem .. "वा" },
			["[क-ह]"] = { stem .. "ी", stem .. "ी" },
		},
		["f-e"] = {
			["."] = { word .. "ा", word .. "े", word .. "ां" },
		},
		["n"] = {
			["ी"] = { word, stem .. "्या" },
			["ू"] = { stem .. "े", stem .. "ा" },
			["[ें]"] = { stem .. "ी", stem .. "्या"},
			["[क-ह]"] = { stem .. "े", stem .. "ा"},
		}
	}
	
	for _,paradigm in pairs(paradigms[g]) do
		if match(word, paradigm .. "$") then
			if num == 3 then
				if data[g][paradigm][3] then
					return data[g][paradigm][3]
				else
					return data[g][paradigm][2] .. "ं"
				end
			else
				return data[g][paradigm][num]
			end
		end
	end
end

local function decline(word, g, num)
	local c, v, v_s = "[क-ह]", "[ा-ौ]", "[अ-औ]"
	local ending = stem_ending(word)
	local stem = stem(word)
	
	-- consonant stem
	if match(word, c .. "$") then
		stem = word
	end
	
	-- rules for stem changes
	if (g == "m" or g == "n") and match(word, "." .. c .. "[ीू]" ..  c .. "$") then
		stem = sub(word, 1, -3) .. ending
	end
	
	if match(word, v .. "ऊ" .. c .. "$") then
		stem = sub(word, 1, -3) .. "व" .. ending
	end
	
	return generate(word, stem, g, num)
end

function export.show(frame)
	local args = frame:getParent().args
	local word = args[1] or mw.title.getCurrentTitle().text
	local g = args["g"] or args[2]
	
	data = [=[
{| class="inflection-table vsSwitcher vsToggleCategory-inflection" style="background:#FEFEFE; text-align:center; border: 1px solid #CCC; min-width:30%"
|- style="background: #d9ebff;"
! class="vsToggleElement" style="text-align: left;" colspan="4" | Declension of ]=] .. mr_format(word) .. " " .. genders[g]

	data = data .. [=[

|- class="vsHide"
! style="background:#eff7ff" | nominative
| ]=] .. wordify(word) .. "||" .. wordify(decline(word, g, 1))
	
	data = data .. [=[

|- class="vsHide"
! style="background:#eff7ff" | oblique
| ]=] .. wordify(decline(word, g, 2)) .. "||" .. wordify(decline(word, g, 3))

	data = data .. [=[

|- class="vsHide"
! style="background:#eff7ff" | dative
| ]=] .. wordify(decline(word, g, 2) .. "ला") .. "||" .. wordify(decline(word, g, 3) .. "ना")

	data = data .. [=[

|- class="vsHide"
! style="background:#eff7ff" | ergative
| ]=] .. wordify(decline(word, g, 2) .. "ने") .. "||" .. wordify(decline(word, g, 3) .. "नी")

	data = data .. [=[

|- class="vsHide"
! style="background:#eff7ff" | instrumental
| ]=] .. wordify(decline(word, g, 2) .. "शी") .. "||" .. wordify(decline(word, g, 3) .. "शी")

	data = data .. [=[

|- class="vsHide"
! style="background:#eff7ff" | locative
| ]=] .. wordify(decline(word, g, 2) .. "त") .. "||" .. wordify(decline(word, g, 3) .. "त")

	data = data .. [=[

|- class="vsHide"
! style="background:#eff7ff" | vocative
| ]=] .. wordify(decline(word, g, 2)) .. "||" .. wordify(decline(word, g, 3) .. "नो")
	
	data = data .. [=[

|}]=]
	
	return data
end

return export