]> source.dussan.org Git - rspamd.git/commitdiff
Implement the case of incrementing backends for bayes
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 8 Jan 2016 16:08:07 +0000 (16:08 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 8 Jan 2016 16:08:07 +0000 (16:08 +0000)
src/libserver/cfg_file.h
src/libstat/backends/redis_backend.c
src/libstat/classifiers/bayes.c

index b03facd3a87fab32bcba4b9c3e5445d3a689bc8e..7b49e48668bfbdd71ccc8f9bad62b9044ad968dc 100644 (file)
@@ -130,6 +130,12 @@ struct rspamd_tokenizer_config {
 
 /* Classifier has all integer values (e.g. bayes) */
 #define RSPAMD_FLAG_CLASSIFIER_INTEGER (1 << 0)
+/*
+ * Set if backend for a classifier is intended to increment and not set values
+ * (e.g. redis)
+ */
+#define RSPAMD_FLAG_CLASSIFIER_INCREMENTING_BACKEND (1 << 1)
+
 /**
  * Classifier config definition
  */
index a83ab308190730333232400b559f569e8c9d6190..240584a5131825d6b12cd3d9cbd836e225d157f3 100644 (file)
@@ -609,6 +609,7 @@ rspamd_redis_init (struct rspamd_stat_ctx *ctx,
                backend->timeout = REDIS_DEFAULT_TIMEOUT;
        }
 
+       stf->clcf->flags |= RSPAMD_FLAG_CLASSIFIER_INCREMENTING_BACKEND;
 
        return (gpointer)backend;
 }
index 694898db5bf41c839de3b1dfa010cf888ae519dd..3204efc16155dca80059d9a646e2d61dc090719b 100644 (file)
@@ -311,10 +311,13 @@ bayes_learn_spam (struct rspamd_classifier * ctx,
        gint id;
        struct rspamd_statfile *st;
        rspamd_token_t *tok;
+       gboolean incrementing;
 
        g_assert (ctx != NULL);
        g_assert (tokens != NULL);
 
+       incrementing = ctx->cfg->flags & RSPAMD_FLAG_CLASSIFIER_INCREMENTING_BACKEND;
+
        for (i = 0; i < tokens->len; i++) {
                tok = g_ptr_array_index (tokens, i);
 
@@ -323,24 +326,26 @@ bayes_learn_spam (struct rspamd_classifier * ctx,
                        st = g_ptr_array_index (ctx->ctx->statfiles, id);
                        g_assert (st != NULL);
 
-                       if (is_spam) {
-                               if (st->stcf->is_spam) {
-                                       tok->values[id]++;
+                       if (!!st->stcf->is_spam == !!is_spam) {
+                               if (incrementing) {
+                                       tok->values[id] = 1;
                                }
-                               else if (tok->values[id] > 0 && unlearn) {
-                                       /* Unlearning */
-                                       tok->values[id]--;
+                               else {
+                                       tok->values[id]++;
                                }
                        }
-                       else {
-                               if (!st->stcf->is_spam) {
-                                       tok->values[id]++;
+                       else if (tok->values[id] > 0 && unlearn) {
+                               /* Unlearning */
+                               if (incrementing) {
+                                       tok->values[id] = -1;
                                }
-                               else if (tok->values[id] > 0 && unlearn) {
-                                       /* Unlearning */
+                               else {
                                        tok->values[id]--;
                                }
                        }
+                       else if (incrementing) {
+                               tok->values[id] = 0;
+                       }
                }
        }