ข้ามไปเนื้อหา

มอดูล:ka-infl-noun

จาก วิกิพจนานุกรม พจนานุกรมเสรี

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

local lang = require("Module:languages").getByCode("ka")
local m_links = require("Module:links")
local strutils = require("Module:string utilities")
local declensionTable = require("Module:ka-infl-noun/declension table")
local postpositionTable = require("Module:ka-infl-noun/postposition table")

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

-- to know what savrtsobi means better see this [[სავრცობი]] 
local function savrtsobi(s)
	dat = uCharAt(s, 1)
	gen = uCharAt(s, 2)
	ins = uCharAt(s, 3)
	adv = uCharAt(s, 4)
	return {
		["dat"] = dat ~= "-" and "("..dat..")" or "",
		["gen"] = gen ~= "-" and "("..gen..")" or "",
		["ins"] = ins ~= "-" and "("..ins..")" or "",
		["adv"] = adv ~= "-" and "("..adv..")" or "",
	}
end


rules = {}
rules["კაცი"] =  {
    ["nom1"] = "ი",
    ["erg1"] = "მა",
    ["dat1"] = "ს",
    ["gen1"] = "ის",
    ["ins1"] = "ით",
    ["adv1"] = "ად",
    ["voc1"] = "ო",
}
rules["დავითი"] =  {
    ["nom1"] = "ი",
    ["erg1"] = "მა",
    ["dat1"] = "ს",
    ["gen1"] = "ის",
    ["ins1"] = "ით",
    ["adv1"] = "ად",
    ["voc1"] = "",
}
rules["ჩაი"] =  {
    ["nom1"] = "",
    ["erg1"] = "მ",
    ["dat1"] = "ს",
    ["gen1"] = "ს",
    ["ins1"] = "თ",
    ["adv1"] = "დ",
    ["voc1"] = "",
}

rules["მთა"] =  {
    ["nom1"] = "",
    ["erg1"] = "მ",
    ["dat1"] = "ს",
    ["gen1"] = "ის",
    ["ins1"] = "ით",
    ["adv1"] = "დ",
    ["voc1"] = "ვ",
}
rules["ანა"] =  {
    ["nom1"] = "",
    ["erg1"] = "მ",
    ["dat1"] = "ს",
    ["gen1"] = "ს",
    ["ins1"] = "თ",
    ["adv1"] = "დ",
    ["voc1"] = "",
}
rules["ხე"] = rules["მთა"]

rules["ხბო"] =  {
    ["nom1"] = "",
    ["erg1"] = "მ",
    ["dat1"] = "ს",
    ["gen1"] = "ს",
    ["ins1"] = "თ",
    ["adv1"] = "დ",
    ["voc1"] = "ვ",
}
rules["დოდო"] = rules["ჩაი"]

local modernPluralRule =  {
    ["nom"] = "ი",
    ["erg"] = "მა",
    ["dat"] = "ს",
    ["gen"] = "ის",
    ["ins"] = "ით",
    ["adv"] = "ად",
    ["voc"] = "ო",
}
local archaicPluralRule =  {
    ["nom"] = "ნი",
    ["erg"] = "თ",
    ["dat"] = "თ",
    ["gen"] = "თ",
    --["ins"]
    --["adv"]
    ["voc"] = "ნო",
}


function init()
	forms = {}
	postpositions = {}
	
	definers = {}
	word = nil
	genstem = nil
	term = nil
	
	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
	SAVRTSOBI = nil
	NOSINGULAR = 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
	for k, v in pairs(postpositions) 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)
	return strutils.format(declensionTable.template, forms) .. "" .. strutils.format(postpositionTable.template, postpositions)
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["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
	
	noplural   = params["noplural"]
	givenname  = params["given name"]
	noarchaic  = params["noarchaic"] 
	nosingular = params["nosingular"]
	
	analyze()
	deriveDefiners()
	fillTable()
	
	--postpositions. this has to happen before making links because it depends on square [[]] brackets
	fillPostpositions()
	
	override(args)
	
	makelinks()
	
	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
	SAVRTSOBI = savrtsobi("აააა")
	--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
			SAVRTSOBI = savrtsobi("-აა-")
		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
	elseif lastLetter == "ა" then
		STEM_SG = word
		STEM_SG_GEN_INST = mw.ustring.sub(word, 0, -2)
		STEM_SG_ADV = word
		STEM_PL = mw.ustring.sub(word, 0, -2)
		
		
		wordType = "მთა"
		if givenname then
			STEM_SG_GEN_INST = word
			wordType = "ანა"
			SAVRTSOBI = savrtsobi("-იი-")
		elseif genstem ~= nil then
			STEM_SG_GEN_INST = genstem
			wordType = "ხბო"
			if #genstem ~= #word then
				STEM_SG_ADV = genstem .. "ა"
				STEM_PL = genstem
				wordType = "მთა"
			else
				SAVRTSOBI = savrtsobi("აიია")
			end
		end
	elseif 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
			SAVRTSOBI = savrtsobi("-იი-")
			STEM_SG_GEN_INST = word
			wordType = "ანა"
		end
		if genstem ~= nil then
			STEM_SG_GEN_INST = genstem
			STEM_PL = genstem
			wordType = "ხბო"
			if #genstem ~= #word then
				STEM_SG_ADV = genstem.."ა"
				wordType = "მთა"
				SAVRTSOBI = savrtsobi("აიია")
			end
		end
	elseif lastLetter == "ო" or lastLetter == "უ" then
		STEM_SG = word
		STEM_SG_GEN_INST = word
		STEM_SG_ADV = word
		STEM_PL = word
		
		SAVRTSOBI = savrtsobi("აიია")
		
		if givenname then
			SAVRTSOBI = savrtsobi("-იი-")
		end
		
		wordType = "ხბო"
		if givenname then
			wordType = "დოდო"
		end
	end
	RULE = rules[wordType]
end



function fillTable()
	forms["term"] = term
	
	forms["nom1"] = NOSINGULAR or "[[" .. DEFINERS["nom12"] .. STEM_SG ..          RULE["nom1"] .. "]]"
	forms["erg1"] = NOSINGULAR or "[[" .. DEFINERS["erg12"] .. STEM_SG ..          RULE["erg1"] .. "]]"
	forms["dat1"] = NOSINGULAR or "[[" .. DEFINERS["dat12"] .. STEM_SG ..          RULE["dat1"] .. "]]" .. SAVRTSOBI["dat"]
	forms["gen1"] = NOSINGULAR or "[[" .. DEFINERS["gen12"] .. STEM_SG_GEN_INST .. RULE["gen1"] .. "]]" .. SAVRTSOBI["gen"]
	forms["ins1"] = NOSINGULAR or "[[" .. DEFINERS["ins12"] .. STEM_SG_GEN_INST .. RULE["ins1"] .. "]]" .. SAVRTSOBI["ins"]
	forms["adv1"] = NOSINGULAR or "[[" .. DEFINERS["adv12"] .. STEM_SG_ADV ..      RULE["adv1"] .. "]]" .. SAVRTSOBI["adv"]
	forms["voc1"] = NOSINGULAR or "[[" .. DEFINERS["voc12"] .. STEM_SG ..          RULE["voc1"] .. "]]"
	
	forms["nom2"] = NOPLURAL or "[[" .. DEFINERS["nom12"] .. STEM_PL .. "ებ" .. modernPluralRule["nom"] .. "]]"
	forms["erg2"] = NOPLURAL or "[[" .. DEFINERS["erg12"] .. STEM_PL .. "ებ" .. modernPluralRule["erg"] .. "]]"
	forms["dat2"] = NOPLURAL or "[[" .. DEFINERS["dat12"] .. STEM_PL .. "ებ" .. modernPluralRule["dat"] .. "]](ა)"
	forms["gen2"] = NOPLURAL or "[[" .. DEFINERS["gen12"] .. STEM_PL .. "ებ" .. modernPluralRule["gen"] .. "]](ა)"
	forms["ins2"] = NOPLURAL or "[[" .. DEFINERS["ins12"] .. STEM_PL .. "ებ" .. modernPluralRule["ins"] .. "]](ა)"
	forms["adv2"] = NOPLURAL or "[[" .. DEFINERS["adv12"] .. STEM_PL .. "ებ" .. modernPluralRule["adv"] .. "]](ა)"
	forms["voc2"] = NOPLURAL or "[[" .. DEFINERS["voc12"] .. STEM_PL .. "ებ" .. modernPluralRule["voc"] .. "]]"
	
	forms["nom3"] = NOPLURAL or NOARCHAIC or "[[" .. DEFINERS["nom3"] .. STEM_SG .. archaicPluralRule["nom"] .. "]]"
	forms["erg3"] = NOPLURAL or NOARCHAIC or "[[" .. DEFINERS["erg3"] .. STEM_SG .. archaicPluralRule["erg"] .. "]](ა)"
	forms["dat3"] = NOPLURAL or NOARCHAIC or "[[" .. DEFINERS["dat3"] .. STEM_SG .. archaicPluralRule["dat"] .. "]](ა)"
	forms["gen3"] = NOPLURAL or NOARCHAIC or "[[" .. DEFINERS["gen3"] .. STEM_SG .. archaicPluralRule["gen"] .. "]](ა)"
	--forms["ins3"]
	--forms["adv3"]
	forms["voc3"] = NOPLURAL or NOARCHAIC or "[[" .. DEFINERS["nom3"] .. STEM_SG .. archaicPluralRule["voc"] .. "]]"

end

function deriveDefiners()
	local marker = {
		["nom12"] = "ი",
		["erg12"] = "მა",
		["dat12"] = "",
		["gen12"] = "ი",
		["ins12"] = "ი",
		["adv12"] = "",
		["voc12"] = "ო",

		["nom3"]  = "ი",
		["erg3"]  = "",
		["dat3"]  = "",
		["gen3"]  = "",
		["voc3"]  = "ნო",
	}

	DEFINERS["nom12"] = ""
	DEFINERS["erg12"] = ""
	DEFINERS["dat12"] = ""
	DEFINERS["gen12"] = ""
	DEFINERS["ins12"] = ""
	DEFINERS["adv12"] = ""
	DEFINERS["voc12"] = ""

	DEFINERS["nom3"] = ""
	DEFINERS["erg3"] = ""
	DEFINERS["dat3"] = ""
	DEFINERS["gen3"] = ""
	DEFINERS["ins3"] = ""
	DEFINERS["adv3"] = ""
	DEFINERS["voc3"] = ""

	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["nom12"] = DEFINERS["nom12"] .. " " .. trimLast + (lastLetter == "ი" and marker["nom12"] 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	
	for case, text in pairs(postpositions) do --WARNING: NOTE, etc.
		postpositions[case] = mw.ustring.gsub(args[case] or "+", "+", postpositions[case])
	end	
end

function makelinks()
	for case, text in pairs(forms) do --WARNING: NOTE, etc.
		forms[case] = mw.ustring.gsub( text, "%[%[(.-)%]%](%([აი]%))", function (captured, sav) -- hyphen is 0 or more non greedy
					return m_links.full_link({lang = lang, term = captured, alt = captured .. sav}) end)
		forms[case] = mw.ustring.gsub( forms[case], "%[%[[^#]-%]%]", function (captured) 
					return m_links.full_link({lang = lang, term = captured}) end)
	end
	
	for postposition, text in pairs(postpositions) do --WARNING: NOTE, etc.
		postpositions[postposition] = mw.ustring.gsub( text, "%[%[(.-)%]%]", function (captured) -- hyphen is 0 or more non greedy
					return m_links.full_link({lang = lang, term = captured}) end)
	end
end


--<number of letters to truncate>, <letters to add>
--plurals are OK.
postpRules = {}
postpRules["კაცი"] = {
	["vit"] = {"dat", 1, "ივით"},
	["ze"] = {"dat", 1, "ზე"},
	["tan"] = {"dat", 1, "თან"},
	["shi"] = {"dat", 1, "ში"},

	["tvis"] = {"gen", 0, "თვის"},
	["ebr" ] = {"gen", 0, "ებრ"},
	["ken" ] = {"gen", 0, "კენ"},
	["gan" ] = {"gen", 0, "გან"},

	["dan" ] = {"ins", 1, "დან"},
	["urt" ] = {"ins", 0, "ურთ"},

	["mde" ] = {"adv", 1, "მდე"}
}
postpRules["დავითი"] = {
	["vit"] = {"dat", 1, "ივით"},
	["ze"] = {"dat", 1, "ზე"},
	["tan"] = {"dat", 1, "თან"},
	["shi"] = {"dat", 1, "ში"},

	["tvis"] = {"gen", 0, "თვის"},
	["ebr" ] = {"gen", 0, "ებრ"},
	["ken" ] = {"gen", 0, "კენ"},
	["gan" ] = {"gen", 0, "გან"},

	["dan" ] = {"ins", 1, "დან"},
	["urt" ] = {"ins", 0, "-"},

	["mde" ] = {"adv", 1, "მდე"}
}
postpRules["ჩაი"] = {
	["vit"] = {"dat", 1, "ვით"},
	["ze"] = {"dat", 1, "ზე"},
	["tan"] = {"dat", 0, "თან"},
	["shi"] = {"dat", 1, "ში"},

	["tvis"] = {"gen", 0, "თვის"},
	["ebr" ] = {"gen", 0, "ებრ"},
	["ken" ] = {"gen", 0, "კენ"},
	["gan" ] = {"gen", 0, "გან"},

	["dan" ] = {"ins", 1, "დან"},
	["urt" ] = {"ins", 0, "ურთ"},

	["mde" ] = {"adv", 1, "მდე"}
}
postpRules["მთა"] = {
	["vit"] = {"dat", 0, "ავით"},
	["ze"] = {"dat", 1, "ზე"},
	["tan"] = {"dat", 0, "თან"},
	["shi"] = {"dat", 1, "ში"},

	["tvis"] = {"gen", 0, "თვის"},
	["ebr" ] = {"gen", 0, "ებრ"},
	["ken" ] = {"gen", 0, "კენ"},
	["gan" ] = {"gen", 0, "გან"},

	["dan" ] = {"ins", 1, "დან"},
	["urt" ] = {"ins", 0, "ურთ"},

	["mde" ] = {"adv", 1, "მდე"}
}
postpRules["ანა"] = {
	["vit"] = {"dat", 0, "ავით"},
	["ze"] = {"dat", 1, "ზე"},
	["tan"] = {"dat", 0, "თან"},
	["shi"] = {"dat", 1, "ში"},

	["tvis"] = {"gen", 0, "თვის"},
	["ebr" ] = {"gen", 0, "ებრ"},
	["ken" ] = {"gen", 0, "კენ"},
	["gan" ] = {"gen", 0, "გან"},

	["dan" ] = {"ins", 1, "დან"},
	["urt" ] = {"ins", 0, "-"},

	["mde" ] = {"adv", 1, "მდე"}
}
postpRules["ხე"] = postpRules["მთა"]
postpRules["ხბო"] = postpRules["მთა"]
postpRules["დოდო"] = postpRules["ანა"]
postpRules["plural"] = postpRules["კაცი"]

function fillPostpositions()
	postpositions["term"] = term

	local postpRule = postpRules[wordType]
	local postpRule_pl = postpRules["plural"]
	for postSuffix, postGrammar in pairs(postpRule) do
		local case = postGrammar[1];
		local pattern = string.rep(".", postGrammar[2]) .. "%]%]";
		if case == "dat" or case == "gen" or  case == "ins" or case == "adv" then
			pattern = pattern .. "%(?[აი]?%)?"
		end
		local res = mw.ustring.gsub(forms[case.."1"], pattern, postGrammar[3].."]]")

		if postGrammar[3] == "-" then res = "-" end
		postpositions[postSuffix] = res
	end

	for postSuffix, postGrammar in pairs(postpRule_pl) do
		local case = postGrammar[1];
		local pattern = string.rep(".", postGrammar[2]) .. "%]%]";
		if case == "dat" or case == "gen" or  case == "ins" or case == "adv" then
			pattern = pattern .. "%(?[აი]?%)?"
		end
		local res = mw.ustring.gsub(forms[case.."2"], pattern, postGrammar[3].."]]")

		if postGrammar[3] == "-" then res = "-" end
		postpositions[postSuffix.."_pl"] = res
	end
end

return export