Remove dead code for relevancy (trie stuff)
[pazpar2-moved-to-github.git] / src / relevance.c
index a30892f..7603a7a 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Pazpar2.
-   Copyright (C) 2006-2008 Index Data
+   Copyright (C) 2006-2009 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
@@ -17,156 +17,25 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 */
 
-#include <ctype.h>
-#include <math.h>
-#include <stdlib.h>
-
 #if HAVE_CONFIG_H
-#include <cconfig.h>
+#include <config.h>
 #endif
 
+#include <math.h>
+#include <stdlib.h>
+
 #include "relevance.h"
 #include "pazpar2.h"
 
-#define USE_TRIE 0
-
 struct relevance
 {
     int *doc_frequency_vec;
     int vec_len;
-#if USE_TRIE
-    struct word_trie *wt;
-#else
     struct word_entry *entries;
     pp2_charset_t pct;
-#endif
     NMEM nmem;
 };
 
-#if USE_TRIE
-#define raw_char(c) (((c) >= 'a' && (c) <= 'z') ? (c) - 'a' : -1)
-
-
-// We use this data structure to recognize terms in input records,
-// and map them to record term vectors for counting.
-struct word_trie
-{
-    struct
-    {
-        struct word_trie *child;
-        int termno;
-    } list[26];
-};
-
-static struct word_trie *create_word_trie_node(NMEM nmem)
-{
-    struct word_trie *res = nmem_malloc(nmem, sizeof(struct word_trie));
-    int i;
-    for (i = 0; i < 26; i++)
-    {
-        res->list[i].child = 0;
-        res->list[i].termno = -1;
-    }
-    return res;
-}
-
-static void word_trie_addterm(NMEM nmem, struct word_trie *n, const char *term, int num)
-{
-
-    while (*term) {
-        int c = tolower(*term);
-        if (c < 'a' || c > 'z')
-            term++;
-        else
-        {
-            c -= 'a';
-            if (!*(++term))
-                n->list[c].termno = num;
-            else
-            {
-                if (!n->list[c].child)
-                {
-                    struct word_trie *new = create_word_trie_node(nmem);
-                    n->list[c].child = new;
-                }
-                word_trie_addterm(nmem, n->list[c].child, term, num);
-            }
-            break;
-        }
-    }
-}
-
-static int word_trie_match(struct word_trie *t, const char *word, int *skipped)
-{
-    int c = raw_char(tolower(*word));
-
-    if (!*word)
-        return 0;
-
-    word++;
-    (*skipped)++;
-    if (!*word || raw_char(*word) < 0)
-    {
-        if (t->list[c].termno > 0)
-            return t->list[c].termno;
-        else
-            return 0;
-    }
-    else
-    {
-        if (t->list[c].child)
-        {
-            return word_trie_match(t->list[c].child, word, skipped);
-        }
-        else
-            return 0;
-    }
-
-}
-
-
-static struct word_trie *build_word_trie(NMEM nmem, const char **terms)
-{
-    struct word_trie *res = create_word_trie_node(nmem);
-    const char **p;
-    int i;
-
-    for (i = 1, p = terms; *p; p++, i++)
-        word_trie_addterm(nmem, res, *p, i);
-    return res;
-}
-
-
-// FIXME. The definition of a word is crude here.. should support
-// some form of localization mechanism?
-void relevance_countwords(struct relevance *r, struct record_cluster *cluster,
-                          const char *words, int multiplier)
-{
-    while (*words)
-    {
-        char c;
-        int res;
-        int skipped = 0;
-        while (*words && (c = raw_char(tolower(*words))) < 0)
-            words++;
-        if (!*words)
-            break;
-        res = word_trie_match(r->wt, words, &skipped);
-        if (res)
-        {
-            words += skipped;
-            cluster->term_frequency_vec[res] += multiplier;
-        }
-        else
-        {
-            while (*words && (c = raw_char(tolower(*words))) >= 0)
-                words++;
-        }
-        cluster->term_frequency_vec[0]++;
-    }
-}
-
-#else
 
 struct word_entry {
     const char *norm_str;
@@ -237,12 +106,8 @@ void relevance_countwords(struct relevance *r, struct record_cluster *cluster,
     pp2_relevance_token_destroy(prt);
 }
 
-#endif
-
-
-
 struct relevance *relevance_create(pp2_charset_t pct,
-                                   NMEM nmem, const char **terms, int numrecs)
+                                   NMEM nmem, const char **terms)
 {
     struct relevance *res = nmem_malloc(nmem, sizeof(struct relevance));
     const char **p;
@@ -254,12 +119,8 @@ struct relevance *relevance_create(pp2_charset_t pct,
     res->doc_frequency_vec = nmem_malloc(nmem, res->vec_len * sizeof(int));
     memset(res->doc_frequency_vec, 0, res->vec_len * sizeof(int));
     res->nmem = nmem;
-#if USE_TRIE
-    res->wt = build_word_trie(nmem, terms);
-#else
     res->entries = build_word_entries(pct, nmem, terms);
     res->pct = pct;
-#endif
     return res;
 }
 
@@ -290,6 +151,7 @@ void relevance_prepare_read(struct relevance *rel, struct reclist *reclist)
     int i;
     float *idfvec = xmalloc(rel->vec_len * sizeof(float));
 
+    reclist_rewind(reclist);
     // Calculate document frequency vector for each term.
     for (i = 1; i < rel->vec_len; i++)
     {
@@ -309,30 +171,34 @@ void relevance_prepare_read(struct relevance *rel, struct reclist *reclist)
         }
     }
     // Calculate relevance for each document
-    for (i = 0; i < reclist->num_records; i++)
+
+    while (1)
     {
         int t;
-        struct record_cluster *rec = reclist->flatlist[i];
-        float relevance;
-        relevance = 0;
+        int relevance = 0;
+        struct record_cluster *rec = reclist_read_record(reclist);
+        if (!rec)
+            break;
         for (t = 1; t < rel->vec_len; t++)
         {
             float termfreq;
             if (!rec->term_frequency_vec[0])
                 break;
             termfreq = (float) rec->term_frequency_vec[t] / rec->term_frequency_vec[0];
-            relevance += termfreq * idfvec[t];
+            relevance += 100000 * (termfreq * idfvec[t] + 0.0000005);  
         }
-        rec->relevance = (int) (relevance * 100000);
+        rec->relevance = relevance;
     }
-    reclist->pointer = 0;
+    reclist_rewind(reclist);
     xfree(idfvec);
 }
 
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab
  */
+