Module:rad-stem

From Laenkea
Revision as of 18:21, 26 November 2023 by TheNightAvl (talk | contribs) (TheNightAvl moved page Module:rad-infl to Module:rad-stem without leaving a redirect)
Jump to navigation Jump to search
--[[
WORK IN PROGRESS
]]--

local export = {}
local getArgs = require('Module:Arguments').getArgs

local m_IPA = require("Module:rad-IPA")
local m_links = require("Module:links")
local m_languages = require("Module:languages")
local m_parameters = require("Module:parameters")

local V = "aáàâảeéèiíìỉoóòøuúùûūủyýỳỷ"
local C = "bcdðfghħjĵkķlmnņpqrsștvwxzþ"
local O_voiced = "bdðgħvxz"
local O_unvoiced = "cfhkķpqsștþ"
local O = O_voiced .. O_unvoiced
local N = "mnņ"
local L = "lr"
local R = L .. N
local J = "jĵw"

stem_data = {
	-- heavy stems --
	['ab'] = 'C', ['av'] = 'C', ['b'] = 'C', ['c'] = 'C', ['Cf'] = 'C', ['Cm'] = 'C', ['Cn'] = 'C', ['Cņ'] = 'C', ['Cv'] = 'C', ['Cþ'] = 'C', ['d'] = 'C', ['ð'] = 'C', ['dz'] = 'C', ['eb'] = 'C', ['ev'] = 'C', ['f'] = 'C', ['g'] = 'C', ['ib'] = 'C', ['íb'] = 'C', ['ir'] = 'C', ['iv'] = 'C', ['ív'] = 'C', ['j'] = 'C', ['jð'] = 'C', ['jþ'] = 'C', ['k'] = 'C', ['l'] = 'C', ['lb'] = 'C', ['m'] = 'C', ['mb'] = 'C', ['n'] = 'C', ['ņ'] = 'C', ['ob'] = 'C', ['p'] = 'C', ['r'] = 'C', ['rb'] = 'C', ['s'] = 'C', ['ș'] = 'C', ['t'] = 'C', ['ub'] = 'C', ['uv'] = 'C', ['v'] = 'C', ['x'] = 'C', ['z'] = 'C', ['þ'] = 'C', ['st'] = 'C', ['șt'] = 'C', ['zd'] = 'C', ['xd'] = 'C',
	
	-- heavy spirants --
	['abH'] = 'CH', ['avH'] = 'CH', ['bH'] = 'CH', ['cH'] = 'CH', ['CmḤ'] = 'CH', ['CnḤ'] = 'CH', ['CņḤ'] = 'CH', ['CvH'] = 'CH', ['dH'] = 'CH', ['ðH'] = 'CH', ['dzH'] = 'CH', ['ebH'] = 'CH', ['evH'] = 'CH', ['fH'] = 'CH', ['gH'] = 'CH', ['ibH'] = 'CH', ['íbH'] = 'CH', ['ivH'] = 'CH', ['ívH'] = 'CH', ['jðH'] = 'CH', ['jH'] = 'CH', ['jþH'] = 'CH', ['kH'] = 'CH', ['lbH'] = 'CH', ['lḤ'] = 'CH', ['mbH'] = 'CH', ['mḤ'] = 'CH', ['nḤ'] = 'CH', ['ņḤ'] = 'CH', ['pH'] = 'CH', ['rbH'] = 'CH', ['rḤ'] = 'CH', ['sH'] = 'CH', ['șH'] = 'CH', ['tH'] = 'CH', ['vH'] = 'CH', ['xH'] = 'CH', ['zH'] = 'CH', ['þH'] = 'CH', ['stH'] = 'CH', ['ștH'] = 'CH', ['zdH'] = 'CH', ['xdH'] = 'CH',
	
	-- semi-light --
	['aG'] = 'G', ['eG'] = 'G', ['G'] = 'G', ['G̣'] = 'G', ['ieG'] = 'G', ['iG'] = 'G', ['oG'] = 'G', ['øG'] = 'G', ['rG̣'] = 'G', ['uG'] = 'G', ['yG'] = 'G',
	
	-- light --
	['a^e'] = 'V', ['a^o'] = 'V', ['ai'] = 'V', ['aijo'] = 'V', ['au'] = 'V', ['C^V'] = 'V', ['Cvo'] = 'V', ['Cvu'] = 'V', ['e'] = 'V', ['i'] = 'V', ['ie'] = 'V', ['io'] = 'V', ['Je'] = 'V', ['Ji'] = 'V', ['nj^e'] = 'V', ['o'] = 'V', ['ø'] = 'V', ['oe'] = 'V', ['ove'] = 'V', ['t^o'] = 'V', ['u'] = 'V', ['uve'] = 'V', ['Vj'] = 'V', ['y'] = 'V',
	
	-- light spirant --
	['a^eH'] = 'VH', ['a^oH'] = 'VH', ['aH'] = 'VH', ['aHH'] = 'VH', ['áHH'] = 'VH', ['âHH'] = 'VH', ['aòH'] = 'VH', ['eH'] = 'VH', ['H'] = 'VH', ['Ḥ'] = 'VH', ['HH'] = 'VH', ['ḤḤ'] = 'VH', ['ieH'] = 'VH', ['ieHH'] = 'VH', ['iH'] = 'VH', ['iḤ'] = 'VH', ['íH'] = 'VH', ['íḤ'] = 'VH', ['iHH'] = 'VH', ['oH'] = 'VH', ['øH'] = 'VH', ['uH'] = 'VH', ['yH'] = 'VH', ['ýH'] = 'VH', ['ýḤ'] = 'VH', ['yHH'] = 'VH',
}

pos_data = { "noun", "verb" }

function table.contains(table, element)
  for _, value in pairs(table) do
    if value == element then
      return true
    end
  end
  return false
end

-- this function resolves all consonant clashes
function resolve_ending(resolution)
	
	local function resolve(to_Match, to_Replace)
		if mw.ustring.find(resolution, to_Match) then
			resolution = mw.ustring.gsub(resolution, to_Match, to_Replace)
			mw.log( "/" .. to_Match .. "/ → " .. to_Replace .. " : " .. resolution )
		end
	end

	-- resolve =g/=d
	resolve("([rv])%=g","%1=u")
	resolve("(j%=g)","j")
	resolve("(j%=d)","x")
	
	resolve("([pf]%=g)","f")
	resolve("([pf]%=d)","f=t")
	resolve("(g%=g)","g")
	resolve("(k%=g)","k")
	resolve("(k%=d)","t")
	
	resolve("(m%=g)","n=g")
	
	resolve("b%=([gd])","v=%1")
	
	resolve("(j[ctþ]%=g)","ș=k")
	resolve("(j[ctþ]%=d)","ș=t")
	
	resolve("j[dð]%=([gd])","x=%1")
	resolve("jdz%=([gd])","x=%1")
	
	resolve("([sș]?[șķ]%=g)","ș=k")
	resolve("([sș]?[șķ]%=d)","ș=t")
	
	resolve("[zx]?d?x%=([gd])","x=%1")
	
	resolve("(s?[cstþ]%=g)","s=k")
	resolve("(s?[cstþ]%=d)","s=t")
	
	resolve("z?[zdð]%=([gd])","z=%1")
	resolve("z?dz%=([gd])","z=%1")
	
	resolve("([" .. C .. "])[mnņ]%=([gd])", "%1=%2")
	
	-- resolve =s
	resolve("([sș])%=s","%1")
	resolve("j%=s","ș")
	
	-- resolve VvC
	resolve("([^eoø])av%=([^" .. V .. "])","%1au=%2")
	resolve("^av%=([^" .. V .. "])","au=%1")
	resolve("(àv%=)","au=")
	
	resolve("([^a])uv%=([^" .. V .. "])","%1ú=%2")
	resolve("([^au])ov%=([^" .. V .. "])","%1ú=%2")
	resolve("^[ou]v%=([^" .. V .. "])","ú=%1")
	resolve("([òù]v%=)","ú=")
	
	resolve("([^i])ev%=([^" .. V .. "])","%1ø=%2")
	resolve("^ev%=([^" .. V .. "])","ø=%1")
	resolve("(èv%=)","ø=")
	
	resolve("([^aeuøâ])iv%=([^" .. V .. "])","%1y=%2")
	resolve("^iv%=([^" .. V .. "])","y=%1")
	resolve("(ìv%=)","y=")
	
	-- resolve CvC
	resolve("([" .. C .. "])v%=([" .. C .. "])","%1u=%2")
	resolve("([" .. C .. "])v%=e","%1u=i")
	
	-- resolve -ûve → -úe
	resolve("ûv%=e", "ú=e")
	
	-- resolve aja, ava
	resolve("([^eoø])a[vj]%=a","%1á")
	resolve("(à[vj]%=a)","á")
	
	-- resolve f/þ~v/ð
	resolve("([" .. V .. O .. L .. J .. "])f%=([" .. V .. J .. "])","%1v=%2")
	resolve("([" .. V .. O .. L .. J .. "])þ%=([" .. V .. J .. "])","%1ð=%2")
	
	-- resolve k/g + front vowel
	resolve("k%=([ie])","c=%1")
	resolve("g%=([ie])","dz=%1")
	
	-- resolve palatal + i
	resolve("([jșxķ])%=i","%1=e")
	
	-- resolve =
	resolve("(v%=g)","v")
	resolve("(%=)","")
	
	return resolution
end

function getNounStem(args) -- rad-inflection|pos|principle_part| 3 | 4 | 5
	local principle_parts = {
		["nom"] = "",
		["gen"] = "",
		["dat"] = "",
		["acc"] = "",
		["ins"] = "",
		["nompl"] = "",
		["genpl"] = "",
		["genpl_short"] = "",
	}
	
	local stem = ""
	local stem_broken = ""
	local stem_broken_e = ""
	local stem_raised = ""
	local ending = ""
	
	-- create stem and ending parameters -- 
	mw.log("Parameters:")
	if stem_data[args[4]] == 'C' or stem_data[args[4]] or stem_data[args[5]] == 'C' or stem_data[args[5]] or stem_data[args[6]] == 'C' or stem_data[args[6]] == 'CH' then
		
		local v_index = 4		-- expected nucleus position
		
		while true do
			if mw.ustring.find(args[v_index], "([%^%~])") then
				if mw.ustring.find(args[v_index], "([%^])") then
					stem = mw.ustring.match(args[v_index], "([^%^]+)%^") or ""
					stem_raised = mw.ustring.match(args[v_index], "%^([^%^]+)")
				else
					stem = args[v_index]
					stem_raised = args[v_index]
				end
				if mw.ustring.find(stem, "([%~])") then
					stem_broken = mw.ustring.match(stem, "%~([^%~%^]+)")
					stem = mw.ustring.match(stem, "([^%~]+)%~")
					if stem_raised == args[v_index] then
						stem_raised = stem_broken
					end
				else
					stem_broken = stem
				end
				break
			else
				if mw.ustring.find(V, mw.ustring.sub(args[v_index], -1)) then
					stem = args[v_index]
					stem_raised = stem
					stem_broken = stem
					break
				elseif v_index == 3 then
					error()
				else
					v_index = 3
				end
			end
		end
		
		if stem_broken == "u" then
			stem_broken_e = "û"
		else
			stem_broken_e = stem_broken
		end
		
		stem = stem .. args[v_index + 1]
		stem_broken = stem_broken .. args[v_index + 1]
		stem_broken_e = stem_broken_e .. args[v_index + 1]
		stem_raised = stem_raised .. args[v_index + 1]
		if v_index > 3 then
			stem = args[v_index - 1] .. stem
			stem_broken = args[v_index - 1] .. stem_broken
			stem_broken_e = args[v_index - 1] .. stem_broken_e
			stem_raised = args[v_index - 1] .. stem_raised
		end
		if args[v_index + 2]~= nil then
			stem = stem .. args[v_index + 2]
			stem_broken = stem_broken .. args[v_index + 2]
			stem_broken_e = stem_broken_e .. args[v_index + 2]
			stem_raised = stem_raised .. args[v_index + 2]
			ending = args[v_index + 2]
		else
			ending = args[v_index + 1]
		end
	elseif stem_data[args[4]] then
		stem = args[3] .. args[4]
		ending = args[4]
		stem_broken = nil
		stem_broken_e = nil
		stem_raised = nil
	elseif stem_data[args[3]] then
		stem = args[3]
		ending = args[3]
		stem_broken = nil
		stem_broken_e = nil
		stem_raised = nil
	else
		error("Invalid format: Please format the arguments as hv|o~u^u|þ, dv|a^u|n|þ, he|rḤ or lorál|eH")
	end
	
	--
	mw.log("Stem: " .. stem)
	if stem_broken then mw.log("Broken stem: " .. stem_broken ) end
	if stem_raised then mw.log("Raised stem: " .. stem_raised ) end
	mw.log("Ending: " .. ending)
	--
	
	-- generate endings from declension type --
	if stem_data[ending] == 'C' or stem_data[ending] == 'CH' then -- heavy stems
		if ending == "j" and not (mw.ustring.sub(stem, -2) == "oj" and mw.ustring.sub(stem, -3) ~= "aoj" and mw.ustring.sub(stem, -3) ~= "uoj") then
			principle_parts["nom"] = stem .. "=s"
			principle_parts["acc"] = stem .. "=d"
		else
			principle_parts["nom"] = stem_broken .. "=s"
			principle_parts["acc"] = stem_broken .. "=d"
		end
		if ending == 'v' and stem_raised == stem_broken then
			principle_parts["gen"] = stem .. "=g"
		else
			principle_parts["gen"] = stem_raised .. "=g"
		end
		principle_parts["dat"] = stem .. "=a"
		principle_parts["ins"] = stem .. "=úr"
		principle_parts["nompl"] = stem_broken_e .. "=e"
		principle_parts["genpl"] = stem .. "=i"
		principle_parts["genpl_short"] = principle_parts["genpl"]
	end
	
	mw.log("——— Resolving ———")
	for part, infl in pairs(principle_parts) do
		principle_parts[part] = resolve_ending(infl)
	end
	
	mw.logObject(principle_parts)
	return principle_parts
end

function getVerbStem(args)

end

function export.getStem(frame)
	local args = getArgs(frame)
	local word = mw.title.getCurrentTitle().subpageText
	local to_Return = ""
	
	if args[1] ~= nil and table.contains(pos_data, args[1]) then
		local pos = args[1]
	else
		local error_text = "Parameter 1 must be:"
		for i, _ in pairs(data) do
			error_text = error_text .. " [" .. i .. "]"
		end
		error(error_text)
	end
	 
	if args[1] == "noun" then
		mw.logObject(getNounStem(args))
		to_Return = getNounStem(args)[args[2]]
	elseif args[1] == "verb" then
		mw.logObject(getVerbStem(args))
		to_Return = getVerbStem(args)[args[2]]
	end
	
	return to_Return
end

return export

--[[
Debug console test string:
=p.getStem(mw.getCurrentFrame():newChild{title="whatever",args={"noun", "nom", "dv", "a^u", "n", "þ"}})
]]--