/* This file is part of Pazpar2.
- Copyright (C) 2006-2009 Index Data
+ Copyright (C) 2006-2012 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
#include <config.h>
#endif
+#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include "relevance.h"
-#include "pazpar2.h"
-
-#define USE_TRIE 0
+#include "session.h"
struct relevance
{
int *doc_frequency_vec;
+ int *term_frequency_vec_tmp;
+ int *term_pos;
int vec_len;
-#if USE_TRIE
- struct word_trie *wt;
-#else
struct word_entry *entries;
- pp2_charset_t pct;
-#endif
+ pp2_charset_token_t prt;
+ int rank_cluster;
+ double follow_factor;
+ double lead_decay;
+ int length_divide;
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];
+struct word_entry {
+ const char *norm_str;
+ const char *display_str;
+ int termno;
+ char *ccl_field;
+ struct word_entry *next;
};
-static struct word_trie *create_word_trie_node(NMEM nmem)
+static struct word_entry *word_entry_match(struct relevance *r,
+ const char *norm_str,
+ const char *rank, int *weight)
{
- struct word_trie *res = nmem_malloc(nmem, sizeof(struct word_trie));
- int i;
- for (i = 0; i < 26; i++)
+ int i = 1;
+ struct word_entry *entries = r->entries;
+ for (; entries; entries = entries->next, i++)
{
- res->list[i].child = 0;
- res->list[i].termno = -1;
+ if (*norm_str && !strcmp(norm_str, entries->norm_str))
+ {
+ const char *cp = 0;
+ int no_read = 0;
+ sscanf(rank, "%d%n", weight, &no_read);
+ rank += no_read;
+ while (*rank == ' ')
+ rank++;
+ if (no_read > 0 && (cp = strchr(rank, ' ')))
+ {
+ if ((cp - rank) == strlen(entries->ccl_field) &&
+ memcmp(entries->ccl_field, rank, cp - rank) == 0)
+ *weight = atoi(cp + 1);
+ }
+ return entries;
+ }
}
- return res;
+ return 0;
}
-static void word_trie_addterm(NMEM nmem, struct word_trie *n, const char *term, int num)
+void relevance_countwords(struct relevance *r, struct record_cluster *cluster,
+ const char *words, const char *rank,
+ const char *name)
{
+ int *w = r->term_frequency_vec_tmp;
+ const char *norm_str;
+ int i, length = 0;
+ double lead_decay = r->lead_decay;
+ struct word_entry *e;
+ WRBUF wr = cluster->relevance_explain1;
+ int printed_about_field = 0;
+
+ pp2_charset_token_first(r->prt, words, 0);
+ for (e = r->entries, i = 1; i < r->vec_len; i++, e = e->next)
+ {
+ w[i] = 0;
+ r->term_pos[i] = 0;
+ }
- while (*term) {
- int c = tolower(*term);
- if (c < 'a' || c > 'z')
- term++;
- else
+ assert(rank);
+ while ((norm_str = pp2_charset_token_next(r->prt)))
+ {
+ int local_weight = 0;
+ e = word_entry_match(r, norm_str, rank, &local_weight);
+ if (e)
{
- c -= 'a';
- if (!*(++term))
- n->list[c].termno = num;
- else
+ int res = e->termno;
+ int j;
+
+ if (!printed_about_field)
{
- if (!n->list[c].child)
+ printed_about_field = 1;
+ wrbuf_printf(wr, "field=%s content=", name);
+ if (strlen(words) > 50)
{
- struct word_trie *new = create_word_trie_node(nmem);
- n->list[c].child = new;
+ wrbuf_xmlputs_n(wr, words, 49);
+ wrbuf_puts(wr, " ...");
}
- word_trie_addterm(nmem, n->list[c].child, term, num);
+ else
+ wrbuf_xmlputs(wr, words);
+ wrbuf_puts(wr, ";\n");
}
- break;
+ assert(res < r->vec_len);
+ w[res] += local_weight / (1 + log2(1 + lead_decay * length));
+ wrbuf_printf(wr, "%s: w[%d] += w(%d) / "
+ "(1+log2(1+lead_decay(%f) * length(%d)));\n",
+ e->display_str, res, local_weight, lead_decay, length);
+ j = res - 1;
+ if (j > 0 && r->term_pos[j])
+ {
+ int d = length + 1 - r->term_pos[j];
+ wrbuf_printf(wr, "%s: w[%d] += w[%d](%d) * follow(%f) / "
+ "(1+log2(d(%d));\n",
+ e->display_str, res, res, w[res],
+ r->follow_factor, d);
+ w[res] += w[res] * r->follow_factor / (1 + log2(d));
+ }
+ for (j = 0; j < r->vec_len; j++)
+ r->term_pos[j] = j < res ? 0 : length + 1;
}
+ length++;
}
-}
-
-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)
+ for (e = r->entries, i = 1; i < r->vec_len; i++, e = e->next)
{
- if (t->list[c].termno > 0)
- return t->list[c].termno;
- else
- return 0;
- }
- else
- {
- if (t->list[c].child)
+ if (length == 0 || w[i] == 0)
+ continue;
+ wrbuf_printf(wr, "%s: tf[%d] += w[%d](%d)", e->display_str, i, i, w[i]);
+ switch (r->length_divide)
{
- return word_trie_match(t->list[c].child, word, skipped);
+ case 0:
+ cluster->term_frequency_vecf[i] += (double) w[i];
+ break;
+ case 1:
+ wrbuf_printf(wr, " / log2(1+length(%d))", length);
+ cluster->term_frequency_vecf[i] +=
+ (double) w[i] / log2(1 + length);
+ break;
+ case 2:
+ wrbuf_printf(wr, " / length(%d)", length);
+ cluster->term_frequency_vecf[i] += (double) w[i] / length;
}
- else
- return 0;
+ cluster->term_frequency_vec[i] += w[i];
+ wrbuf_printf(wr, " (%f);\n", cluster->term_frequency_vecf[i]);
}
+ cluster->term_frequency_vec[0] += length;
}
-
-static struct word_trie *build_word_trie(NMEM nmem, const char **terms)
+static void pull_terms(struct relevance *res, struct ccl_rpn_node *n)
{
- struct word_trie *res = create_word_trie_node(nmem);
- const char **p;
+ char **words;
+ int numwords;
+ char *ccl_field;
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)
+ switch (n->kind)
{
- 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
+ case CCL_RPN_AND:
+ case CCL_RPN_OR:
+ case CCL_RPN_NOT:
+ case CCL_RPN_PROX:
+ pull_terms(res, n->u.p[0]);
+ pull_terms(res, n->u.p[1]);
+ break;
+ case CCL_RPN_TERM:
+ nmem_strsplit(res->nmem, " ", n->u.t.term, &words, &numwords);
+ for (i = 0; i < numwords; i++)
{
- while (*words && (c = raw_char(tolower(*words))) >= 0)
- words++;
+ const char *norm_str;
+
+ ccl_field = nmem_strdup_null(res->nmem, n->u.t.qual);
+
+ pp2_charset_token_first(res->prt, words[i], 0);
+ while ((norm_str = pp2_charset_token_next(res->prt)))
+ {
+ struct word_entry **e = &res->entries;
+ while (*e)
+ e = &(*e)->next;
+ *e = nmem_malloc(res->nmem, sizeof(**e));
+ (*e)->norm_str = nmem_strdup(res->nmem, norm_str);
+ (*e)->ccl_field = ccl_field;
+ (*e)->termno = res->vec_len++;
+ (*e)->display_str = nmem_strdup(res->nmem, words[i]);
+ (*e)->next = 0;
+ }
}
- cluster->term_frequency_vec[0]++;
+ break;
+ default:
+ break;
}
}
-#else
-
-struct word_entry {
- const char *norm_str;
- int termno;
- struct word_entry *next;
-};
-
-static void add_word_entry(NMEM nmem,
- struct word_entry **entries,
- const char *norm_str,
- int term_no)
+struct relevance *relevance_create_ccl(pp2_charset_fact_t pft,
+ struct ccl_rpn_node *query,
+ int rank_cluster,
+ double follow_factor, double lead_decay,
+ int length_divide)
{
- struct word_entry *ne = nmem_malloc(nmem, sizeof(*ne));
- ne->norm_str = nmem_strdup(nmem, norm_str);
- ne->termno = term_no;
-
- ne->next = *entries;
- *entries = ne;
-}
-
+ NMEM nmem = nmem_create();
+ struct relevance *res = nmem_malloc(nmem, sizeof(*res));
+ int i;
-int word_entry_match(struct word_entry *entries, const char *norm_str)
-{
- for (; entries; entries = entries->next)
- {
- if (!strcmp(norm_str, entries->norm_str))
- return entries->termno;
- }
- return 0;
-}
+ res->nmem = nmem;
+ res->entries = 0;
+ res->vec_len = 1;
+ res->rank_cluster = rank_cluster;
+ res->follow_factor = follow_factor;
+ res->lead_decay = lead_decay;
+ res->length_divide = length_divide;
+ res->prt = pp2_charset_token_create(pft, "relevance");
-static struct word_entry *build_word_entries(pp2_charset_t pct, NMEM nmem,
- const char **terms)
-{
- int termno = 1; /* >0 signals THERE is an entry */
- struct word_entry *entries = 0;
- const char **p = terms;
+ pull_terms(res, query);
- for (; *p; p++)
- {
- pp2_relevance_token_t prt = pp2_relevance_tokenize(pct, *p);
- const char *norm_str;
+ res->doc_frequency_vec = nmem_malloc(nmem, res->vec_len * sizeof(int));
+ for (i = 0; i < res->vec_len; i++)
+ res->doc_frequency_vec[i] = 0;
- while ((norm_str = pp2_relevance_token_next(prt)))
- add_word_entry(nmem, &entries, norm_str, termno);
+ // worker array
+ res->term_frequency_vec_tmp =
+ nmem_malloc(res->nmem,
+ res->vec_len * sizeof(*res->term_frequency_vec_tmp));
- pp2_relevance_token_destroy(prt);
+ res->term_pos =
+ nmem_malloc(res->nmem, res->vec_len * sizeof(*res->term_pos));
- termno++;
- }
- return entries;
+ return res;
}
-void relevance_countwords(struct relevance *r, struct record_cluster *cluster,
- const char *words, int multiplier)
+void relevance_destroy(struct relevance **rp)
{
- pp2_relevance_token_t prt = pp2_relevance_tokenize(r->pct, words);
-
- const char *norm_str;
-
- while ((norm_str = pp2_relevance_token_next(prt)))
+ if (*rp)
{
- int res = word_entry_match(r->entries, norm_str);
- if (res)
- cluster->term_frequency_vec[res] += multiplier;
- cluster->term_frequency_vec[0]++;
+ pp2_charset_token_destroy((*rp)->prt);
+ nmem_destroy((*rp)->nmem);
+ *rp = 0;
}
- pp2_relevance_token_destroy(prt);
-}
-
-#endif
-
-
-
-struct relevance *relevance_create(pp2_charset_t pct,
- NMEM nmem, const char **terms, int numrecs)
-{
- struct relevance *res = nmem_malloc(nmem, sizeof(struct relevance));
- const char **p;
- int i;
-
- for (p = terms, i = 0; *p; p++, i++)
- ;
- res->vec_len = ++i;
- 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;
}
void relevance_newrec(struct relevance *r, struct record_cluster *rec)
{
if (!rec->term_frequency_vec)
{
- rec->term_frequency_vec = nmem_malloc(r->nmem, r->vec_len * sizeof(int));
- memset(rec->term_frequency_vec, 0, r->vec_len * sizeof(int));
+ int i;
+
+ // term frequency [1,..] . [0] is total length of all fields
+ rec->term_frequency_vec =
+ nmem_malloc(r->nmem,
+ r->vec_len * sizeof(*rec->term_frequency_vec));
+ for (i = 0; i < r->vec_len; i++)
+ rec->term_frequency_vec[i] = 0;
+
+ // term frequency divided by length of field [1,...]
+ rec->term_frequency_vecf =
+ nmem_malloc(r->nmem,
+ r->vec_len * sizeof(*rec->term_frequency_vecf));
+ for (i = 0; i < r->vec_len; i++)
+ rec->term_frequency_vecf[i] = 0.0;
}
}
-
void relevance_donerecord(struct relevance *r, struct record_cluster *cluster)
{
int i;
int i;
float *idfvec = xmalloc(rel->vec_len * sizeof(float));
+ reclist_enter(reclist);
// Calculate document frequency vector for each term.
for (i = 1; i < rel->vec_len; i++)
{
idfvec[i] = 0;
else
{
- // This conditional may be terribly wrong
- // It was there to address the situation where vec[0] == vec[i]
- // which leads to idfvec[i] == 0... not sure about this
- // Traditional TF-IDF may assume that a word that occurs in every
- // record is irrelevant, but this is actually something we will
- // see a lot
- if ((idfvec[i] = log((float) rel->doc_frequency_vec[0] /
- rel->doc_frequency_vec[i])) < 0.0000001)
- idfvec[i] = 1;
+ /* add one to nominator idf(t,D) to ensure a value > 0 */
+ idfvec[i] = log((float) (1 + rel->doc_frequency_vec[0]) /
+ rel->doc_frequency_vec[i]);
}
}
// 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;
- for (t = 1; t < rel->vec_len; t++)
+ int relevance = 0;
+ WRBUF w;
+ struct word_entry *e = rel->entries;
+ struct record_cluster *rec = reclist_read_record(reclist);
+ if (!rec)
+ break;
+ w = rec->relevance_explain2;
+ wrbuf_rewind(w);
+ wrbuf_puts(w, "relevance = 0;\n");
+ for (i = 1; i < rel->vec_len; i++)
+ {
+ float termfreq = (float) rec->term_frequency_vecf[i];
+ int add = 100000 * termfreq * idfvec[i];
+
+ wrbuf_printf(w, "idf[%d] = log(((1 + total(%d))/termoccur(%d));\n",
+ i, rel->doc_frequency_vec[0],
+ rel->doc_frequency_vec[i]);
+ wrbuf_printf(w, "%s: relevance += 100000 * tf[%d](%f) * "
+ "idf[%d](%f) (%d);\n",
+ e->display_str, i, termfreq, i, idfvec[i], add);
+ relevance += add;
+ e = e->next;
+ }
+ if (!rel->rank_cluster)
+ {
+ struct record *record;
+ int cluster_size = 0;
+
+ for (record = rec->records; record; record = record->next)
+ cluster_size++;
+
+ wrbuf_printf(w, "score = relevance(%d)/cluster_size(%d);\n",
+ relevance, cluster_size);
+ relevance /= cluster_size;
+ }
+ else
{
- 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];
+ wrbuf_printf(w, "score = relevance(%d);\n", relevance);
}
- rec->relevance = (int) (relevance * 100000);
+ rec->relevance_score = relevance;
}
- reclist->pointer = 0;
+ reclist_leave(reclist);
xfree(idfvec);
}