Module:Polola: Difference between revisions

From Linguifex
Jump to navigation Jump to search
Nyima (talk | contribs)
Created page with "local p = {} local deictic = { s = "present / visible / local", l = "distant / hidden / abstract" } local ontology = { d = "object (inanimate matter)", f = "living being (biological entity)", r = "energy (radiation / physical process)", g = "abstract thing (concept / thought)" } local dynamics = { g = "moving / changing / active", h = "stationary / static / at rest" } local operators = { ["+"] = " '''[OR]''' ", ["*"] = " '''[AN..."
 
Nyima (talk | contribs)
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


local deictic = {
-- Helper function to trim whitespaces
    s = "present / visible / local",
local function trim(s)
    l = "distant / hidden / abstract"
     return s:match("^%s*(.-)%s*$")
}
 
local ontology = {
    d = "object (inanimate matter)",
    f = "living being (biological entity)",
    r = "energy (radiation / physical process)",
    g = "abstract thing (concept / thought)"
}
 
local dynamics = {
    g = "moving / changing / active",
    h = "stationary / static / at rest"
}
 
local operators = {
    ["+"] = " '''[OR]''' ",
    ["*"] = " '''[AND]''' ",
    ["-"] = " '''[NOT / EXCLUDING]:''' ",
    ["/"] = " '''[IN RELATION TO / DEPENDING ON]:''' ",
    ["%"] = " '''[SUBSET / REMAINDER OF]:''' ",
    ["("] = " ( ",
    [")"] = " ) ",
    [")("] = " '''[XOR / EXCLUSIVE OR]''' ",
    ["0"] = " '''[FALSE / VOID]''' ",
    ["1"] = " '''[TRUE / EXISTENCE]''' "
}
 
local function translate_word(word)
     if #word ~= 3 then return word end
    local c = string.sub(word, 1, 1)
    local o = string.sub(word, 2, 2)
    local d = string.sub(word, 3, 3)
    if deictic[c] and ontology[o] and dynamics[d] then
        return string.format("<span style='color:#0066cc;'>&lt;%s %s %s&gt;</span>", deictic[c], dynamics[d], ontology[o])
    end
    return word
end
end


-- Parse function for Polola language grammar and structures
function p.parse(frame)
function p.parse(frame)
     local expr = frame.args.expr or frame:getParent().args.expr or ""
    -- Read 'expr' parameter from template or direct call
     if expr == "" then return "''No Polola expression provided!''" end
     local input = frame.args.expr or frame:getParent().args.expr or ""
    expr = string.gsub(expr, ")%s*(", " )( ")
     if trim(input) == "" then
    for op in string.gmatch("+*-/%()", ".") do
        return "<span style='color:red;'>Error: No expression provided!</span>"
        expr = string.gsub(expr, "%" .. op, " " .. op .. " ")
     end
     end
     local result = {}
     local result = {}
     for token in string.gmatch(expr, "%S+") do
   
         local word, num = string.match(token, "^([sl][dfrg][gh])(%d+)$")
    -- Safe tokenization using escaped pattern matching
         if word and num then
    -- % character escapes operators like +, *, /, and brackets () so they don't cause capture errors
             table.insert(result, string.format("'''%s units of''' %s", num, translate_word(word)))
     for token in string.gmatch(input, "([^%s%+%*%/]+)") do
        elseif operators[token] then
        -- Clean brackets from the token safely
            table.insert(result, operators[token])
         local clean_token = token:gsub("%(", ""):gsub("%)", "")
        else
       
             table.insert(result, translate_word(token))
        -- Separate the base word from numeric quantifiers (e.g., sfg3 -> base: sfg, quantifier: 3)
        local base_word, quantifier = clean_token:match("^([a-z]+)(%d*)$")
       
         if base_word then
             local meaning = ""
            -- Basic Polola vocabulary translation logic
            if string.sub(base_word, 1, 1) == "l" then
                meaning = "distant/hidden concept (" .. base_word .. ")"
            elseif string.sub(base_word, 1, 1) == "s" then
                meaning = "present/near concept (" .. base_word .. ")"
            else
                meaning = "general concept (" .. base_word .. ")"
            end
           
            -- Append quantifier info if present
            if quantifier and quantifier ~= "" then
                meaning = meaning .. " [quantifier: " .. quantifier .. "x]"
            end
             table.insert(result, meaning)
         end
         end
     end
     end
     return table.concat(result, "")
 
     -- Check for complex structural operators
    local structure_info = ""
    if input:match("%(") or input:match("%+") or input:match("%*") or input:match("%/") then
        structure_info = "<br/><small><i>Structure: Complex logical/associative relation detected.</i></small>"
    end
 
    -- Return final formatted output
    if #result == 0 then
        return "Polola syntax: " .. input .. " → Uninterpretable expression."
    else
        return "Polola syntax: <b>" .. input .. "</b><br/>Interpretation: " .. table.concat(result, ", ") .. structure_info
    end
end
end


return p
return p

Latest revision as of 08:34, 3 July 2026



local p = {}

-- Helper function to trim whitespaces
local function trim(s)
    return s:match("^%s*(.-)%s*$")
end

-- Parse function for Polola language grammar and structures
function p.parse(frame)
    -- Read 'expr' parameter from template or direct call
    local input = frame.args.expr or frame:getParent().args.expr or ""
    if trim(input) == "" then
        return "<span style='color:red;'>Error: No expression provided!</span>"
    end

    local result = {}
    
    -- Safe tokenization using escaped pattern matching
    -- % character escapes operators like +, *, /, and brackets () so they don't cause capture errors
    for token in string.gmatch(input, "([^%s%+%*%/]+)") do
        -- Clean brackets from the token safely
        local clean_token = token:gsub("%(", ""):gsub("%)", "")
        
        -- Separate the base word from numeric quantifiers (e.g., sfg3 -> base: sfg, quantifier: 3)
        local base_word, quantifier = clean_token:match("^([a-z]+)(%d*)$")
        
        if base_word then
            local meaning = ""
            -- Basic Polola vocabulary translation logic
            if string.sub(base_word, 1, 1) == "l" then
                meaning = "distant/hidden concept (" .. base_word .. ")"
            elseif string.sub(base_word, 1, 1) == "s" then
                meaning = "present/near concept (" .. base_word .. ")"
            else
                meaning = "general concept (" .. base_word .. ")"
            end
            
            -- Append quantifier info if present
            if quantifier and quantifier ~= "" then
                meaning = meaning .. " [quantifier: " .. quantifier .. "x]"
            end
            table.insert(result, meaning)
        end
    end

    -- Check for complex structural operators
    local structure_info = ""
    if input:match("%(") or input:match("%+") or input:match("%*") or input:match("%/") then
        structure_info = "<br/><small><i>Structure: Complex logical/associative relation detected.</i></small>"
    end

    -- Return final formatted output
    if #result == 0 then
        return "Polola syntax: " .. input .. " → Uninterpretable expression."
    else
        return "Polola syntax: <b>" .. input .. "</b><br/>Interpretation: " .. table.concat(result, ", ") .. structure_info
    end
end

return p