Index: /var/www/similarity/sites/default/modules/similarity/classes/similarity_taxonomy.inc
index c7f9778..74ba7ce 100644
--- a/classes/similarity_taxonomy.inc
+++ b/classes/similarity_taxonomy.inc
@@ -30,14 +30,6 @@ class similarity_taxonomy extends similarity_node {
       }
     }
     $type_string = "(" . $type_string . ")";
-    $db_nids = db_query("SELECT n.nid, n.vid FROM {node} n WHERE " . $type_string, $this->types);
-    while($nid_obj = db_fetch_object($db_nids)) {
-      $terms = db_query("SELECT d.name FROM {term_data} d JOIN {term_node} tn ON tn.tid = d.tid WHERE tn.nid = %d", $nid_obj->nid);
-      while($term_data_row = db_fetch_object($terms)) {
-        // TODO: can we use term_data->weight?
-        $possible_nids[$nid_obj->nid][strtolower($term_data_row->name)] = 1;
-      }
-    }
     
     // set limits
     $last = variable_get($this->machine_name . '_cron_last', 0);
@@ -49,24 +41,27 @@ class similarity_taxonomy extends similarity_node {
     $result = db_query_range('SELECT n.nid, n.changed FROM {node} n WHERE n.changed > %d AND ' . $type_string . ' ORDER BY changed ASC', $args, 0, $limit);
 
     $last_changed = 0;
-   
+    
+    // The SQL
+    $cooccurance_sql = "SELECT t2.nid, COUNT(t2.tid) as count FROM {term_node} t1 JOIN {term_node} t2 ON t2.tid = t1.tid AND t2.nid <> t1.nid WHERE t1.nid = %d GROUP BY t2.nid";
+    $total_sql = "SELECT COUNT(tid) FROM {term_node} WHERE nid = %d";
     while ($changed_obj = db_fetch_object($result)) {
-      $current_terms = $possible_nids[$changed_obj->nid];
-      $last_changed = $changed_obj->changed;
+      $other_result = db_query($cooccurance_sql, $changed_obj->nid);
       
-      // process the terms
-      if (!empty($current_terms)) {
-        foreach($possible_nids as $other_nid => $other_terms) {
-          if ($other_nid != $changed_obj->nid && !empty($other_terms)) {
-            $this->save_similarity($changed_obj->nid, $other_nid, similarity_cosine($current_terms, $other_terms));
-          }
+      // TODO: Cache this bad boy so we only query for it once
+      $target_count = db_result(db_query($total_sql, $changed_obj->nid));
+      while ($term_row = db_fetch_object($other_result)) {
+        if ($term_row->count) {
+          $other_count = db_result(db_query($total_sql, $term_row->nid));
+          $denom = sqrt($target_count * $other_count);
+          $this->save_similarity($changed_obj->nid, $term_row->nid, ($term_row->count / $denom));
         }
       }
-      // then insert
-      $this->write_similarities();
-      if ($last_changed > 0 ) {
-        variable_set($this->machine_name . '_cron_last', $last_changed);
-      }
+    }
+    // then insert
+    $this->write_similarities();
+    if ($last_changed > 0 ) {
+      variable_set($this->machine_name . '_cron_last', $last_changed);
     }
   }
 }
\ No newline at end of file
