1 /* $Id: relevance.c,v 1.12 2007-05-10 09:26:19 adam Exp $
2 Copyright (c) 2006-2007, Index Data.
4 This file is part of Pazpar2.
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
30 #include "relevance.h"
37 int *doc_frequency_vec;
42 struct word_entry *entries;
47 #define raw_char(c) (((c) >= 'a' && (c) <= 'z') ? (c) - 'a' : -1)
51 // We use this data structure to recognize terms in input records,
52 // and map them to record term vectors for counting.
57 struct word_trie *child;
62 static struct word_trie *create_word_trie_node(NMEM nmem)
64 struct word_trie *res = nmem_malloc(nmem, sizeof(struct word_trie));
66 for (i = 0; i < 26; i++)
68 res->list[i].child = 0;
69 res->list[i].termno = -1;
74 static void word_trie_addterm(NMEM nmem, struct word_trie *n, const char *term, int num)
78 int c = tolower(*term);
79 if (c < 'a' || c > 'z')
85 n->list[c].termno = num;
88 if (!n->list[c].child)
90 struct word_trie *new = create_word_trie_node(nmem);
91 n->list[c].child = new;
93 word_trie_addterm(nmem, n->list[c].child, term, num);
100 static int word_trie_match(struct word_trie *t, const char *word, int *skipped)
102 int c = raw_char(tolower(*word));
109 if (!*word || raw_char(*word) < 0)
111 if (t->list[c].termno > 0)
112 return t->list[c].termno;
118 if (t->list[c].child)
120 return word_trie_match(t->list[c].child, word, skipped);
129 static struct word_trie *build_word_trie(NMEM nmem, const char **terms)
131 struct word_trie *res = create_word_trie_node(nmem);
135 for (i = 1, p = terms; *p; p++, i++)
136 word_trie_addterm(nmem, res, *p, i);
143 const char *norm_str;
145 struct word_entry *next;
148 static void add_word_entry(NMEM nmem,
149 struct word_entry **entries,
150 const char *norm_str,
153 struct word_entry *ne = nmem_malloc(nmem, sizeof(*ne));
154 ne->norm_str = nmem_strdup(nmem, norm_str);
155 ne->termno = term_no;
162 int word_entry_match(struct word_entry *entries, const char *norm_str)
164 for (; entries; entries = entries->next)
166 if (!strcmp(norm_str, entries->norm_str))
167 return entries->termno;
172 static struct word_entry *build_word_entries(NMEM nmem,
175 int termno = 1; /* >0 signals THERE is an entry */
176 struct word_entry *entries = 0;
177 const char **p = terms;
178 WRBUF norm_str = wrbuf_alloc();
185 int c = raw_char(*cp);
187 wrbuf_putc(norm_str, c);
190 if (wrbuf_len(norm_str))
191 add_word_entry(nmem, &entries, wrbuf_cstr(norm_str),
193 wrbuf_rewind(norm_str);
196 if (wrbuf_len(norm_str))
197 add_word_entry(nmem, &entries, wrbuf_cstr(norm_str), termno);
198 wrbuf_rewind(norm_str);
201 wrbuf_destroy(norm_str);
211 struct relevance *relevance_create(NMEM nmem, const char **terms, int numrecs)
213 struct relevance *res = nmem_malloc(nmem, sizeof(struct relevance));
217 for (p = terms, i = 0; *p; p++, i++)
220 res->doc_frequency_vec = nmem_malloc(nmem, res->vec_len * sizeof(int));
221 memset(res->doc_frequency_vec, 0, res->vec_len * sizeof(int));
224 res->wt = build_word_trie(nmem, terms);
226 res->entries = build_word_entries(nmem, terms);
231 void relevance_newrec(struct relevance *r, struct record_cluster *rec)
233 if (!rec->term_frequency_vec)
235 rec->term_frequency_vec = nmem_malloc(r->nmem, r->vec_len * sizeof(int));
236 memset(rec->term_frequency_vec, 0, r->vec_len * sizeof(int));
241 // FIXME. The definition of a word is crude here.. should support
242 // some form of localization mechanism?
243 void relevance_countwords(struct relevance *r, struct record_cluster *cluster,
244 const char *words, int multiplier)
247 WRBUF norm_str = wrbuf_alloc();
256 while (*words && (c = raw_char(tolower(*words))) < 0)
261 res = word_trie_match(r->wt, words, &skipped);
265 cluster->term_frequency_vec[res] += multiplier;
269 while (*words && (c = raw_char(tolower(*words))) >= 0)
273 while (*words && (c = raw_char(tolower(*words))) >= 0)
275 wrbuf_putc(norm_str, c);
278 res = word_entry_match(r->entries, wrbuf_cstr(norm_str));
280 cluster->term_frequency_vec[res] += multiplier;
281 wrbuf_rewind(norm_str);
283 cluster->term_frequency_vec[0]++;
286 wrbuf_destroy(norm_str);
290 void relevance_donerecord(struct relevance *r, struct record_cluster *cluster)
294 for (i = 1; i < r->vec_len; i++)
295 if (cluster->term_frequency_vec[i] > 0)
296 r->doc_frequency_vec[i]++;
298 r->doc_frequency_vec[0]++;
303 static int comp(const void *p1, const void *p2)
306 struct record **r1 = (struct record **) p1;
307 struct record **r2 = (struct record **) p2;
308 res = (*r2)->relevance - (*r1)->relevance;
317 static int comp(const void *p1, const void *p2)
319 struct record_cluster **r1 = (struct record_cluster **) p1;
320 struct record_cluster **r2 = (struct record_cluster **) p2;
321 return (*r2)->relevance - (*r1)->relevance;
326 // Prepare for a relevance-sorted read
327 void relevance_prepare_read(struct relevance *rel, struct reclist *reclist)
330 float *idfvec = xmalloc(rel->vec_len * sizeof(float));
332 // Calculate document frequency vector for each term.
333 for (i = 1; i < rel->vec_len; i++)
335 if (!rel->doc_frequency_vec[i])
339 // This conditional may be terribly wrong
340 // It was there to address the situation where vec[0] == vec[i]
341 // which leads to idfvec[i] == 0... not sure about this
342 // Traditional TF-IDF may assume that a word that occurs in every
343 // record is irrelevant, but this is actually something we will
345 if ((idfvec[i] = log((float) rel->doc_frequency_vec[0] /
346 rel->doc_frequency_vec[i])) < 0.0000001)
350 // Calculate relevance for each document
351 for (i = 0; i < reclist->num_records; i++)
354 struct record_cluster *rec = reclist->flatlist[i];
357 for (t = 1; t < rel->vec_len; t++)
360 if (!rec->term_frequency_vec[0])
362 termfreq = (float) rec->term_frequency_vec[t] / rec->term_frequency_vec[0];
363 relevance += termfreq * idfvec[t];
365 rec->relevance = (int) (relevance * 100000);
368 qsort(reclist->flatlist, reclist->num_records, sizeof(struct record*), comp);
370 reclist->pointer = 0;
377 * indent-tabs-mode: nil
379 * vim: shiftwidth=4 tabstop=8 expandtab