From 0431089dd2bd506171d5cc22183d53c9b81f611e Mon Sep 17 00:00:00 2001 From: ubq323 Date: Tue, 5 Nov 2024 18:55:43 +0000 Subject: initial --- conlex.lua | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100755 conlex.lua (limited to 'conlex.lua') diff --git a/conlex.lua b/conlex.lua new file mode 100755 index 0000000..afee980 --- /dev/null +++ b/conlex.lua @@ -0,0 +1,116 @@ +#!/usr/bin/env lua5.3 +local function template(str, args) + return (str:gsub("%%(%w+)%%", args)) +end + +local function urldecode(s) + return s:gsub('+',' ') + :gsub('%%(%x%x)',function(x) + return string.char(tonumber(x,16)) + end) +end +local function parse_qs(s) + local ix = 1 + local res = {} + while true do + local _,next_ix,ek,ev = s:find("([^=]+)=([^&]*)&?",ix) + if not ek then break end + local k,v = urldecode(ek), urldecode(ev) + res[k] = v + ix = next_ix + 1 + end + return res +end + +local function eprint(...) + local s = {} + for i = 1, select('#',...) do + s[i] = tostring(select(i, ...)) + end + io.stderr:write(table.concat(s, "\t"), "\n") +end + +local document_template +do + local template_file = assert(io.open"template.html") + document_template = template_file:read"a" + template_file:close() +end + +local function load_dict(filename) + local res = {} + for line in io.lines(filename) do + local word, pos, def = line:match "^(.-)\t(.-)\t(.-)$" + if word then + res[word] = {pos=pos, def=def, word=word} + end + end + return res +end + +local function format_def(found_word, entry) + local defc + if entry then + defc = template("%word% %pos%. %def%", entry) + else + defc = "????" + end + return template( + "%defc%%wi%", + {defc=defc, wi=found_word} + ) +end + +local glossors = {} + +function glossors.sanila(text) + local words = load_dict "sanila.tsv" + + local items = {} + -- sanila morphology is easy enough + for word in text:gmatch("%S+") do + local stripped = word:match("^(.-)[.,]*$") + local entry = words[stripped] + table.insert(items, format_def(word, entry)) + end + + return items +end + +local function engloss(langue, text) + local items = glossors[langue](text) + return template([[ +
%text%
+
]], {text=table.concat(items, "\n")}) +end + +local function enform() + return [[ +
+
+ + +
+
]] +end + +local function response(content) + print("Content-Type: text/html") + print() + print(template(document_template, {content=content})) +end + +local function main() + local text = nil + local qs = os.getenv"QUERY_STRING" + if qs then text = parse_qs(qs).q end + + if not text then + response(enform()) + else + response(engloss('sanila', text)) + end +end + +main() + -- cgit v1.2.3