Jump to content

Module:aii-conj/sandbox

From Wiktionary, the free dictionary


local export = {}

local U = require("Module:string/char")
local rsub = mw.ustring.gsub
local rmatch = mw.ustring.match
local rlen = mw.ustring.len

-- Vowel Diacritics
local KHWASA_RWASA = U(0x73C)
local KHWASA_RWAKHA = U(0x73F)
local PTAKHA = U(0x732)
local ZQAPA = U(0x735)
local ZLAMA_KIRYA = U(0x738)
local ZLAMA_YAREEKHA = U(0x739)
local VOWEL_DIACRITICS = KHWASA_RWASA .. KHWASA_RWAKHA .. PTAKHA .. ZQAPA .. ZLAMA_KIRYA .. ZLAMA_YAREEKHA

-- Non-vowel Diacritics
local COMBINING_TILDE_BELOW = U(0x330)
local COMBINING_TILDE_ABOVE = U(0x303)
local QUSHSHAYA = U(0x741)
local RUKKAKHA = U(0x742)
local COMBINING_BREVE_BELOW = U(0x32E)
local TALQANA_ABOVE = U(0x747)
local NON_VOWEL_DIACRITICS = COMBINING_TILDE_BELOW .. COMBINING_TILDE_ABOVE .. QUSHSHAYA .. RUKKAKHA .. COMBINING_BREVE_BELOW .. TALQANA_ABOVE

-- Consonants and Vowel Helpers
local CONSONANTS = 'ܦܒܬܛܕܟܓܩܣܨܙܫܚܥܗܡܢܪܠ'
local CONSONANTS_MINUS_E = rsub(CONSONANTS, 'ܥ', '')
local ALAP = 'ܐ'
local WAW = 'ܘ'
local YUDH = 'ܝ'
local VOWEL_HELPERS = ALAP .. WAW .. YUDH
local LETTERS = CONSONANTS .. VOWEL_HELPERS

-- Special Cases
local special_cases = {
    -- otherwise would match Template:aii-conj-verb/G-2i
    ['ܟܵܐܹܒ'] = {'G', 'ܟ', 'ܐ', 'ܒ'},
    ['ܛܵܐܹܒ݂'] = {'G', 'ܛ', 'ܐ', 'ܒ݂'},
    ['ܫܵܐܹܠ'] = {'G', 'ܫ', 'ܐ', 'ܠ'},
    ['ܣܵܐܹܢ'] = {'G', 'ܣ', 'ܐ', 'ܢ'}
}

-- Create Capture Groups
local function create_cg(characters)
    return '(' .. '[' .. characters .. ']' .. '[' .. NON_VOWEL_DIACRITICS .. ']?' .. ')'
end

local function match_groups(text, pattern)
    local matches = {}
    local g1, g2, g3, g4, g5 = rmatch(text, pattern)
    if g1 then table.insert(matches, g1) end
    if g2 then table.insert(matches, g2) end
    if g3 then table.insert(matches, g3) end
    if g4 then table.insert(matches, g4) end
    if g5 then table.insert(matches, g5) end
    
    return matches
end

-- https://en.wiktionary.org/wiki/Category:Assyrian_Neo-Aramaic_verb_inflection-table_templates
local patterns = {
    {'C', 4, '^ܡ' .. PTAKHA .. create_cg(LETTERS) .. create_cg(CONSONANTS .. ALAP .. WAW) .. ZLAMA_KIRYA .. create_cg(CONSONANTS) .. '$'},
    {'C-1o', 4, '^ܡ' .. ZQAPA .. WAW .. create_cg(CONSONANTS) .. ZLAMA_KIRYA .. create_cg(CONSONANTS) .. '$'},
    {'C-1o3i', 4, '^ܡ' .. ZQAPA .. WAW .. create_cg(CONSONANTS) .. ZLAMA_YAREEKHA .. ALAP .. '$'},
    {'C-2d3i', 3, '^ܡ' .. PTAKHA .. create_cg(LETTERS) .. ZLAMA_YAREEKHA .. ALAP .. '$'},
    {'C-3i', 4, '^ܡ' .. PTAKHA .. create_cg(LETTERS) .. create_cg(LETTERS) .. ZLAMA_YAREEKHA .. ALAP .. '$'},
    {'C-drop', 3, '^ܡ' .. PTAKHA .. create_cg(CONSONANTS) .. ZLAMA_KIRYA .. create_cg(CONSONANTS) .. '$'},
    {'C-mv', 4, '^ܡ' .. PTAKHA .. create_cg(LETTERS) .. YUDH .. ZLAMA_KIRYA .. create_cg(CONSONANTS) .. '$'},

    {'D', 4, '^ܡ' .. create_cg(LETTERS) .. PTAKHA .. create_cg(LETTERS) .. ZLAMA_KIRYA .. create_cg(LETTERS) .. '$'},
    {'D-1o', 5, '^ܡܫ' .. ZQAPA .. WAW .. create_cg(LETTERS) .. ZLAMA_KIRYA .. create_cg(LETTERS) .. '$'},
    {'D-3i', 4, '^ܡ' .. create_cg(LETTERS) .. PTAKHA .. create_cg(CONSONANTS .. WAW) .. ZLAMA_YAREEKHA .. ALAP .. '$'},

    {'G', 3, '^' .. create_cg(CONSONANTS) .. ZQAPA .. create_cg(CONSONANTS .. WAW) .. ZLAMA_YAREEKHA .. create_cg(CONSONANTS_MINUS_E) .. '$'},
    {'G-1i', 3, '^' .. create_cg(ALAP .. YUDH) .. ZQAPA .. create_cg(CONSONANTS .. ALAP) .. ZLAMA_YAREEKHA .. create_cg(CONSONANTS) .. '$'},
    {'G-1i3i', 3, '^' .. YUDH .. ZQAPA .. create_cg(CONSONANTS) .. ZLAMA_YAREEKHA .. ALAP .. '$'},
    {'G-2i', 3, '^' .. create_cg(CONSONANTS) .. ZQAPA .. '[' .. ALAP .. YUDH .. ']' .. ZLAMA_YAREEKHA .. create_cg(CONSONANTS) .. '$'},
    {'G-3e', 3, '^' .. create_cg(CONSONANTS) .. ZQAPA .. create_cg(CONSONANTS) .. ZLAMA_YAREEKHA .. 'ܥ$'},
    {'G-3i', 3, '^' .. create_cg(CONSONANTS .. ALAP) .. ZQAPA .. create_cg(CONSONANTS .. WAW .. YUDH) .. ZLAMA_YAREEKHA .. ALAP .. '$'},

    {'Gt', 5, '^ܡ' .. ZLAMA_KIRYA .. 'ܬ' .. RUKKAKHA .. create_cg(CONSONANTS) .. create_cg(CONSONANTS) .. ZLAMA_KIRYA .. create_cg(CONSONANTS) .. '$'},

    {'Penta', 6, '^ܡ' .. create_cg(LETTERS) .. PTAKHA .. create_cg(CONSONANTS) .. create_cg(CONSONANTS) .. create_cg(LETTERS) .. ZLAMA_KIRYA .. create_cg(CONSONANTS) .. '$'},

    {'Q', 5, '^ܡ' .. create_cg(LETTERS) .. PTAKHA .. create_cg(CONSONANTS .. WAW .. YUDH) .. create_cg(CONSONANTS .. ALAP .. WAW) .. ZLAMA_KIRYA .. create_cg(CONSONANTS) .. '$'},
    {'Q-w', 5, '^ܡ' .. create_cg(LETTERS) .. PTAKHA .. create_cg(CONSONANTS) .. create_cg(CONSONANTS) .. ZLAMA_YAREEKHA .. ALAP .. '$'}
}


function export._conj(aii_v)
    -- given the non-past, 3rd person, single, masculine form of a verb
    -- return the template name and the arguments to that template which generate the other forms
    local matched_patterns = {}
    local vowel_diacritics_stripped = rsub(aii_v, '[^' .. LETTERS .. ']', '')

    if special_cases[aii_v] then
        local pattern = special_cases[aii_v][1]
        local args = {unpack(special_cases[aii_v], 2)}
        return {
            title = "aii-conj-verb/" .. pattern,
            args = args,
        }
    end

    for _, pattern_tuple in ipairs(patterns) do
        local pattern, num_atwateh, regex = pattern_tuple[1], pattern_tuple[2], pattern_tuple[3]
        local args = match_groups(aii_v, regex)

        if rlen(vowel_diacritics_stripped) == num_atwateh and #args > 0 then
            table.insert(matched_patterns, {
                title = 'aii-conj-verb/' .. pattern,
                args = args
            })
        end
    end

    if #matched_patterns == 0 then
        error(aii_v .. ' matched no existing conjugation patterns')
    elseif #matched_patterns == 1 then
        return matched_patterns[1]
    else
        error(aii_v .. ' matched multiple patterns - it should only match one conjugation pattern')
    end
end

function export.preventDirectInvocation(frame)
	-- Ensures that verb conjugation templates can only be called from [[Module:aii-conj]]
    local currentTitle = mw.title.getCurrentTitle()
    if currentTitle.namespace == 0 then
        error("Please use [[Template:aii-conj]] instead. Direct use of this template is not allowed.", 0)
    end
end

function export.conj(frame)
    local aii_v = frame.args[1]
    local result = export._conj(aii_v)
    return frame:expandTemplate{ title = result.title, args = result.args }
end

return export