Module:mr-decl

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


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', ['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_ending(word)
	return sub(word, -1)
end

local function s_obl(word, g)
	local ending = stem_ending(word)
	local stem = stem(word)
	
	data = {
		["m"] = { ["ी"] = word, ["ा"] = stem .. "्या", ["ू"] = stem .. "वा", ["default"] = word .. "ा"},
		["f"] = { ["ी"] = word, ["ा"] = stem .. "े", ["default"] = word .. "ी"},
		["f-e"] = { ["default"] = word .. "े"},
		["n"] = { ["ी"] = stem .. "्या", ["े"] = stem .. "्या", ["default"] = word .. "ा"}
	}
	
	return data[g][ending] or data[g]["default"]
end

local function p_dir(word, g)
	local ending = stem_ending(word)
	local stem = stem(word)
	
	data = {
		["m"] = { ["ा"] = stem .. "े", ["default"] = word },
		["f"] = { ["ी"] = stem .. "्या", ["ा"] = word, ["default"] = word .. "ी"},
		["f-e"] = { ["default"] = word .. "ा"},
		["n"] = { ["ू"] = stem .. "ं", -- TODO: alternately final "e"
			["े"] = stem .. "ी",
			["default"] = stem .. "ं" -- TODO: alternately final "e"
		}
	}
	-- it's ugly but it works ^_^
	if g == "n" and (stem_ending(m_translit.tr(word)) == "a" or ending == "ी") then
		return word
	end
	
	return data[g][ending] or data[g]["default"]
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" data-toggle-category="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="vsShow"
| style="background:#eff7ff" | ''dir. sg.''
| ]=] .. wordify(word) .. [=[

|- class="vsShow"
| style="background:#eff7ff" | ''dir. pl.''
| ]=] .. wordify(p_dir(word, g))

	data = data .. [=[

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

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

	data = data .. [=[

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

	data = data .. [=[

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

	data = data .. [=[

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

	data = data .. [=[

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

	data = data .. [=[

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

|}]=]
	
	return data
end

return export