#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;
pp2_relevance_token_destroy(prt);
}
-#endif
-
-
-
struct relevance *relevance_create(pp2_charset_t pct,
NMEM nmem, const char **terms)
{
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;
}