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;
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_charset_t pct, NMEM nmem,
74 int termno = 1; /* >0 signals THERE is an entry */
75 struct word_entry *entries = 0;
76 const char **p = terms;
80 pp2_relevance_token_t prt = pp2_relevance_tokenize(pct, *p, 0);
83 while ((norm_str = pp2_relevance_token_next(prt)))
84 add_word_entry(nmem, &entries, norm_str, termno);
86 pp2_relevance_token_destroy(prt);
93 void relevance_countwords(struct relevance *r, struct record_cluster *cluster,
94 const char *words, int multiplier, const char *name)
96 pp2_relevance_token_t prt = pp2_relevance_tokenize(r->pct, words, 0);
97 int *mult = cluster->term_frequency_vec_tmp;
101 for (i = 1; i < r->vec_len; i++)
104 while ((norm_str = pp2_relevance_token_next(prt)))
106 int res = word_entry_match(r->entries, norm_str);
109 assert(res < r->vec_len);
110 mult[res] += multiplier;
115 for (i = 1; i < r->vec_len; i++)
117 if (length > 0) /* only add if non-empty */
118 cluster->term_frequency_vecf[i] += (double) mult[i] / length;
119 cluster->term_frequency_vec[i] += mult[i];
122 cluster->term_frequency_vec[0] += length;
123 pp2_relevance_token_destroy(prt);
126 struct relevance *relevance_create(pp2_charset_t pct,
127 NMEM nmem, const char **terms)
129 struct relevance *res = nmem_malloc(nmem, sizeof(struct relevance));
133 for (p = terms, i = 0; *p; p++, i++)
136 res->doc_frequency_vec = nmem_malloc(nmem, res->vec_len * sizeof(int));
137 memset(res->doc_frequency_vec, 0, res->vec_len * sizeof(int));
139 res->entries = build_word_entries(pct, nmem, terms);
144 void relevance_newrec(struct relevance *r, struct record_cluster *rec)
146 if (!rec->term_frequency_vec)
150 // term frequency [1,..] . [0] is total length of all fields
151 rec->term_frequency_vec =
153 r->vec_len * sizeof(*rec->term_frequency_vec));
154 for (i = 0; i < r->vec_len; i++)
155 rec->term_frequency_vec[i] = 0;
157 // term frequency divided by length of field [1,...]
158 rec->term_frequency_vecf =
160 r->vec_len * sizeof(*rec->term_frequency_vecf));
161 for (i = 0; i < r->vec_len; i++)
162 rec->term_frequency_vecf[i] = 0.0;
164 // for relevance_countwords (so we don't have to xmalloc/xfree)
165 rec->term_frequency_vec_tmp =
167 r->vec_len * sizeof(*rec->term_frequency_vec_tmp));
172 void relevance_donerecord(struct relevance *r, struct record_cluster *cluster)
176 for (i = 1; i < r->vec_len; i++)
177 if (cluster->term_frequency_vec[i] > 0)
178 r->doc_frequency_vec[i]++;
180 r->doc_frequency_vec[0]++;
183 // Prepare for a relevance-sorted read
184 void relevance_prepare_read(struct relevance *rel, struct reclist *reclist)
187 float *idfvec = xmalloc(rel->vec_len * sizeof(float));
189 reclist_rewind(reclist);
190 // Calculate document frequency vector for each term.
191 for (i = 1; i < rel->vec_len; i++)
193 if (!rel->doc_frequency_vec[i])
197 // This conditional may be terribly wrong
198 // It was there to address the situation where vec[0] == vec[i]
199 // which leads to idfvec[i] == 0... not sure about this
200 // Traditional TF-IDF may assume that a word that occurs in every
201 // record is irrelevant, but this is actually something we will
203 if ((idfvec[i] = log((float) rel->doc_frequency_vec[0] /
204 rel->doc_frequency_vec[i])) < 0.0000001)
208 // Calculate relevance for each document
214 struct record_cluster *rec = reclist_read_record(reclist);
217 for (t = 1; t < rel->vec_len; t++)
221 termfreq = (float) rec->term_frequency_vecf[t];
223 if (rec->term_frequency_vec[0])
226 rec->term_frequency_vec[t] / rec->term_frequency_vec[0] ;
231 relevance += 100000 * (termfreq * idfvec[t] + 0.0000005);
233 rec->relevance = relevance;
235 reclist_rewind(reclist);
242 * c-file-style: "Stroustrup"
243 * indent-tabs-mode: nil
245 * vim: shiftwidth=4 tabstop=8 expandtab