Module:rad-stem

Revision as of 14:23, 3 December 2024 by TheNightAvl (talk | contribs)
local export = {}
local getArgs = require("Module:Arguments").getArgs
local m_table = require("Module:table")

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 --
    ["b"] = "C",
    ["c"] = "C",
    ["d"] = "C",
    ["ð"] = "C",
    ["dz"] = "C",
    ["f"] = "C",
    ["g"] = "C",
    ["h"] = "C",
    ["ħ"] = "C",
    ["j"] = "C",
    ["k"] = "C",
    ["ķ"] = "C",
    ["l"] = "C",
    ["m"] = "C",
    ["n"] = "C",
    ["ņ"] = "C",
    ["p"] = "C",
    ["r"] = "C",
    ["s"] = "C",
    ["ș"] = "C",
    ["t"] = "C",
    ["v"] = "C",
    ["x"] = "C",
    ["z"] = "C",
    ["þ"] = "C",
    -- heavy spirants --
    ["bH"] = "CH",
    ["cH"] = "CH",
    ["dH"] = "CH",
    ["ðH"] = "CH",
    ["dzH"] = "CH",
    ["fH"] = "CH",
    ["gH"] = "CH",
    ["jH"] = "CH",
    ["kH"] = "CH",
    ["lḤ"] = "CH",
    ["mḤ"] = "CH",
    ["nḤ"] = "CH",
    ["ņḤ"] = "CH",
    ["pH"] = "CH",
    ["rḤ"] = "CH",
    ["sH"] = "CH",
    ["șH"] = "CH",
    ["tH"] = "CH",
    ["vH"] = "CH",
    ["xH"] = "CH",
    ["zH"] = "CH",
    ["þH"] = "CH",
    -- semi-light --
    ["a^eG"] = "G",
    ["a^oG"] = "G",
    ["eG"] = "G",
    ["G"] = "G",
    ["ieG"] = "G",
    ["iG"] = "G",
    ["oG"] = "G",
    ["øG"] = "G",
    ["uG"] = "G",
    ["yG"] = "G",
    ["yeG"] = "G",
    -- light --
    ["a"] = "V",
    ["a^e"] = "V",
    ["a^o"] = "V",
    ["ai"] = "V",
    ["au"] = "V",
    ["e"] = "V",
    ["i"] = "V",
    ["ie"] = "V",
    ["iè"] = "V",
    ["nj^e"] = "V",
    ["o"] = "V",
    ["o*"] = "V",
    ["ø"] = "V",
    ["t^o"] = "V",
    ["ķ^o"] = "V",
    ["u"] = "V",
    ["ù"] = "V",
    ["y"] = "V",
    -- light spirant --
    ["H"] = "VH",
    ["HH"] = "VH",
    ["Ḥ"] = "VH",
    ["ḤḤ"] = "VH"
}

light_noun_data = {
    -- NOM, V, ^V, dat, acc, ins, pl, voc
    ["a"] = {"a", "a", "a", "an", "ast"},
    ["a^e"] = {"Ra", "a", "e", "an", "ast"},
    ["a^o"] = {"Ra", "a", "u", "an", "ast"},
    ["ai"] = {"ai", "ai"},
    ["áj"] = {"áj", "áj"},
    ["au"] = {"au", "au"},
    ["e"] = {"Ra", "e", "=i"},
    ["i"] = {"Re", "=i"},
    ["ie"] = {"íRa", "ie"},
    ["iè"] = {"ia", "iè"},
    ["o"] = {"Ra", "o", "u", "un", "ust"},
    ["o*"] = {"o*", "o", "o*", "on*", "ost*"},
    ["ø"] = {"Ra", "ø", "y"},
    ["u"] = {"Re", "u"},
    ["ù"] = {"ù", "ù"},
    ["y"] = {"Re", "y"},
    ["a^eG"] = {"ár", "í", "á", "ád", "aúr", "ai", "áse"},
    ["a^oG"] = {"ár", "ív", "á", "ád", "aúr", "ai", "áse"},
    ["eG"] = {"ír", "í", "ea", "íd", "eúr", "í", "íse"},
    ["iG"] = {"ír", "í", "ea", "íd", "iúr", "í", "íse"},
    ["ieG"] = {"ỉr", "ỉ", "iea", "ỉd", "ieúr", "ỉ", "ỉse"},
    ["oG"] = {"úr", "ív", "oa", "úd", "oúr", "í", "úse"},
    ["øG"] = {"ýr", "ýj", "øa", "ýd", "øúr", "ý", "ýse"},
    ["uG"] = {"úr", "ív", "oa", "úd", "ủr", "í", "úse"},
    ["yG"] = {"ýr", "ýj", "øa", "ýd", "yúr", "ý", "ýse"},
    ["yeG"] = {"ỷr", "ỷ", "yea", "ỷd", "yeúr", "ỷ", "ỷse"},
    ["G"] = {"jr", "j", "a", "x", "úr", "j", "șe"}
}

pos_data = {"noun", "verb"}

-- 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 heavy spirant stems --

    resolve("", "")

    resolve("([^Ḥ])Ḥ%=e", "%1ge")
    resolve("(ỉ[HḤ][HḤ]?%=e)$", "ỉè")
    resolve("(ie[HḤ][HḤ]?%=e)$", "íe")
    resolve("(ye[HḤ][HḤ]?%=e)$", "ýe")
    resolve("([eoø]a)[HḤ][HḤ]?%=e$", "%1aje")
    resolve("(a[HḤ][HḤ]?%=e)$", "ai")
    resolve("(e[HḤ][HḤ]?%=e)$", "ei")
    resolve("(ø[HḤ][HḤ]?%=e)$", "øi")
    resolve("(i[HḤ][HḤ]?%=e)$", "iè")
    resolve("(y[HḤ][HḤ]?%=e)$", "yè")
    resolve("(â[HḤ][HḤ]%=e)$", "âi")

    resolve("(ỉH%=i%=s)", "ỉes")
    resolve("(ieH%=i%=s)", "ies")
    resolve("(ieH%=i)", "eí")
    resolve("([ei][HḤ]%=i)", "=í")
    resolve("([íỉ][HḤ]%=i)", "ỉ")
    resolve("([øy][HḤ]%=i)", "=ý")
    resolve("(ý[HḤ]%=i)", "ỷ")

    resolve("([ouû]H%=[ei])", "ui")
    resolve("([ouû]H%=se)", "ûse")
    resolve("(aH%=[ei])", "ai")
    resolve("([" .. V .. "])H%=[ei]", "%1j=e")
    resolve("ieH%=([aú])", "eív=%1")
    resolve("([" .. V .. "])H%=([aú])", "%1v=%2")

    -- resolve semi-light and light spirant stems
    resolve("(%=ý%=s)$", "yes")
    resolve("(%=ú%=s)$", "uos")
    resolve("(%=ỉ%=s)$", "ỉes")
    resolve("([" .. C .. "][" .. C .. "])%=í%=s$", "%1ies")
    resolve("([jșķx])%=í%=s$", "%1ies")
    resolve("(%=í%=s)$", "jes")

    -- resolve light stems
    resolve("%=tr", "t=s")
    resolve("%=njr", "nj=s")
    resolve("aij%=R([ae])$", "aí%1")
    resolve("([^a])uv%=R([ae])$", "%1ú=%2")
    resolve("([^au])ov%=R([ae])$", "%1ú=%2")
    resolve("^[ou]v%=R([ae])$", "ú=%1")
    resolve("[ùò]v%=R([ae])$", "ú%1")
    resolve("uav%=Ra$", "uò=Ra")
    resolve("([^eoø])av%=Ra$", "%1o=Ra")
    resolve("uav%=(R?e)$", "uò=%1")
    resolve("([^eoø])av%=(R?e)$", "%1o=%2")
    resolve("([oò])%=Ra$", "%1à")

    -- resolve spirants
    resolve("ḤḤ%=g", "=k")
    resolve("ḤḤ%=d", "=t")
    resolve("ḤḤ%=s", "=s")
    resolve("ḤḤ%=a", "=à")
    resolve("ḤḤ%=úr", "úr")
    resolve("ḤḤ%=e", "=e")
    resolve("ḤḤ%=i", "=șe")

    resolve("HH%=g", "=ak")
    resolve("HH%=d", "=at")
    resolve("HH%=s", "=as")
    resolve("HH%=a", "=à")
    resolve("HH%=úr", "úr")
    resolve("HH%=e", "=e")
    resolve("HH%=i", "=așe")
    resolve("y%=à$", "ya")
    resolve("y%=a$", "øa")
    resolve("i%=à$", "ia")
    resolve("i%=a$", "ea")
    resolve("u%=à$", "ua")
    resolve("u%=a$", "oa")
    resolve("[aà]%=[aà]", "á")
    resolve("[áả]%=[aà]", "ả")
    resolve("â%=([aàáả])", "o%1")

    resolve("kH%=g", "k")
    resolve("kH%=d", "kt")
    resolve("[ḤH]%=g", "g")
    resolve("[ḤH]%=([ds])", "=%1")
    resolve("Ḥ%=i", "j=i")
    resolve("H%=i", "ș=i")
    resolve("Ḥ%=([" .. V .. "])", "g%1")
    
    -- resolve =g/=d
    resolve("([rvħh])%=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("([^a])u([sș])%=se", "%1û%2e")
    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("([^iy])ev%=([^" .. V .. "])", "%1ø=%2")
    resolve("^ev%=([^" .. V .. "])", "ø=%1")
    resolve("(èv%=)", "ø=")

    resolve("([^aeuøuâ])iv%=([^" .. V .. "])", "%1y=%2")
    resolve("^iv%=([^" .. V .. "])", "y=%1")
    resolve("(ìv%=)", "y=")

    -- resolve CvC
    -- resolve("mv%=([" .. C .. "])","nu=%1")
    resolve("([" .. C .. "])v%=([" .. C .. "])", "%1u=%2")
    resolve("vv%=([aiuú])", "v=%1")
    resolve("([" .. C .. "])v%=R?e$", "%1u=i")
    resolve("([" .. C .. "])v%=Ra$", "%1u=a")

    -- resolve -ûve → -úe
    resolve("ûv%=R?e", "ú=e")
    resolve("ûv%=R?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 misc.
    resolve("(v%=g)", "v")
    resolve("[HḤ][HḤ]%=", "=")
    resolve("[HḤ]%=", "=")
    resolve("au°?%=R?a", "o=à")
    resolve("au°?%=R?([" .. V .. "])", "o=%1")
    resolve("%=(%S*)R", "=%1")
    resolve("(%=)", "")
    resolve("(°)", "")

    return resolution
end

function export.getNounStem(args) -- rad-inflection|pos|principle_part| 3 | 4 | 5, outputs table of principle parts
    local principle_parts = {
        ["nom"] = "",
        ["voc"] = "",
        ["gen"] = "",
        ["dat"] = "",
        ["acc"] = "",
        ["ins"] = "",
        ["nompl"] = "",
        ["genpl"] = "",
        ["genpl_short"] = "",
        ["anim"] = ""
    }

    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]] or stem_data[args[5]] or stem_data[args[6]] then
        local v_index = 4 -- max nucleus position

        while true do
            if mw.ustring.find(args[v_index], "([%^%~])") and args[v_index + 1] ~= nil 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
                    end
                else
                    stem_broken = stem
                end
                break
            else
                if mw.ustring.find(V, mw.ustring.sub(args[v_index], -1)) and args[v_index + 1] ~= nil then
                    stem = args[v_index]
                    stem_raised = stem
                    stem_broken = stem
                    break
                elseif v_index == 3 then
                    stem = args[3]
                    stem_raised = stem
                    stem_broken = stem
                    break
                else
                    v_index = 3
                end
            end
        end

        if stem_broken == "u" then
            stem_broken_e = "û"
        else
            stem_broken_e = stem_broken
        end

        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
            ending = args[v_index + 2]

            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]
        elseif args[v_index + 1] ~= nil then
            ending = args[v_index + 1]
        elseif stem_data[args[v_index]] then
            ending = args[v_index]
            stem = ""
            stem_broken = ""
            stem_broken_e = ""
            stem_raised = ""
        else
            error("No valid ending detected")
        end
    elseif stem_data[args[3]] then
        ending = args[3]
        stem = ""
        stem_broken = ""
        stem_broken_e = ""
        stem_raised = ""
    else
        error("Invalid format: Please format the arguments as hv|o~u^u|þ, dv|a^u|n|þ, he|rḤ or lorál|eH")
    end

    if stem_data[ending] == "C" or stem_data[ending] == "CH" or stem_data[ending] == "VH" then
        stem = stem .. ending
        stem_broken = stem_broken .. ending
        stem_broken_e = stem_broken_e .. ending
        stem_raised = stem_raised .. ending
    end
     --

    --[[
	mw.log("Stem: " .. stem)
	if stem_broken then mw.log("Broken stem: " .. stem_broken ) end
	if stem_broken_e then mw.log("Broken stem (û): " .. stem_broken_e ) end
	if stem_raised then mw.log("Raised stem: " .. stem_raised ) end
	mw.log("Ending: " .. ending)
	]] -- generate endings from declension type --

    -- HEAVY AND HEAVY SPIRANT --
    if stem_data[ending] == "C" or stem_data[ending] == "CH" or stem_data[ending] == "VH" then
        -- LIGHT --
        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"
            principle_parts["voc"] = stem .. "=se"
        else
            principle_parts["nom"] = stem_broken .. "=s"
            principle_parts["acc"] = stem_broken .. "=d"
            principle_parts["voc"] = stem_broken .. "=se"
        end
        if (ending == "v" or ending == "r") and stem_raised == stem then
            principle_parts["gen"] = stem .. "=g"
        elseif stem_raised ~= stem then
            principle_parts["gen"] = stem_raised .. "=g"
        else
            principle_parts["gen"] = stem_broken .. "=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"] .. "=s"
        principle_parts["anim"] = stem --for adjs only
    elseif ending == "nj^e" then
        principle_parts["nom"] = stem_broken .. "nș"
        principle_parts["voc"] = stem_broken .. "nșe"
        principle_parts["gen"] = stem_broken_e .. "njesk"
        principle_parts["dat"] = stem_broken_e .. "=" .. "nja"
        principle_parts["acc"] = stem_broken .. "nșt"
        principle_parts["ins"] = stem .. "nivúr"
        principle_parts["nompl"] = stem_broken .. "nxe"
        principle_parts["genpl"] = stem_broken_e .. "nje"
        principle_parts["genpl_short"] = stem_broken_e .. "nje=s"
    elseif mw.ustring.match(ending, "^[tķ]%^o$") then
    	local temp_stem = mw.ustring.match(ending, "^([tķ])%^o$")
        principle_parts["nom"] = stem_broken .. temp_stem .. "s"
        principle_parts["voc"] = stem_broken .. temp_stem .. "se"
        principle_parts["gen"] = stem .. temp_stem .. "usk"
        principle_parts["dat"] = stem .. temp_stem .. "a"
        principle_parts["acc"] = stem_broken .. temp_stem .. "st"
        principle_parts["ins"] = stem_broken .. temp_stem .. "vúr"
        principle_parts["nompl"] = stem_broken .. temp_stem .. "se"
        principle_parts["genpl"] = stem_broken .. temp_stem .. "s"
        principle_parts["genpl_short"] = stem .. temp_stem .. "u=s"
    elseif stem_data[ending] == "V" then
        -- SEMI-LIGHT
        principle_parts["nom"] = stem_broken_e .. "=" .. light_noun_data[ending][1]
        principle_parts["voc"] = stem .. light_noun_data[ending][2] .. "se"
        principle_parts["gen"] = stem .. (light_noun_data[ending][3] or light_noun_data[ending][2]) .. "sk"
        principle_parts["dat"] = stem .. (light_noun_data[ending][4] or (light_noun_data[ending][2] .. "n"))
        principle_parts["acc"] = stem .. (light_noun_data[ending][5] or (light_noun_data[ending][2] .. "st"))
        principle_parts["ins"] = stem .. (light_noun_data[ending][6] or (light_noun_data[ending][2] .. "vúr"))
        principle_parts["nompl"] = stem .. light_noun_data[ending][2] .. "re"
        principle_parts["genpl"] = stem .. light_noun_data[ending][2]
        principle_parts["genpl_short"] = stem .. (light_noun_data[ending][3] or light_noun_data[ending][2]) .. "=s"
    elseif stem_data[ending] == "G" then
        principle_parts["nom"] = stem_broken .. light_noun_data[ending][1]
        principle_parts["voc"] = stem .. light_noun_data[ending][7]
        principle_parts["gen"] = stem_broken .. light_noun_data[ending][2]
        principle_parts["dat"] = stem .. light_noun_data[ending][3]
        principle_parts["acc"] = stem .. light_noun_data[ending][4]
        principle_parts["ins"] = stem .. light_noun_data[ending][5]
        principle_parts["nompl"] = stem_broken .. light_noun_data[ending][6] .. "re"
        principle_parts["genpl"] = stem_broken .. light_noun_data[ending][6]
        principle_parts["genpl_short"] = stem_broken .. "=" .. light_noun_data[ending][6] .. "=s"
    end

    --mw.log("——— Resolving ———")
    for part, infl in pairs(principle_parts) do
        principle_parts[part] = resolve_ending(infl)
    end

    principle_parts["type"] = stem_data[ending]

    --mw.logObject(principle_parts)
    return principle_parts
end

function export.getVerbStem(args) -- outputs table of principle parts
    if args["pre"] then
        for arg, value in pairs(args) do
            if arg ~= "pre" and arg ~= "t" and arg ~= "vstem" and not mw.ustring.match(arg, "note$") then
                args[arg] = args["pre"] .. value
            end
        end
    end

    local principle_parts = {
        ["inf"] = args[1],
        ["2s"] = args[2],
        ["pst"] = args[3],
        ["prespart"] = args[4],
        ["pspart"] = args[5],
        ["passinf"] = args[6],
        ["passprespart"] = args[7] or args[6]
    }

    if args["vstem"] then
        if args.vstem == "true" then
            principle_parts["vstem"] = true
        elseif args.vstem == "false" then
            principle_parts["vstem"] = false
        end
    elseif
        mw.ustring.match(principle_parts["2s"], "[^n]v$") and not mw.ustring.match(principle_parts["inf"], "ft$") and
            not mw.ustring.match(principle_parts["inf"], "aut$")
     then
        principle_parts["vstem"] = true
    else
        principle_parts["vstem"] = false
    end

    -- non-finite parameters
    if principle_parts.vstem then
        principle_parts["vnoun"] = args["vnoun"] or mw.ustring.gsub(principle_parts["2s"], "v$", "nș")
    else
        principle_parts["vnoun"] = args["vnoun"] or (mw.ustring.gsub(principle_parts["pspart"] .. "aș", "âaș$", "oàș"))
    end
    if mw.ustring.match(principle_parts["pspart"], "ru$") then
        principle_parts["pspart"] = mw.ustring.gsub(principle_parts["pspart"], "ru$", "rủ")
    else
        principle_parts["pspart"] = mw.ustring.gsub(principle_parts["pspart"] .. "ú", "[uú]ú$", "ủ")
    end

    -- nonpast parameters
    if args["1s"] then
        principle_parts["1s"] = args["1s"]
    else
        if principle_parts["vstem"] then
            principle_parts["1s"] = mw.ustring.gsub(principle_parts["2s"], "v$", "") .. "e"
        else
            principle_parts["1s"] = principle_parts["2s"] .. "e"
        end
        principle_parts["1s"] = mw.ustring.gsub(principle_parts["1s"], "([âø])e$", "%1i")
        principle_parts["1s"] = mw.ustring.gsub(principle_parts["1s"], "([^eoø])ae$", "%1ai")
        principle_parts["1s"] = mw.ustring.gsub(principle_parts["1s"], "([^i])ee$", "%1ei")
        principle_parts["1s"] = mw.ustring.gsub(principle_parts["1s"], "([^aeuøâ])ie$", "%1iè")
        principle_parts["1s"] = mw.ustring.gsub(principle_parts["1s"], "ye$", "yè")
        principle_parts["1s"] = mw.ustring.gsub(principle_parts["1s"], "([^au])oe$", "%1ui")
        principle_parts["1s"] = mw.ustring.gsub(principle_parts["1s"], "([^a])ue$", "%1ui")
        principle_parts["1s"] = mw.ustring.gsub(principle_parts["1s"], "uave$", "uòe")
        principle_parts["1s"] = mw.ustring.gsub(principle_parts["1s"], "([^eøo])ave$", "%1oe")
        principle_parts["1s"] = mw.ustring.gsub(principle_parts["1s"], "iee$", "íe")
        principle_parts["1s"] = mw.ustring.gsub(principle_parts["1s"], "yee$", "ýe")
        principle_parts["1s"] = mw.ustring.gsub(principle_parts["1s"], "uoe$", "úe")
        principle_parts["1s"] = mw.ustring.gsub(principle_parts["1s"], "aoe$", "aoi")
        principle_parts["1s"] = mw.ustring.gsub(principle_parts["1s"], "nve$", "nui")
    end
    if args["3s"] then
        principle_parts["3s"] = args["3s"]
    else
        principle_parts["3s"] = principle_parts["2s"] .. "ș"
        principle_parts["3s"] = mw.ustring.gsub(principle_parts["3s"], "([^eoø])aș$", "%1áș")
        principle_parts["3s"] = mw.ustring.gsub(principle_parts["3s"], "áș$", "ảș")
        principle_parts["3s"] = mw.ustring.gsub(principle_parts["3s"], "([^aeuøâ])iș$", "%1eaș")
        principle_parts["3s"] = mw.ustring.gsub(principle_parts["3s"], "([^i])eș$", "%1eaș")
        principle_parts["3s"] = mw.ustring.gsub(principle_parts["3s"], "([^a])uș$", "%1oaș")
        principle_parts["3s"] = mw.ustring.gsub(principle_parts["3s"], "([^au])oș$", "%1oaș")
        principle_parts["3s"] = mw.ustring.gsub(principle_parts["3s"], "aoș$", "aôș")
        principle_parts["3s"] = mw.ustring.gsub(principle_parts["3s"], "[øy]ș$", "øaș")
        principle_parts["3s"] = mw.ustring.gsub(principle_parts["3s"], "âș$", "oaș")
        if principle_parts["vstem"] then
            principle_parts["3s"] = mw.ustring.gsub(principle_parts["3s"], "vș$", "ș")
        end
        principle_parts["3s"] = mw.ustring.gsub(principle_parts["3s"], "[sș]ș$", "ș")
        principle_parts["3s"] = mw.ustring.gsub(principle_parts["3s"], "nvș$", "mș")
        principle_parts["3s"] = mw.ustring.gsub(principle_parts["3s"], "([^eoø])avș$", "%1auș")
        principle_parts["3s"] = mw.ustring.gsub(principle_parts["3s"], "([nņlr])gș$", "%1ș")
    end
    principle_parts["pl"] = args["pl"]
    principle_parts["a"] = args["a"] or mw.ustring.gsub(principle_parts["2s"] .. "a", "([eoø])a$", "%1à")
    principle_parts["a"] = mw.ustring.gsub(principle_parts["a"], "aa$", "á")
    principle_parts["a"] = mw.ustring.gsub(principle_parts["a"], "áa$", "ả")
    principle_parts["a"] = mw.ustring.gsub(principle_parts["a"], "âa$", "oà")
    if principle_parts["vstem"] then
        principle_parts["a"] = mw.ustring.gsub(principle_parts["a"], "ava$", "á")
    end
    --principle_parts["aa"] = args["aa"] --or mw.ustring.gsub(principle_parts["2s"] .. "á", "([^eoø])avá$", "%1áva")
    --principle_parts["ai"] = args["ai"] --or mw.ustring.gsub(principle_parts["2s"] .. "ai", "([^eoø])avai$", "%1áe")
    if principle_parts["vstem"] then
        principle_parts["depers"] = args["depers"] or mw.ustring.gsub(principle_parts["2s"], "v$", "")
    else
        principle_parts["depers"] = args["depers"]
        if principle_parts["depers"] then
            principle_parts["depers"] = mw.ustring.gsub(principle_parts["depers"], "([^eoø])av$", "%1au")
        end
    end
    --principle_parts["o"] = mw.ustring.gsub(principle_parts["2s"] .. "o", "([au])o$", "%1ò")
    local imp_temp = args["depers"]
    if imp_temp then
        if mw.ustring.match(imp_temp, "au$") then
            imp_temp = nil
        elseif mw.ustring.find(imp_temp, "[uú]$") then
            imp_temp = imp_temp .. "a"
        else
        	imp_temp = principle_parts["2s"] .. "a"
        end
    end
    if principle_parts["vstem"] then
        principle_parts["imp"] = args["imp"] or mw.ustring.gsub(principle_parts["2s"], "v$", "ja")
    else
        principle_parts["imp"] = args["imp"] or imp_temp or nil
    end
    if args["r"] then
        principle_parts["r"] = args["r"]
    else
        principle_parts["r"] = (principle_parts["depers"] or principle_parts["2s"]) .. "r"
        if principle_parts["vstem"] then
            principle_parts["r"] = mw.ustring.gsub(principle_parts["r"], "vr$", "r")
        end
        principle_parts["r"] = mw.ustring.gsub(principle_parts["r"], "([sș])r$", "%1tr")
        principle_parts["r"] = mw.ustring.gsub(principle_parts["r"], "([zx])r$", "%1dr")
        principle_parts["r"] = mw.ustring.gsub(principle_parts["r"], "rr$", "r")
        principle_parts["r"] = mw.ustring.gsub(principle_parts["r"], "nr$", "rr")
        principle_parts["r"] = mw.ustring.gsub(principle_parts["r"], "mr$", "mbr")
        principle_parts["r"] = mw.ustring.gsub(principle_parts["r"], "ņr$", "ņgr")
        principle_parts["r"] = mw.ustring.gsub(principle_parts["r"], "nvr$", "nur")
    end

    -- past parameters
    principle_parts["apst"] = args["apst"] or mw.ustring.gsub(principle_parts["pst"] .. "a", "([eoø])a$", "%1à")
    principle_parts["apst"] = mw.ustring.gsub(principle_parts["apst"], "aa$", "á")
    principle_parts["apst"] = mw.ustring.gsub(principle_parts["apst"], "áa$", "ả")
    principle_parts["apst"] = mw.ustring.gsub(principle_parts["apst"], "âa$", "oà")
    -- principle_parts["aipst"] = args["aipst"]
    principle_parts["epst"] = args["epst"]
    if args["rpst"] then
        principle_parts["rpst"] = args["rpst"]
    else
        principle_parts["rpst"] = principle_parts["pst"] .. "r"
        principle_parts["rpst"] = mw.ustring.gsub(principle_parts["rpst"], "([sș])r$", "%1tr")
        principle_parts["rpst"] = mw.ustring.gsub(principle_parts["rpst"], "([zx])r$", "%1dr")
        principle_parts["rpst"] = mw.ustring.gsub(principle_parts["rpst"], "rr$", "r")
        principle_parts["rpst"] = mw.ustring.gsub(principle_parts["rpst"], "nr$", "rr")
        principle_parts["rpst"] = mw.ustring.gsub(principle_parts["rpst"], "mr$", "mbr")
        principle_parts["rpst"] = mw.ustring.gsub(principle_parts["rpst"], "ņr$", "ņgr")
    end

    return principle_parts
end

function export.getStem(frame)
    local args = getArgs(frame)
    local word = mw.title.getCurrentTitle().subpageText
    local to_Return = ""

    if args[1] ~= nil and m_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
        to_Return = export.getNounStem(args)[args[2]]
    elseif args[1] == "verb" then
        to_Return = export.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", "þ"}})
]]--