/* This file is part of Pazpar2.
- Copyright (C) 2006-2010 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 <stdlib.h>
#include "relevance.h"
-#include "pazpar2.h"
+#include "session.h"
struct relevance
{
int *doc_frequency_vec;
int vec_len;
struct word_entry *entries;
- pp2_charset_t pct;
+ pp2_charset_token_t prt;
+ int rank_cluster;
NMEM nmem;
};
-
struct word_entry {
const char *norm_str;
int termno;
+ char *ccl_field;
struct word_entry *next;
};
-static void add_word_entry(NMEM nmem,
- struct word_entry **entries,
- const char *norm_str,
- int term_no)
-{
- 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;
-}
-
-
-int word_entry_match(struct word_entry *entries, const char *norm_str)
+static int word_entry_match(struct word_entry *entries, const char *norm_str,
+ const char *rank, int *mult)
{
for (; entries; entries = entries->next)
{
- if (!strcmp(norm_str, entries->norm_str))
+ if (*norm_str && !strcmp(norm_str, entries->norm_str))
+ {
+ const char *cp = 0;
+ int no_read = 0;
+ sscanf(rank, "%d%n", mult, &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)
+ *mult = atoi(cp + 1);
+ }
return entries->termno;
+ }
}
return 0;
}
-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;
-
- for (; *p; p++)
- {
- pp2_relevance_token_t prt = pp2_relevance_tokenize(pct, *p, 0);
- const char *norm_str;
-
- while ((norm_str = pp2_relevance_token_next(prt)))
- add_word_entry(nmem, &entries, norm_str, termno);
-
- pp2_relevance_token_destroy(prt);
-
- termno++;
- }
- return entries;
-}
-
void relevance_countwords(struct relevance *r, struct record_cluster *cluster,
- const char *words, int multiplier, const char *name)
+ const char *words, const char *rank,
+ const char *name)
{
- 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;
-
+ pp2_charset_token_first(r->prt, words, 0);
for (i = 1; i < r->vec_len; i++)
mult[i] = 0;
- while ((norm_str = pp2_relevance_token_next(prt)))
+ assert(rank);
+ while ((norm_str = pp2_charset_token_next(r->prt)))
{
- int res = word_entry_match(r->entries, norm_str);
+ int local_mult = 0;
+ int res = word_entry_match(r->entries, norm_str, rank, &local_mult);
if (res)
{
assert(res < r->vec_len);
- mult[res] += multiplier;
+ mult[res] += local_mult;
}
length++;
}
}
cluster->term_frequency_vec[0] += length;
- pp2_relevance_token_destroy(prt);
}
-struct relevance *relevance_create(pp2_charset_t pct,
- NMEM nmem, const char **terms)
+static void pull_terms(struct relevance *res, struct ccl_rpn_node *n)
{
- struct relevance *res = nmem_malloc(nmem, sizeof(struct relevance));
- const char **p;
+ char **words;
+ int numwords;
+ char *ccl_field;
+ int i;
+
+ switch (n->kind)
+ {
+ 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++)
+ {
+ 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)->next = 0;
+ }
+ }
+ break;
+ default:
+ break;
+ }
+}
+
+struct relevance *relevance_create_ccl(pp2_charset_fact_t pft,
+ struct ccl_rpn_node *query,
+ int rank_cluster)
+{
+ NMEM nmem = nmem_create();
+ struct relevance *res = nmem_malloc(nmem, sizeof(*res));
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;
- res->entries = build_word_entries(pct, nmem, terms);
- res->pct = pct;
+ res->entries = 0;
+ res->vec_len = 1;
+ res->rank_cluster = rank_cluster;
+ res->prt = pp2_charset_token_create(pft, "relevance");
+
+ pull_terms(res, query);
+
+ 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;
return res;
}
+void relevance_destroy(struct relevance **rp)
+{
+ if (*rp)
+ {
+ pp2_charset_token_destroy((*rp)->prt);
+ nmem_destroy((*rp)->nmem);
+ *rp = 0;
+ }
+}
+
void relevance_newrec(struct relevance *r, struct record_cluster *rec)
{
if (!rec->term_frequency_vec)
}
}
-
void relevance_donerecord(struct relevance *r, struct record_cluster *cluster)
{
int i;
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++)
{
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
-
while (1)
{
int t;
break;
for (t = 1; t < rel->vec_len; t++)
{
- float termfreq;
-#if 1
- termfreq = (float) rec->term_frequency_vecf[t];
-#else
- if (rec->term_frequency_vec[0])
- {
- termfreq = (float)
- rec->term_frequency_vec[t] / rec->term_frequency_vec[0] ;
- }
- else
- termfreq = 0.0;
-#endif
- relevance += 100000 * (termfreq * idfvec[t] + 0.0000005);
+ float termfreq = (float) rec->term_frequency_vecf[t];
+ relevance += 100000 * termfreq * idfvec[t];
+ }
+ if (!rel->rank_cluster)
+ {
+ struct record *record;
+ int cluster_size = 0;
+
+ for (record = rec->records; record; record = record->next)
+ cluster_size++;
+
+ relevance /= cluster_size;
}
- rec->relevance = relevance;
+ rec->relevance_score = relevance;
}
- reclist_rewind(reclist);
+ reclist_leave(reclist);
xfree(idfvec);
}