Module:Polola: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
-- | -- Helper function to trim whitespaces | ||
local function trim(s) | local function trim(s) | ||
return s:match("^%s*(.-)%s*$") | return s:match("^%s*(.-)%s*$") | ||
end | end | ||
-- | -- Parse function for Polola language grammar and structures | ||
function p.parse(frame) | function p.parse(frame) | ||
-- | -- Read 'expr' parameter from template or direct call | ||
local input = frame.args.expr or frame:getParent().args.expr or "" | local input = frame.args.expr or frame:getParent().args.expr or "" | ||
if trim(input) == "" then | if trim(input) == "" then | ||
return "<span style='color:red;'> | return "<span style='color:red;'>Error: No expression provided!</span>" | ||
end | end | ||
local result = {} | 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 | |||
for token in string.gmatch( | |||
-- | |||
local clean_token = token:gsub("%(", ""):gsub("%)", "") | 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*)$") | local base_word, quantifier = clean_token:match("^([a-z]+)(%d*)$") | ||
if base_word then | if base_word then | ||
local meaning = "" | local meaning = "" | ||
-- | -- Basic Polola vocabulary translation logic | ||
if string.sub(base_word, 1, 1) == "l" then | if string.sub(base_word, 1, 1) == "l" then | ||
meaning = " | meaning = "distant/hidden concept (" .. base_word .. ")" | ||
elseif string.sub(base_word, 1, 1) == "s" then | elseif string.sub(base_word, 1, 1) == "s" then | ||
meaning = " | meaning = "present/near concept (" .. base_word .. ")" | ||
else | else | ||
meaning = " | meaning = "general concept (" .. base_word .. ")" | ||
end | end | ||
-- Append quantifier info if present | |||
if quantifier and quantifier ~= "" then | if quantifier and quantifier ~= "" then | ||
meaning = meaning .. " [" .. quantifier .. "x | meaning = meaning .. " [quantifier: " .. quantifier .. "x]" | ||
end | end | ||
table.insert(result, meaning) | table.insert(result, meaning) | ||
| Line 47: | Line 44: | ||
end | end | ||
-- | -- Check for complex structural operators | ||
local structure_info = "" | local structure_info = "" | ||
if input:match("%(") or input:match("%+") or input:match("%*") then | if input:match("%(") or input:match("%+") or input:match("%*") or input:match("%/") then | ||
structure_info = "<br/><small><i> | structure_info = "<br/><small><i>Structure: Complex logical/associative relation detected.</i></small>" | ||
end | end | ||
-- | -- Return final formatted output | ||
if #result == 0 then | if #result == 0 then | ||
return "Polola | return "Polola syntax: " .. input .. " → Uninterpretable expression." | ||
else | else | ||
return "Polola | return "Polola syntax: <b>" .. input .. "</b><br/>Interpretation: " .. table.concat(result, ", ") .. structure_info | ||
end | end | ||
end | end | ||
return p | return p | ||
Latest revision as of 08:34, 3 July 2026
- This module lacks a documentation subpage. Please create it.
- Useful links: subpage list • links • transclusions • testcases • sandbox
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