| 1 |
-- Lua script to perform bayes classification (multi-class) |
| 2 |
-- This script accepts the following parameters: |
| 3 |
-- key1 - prefix for bayes tokens (e.g. for per-user classification) |
| 4 |
-- argv1 - class labels: table of all class labels as "TABLE:label1,label2,..." |
| 5 |
-- argv2 - set of tokens encoded in messagepack array of strings |
| 6 |
|
| 7 |
local prefix = KEYS[1] |
| 8 |
local class_labels_arg = ARGV[1] |
| 9 |
local input_tokens = cmsgpack.unpack(ARGV[2]) |
| 10 |
|
| 11 |
-- Parse class labels (always expect TABLE: format) |
| 12 |
local class_labels = {} |
| 13 |
if string.match(class_labels_arg, "^TABLE:") then |
| 14 |
local labels_str = string.sub(class_labels_arg, 7) -- Remove "TABLE:" prefix |
| 15 |
for label in string.gmatch(labels_str, "([^,]+)") do |
| 16 |
table.insert(class_labels, label) |
| 17 |
end |
| 18 |
else |
| 19 |
-- Legacy single class - convert to array |
| 20 |
class_labels = { class_labels_arg } |
| 21 |
end |
| 22 |
|
| 23 |
-- Get learned counts for all classes (ordered) |
| 24 |
local learned_counts = {} |
| 25 |
for _, label in ipairs(class_labels) do |
| 26 |
local key = 'learns_' .. string.lower(label) |
| 27 |
-- Handle legacy keys for backward compatibility |
| 28 |
if label == 'H' then |
| 29 |
key = 'learns_ham' |
| 30 |
elseif label == 'S' then |
| 31 |
key = 'learns_spam' |
| 32 |
end |
| 33 |
table.insert(learned_counts, tonumber(redis.call('HGET', prefix, key)) or 0) |
| 34 |
end |
| 35 |
|
| 36 |
-- Get token data for all classes (ordered) |
| 37 |
local token_results = {} |
| 38 |
for i, _ in ipairs(class_labels) do |
| 39 |
token_results[i] = {} |
| 40 |
end |
| 41 |
|
| 42 |
-- Check if we have any learning data |
| 43 |
local has_learns = false |
| 44 |
for _, count in ipairs(learned_counts) do |
| 45 |
if count > 0 then |
| 46 |
has_learns = true |
| 47 |
break |
| 48 |
end |
| 49 |
end |
| 50 |
|
| 51 |
if has_learns then |
| 52 |
-- Process each token |
| 53 |
for i, token in ipairs(input_tokens) do |
| 54 |
local token_data = redis.call('HMGET', token, unpack(class_labels)) |
| 55 |
|
| 56 |
if token_data then |
| 57 |
for j, _ in ipairs(class_labels) do |
| 58 |
local count = token_data[j] |
| 59 |
if count and tonumber(count) > 0 then |
| 60 |
table.insert(token_results[j], { i, tonumber(count) }) |
| 61 |
end |
| 62 |
end |
| 63 |
end |
| 64 |
end |
| 65 |
end |
| 66 |
|
| 67 |
-- Always return ordered arrays: [learned_counts_array, token_results_array] |
| 68 |
return { learned_counts, token_results } |