--- spam.module.orig	2005-01-25 21:46:14.000000000 +0000
+++ spam.module	2005-01-25 21:43:40.000000000 +0000
@@ -1097,40 +1097,37 @@
 
 /**
  * Test array of words against known words, determine probability is spam
- *  
+ *
  * @param $tokens An array of tokens
  * @return An int from 1 to 99 which is the probability that $tokens are spam
  */
 // test whether or not current content in form of token array is spam
 function _spam_rating($tokens = array(), $weight = 0) {
-  // build token array, use drift as index
-  foreach ($tokens as $token) {
-    $p = db_fetch_object(db_query("SELECT probability FROM {spam_tokens} WHERE token = '%s'", $token));
-    if (!$p->probability) {
-      $p->probability = variable_get('spam_unknown_probability', 40);
-    }
-    // get drift from median of 50
-    $t["$token,$p->probability"] = abs($p->probability - 50);
-  }
-  // sort so largest drift is first
-  asort($t);
-  $keys = array_keys($t);
-  $max = variable_get('spam_interesting_tokens', 15);
-  $total = 0;
-  // grab n tokens with largest drift
-  for ($i = 0; $i < $max; $i++) {
-    if ($pair = array_shift($keys)) {
-      $p = explode(',',$pair);
-      // add up combined probabilities
-      $total = $total + $p[1];
+
+  // $drift - minimal drift from the median - a soft-ish shoulder of the filter
+  // $max - maximum number of tokens to evaluate, a hard shoulder of the filter
+  $probs = array();
+  $drift = variable_get('spam_min_drift', 40);
+  //since the tokens are already filtered by drift, it may prove useful to
+  //get a few more anyway - this is more a memory saving and speed improvement
+  //measure than precision. It is somewhat akin to brain surgery, but fast.
+  $max = variable_get('spam_interesting_tokens', 40);
+  $num=0;
+
+  //Speed increase depends on asort() and the number of interesting tokens considered
+  //vs the added comparison and evaluation in SQL
+  foreach($tokens as $token){
+    $result = db_query("SELECT probability FROM {spam_tokens} WHERE token='%s' AND (ABS(50 - probablility) >= %d) AND last >= %d SORT BY ABS(50 - probability)", $token,$drift,$drift);
+    if($p->probability){
+      $probs += $p->probability;
+      $num++;
     }
-    else {
-      // no more tokens
+    if($num>$max) {
       break;
     }
   }
 
-  $rating = ($total + $weight) / $i;
+  $rating = ($probs + $weight) / $num;
   if ($rating > 99)
     $rating = 99;
   else if ($rating < 1)
