Module:ka-form of

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

Implements {{ka-form of}}


-- Note: plural dative suffixed with postposition -ზე

local export = {}

local termTypes = require("Module:ka-form of/data").termTypes

local form_of_t = require("Module:form of")
local lang = require("Module:languages").getByCode("ka")
local appendixLinks = {adjective = "Appendix:Georgian_adjectives#Adjectival declension", noun = "Appendix:Georgian noun declension"}

local function trimLast(form, len)
	return mw.ustring.sub(form, 0, -(1 + len))
end
local function trimStart(form, len)
	return mw.ustring.sub(form, len + 1)
end
local function endsWithVowel(term)
	local lastLetter = mw.ustring.sub(term, -1)
	return mw.ustring.find("აეიოუ", lastLetter) ~= nil
end
local function startsWith(term, prefix)
	return mw.ustring.sub(term, 0, mw.ustring.len(prefix)) == prefix
end
local function endsWith(term, suffix)
  return mw.ustring.sub(term, mw.ustring.len(term) - mw.ustring.len(suffix) + 1) == suffix
end

--Entry point
function export.main(frame)
	local args = frame:getParent().args
	local form = args["term"] or mw.title.getCurrentTitle().text --PAGENAME
	local lemma_obj = {lang = lang, gloss = args["gloss"]}
	
	if (args[1] == "adj") then
		return show_adjective(args, form, lemma_obj)
	else
		return show_noun(args, form, lemma_obj)
	end
end

function show_adjective(args, form, lemma_obj)
	local case = nil
	if not endsWithVowel(form) then
		lemma_obj["term"] = form .. "ი"--XXX: LOL
		case = "Adverbial and dative"
	elseif endsWith(form, "მა") then
		lemma_obj["term"] = mw.ustring.sub(form, 0, -3) .. "ი"
		case = "Ergative"
	else
		lemma_obj["term"] = mw.ustring.sub(form, 0, -2) .. "ი"
		case = "Vocative"
	end
	return form_of_t.format_form_of	{
		text = "[[" .. appendixLinks["adjective"] .. "|" .. case .. "]] of",
		lemmas = {lemma_obj},
		lemma_face = "term"
	}
end

function check(form, suffix, case, data, postsuffix)
	if data.case ~= nil then return end

	if endsWith(form, suffix) then
		data.case = case
		data.suffix = postsuffix
		if endsWith(form, "ებ" .. suffix) then
			data.isPlural = true
			data.lemma = trimLast(form, 2 + mw.ustring.len(suffix)) .. "ი"
		else
			data.lemma = trimLast(form, mw.ustring.len(suffix)) .. "ი"
		end
	end
end

local formCodes = {"nom2",
"nom3",
"erg1",
"erg2",
"erg3",
"dat1",
"dat2",
"dat3",
"gen1",
"gen2",
"gen3",
"dat1a",
"gen1a",
"ins1a",
"adv1a",
"dat2a",
"gen2a",
"ins2a",
"adv2a",
"erg3a",
"dat3a",
"gen3a",
"ins1",
"ins2",
"adv1",
"adv2",
"voc1",
"voc2",
"voc3",
"ze",
"ze_pl",
"tan",
"tan_pl",
"shi",
"shi_pl",
"vit",
"vit_pl",
"tvis",
"tvis_pl",
"ebr",
"ebr_pl",
"ken",
"ken_pl",
"gan",
"gan_pl",
"dan",
"dan_pl",
"urt",
"urt_pl",
"mde",
"mde_pl",

"c_nom1",
"c_erg1",
"c_dat1a",
"c_gen1a",
"c_ins1a",
"c_adv1a",
"c_nom2",
"c_erg2",
"c_dat2a",
"c_gen2a",
"c_ins2a",
"c_adv2a",


"c_vit",
"c_ze",
"c_tan",
"c_shi",
"c_tvis",
"c_ebr",
"c_ken",
"c_gan",
"c_dan",
"c_urt",
"c_mde",

"c_vit_pl",
"c_ze_pl",
"c_tan_pl",
"c_shi_pl",
"c_tvis_pl",
"c_ebr_pl",
"c_ken_pl",
"c_gan_pl",
"c_dan_pl",
"c_urt_pl",
"c_mde_pl"}

export.formNames = {}
local formMapping = {nom = "nominative", gen = "genitive", erg = "ergative", dat = "dative", ins= "instrumental", adv = "adverbial", voc = "vocative"}
local postSuffix = {mde = "-მდე", urt = "-ურთ", dan = "-დან", ken = "-კენ", 
vit = "-ვით", ze = "-ზე", gan="-გან", tan = "-თან", tvis = "-თვის", shi = "-ში", ebr = "-ებრ"}

for _, val in pairs(formCodes) do
	local isPlural = false
	local hasCParticle = false
	local hasSavrtsobi = false
	
	isPlural = endsWith(val, "_pl"); if isPlural then val = trimLast(val, 3) end
	hasCParticle = startsWith(val, "c_"); if hasCParticle then val = trimStart(val, 2) end
	hasSavrtsobi = endsWith(val, "a"); if hasSavrtsobi then val = trimLast(val, 1) end
	isArchaic = endsWith(val, "3"); if isArchaic then val = trimLast(val, 1) end
	
	isPlural = isPlural or endsWith(val, "2")
	if endsWith(val, "2") then val = trimLast(val, 1) end
	if endsWith(val, "1") then val = trimLast(val, 1) end
	
	local name = ""
	
	if isPlural then
		name = name .. "plural "
	end
	if isArchaic then 
		name = name .. "archaic plural "
	end
	
	if formMapping[val] then
		name = name .. "[[" .. appendixLinks["noun"] .."|" .. formMapping[val] .. "]] "
	else
		name = name .. "pospositional suffixed with [[" ..postSuffix[val] .. "#Georgian|" .. postSuffix[val] .. "]] "
	end
	
	if hasCParticle or hasSavrtsobi then
		name = name .. "(with "
		if hasSavrtsobi then name = name .. "[[სავრცობი|savrtsobi]]" end
		if hasSavrtsobi and hasCParticle then name = name .. " and " end
		if hasCParticle then name = name .. "[[-ც#Georgian|-ც]] particle" end
		name = name .. ") "
	end
	
	name = name .. "of "
	
	table.insert(export.formNames, name)
end

function show_noun(args, form, lemma_obj)
	if args["hint"] then
		local hint = args["hint"]
		local index = 1
		for _, fc in pairs(formCodes) do
			if fc == hint then 
				lemma_obj.term = args[1]
				return form_of_t.format_form_of {
					text = export.formNames[index], lemmas = {lemma_obj},
					lemma_face = "term"
				}
			end
			index = index + 1
		end
		local availableFormCodes = table.concat(formCodes, ", ")
		return error("no such code exists. Available codes are: " .. availableFormCodes)
	end
	
	local variants = {}
	local res = nil
	for _, termType in pairs(termTypes) do
		for formId, possibility in pairs(termType.forms) do
			if endsWith(form, possibility.form) then
				local lemma = trimLast(form, mw.ustring.len(possibility.form)) .. termType.lemma
				if lemma == args[1] then
					lemma_obj.term = lemma
					if res then
						res = res .. "\n# "
					else
						res = ""
					end
					res = res .. form_of_t.format_form_of {
						text = export.formNames[formId],
						lemmas = {lemma_obj},
						lemma_face = "term"
					} -- for words like [[დროს]] very  ugly XXX:
				end
			end
		end
	end
	if res then	return res
	else return error("Appropriate form not found")
	end
end

return export