1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2008 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "relevance.h"
35 int *doc_frequency_vec;
40 struct word_entry *entries;
47 #define raw_char(c) (((c) >= 'a' && (c) <= 'z') ? (c) - 'a' : -1)
50 // We use this data structure to recognize terms in input records,
51 // and map them to record term vectors for counting.
56 struct word_trie *child;
61 static struct word_trie *create_word_trie_node(NMEM nmem)
63 struct word_trie *res = nmem_malloc(nmem, sizeof(struct word_trie));
65 for (i = 0; i < 26; i++)
67 res->list[i].child = 0;
68 res->list[i].termno = -1;
73 static void word_trie_addterm(NMEM nmem, struct word_trie *n, const char *term, int num)
77 int c = tolower(*term);
78 if (c < 'a' || c > 'z')
84 n->list[c].termno = num;
87 if (!n->list[c].child)
89 struct word_trie *new = create_word_trie_node(nmem);
90 n->list[c].child = new;
92 word_trie_addterm(nmem, n->list[c].child, term, num);
99 static int word_trie_match(struct word_trie *t, const char *word, int *skipped)
101 int c = raw_char(tolower(*word));
108 if (!*word || raw_char(*word) < 0)
110 if (t->list[c].termno > 0)
111 return t->list[c].termno;
117 if (t->list[c].child)
119 return word_trie_match(t->list[c].child, word, skipped);
128 static struct word_trie *build_word_trie(NMEM nmem, const char **terms)
130 struct word_trie *res = create_word_trie_node(nmem);
134 for (i = 1, p = terms; *p; p++, i++)
135 word_trie_addterm(nmem, res, *p, i);
140 // FIXME. The definition of a word is crude here.. should support
141 // some form of localization mechanism?
142 void relevance_countwords(struct relevance *r, struct record_cluster *cluster,
143 const char *words, int multiplier)
150 while (*words && (c = raw_char(tolower(*words))) < 0)
154 res = word_trie_match(r->wt, words, &skipped);
158 cluster->term_frequency_vec[res] += multiplier;
162 while (*words && (c = raw_char(tolower(*words))) >= 0)
165 cluster->term_frequency_vec[0]++;
172 const char *norm_str;
174 struct word_entry *next;
177 static void add_word_entry(NMEM nmem,
178 struct word_entry **entries,
179 const char *norm_str,
182 struct word_entry *ne = nmem_malloc(nmem, sizeof(*ne));
183 ne->norm_str = nmem_strdup(nmem, norm_str);
184 ne->termno = term_no;
191 int word_entry_match(struct word_entry *entries, const char *norm_str)
193 for (; entries; entries = entries->next)
195 if (!strcmp(norm_str, entries->norm_str))
196 return entries->termno;
201 static struct word_entry *build_word_entries(pp2_charset_t pct, NMEM nmem,
204 int termno = 1; /* >0 signals THERE is an entry */
205 struct word_entry *entries = 0;
206 const char **p = terms;
210 pp2_relevance_token_t prt = pp2_relevance_tokenize(pct, *p);
211 const char *norm_str;
213 while ((norm_str = pp2_relevance_token_next(prt)))
214 add_word_entry(nmem, &entries, norm_str, termno);
216 pp2_relevance_token_destroy(prt);
223 void relevance_countwords(struct relevance *r, struct record_cluster *cluster,
224 const char *words, int multiplier)
226 pp2_relevance_token_t prt = pp2_relevance_tokenize(r->pct, words);
228 const char *norm_str;
230 while ((norm_str = pp2_relevance_token_next(prt)))
232 int res = word_entry_match(r->entries, norm_str);
234 cluster->term_frequency_vec[res] += multiplier;
235 cluster->term_frequency_vec[0]++;
237 pp2_relevance_token_destroy(prt);
244 struct relevance *relevance_create(pp2_charset_t pct,
245 NMEM nmem, const char **terms, int numrecs)
247 struct relevance *res = nmem_malloc(nmem, sizeof(struct relevance));
251 for (p = terms, i = 0; *p; p++, i++)
254 res->doc_frequency_vec = nmem_malloc(nmem, res->vec_len * sizeof(int));
255 memset(res->doc_frequency_vec, 0, res->vec_len * sizeof(int));
258 res->wt = build_word_trie(nmem, terms);
260 res->entries = build_word_entries(pct, nmem, terms);
266 void relevance_newrec(struct relevance *r, struct record_cluster *rec)
268 if (!rec->term_frequency_vec)
270 rec->term_frequency_vec = nmem_malloc(r->nmem, r->vec_len * sizeof(int));
271 memset(rec->term_frequency_vec, 0, r->vec_len * sizeof(int));
276 void relevance_donerecord(struct relevance *r, struct record_cluster *cluster)
280 for (i = 1; i < r->vec_len; i++)
281 if (cluster->term_frequency_vec[i] > 0)
282 r->doc_frequency_vec[i]++;
284 r->doc_frequency_vec[0]++;
289 static int comp(const void *p1, const void *p2)
292 struct record **r1 = (struct record **) p1;
293 struct record **r2 = (struct record **) p2;
294 res = (*r2)->relevance - (*r1)->relevance;
303 static int comp(const void *p1, const void *p2)
305 struct record_cluster **r1 = (struct record_cluster **) p1;
306 struct record_cluster **r2 = (struct record_cluster **) p2;
307 return (*r2)->relevance - (*r1)->relevance;
312 // Prepare for a relevance-sorted read
313 void relevance_prepare_read(struct relevance *rel, struct reclist *reclist)
316 float *idfvec = xmalloc(rel->vec_len * sizeof(float));
318 // Calculate document frequency vector for each term.
319 for (i = 1; i < rel->vec_len; i++)
321 if (!rel->doc_frequency_vec[i])
325 // This conditional may be terribly wrong
326 // It was there to address the situation where vec[0] == vec[i]
327 // which leads to idfvec[i] == 0... not sure about this
328 // Traditional TF-IDF may assume that a word that occurs in every
329 // record is irrelevant, but this is actually something we will
331 if ((idfvec[i] = log((float) rel->doc_frequency_vec[0] /
332 rel->doc_frequency_vec[i])) < 0.0000001)
336 // Calculate relevance for each document
337 for (i = 0; i < reclist->num_records; i++)
340 struct record_cluster *rec = reclist->flatlist[i];
343 for (t = 1; t < rel->vec_len; t++)
346 if (!rec->term_frequency_vec[0])
348 termfreq = (float) rec->term_frequency_vec[t] / rec->term_frequency_vec[0];
349 relevance += termfreq * idfvec[t];
351 rec->relevance = (int) (relevance * 100000);
354 qsort(reclist->flatlist, reclist->num_records, sizeof(struct record*), comp);
356 reclist->pointer = 0;
363 * indent-tabs-mode: nil
365 * vim: shiftwidth=4 tabstop=8 expandtab