| 1 |
-- Lua script to perform bayes learning (multi-class) |
| 2 |
-- This script accepts the following parameters: |
| 3 |
-- key1 - prefix for bayes tokens (e.g. for per-user classification) |
| 4 |
-- argv1 - class label string (e.g. "S", "H", "T") |
| 5 |
-- argv2 - string symbol |
| 6 |
-- argv3 - boolean is_unlearn |
| 7 |
-- argv4 - set of tokens encoded in messagepack array of strings |
| 8 |
-- argv5 - set of text tokens (if any) encoded in messagepack array of strings (size must be twice of `ARGV[4]`) |
| 9 |
|
| 10 |
local prefix = KEYS[1] |
| 11 |
local class_label = ARGV[1] |
| 12 |
local symbol = ARGV[2] |
| 13 |
local is_unlearn = ARGV[3] == 'true' and true or false |
| 14 |
local input_tokens = cmsgpack.unpack(ARGV[4]) |
| 15 |
local text_tokens |
| 16 |
|
| 17 |
if ARGV[5] then |
| 18 |
text_tokens = cmsgpack.unpack(ARGV[5]) |
| 19 |
end |
| 20 |
|
| 21 |
-- Handle RS_<ID> HASH booleans and full class name strings for backward compatibility |
| 22 |
if class_label == 'true' or class_label == 'spam' then |
| 23 |
class_label = 'S' |
| 24 |
elseif class_label == 'false' or class_label == 'ham' then |
| 25 |
class_label = 'H' |
| 26 |
end |
| 27 |
|
| 28 |
local hash_key = class_label |
| 29 |
local learned_key = 'learns_' .. string.lower(class_label) |
| 30 |
|
| 31 |
-- Handle RS HASH keys for backward compatibility |
| 32 |
if class_label == 'S' then |
| 33 |
learned_key = 'learns_spam' |
| 34 |
elseif class_label == 'H' then |
| 35 |
learned_key = 'learns_ham' |
| 36 |
end |
| 37 |
|
| 38 |
redis.call('SADD', symbol .. '_keys', prefix) |
| 39 |
redis.call('HSET', prefix, 'version', '2') -- new schema |
| 40 |
|
| 41 |
-- Update learned count, but prevent it from going negative |
| 42 |
if is_unlearn then |
| 43 |
local current_count = tonumber(redis.call('HGET', prefix, learned_key)) or 0 |
| 44 |
if current_count > 0 then |
| 45 |
redis.call('HINCRBY', prefix, learned_key, -1) |
| 46 |
end |
| 47 |
else |
| 48 |
redis.call('HINCRBY', prefix, learned_key, 1) |
| 49 |
end |
| 50 |
|
| 51 |
for i, token in ipairs(input_tokens) do |
| 52 |
-- Update token count, but prevent it from going negative |
| 53 |
if is_unlearn then |
| 54 |
local current_token_count = tonumber(redis.call('HGET', token, hash_key)) or 0 |
| 55 |
if current_token_count > 0 then |
| 56 |
redis.call('HINCRBY', token, hash_key, -1) |
| 57 |
end |
| 58 |
else |
| 59 |
redis.call('HINCRBY', token, hash_key, 1) |
| 60 |
end |
| 61 |
|
| 62 |
if text_tokens then |
| 63 |
local tok1 = text_tokens[i * 2 - 1] |
| 64 |
local tok2 = text_tokens[i * 2] |
| 65 |
|
| 66 |
if tok1 then |
| 67 |
if tok2 then |
| 68 |
redis.call('HSET', token, 'tokens', string.format('%s:%s', tok1, tok2)) |
| 69 |
else |
| 70 |
redis.call('HSET', token, 'tokens', tok1) |
| 71 |
end |
| 72 |
|
| 73 |
if is_unlearn then |
| 74 |
local current_z_score = tonumber(redis.call('ZSCORE', prefix .. '_z', token)) or 0 |
| 75 |
if current_z_score > 0 then |
| 76 |
redis.call('ZINCRBY', prefix .. '_z', -1, token) |
| 77 |
end |
| 78 |
else |
| 79 |
redis.call('ZINCRBY', prefix .. '_z', 1, token) |
| 80 |
end |
| 81 |
end |
| 82 |
end |
| 83 |
end |