Fix connection reuse
[pazpar2-moved-to-github.git] / src / relevance.c
index 072a894..c6b7829 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Pazpar2.
-   Copyright (C) 2006-2009 Index Data
+   Copyright (C) 2006-2010 Index Data
 
 Pazpar2 is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
@@ -26,7 +26,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <stdlib.h>
 
 #include "relevance.h"
-#include "pazpar2.h"
+#include "session.h"
 
 struct relevance
 {
@@ -77,7 +77,7 @@ static struct word_entry *build_word_entries(pp2_charset_t pct, NMEM nmem,
 
     for (; *p; p++)
     {
-        pp2_relevance_token_t prt = pp2_relevance_tokenize(pct, *p);
+        pp2_relevance_token_t prt = pp2_relevance_tokenize(pct, *p, 0);
         const char *norm_str;
 
         while ((norm_str = pp2_relevance_token_next(prt)))
@@ -93,7 +93,7 @@ static struct word_entry *build_word_entries(pp2_charset_t pct, NMEM nmem,
 void relevance_countwords(struct relevance *r, struct record_cluster *cluster,
                           const char *words, int multiplier, const char *name)
 {
-    pp2_relevance_token_t prt = pp2_relevance_tokenize(r->pct, words);
+    pp2_relevance_token_t prt = pp2_relevance_tokenize(r->pct, words, 0);
     int *mult = cluster->term_frequency_vec_tmp;
     const char *norm_str;
     int i, length = 0;
@@ -114,7 +114,8 @@ void relevance_countwords(struct relevance *r, struct record_cluster *cluster,
 
     for (i = 1; i < r->vec_len; i++)
     {
-        cluster->term_frequency_vecf[i] += (double) mult[i] / length;
+        if (length > 0) /* only add if non-empty */
+            cluster->term_frequency_vecf[i] += (double) mult[i] / length;
         cluster->term_frequency_vec[i] += mult[i];
     }
 
@@ -185,7 +186,7 @@ void relevance_prepare_read(struct relevance *rel, struct reclist *reclist)
     int i;
     float *idfvec = xmalloc(rel->vec_len * sizeof(float));
 
-    reclist_rewind(reclist);
+    reclist_enter(reclist);
     // Calculate document frequency vector for each term.
     for (i = 1; i < rel->vec_len; i++)
     {
@@ -229,9 +230,9 @@ void relevance_prepare_read(struct relevance *rel, struct reclist *reclist)
 #endif
             relevance += 100000 * (termfreq * idfvec[t] + 0.0000005);  
         }
-        rec->relevance = relevance;
+        rec->relevance_score = relevance;
     }
-    reclist_rewind(reclist);
+    reclist_leave(reclist);
     xfree(idfvec);
 }