Jump to content

Module:lzz-dec-noun

From Wiktionary, the free dictionary


local export = {}
--local lzz_translit = require("Module:lzz-translit"); --to be deleted

local lang = require("Module:languages").getByCode("lzz")
local full_link = require("Module:links").full_link
local strutils = require("Module:string utilities")
local declensionTable = require("Module:lzz-dec-noun/declension table")

--unicode charAt
function uCharAt(str, index)
	return mw.ustring.sub(str, 1, 1)
end



rules = {}
rules["მცხული"] =  {
    ["abs1"] = "ი",
    ["erg1"] = "იქ",
    ["dat1"] = "ის",
    ["gen1"] = "იშ",
    ["dir1"] = "იშე",
    ["abl1"] = "იშენ",
    ["loc1"] = "ის",
    ["ins1"] = "ითენ",
}
rules["ნჯა"] =  {
    ["abs1"] = "",
    ["erg1"] = "ქ",
    ["dat1"] = "ს",
    ["gen1"] = "შ",
    ["dir1"] = "შე",
    ["abl1"] = "შენ",
    ["loc1"] = "ს",
    ["ins1"] = "თენ"
}

local CumaRule =  {
    ["abs2"] = "ლეფე",
    ["erg2"] = "ლეფექ",
    ["dat2"] = "ლეფეს",
    ["gen2"] = "ლეფეშ",
    ["dir2"] = "ლეფეშე",
    ["abl2"] = "ლეფეშენ",
    ["loc2"] = "ლეფეს",
    ["ins2"] = "თენ"
}

local modernPluralRule =  {
    ["abs2"] = "",
    ["erg2"] = "ქ",
    ["dat2"] = "ს",
    ["gen2"] = "შ",
    ["dir2"] = "შე",
    ["abl2"] = "შენ",
    ["loc2"] = "ს",
    ["ins2"] = "თენ",
}

function init()
	forms = {}
	
	definers = {}
	word = nil
	genstem = nil
	term = nil
	
	lplural = false
	noplural = false
	givenname = false
	noarchaic = false
	nosingular = false

	wordType = nil

	DEFINERS = {}

	terms = nil
	
	--
	
	STEM_SG = nil
	STEM_SG_ADV = nil
	STEM_SG_GEN_INST = nil
	STEM_PL = nil
	RULE = nil
	NOSINGULAR = nil
	lplural = nil
	NOPLURAL = nil
	NOARCHAIC = nil
end

function export.getForms(args)
	main(args, 1)
	local combined = {}
	for k, v in pairs(forms) do
		combined[k] = mw.ustring.gsub(v, "[%[%}}", "")
	end
	return combined
end

--function export.test(args, form)
--	main(args, 1)
--	return mw.ustring.gsub(forms[form], "[%[%}}", "")
--end

function export.show(frame)
	if mw.text.split(mw.title.getCurrentTitle().prefixedText, ":")[1] == "Template" then  return "" end
	local args = frame:getParent().args
	main(args, 0)
	for i, v in pairs(forms) do
		if mw.ustring.find(v, "%(") then
			forms[i] = full_link {
				lang = lang,
				term = mw.ustring.gsub(v, "%(.-%)", ""),
				alt = v
			}
		else
			forms[i] = full_link { lang = lang, term = v }
		end
	end
	return strutils.format(declensionTable.template, forms) .. ""
end

function main(args)
	init()
	term = args.term or mw.title.getCurrentTitle().text --'term' arg is for debug and testing purposes
	
	terms = mw.text.split(term, " ")
	if #terms == 1 then 
		word = term
	else
		word = terms[#terms]
		table.remove(terms, #terms)
		definers = terms
	end
	
	local i = 1
	
	if args[1] ~= nil and (args[1] == "" or mw.ustring.find(args[1], "[ა-ჰ]") ~= nil) then
		genstem = args[1]
		if genstem == "" then genstem = word end
		i = i + 1
	end
	
	local params = {}
	params["lplural"] = false
	params["noplural"] = false
	params["given name"] = false
	params["noarchaic"] = false
	params["nosingular"] = false
	
	local newarg = args[i] 
	while newarg ~= nil do
		if newarg == "-" then newarg = "noplural" end
		
		params[newarg] = true
		newarg = args[i]
		i = i + 1
	end
	
	while newarg ~= nil do
		if newarg == "l" then newarg = "lplural" end
		
		params[newarg] = true
		newarg = args[i]
		i = i + 1
	end
	
	lplural    = params["lplural"]
	noplural   = params["noplural"]
	givenname  = params["given name"]
	noarchaic  = params["noarchaic"] 
	nosingular = params["nosingular"]
	
	analyze()
	deriveDefiners()
	fillTable()
	
	override(args)
	
	
	forms["note"] = ""
end



function analyze()
	--initializing defaults for all endings
	if nosingular then NOSINGULAR = "" end
	if noplural or givenname then NOPLURAL = "" end
	if noarchaic then NOARCHAIC = "" end
	--end defaultization xD
	
	local lastLetter = mw.ustring.sub(word, -1)
	if lastLetter == "ი" then
		STEM_SG = mw.ustring.sub(word, 0, -2)
		STEM_SG_GEN_INST = genstem or STEM_SG
		STEM_SG_ADV = genstem or STEM_SG
		
		STEM_PL = genstem or STEM_SG
		
		wordType = "მცხული"
		
		if givenname then
		end
		if genstem ~= nil then
			if #genstem == #word then
				STEM_SG = genstem
				wordType = "მცხული"
				if not givenname then
					STEM_PL = mw.ustring.sub(word, 0, -2); --rules : "მცხული"
				end
			end
		else
			if givenname then
				wordType = "ნჯა"
			end
		end
		RULE = rules[wordType]
	elseif lastLetter == "ა" and lplural == true then
		STEM_SG = mw.ustring.sub(word, 0, -2)
		STEM_SG_GEN_INST = genstem or STEM_SG
		STEM_SG_ADV = genstem or STEM_SG
		
		STEM_PL = genstem or STEM_SG
		
		RULE = rules[wordType]
	elseif lastLetter == "ა" or lastLetter == "ე" or lastLetter == "ო" or lastLetter == "უ" then 
		STEM_SG = word
		STEM_SG_GEN_INST = mw.ustring.sub(word, 0, -2)
		STEM_SG_ADV = word
		STEM_PL = word
		
		
		wordType = "ნჯა"
		if givenname then
			STEM_SG_GEN_INST = word
			wordType = "ნჯა"
		elseif genstem ~= nil then
			STEM_SG_GEN_INST = genstem
			wordType = "ნჯა"
			if #genstem ~= #word then
				STEM_SG_ADV = genstem .. "ა"
				STEM_PL = genstem
				wordType = "ნჯა"
			else
			end
		end
		
	RULE = rules[wordType]
end



function fillTable()
	forms["term"] = term
	
	forms["abs1"] = NOSINGULAR or "" .. DEFINERS["abs12"] .. STEM_SG ..          RULE["abs1"] .. ""
	forms["erg1"] = NOSINGULAR or "" .. DEFINERS["erg12"] .. STEM_SG ..          RULE["erg1"] .. ""
	forms["dat1"] = NOSINGULAR or "" .. DEFINERS["dat12"] .. STEM_SG ..          RULE["dat1"] .. ""
	forms["gen1"] = NOSINGULAR or "" .. DEFINERS["gen12"] .. STEM_SG ..          RULE["gen1"] .. "(ი)"
	forms["dir1"] = NOSINGULAR or "" .. DEFINERS["dir12"] .. STEM_SG ..          RULE["dir1"] .. "" 
	forms["abl1"] = NOSINGULAR or "" .. DEFINERS["abl12"] .. STEM_SG ..          RULE["abl1"] .. ""
	forms["loc1"] = NOSINGULAR or "" .. DEFINERS["loc12"] .. STEM_SG ..          RULE["loc1"] .. ""
	forms["ins1"] = NOSINGULAR or "" .. DEFINERS["ins12"] .. STEM_SG ..          RULE["ins1"] .. ""
	
	if lastLetter == "ი" then
	forms["abs2"] = NOPLURAL or "" .. DEFINERS["abs12"] .. STEM_PL .. "ეფე" .. modernPluralRule["abs2"] .. ""
	forms["erg2"] = NOPLURAL or "" .. DEFINERS["erg12"] .. STEM_PL .. "ეფე" .. modernPluralRule["erg2"] .. ""
	forms["dat2"] = NOPLURAL or "" .. DEFINERS["dat12"] .. STEM_PL .. "ეფე" .. modernPluralRule["dat2"] .. ""
	forms["gen2"] = NOPLURAL or "" .. DEFINERS["gen12"] .. STEM_PL .. "ეფე" .. modernPluralRule["gen2"] .. "(ი)"
	forms["dir2"] = NOPLURAL or "" .. DEFINERS["dir12"] .. STEM_PL .. "ეფე" .. modernPluralRule["dir2"] .. ""
	forms["abl2"] = NOPLURAL or "" .. DEFINERS["abl12"] .. STEM_PL .. "ეფე" .. modernPluralRule["abl2"] .. ""
	forms["loc2"] = NOPLURAL or "" .. DEFINERS["loc12"] .. STEM_PL .. "ეფე" .. modernPluralRule["loc2"] .. ""
	forms["ins2"] = NOPLURAL or "" .. DEFINERS["ins12"] .. STEM_PL .. "ეფე" .. modernPluralRule["ins2"] .. ""
	elseif lastLetter == "ა" and newarg == "l" then
	forms["abs2"] = NOPLURAL or "" .. DEFINERS["abs12"] .. STEM_PL .. "ლეფე" .. CumaRule["abs2"] .. ""
	forms["erg2"] = NOPLURAL or "" .. DEFINERS["erg12"] .. STEM_PL .. "ლეფე" .. CumaRule["erg2"] .. ""
	forms["dat2"] = NOPLURAL or "" .. DEFINERS["dat12"] .. STEM_PL .. "ლეფე" .. CumaRule["dat2"] .. ""
	forms["gen2"] = NOPLURAL or "" .. DEFINERS["gen12"] .. STEM_PL .. "ლეფე" .. CumaRule["gen2"] .. "(ი)"
	forms["dir2"] = NOPLURAL or "" .. DEFINERS["dir12"] .. STEM_PL .. "ლეფე" .. CumaRule["dir2"] .. ""
	forms["abl2"] = NOPLURAL or "" .. DEFINERS["abl12"] .. STEM_PL .. "ლეფე" .. CumaRule["abl2"] .. ""
	forms["loc2"] = NOPLURAL or "" .. DEFINERS["loc12"] .. STEM_PL .. "ლეფე" .. CumaRule["loc2"] .. ""
	forms["ins2"] = NOPLURAL or "" .. DEFINERS["ins12"] .. STEM_PL .. "ლეფე" .. CumaRule["ins2"] .. ""
	elseif lastLetter == "ა" or lastLetter == "ე" or lastLetter == "ო" or lastLetter == "უ" then 
	forms["abs2"] = NOPLURAL or "" .. DEFINERS["abs12"] .. STEM_PL .. "ფე" .. modernPluralRule["abs2"] .. ""
	forms["erg2"] = NOPLURAL or "" .. DEFINERS["erg12"] .. STEM_PL .. "ფე" .. modernPluralRule["erg2"] .. ""
	forms["dat2"] = NOPLURAL or "" .. DEFINERS["dat12"] .. STEM_PL .. "ფე" .. modernPluralRule["dat2"] .. ""
	forms["gen2"] = NOPLURAL or "" .. DEFINERS["gen12"] .. STEM_PL .. "ფე" .. modernPluralRule["gen2"] .. "(ი)"
	forms["dir2"] = NOPLURAL or "" .. DEFINERS["dir12"] .. STEM_PL .. "ფე" .. modernPluralRule["dir2"] .. ""
	forms["abl2"] = NOPLURAL or "" .. DEFINERS["abl12"] .. STEM_PL .. "ფე" .. modernPluralRule["abl2"] .. ""
	forms["loc2"] = NOPLURAL or "" .. DEFINERS["loc12"] .. STEM_PL .. "ფე" .. modernPluralRule["loc2"] .. ""
	forms["ins2"] = NOPLURAL or "" .. DEFINERS["ins12"] .. STEM_PL .. "ფე" .. modernPluralRule["ins2"] .. ""
	end
end

function deriveDefiners()
	local marker = {
		["abs12"] = "ი",
		["erg12"] = "იქ",
		["dat12"] = "ის",
		["gen12"] = "იშ",
		["dir12"] = "იშე",
		["abl12"] = "იშენ",
		["loc12"] = "ის",
		["ins12"] = "ითენ",

	}

	DEFINERS["abs12"] = ""
	DEFINERS["erg12"] = ""
	DEFINERS["dat12"] = ""
	DEFINERS["gen12"] = ""
	DEFINERS["dir12"] = ""
	DEFINERS["abl12"] = ""
	DEFINERS["loc12"] = ""
	DEFINERS["ins12"] = ""

	for index, definer in pairs(definers) do
		local trimLast = mw.ustring.sub(definer, 0, -2);
		local lastLetter = mw.ustring.sub(definer, -1)

		for case, val in pairs(DEFINERS) do
			DEFINERS[case] = DEFINERS[case] .. trimLast .. (lastLetter == "ი" and marker[case] or lastLetter) .. " "
		end
		--DEFINERS["abs12"] = DEFINERS["abs12"] .. " " .. trimLast + (lastLetter == "ი" and marker["abs12"] or lastLetter)
	end
end

function override(args)
	for case, text in pairs(forms) do --WARNING: NOTE, etc.
		forms[case] = mw.ustring.gsub(args[case] or "+", "+", forms[case])
	end	
end

return export

end
return export