1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2010 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"
33 int *doc_frequency_vec;
35 struct word_entry *entries;
36 pp2_relevance_token_t prt;
44 struct word_entry *next;
47 static void add_word_entry(NMEM nmem,
48 struct word_entry **entries,
52 struct word_entry *ne = nmem_malloc(nmem, sizeof(*ne));
53 ne->norm_str = nmem_strdup(nmem, norm_str);
61 int word_entry_match(struct word_entry *entries, const char *norm_str)
63 for (; entries; entries = entries->next)
65 if (!strcmp(norm_str, entries->norm_str))
66 return entries->termno;
71 static struct word_entry *build_word_entries(pp2_relevance_token_t prt,
75 int termno = 1; /* >0 signals THERE is an entry */
76 struct word_entry *entries = 0;
77 const char **p = terms;
83 pp2_relevance_first(prt, *p, 0);
84 while ((norm_str = pp2_relevance_token_next(prt)))
85 add_word_entry(nmem, &entries, norm_str, termno);
91 void relevance_countwords(struct relevance *r, struct record_cluster *cluster,
92 const char *words, int multiplier, const char *name)
94 int *mult = cluster->term_frequency_vec_tmp;
98 pp2_relevance_first(r->prt, words, 0);
99 for (i = 1; i < r->vec_len; i++)
102 while ((norm_str = pp2_relevance_token_next(r->prt)))
104 int res = word_entry_match(r->entries, norm_str);
107 assert(res < r->vec_len);
108 mult[res] += multiplier;
113 for (i = 1; i < r->vec_len; i++)
115 if (length > 0) /* only add if non-empty */
116 cluster->term_frequency_vecf[i] += (double) mult[i] / length;
117 cluster->term_frequency_vec[i] += mult[i];
120 cluster->term_frequency_vec[0] += length;
123 struct relevance *relevance_create(pp2_charset_t pct,
124 NMEM nmem, const char **terms)
126 struct relevance *res = nmem_malloc(nmem, sizeof(struct relevance));
130 for (p = terms, i = 0; *p; p++, i++)
133 res->doc_frequency_vec = nmem_malloc(nmem, res->vec_len * sizeof(int));
134 memset(res->doc_frequency_vec, 0, res->vec_len * sizeof(int));
136 res->prt = pp2_relevance_tokenize(pct);
137 res->entries = build_word_entries(res->prt, nmem, terms);
141 void relevance_destroy(struct relevance **rp)
145 pp2_relevance_token_destroy((*rp)->prt);
150 void relevance_newrec(struct relevance *r, struct record_cluster *rec)
152 if (!rec->term_frequency_vec)
156 // term frequency [1,..] . [0] is total length of all fields
157 rec->term_frequency_vec =
159 r->vec_len * sizeof(*rec->term_frequency_vec));
160 for (i = 0; i < r->vec_len; i++)
161 rec->term_frequency_vec[i] = 0;
163 // term frequency divided by length of field [1,...]
164 rec->term_frequency_vecf =
166 r->vec_len * sizeof(*rec->term_frequency_vecf));
167 for (i = 0; i < r->vec_len; i++)
168 rec->term_frequency_vecf[i] = 0.0;
170 // for relevance_countwords (so we don't have to xmalloc/xfree)
171 rec->term_frequency_vec_tmp =
173 r->vec_len * sizeof(*rec->term_frequency_vec_tmp));
178 void relevance_donerecord(struct relevance *r, struct record_cluster *cluster)
182 for (i = 1; i < r->vec_len; i++)
183 if (cluster->term_frequency_vec[i] > 0)
184 r->doc_frequency_vec[i]++;
186 r->doc_frequency_vec[0]++;
189 // Prepare for a relevance-sorted read
190 void relevance_prepare_read(struct relevance *rel, struct reclist *reclist)
193 float *idfvec = xmalloc(rel->vec_len * sizeof(float));
195 reclist_enter(reclist);
196 // Calculate document frequency vector for each term.
197 for (i = 1; i < rel->vec_len; i++)
199 if (!rel->doc_frequency_vec[i])
203 // This conditional may be terribly wrong
204 // It was there to address the situation where vec[0] == vec[i]
205 // which leads to idfvec[i] == 0... not sure about this
206 // Traditional TF-IDF may assume that a word that occurs in every
207 // record is irrelevant, but this is actually something we will
209 if ((idfvec[i] = log((float) rel->doc_frequency_vec[0] /
210 rel->doc_frequency_vec[i])) < 0.0000001)
214 // Calculate relevance for each document
220 struct record_cluster *rec = reclist_read_record(reclist);
223 for (t = 1; t < rel->vec_len; t++)
227 termfreq = (float) rec->term_frequency_vecf[t];
229 if (rec->term_frequency_vec[0])
232 rec->term_frequency_vec[t] / rec->term_frequency_vec[0] ;
237 relevance += 100000 * (termfreq * idfvec[t] + 0.0000005);
239 rec->relevance_score = relevance;
241 reclist_leave(reclist);
248 * c-file-style: "Stroustrup"
249 * indent-tabs-mode: nil
251 * vim: shiftwidth=4 tabstop=8 expandtab