diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-12-02 12:36:17 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-12-02 12:36:17 +0000 |
commit | 44c6c563c2faf5ca97646e92e8382b972d2d513f (patch) | |
tree | 10bb13efb26233f45b0f53a84f086d5364bc3be3 /lualib/redis_scripts | |
parent | 69381e238b339065c12c61ba458873ddec1142d3 (diff) | |
download | rspamd-44c6c563c2faf5ca97646e92e8382b972d2d513f.tar.gz rspamd-44c6c563c2faf5ca97646e92e8382b972d2d513f.zip |
[Project] Add classify redis script
Diffstat (limited to 'lualib/redis_scripts')
-rw-r--r-- | lualib/redis_scripts/bayes_classify.lua | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lualib/redis_scripts/bayes_classify.lua b/lualib/redis_scripts/bayes_classify.lua new file mode 100644 index 000000000..c2654e476 --- /dev/null +++ b/lualib/redis_scripts/bayes_classify.lua @@ -0,0 +1,26 @@ +-- Lua script to perform bayes classification +-- This script accepts the following parameters: +-- key1 - prefix for bayes tokens (e.g. for per-user classification) +-- key2 - set of tokens encoded in messagepack array of int64_t + +local prefix = KEYS[1] +local input_tokens = cmsgpack.unpack(KEYS[2]) +local output_spam = {} +local output_ham = {} + +for i, token in ipairs(input_tokens) do + local token_data = redis.call('HMGET', prefix .. tostring(token), 'H', 'S') + + if token_data then + local ham_count = tonumber(token_data[1]) or 0 + local spam_count = tonumber(token_data[2]) or 0 + + output_ham[i] = ham_count + output_spam[i] = spam_count + else + output_ham[i] = 0 + output_spam[i] = 0 + end +end + +return cmsgpack.pack({ output_ham, output_spam })
\ No newline at end of file |