Jump to content

Module:User:Kutchkutch/mr-decl

From Wiktionary, the free dictionary


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 tc = table.concat

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

function export.wordify(text, text_translit)
	local words, translits = {}, {}
	for word in mw.text.gsplit(text, ",") do
		table.insert(words, mr_format(word))
	end

	table.insert(translits, text_translit)	

	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#Marathi|'):format(gsub(text, "़", "")) .. 
		('%s]]</span>'):format(text)
	return text
end

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

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

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

local function get_penultimate_letter(word)
	return sub(word, -2, -2)
end

local function weaken_penultimate_iu(word)
	local weakened_stem = ""
	local last_letter = get_last_letter(word)
	local penultimate_letter = get_penultimate_letter(word)
	local penultimate_stem = get_without_last_two_letters(word)

	-- īC stem
	if penultimate_letter == "ी" then 
		weakened_stem = penultimate_stem .. "ि"  .. last_letter
	elseif penultimate_letter == "ई" then 
		weakened_stem = penultimate_stem .. "इ"  .. last_letter

	-- ūC stem
	elseif penultimate_letter == "ू" then 
		weakened_stem = penultimate_stem .. "ु"  .. last_letter
	elseif penultimate_letter == "ऊ" then 
		weakened_stem = penultimate_stem .. "उ"  .. last_letter
	end
	return weakened_stem
end

local function get_s_obl(word, g)

	local penultimate_letter = get_penultimate_letter(word)
	local stem = get_without_last_letter(word)
	local ending = get_last_letter(word)

	-- masc rā-stem
	if g == "m" and penultimate_letter == "र" then
		stem = get_without_last_two_letters(word)
		ending = "र" .. get_last_letter(word)
	end
	
	data = {
		["m"] = { 
					["default"] = word .. "ा",
					["ा"] = stem .. "्या", 
					["रा"] = stem .. "ऱ्या", 

					["ी"] = stem .. "्या", 
					["ू"] = stem .. "वा", 
					["ृ"] = word, 
					["ो"] = word, 
				},
		["f"] = { 
					["default"] = word .. "ी",
					["ा"] = stem .. "े", 
					["ी"] = word, 
					["ू"] = stem .. "वे", 
					["ृ"] = word, 
					["ै"] = stem .. "या", 
					["ो"] = word,
				},
		["f-e"] = { 
					["default"] = word .. "े"
				},
		["n"] = { 
					["default"] = word .. "ा",
					["ी"] = stem .. "्या",
					["ू"] = stem .. "वा", 
					["े"] = stem .. "्या",
				}
	}
	
	return data[g][ending] or data[g]["default"]
end

local function get_p_dir(word, g)
	local ending = get_last_letter(word)
	local stem = get_without_last_letter(word)
	
	data = {
		["m"] = { 
						["default"] = {word} ,
						["ा"] = {stem .. "े"}, 
						["ृ"] = {word}, 
						["ो"] = {word}, 
				},
		["f"] = { 
						["default"] = {word .. "ी"},
						["ा"] = {word}, 
						["ी"] = {stem .. "्या"}, 
						["ू"] = {stem .. "वा"}, 
						["ृ"] = {word}, 
						["ै"] = {stem .. "या"}, 
						["ो"] = {stem .. "ा"}, 
				},
		["f-e"] = { 
						["default"] = {word .. "ा"},
				},
		["n"] = { 
						["default"] = {word .. "े", word .. "ं"},
						["ू"] = {stem .. "वे", stem .. "वं"},
						["े"] = {stem .. "ी"},
				}
	}
	-- it's ugly but it works ^_^
	if g == "n" and (get_last_letter(m_translit.tr(word)) == "a" or ending == "ी") then
		return {word}
	end
	
	return data[g][ending] or data[g]["default"]
end

local function get_p_obl(word, g)
	local ending = get_last_letter(word)
	local stem = get_without_last_letter(word)

	local plural_direct = get_p_dir(word, g)[1]
	local singular_oblique = get_s_obl(word, g)

	
	data = {
		["m"] = { 
					["default"] = singular_oblique .. "ं",

					["ा"] = singular_oblique .. "ं", 
					["ी"] = singular_oblique .. "ं", 
					["ू"] = singular_oblique .. "ं", 
					["ृ"] = singular_oblique .. "ं", 
					["ो"] = singular_oblique .. "ं", 
				},
		["f"] = { 
					["default"] = plural_direct .. "ं",
					["ा"] = plural_direct .. "ं", 
					["ी"] = plural_direct .. "ं", 
					["ू"] = plural_direct .. "ं", 
					["ृ"] = plural_direct .. "ं", 
					["ै"] = plural_direct .. "ं", 
					["ो"] = plural_direct .. "ं",
				},
		["f-e"] = { 
					["default"] = plural_direct .. "ं",
				},
		["n"] = { 
					["default"] = singular_oblique .. "ं",
					["ी"] = singular_oblique.. "ं",
					["ू"] = singular_oblique .. "ं", 
					["े"] = singular_oblique .. "ं",
				}
	}
	
	return data[g][ending] or data[g]["default"]
end

function get_weakened_cases(word, g, case)
	
	local weakened_stem = weaken_penultimate_iu(word)
	
		weakened_iu_cases = {
		["m"] = { 
					["direct plural"] = get_p_dir(word, g), 
					["oblique singular"] = get_s_obl(weakened_stem, g),
					["oblique plural"] = get_p_obl(weakened_stem, g), 
				},
		["f"] = { 
					["direct plural"] = get_p_dir(weakened_stem, g), 
					["oblique singular"] = get_s_obl(weakened_stem, g),
					["oblique plural"] = get_p_obl(weakened_stem, g), 
				},
		["f-e"] = { 
					["direct plural"] = get_p_dir(weakened_stem, g), 
					["oblique singular"] = get_s_obl(weakened_stem, g),
					["oblique plural"] = get_p_obl(weakened_stem, g), 
				},
		["n"] = { 
					["direct plural"] = get_p_dir(weakened_stem, g), 
					["oblique singular"] = get_s_obl(weakened_stem, g),
					["oblique plural"] = get_p_obl(weakened_stem, g), 
				}
	}
	
	return weakened_iu_cases[g][case]
end

function handle_options(word, g, options)

	local singular_oblique, plural_oblique, plural_direct = "", "", ""

	-- no options
	if options == nil then
		plural_direct = get_p_dir(word, g)
		singular_oblique = get_s_obl(word, g)
		plural_oblique = get_p_obl(word, g)

	-- weaken_iu option
	elseif options["weaken_iu"] == "true" then
		plural_direct = get_weakened_cases(word, g, "direct plural")
		singular_oblique = get_weakened_cases(word, g, "oblique singular")
		plural_oblique = get_weakened_cases(word, g, "oblique plural")
		
	elseif options["number"] == "sg-only" then
		plural_direct = "-"
		singular_oblique = get_s_obl(word, g)
		plural_oblique = "-"
	end

	return singular_oblique, plural_oblique, plural_direct
end


function export.forms(word, g, options)

	local singular_oblique, plural_oblique, plural_direct = handle_options(word, g, options)

	form = {}
	translit_form = {}

	-- dir. sg.
	form.dirs = {word}                                
	translit_form.dirs = {m_translit.tr(word, "mr", "Deva")}
	
	-- dir. pl.
	if plural_direct == "-" then
		form.dirp = {"-"}  
		translit_form.dirp = {"-"}
	elseif plural_direct[2] == nil then
		form.dirp = {plural_direct[1]}  
		translit_form.dirp = {m_translit.tr(plural_direct[1])}
	else 
		form.dirp = {plural_direct[1], plural_direct[2]}  
		translit_form.dirp = {m_translit.tr(plural_direct[1]), m_translit.tr(plural_direct[2])}
	end
	
	-- obl. sg.
	form.obls = {singular_oblique}                    
	translit_form.obls = {m_translit.tr(singular_oblique)}
	
	 -- obl. pl.
	form.oblp = {plural_oblique}                     
	translit_form.oblp = {m_translit.tr(plural_oblique)}

	-- dative
	form.dats = {singular_oblique .. "ला"} 
	translit_form.dats = {m_translit.tr(singular_oblique .. "ला")}

	form.datp = {plural_oblique .. "ना"}
	translit_form.datp = {m_translit.tr(plural_oblique .. "ना")}

	-- ergative
	form.ergs = {singular_oblique .. "ने"}
	translit_form.ergs = {m_translit.tr(singular_oblique .. "ने")}

	form.ergp = {plural_oblique .. "नी"}
	translit_form.ergp = {m_translit.tr(plural_oblique .. "नी")}

	-- instrumental
	form.inss = {singular_oblique .. "शी"}
	translit_form.inss = {m_translit.tr(singular_oblique .. "शी")}

	form.insp = {plural_oblique .. "शी"}
	translit_form.insp = {m_translit.tr(plural_oblique .. "शी")}

	-- locative
	form.locs = {singular_oblique .. "त"}  
	translit_form.locs = {m_translit.tr(singular_oblique .. "त")}
 
	form.locp = {plural_oblique .. "त"}
	translit_form.locp = {m_translit.tr(plural_oblique .. "त")}

	-- vocative
	form.vocs = {singular_oblique}
	translit_form.vocs = {m_translit.tr(singular_oblique)}

	form.vocp = {plural_oblique .. "नो"}
	translit_form.vocp = {m_translit.tr(plural_oblique .. "नो")}

	-- genitive singular
	form.gsns = {singular_oblique .. "च़े" , singular_oblique  .. "च़ं"}
	translit_form.gsns = {m_translit.tr(singular_oblique .. "च़े"), m_translit.tr(singular_oblique .. "च़ं")}

	form.gsnp = {singular_oblique .. "ची"}
	translit_form.gsnp = {m_translit.tr(singular_oblique .. "ची")}

	form.gsms = {singular_oblique .. "च़ा"}
	translit_form.gsms = {m_translit.tr(singular_oblique .. "च़ा")}

	form.gsmp = {singular_oblique .. "च़े"}
	translit_form.gsmp = {m_translit.tr(singular_oblique .. "च़े")}

	form.gsfs = {singular_oblique .. "ची"}
	translit_form.gsfs = {m_translit.tr(singular_oblique .. "ची")}

	form.gsfp = {singular_oblique .. "च्या"}
	translit_form.gsfp = {m_translit.tr(singular_oblique .. "च्या")}

	form.gso = {singular_oblique .. "च्या"}
	translit_form.gso = {m_translit.tr(singular_oblique .. "च्या")}

	-- genitive plural
	form.gpns = {plural_oblique .. "च़े", plural_oblique .. "च़ं"}
	translit_form.gpns = {m_translit.tr(plural_oblique .. "च़े"), m_translit.tr(plural_oblique .. "च़ं")}

	form.gpnp = {plural_oblique .. "ची"}
	translit_form.gpnp = {m_translit.tr(plural_oblique .. "ची")}

	form.gpms = {plural_oblique .. "च़ा"}
	translit_form.gpms = {m_translit.tr(plural_oblique .. "च़ा")}

	form.gpmp = {plural_oblique .. "च़े"}
	translit_form.gpmp = {m_translit.tr(plural_oblique .. "च़े")}

	form.gpfs = {plural_oblique .. "ची"}
	translit_form.gpfs = {m_translit.tr(plural_oblique .. "ची")}

	form.gpfp = {plural_oblique .. "च्या"}
	translit_form.gpfp = {m_translit.tr(plural_oblique .. "च्या")}

	form.gpo = {plural_oblique .. "च्या"}
	translit_form.gpo = {m_translit.tr(plural_oblique .. "च्या")}

	return form, translit_form
end

local function tcc(words)
	return tc(words, ', ')
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]
	local form, translit_form = export.forms(word, g, args[3])
	
	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="vsShow"
| style="background:#eff7ff" | ''dir. sg.''
| ]=] .. export.wordify(tcc(form.dirs), tcc(translit_form.dirs)) .. [=[

|- class="vsShow"
| style="background:#eff7ff" | ''dir. pl.''
| ]=] .. export.wordify(tcc(form.dirp), tcc(translit_form.dirp))

	data = data .. [=[

|- class="vsHide"
! style="background:#eff7ff" | nominative
| ]=] .. export.wordify(tcc(form.dirs), tcc(translit_form.dirs)) .. "||" .. export.wordify(tcc(form.dirp), tcc(translit_form.dirp))
	
	data = data .. [=[

|- class="vsHide"
! style="background:#eff7ff" | oblique
| ]=] .. export.wordify(tcc(form.obls), tcc(translit_form.obls)) .. "||" .. export.wordify(tcc(form.oblp), tcc(translit_form.oblp))

	data = data .. [=[

|- class="vsHide"
! style="background:#eff7ff" | dative
| ]=] .. export.wordify(tcc(form.dats), tcc(translit_form.dats)) .. "||" .. export.wordify(tcc(form.datp), tcc(translit_form.datp))

	data = data .. [=[

|- class="vsHide"
! style="background:#eff7ff" | ergative
| ]=] .. export.wordify(tcc(form.ergs), tcc(translit_form.ergs)) .. "||" ..export.wordify(tcc(form.ergp), tcc(translit_form.ergp))

	data = data .. [=[

|- class="vsHide"
! style="background:#eff7ff" | instrumental
| ]=] .. export.wordify(tcc(form.inss), tcc(translit_form.inss)) .. "||" .. export.wordify(tcc(form.insp), tcc(translit_form.insp))

	data = data .. [=[

|- class="vsHide"
! style="background:#eff7ff" | locative
| ]=] .. export.wordify(tcc(form.locs), tcc(translit_form.locs)) .. "||" .. export.wordify(tcc(form.locp), tcc(translit_form.locp))

	data = data .. [=[

|- class="vsHide"
! style="background:#eff7ff" | vocative
| ]=] .. export.wordify(tcc(form.vocs), tcc(translit_form.vocs)) .. "||" .. export.wordify(tcc(form.vocp), tcc(translit_form.vocp))

    data = data .. [=[

|}
]=]
  
    data = 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="8" | Genitive declension of ]=]  .. mr_format(word)

    data = data .. [=[

|- class="vsHide"
! style="background:#eff7ff" | 
! style="background:#eff7ff" colspan=2| neuter object
! style="background:#eff7ff" colspan=2 | masculine object
! style="background:#eff7ff" colspan=2| feminine object
! style="background:#eff7ff" colspan=2| oblique
|- class="vsHide"
! style="background:#eff7ff" | 
! style="background:#eff7ff" | singular<sup>*</sup>
! style="background:#eff7ff" | plural
! style="background:#eff7ff" | singular
! style="background:#eff7ff" | plural
! style="background:#eff7ff" | singular
! style="background:#eff7ff" | plural
! style="background:#eff7ff" | 
|- class="vsHide"
! style="background:#eff7ff" | singular subject]=]

    data = data .. [=[

|
]=]

    data = data .. export.wordify(tcc(form.gsns), tcc(translit_form.gsns)) .. [=[

|
]=]

    data = data .. export.wordify(tcc(form.gsnp), tcc(translit_form.gsnp)) .. [=[

|
]=]

    data = data .. export.wordify(tcc(form.gsms), tcc(translit_form.gsms)) .. [=[

|
]=]

    data = data .. export.wordify(tcc(form.gsmp), tcc(translit_form.gsmp)) .. [=[

|
]=]

    data = data .. export.wordify(tcc(form.gsfs), tcc(translit_form.gsfs)) .. [=[

|
]=]

    data = data .. export.wordify(tcc(form.gsfp), tcc(translit_form.gsfp)) .. [=[

|
]=]

    data = data .. export.wordify(tcc(form.gso), tcc(translit_form.gso)) .. [=[


|- class="vsHide"
! style="background:#eff7ff" | plural subject

|
]=]

    data = data .. export.wordify(tcc(form.gpns), tcc(translit_form.gpns)) .. [=[

|
]=]

    data = data .. export.wordify(tcc(form.gpnp), tcc(translit_form.gpnp)) .. [=[

|
]=]

    data = data .. export.wordify(tcc(form.gpms), tcc(translit_form.gpms)) .. [=[

|
]=]

    data = data .. export.wordify(tcc(form.gpmp), tcc(translit_form.gpmp)) .. [=[

|
]=]

    data = data .. export.wordify(tcc(form.gpfs), tcc(translit_form.gpfs)) .. [=[

|
]=]

    data = data .. export.wordify(tcc(form.gpfp), tcc(translit_form.gpfp)) .. [=[

|
]=]

    data = data .. export.wordify(tcc(form.gpo), tcc(translit_form.gpo)) .. [=[

|}
]=]

	return data
end

return export