From 9c48c734a4e6b2a14c7535427c2c377d51953d3c Mon Sep 17 00:00:00 2001 From: Jaromil Date: Fri, 3 May 2024 00:38:47 +0200 Subject: [PATCH] feat: Norvig's spelling corrector loaded and tested --- src/lua/init.lua | 1 + src/lua/spell.lua | 105 +++++++++++++++++++++++++++++++++++++ test/lua/introspection.lua | 6 +++ 3 files changed, 112 insertions(+) create mode 100644 src/lua/spell.lua diff --git a/src/lua/init.lua b/src/lua/init.lua index 71f95e3ca..71639b8ac 100644 --- a/src/lua/init.lua +++ b/src/lua/init.lua @@ -67,6 +67,7 @@ TIME = require'time' INSPECT = require('inspect') QSORT = require('qsort_op') -- optimized table sort table.sort = QSORT -- override native table sort +SPELL = require('spell') JSON = require('zenroom_json') ECDH = require('zenroom_ecdh') -- ECDH public keys cannot function as ECP because of IANA 7303 diff --git a/src/lua/spell.lua b/src/lua/spell.lua new file mode 100644 index 000000000..f470d70de --- /dev/null +++ b/src/lua/spell.lua @@ -0,0 +1,105 @@ +-- Spelling Corrector. +-- +-- Copyright 2014 Francisco Zamora-Martinez +-- Copyright 2024 Jaromil (Dyne.org) +-- Adaptation of Peter Norvig python Spelling Corrector: +-- http://norvig.com/spell-correct.html +-- Open source code under MIT license: http://www.opensource.org/licenses/mit-license.php + +local yield,wrap = coroutine.yield,coroutine.wrap +local alphabet_str,alphabet = 'abcdefghijklmnopqrstuvwxyz',{} +for a in alphabet_str:gmatch(".") do alphabet[#alphabet+1] = a end +spell = {} + +local function list(w) return pairs{[w]=true} end + +function spell:max(...) + local arg,max,hyp = table.pack(...),0,nil + for w in table.unpack(arg) do + local p = self.model[w] or 1 + if p>max or ( p==max and hyp