You are here

function bayesian_tokens_update in Spam 5.3

Update token probabilities in database.

1 call to bayesian_tokens_update()
bayesian_spamapi in filters/bayesian/bayesian.module
Spam API Hook

File

filters/bayesian/bayesian.module, line 112

Code

function bayesian_tokens_update($class, $tokens, $yes, $type = NULL, $id = 0) {
  if (!is_array($tokens) || empty($tokens)) {
    return;
  }
  foreach ($tokens as $token) {
    $old = db_fetch_object(db_query("SELECT probability, yes_count, no_count FROM {bayesian_tokens} WHERE class = '%s' AND token = '%s'", $class, $token));
    if ($old->probability) {
      $total = $old->yes_count + $old->no_count + 1;
      $probability = spam_sanitize_score(($old->yes_count + ($yes ? 1 : 0)) / $total * 100);
      spam_log(SPAM_DEBUG, 'bayesian_tokens_update', t('update token(@token) class(@class) yes(@yes) no(@no) prob(@prob): added @new', array(
        '@token' => $token,
        '@class' => $class,
        '@yes' => $old->yes_count + ($yes ? 1 : 0),
        '@no' => $old->no_count + ($yes ? 0 : 1),
        '@prob' => $probability,
        '@new' => $yes ? 'yes' : 'no',
      )), $type, $id);
      if ($yes) {
        db_query("UPDATE {bayesian_tokens} SET yes_count = yes_count + 1, probability = %d, last = %d WHERE class = '%s' AND token = '%s'", $probability, time(), $class, $token);
      }
      else {
        db_query("UPDATE {bayesian_tokens} SET no_count = no_count + 1, probability = %d, last = %d WHERE class = '%s' AND token = '%s'", $probability, time(), $class, $token);
      }
    }
    else {
      $probability = $yes ? 99 : 1;
      spam_log(SPAM_DEBUG, 'bayesian_tokens_update', t('insert token(@token) class(@class) probability(@probability)', array(
        '@token' => $token,
        '@class' => $class,
        '@probability' => $probability,
      )), $type, $id);
      db_query("INSERT INTO {bayesian_tokens} (class, token, yes_count, no_count, probability, last) VALUES('%s', '%s', %d, %d, %d, %d)", $class, $token, $yes ? 1 : 0, $yes ? 0 : 1, $probability, time());
    }
  }
}