1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2009 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);
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);
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 cluster->term_frequency_vecf[i] += (double) mult[i] / length;
118 cluster->term_frequency_vec[i] += mult[i];
121 cluster->term_frequency_vec[0] += length;
122 pp2_relevance_token_destroy(prt);
125 struct relevance *relevance_create(pp2_charset_t pct,
126 NMEM nmem, const char **terms)
128 struct relevance *res = nmem_malloc(nmem, sizeof(struct relevance));
132 for (p = terms, i = 0; *p; p++, i++)
135 res->doc_frequency_vec = nmem_malloc(nmem, res->vec_len * sizeof(int));
136 memset(res->doc_frequency_vec, 0, res->vec_len * sizeof(int));
138 res->entries = build_word_entries(pct, nmem, terms);
143 void relevance_newrec(struct relevance *r, struct record_cluster *rec)
145 if (!rec->term_frequency_vec)
149 // term frequency [1,..] . [0] is total length of all fields
150 rec->term_frequency_vec =
152 r->vec_len * sizeof(*rec->term_frequency_vec));
153 for (i = 0; i < r->vec_len; i++)
154 rec->term_frequency_vec[i] = 0;
156 // term frequency divided by length of field [1,...]
157 rec->term_frequency_vecf =
159 r->vec_len * sizeof(*rec->term_frequency_vecf));
160 for (i = 0; i < r->vec_len; i++)
161 rec->term_frequency_vecf[i] = 0.0;
163 // for relevance_countwords (so we don't have to xmalloc/xfree)
164 rec->term_frequency_vec_tmp =
166 r->vec_len * sizeof(*rec->term_frequency_vec_tmp));
171 void relevance_donerecord(struct relevance *r, struct record_cluster *cluster)
175 for (i = 1; i < r->vec_len; i++)
176 if (cluster->term_frequency_vec[i] > 0)
177 r->doc_frequency_vec[i]++;
179 r->doc_frequency_vec[0]++;
182 // Prepare for a relevance-sorted read
183 void relevance_prepare_read(struct relevance *rel, struct reclist *reclist)
186 float *idfvec = xmalloc(rel->vec_len * sizeof(float));
188 reclist_rewind(reclist);
189 // Calculate document frequency vector for each term.
190 for (i = 1; i < rel->vec_len; i++)
192 if (!rel->doc_frequency_vec[i])
196 // This conditional may be terribly wrong
197 // It was there to address the situation where vec[0] == vec[i]
198 // which leads to idfvec[i] == 0... not sure about this
199 // Traditional TF-IDF may assume that a word that occurs in every
200 // record is irrelevant, but this is actually something we will
202 if ((idfvec[i] = log((float) rel->doc_frequency_vec[0] /
203 rel->doc_frequency_vec[i])) < 0.0000001)
207 // Calculate relevance for each document
213 struct record_cluster *rec = reclist_read_record(reclist);
216 for (t = 1; t < rel->vec_len; t++)
220 termfreq = (float) rec->term_frequency_vecf[t];
222 if (rec->term_frequency_vec[0])
225 rec->term_frequency_vec[t] / rec->term_frequency_vec[0] ;
230 relevance += 100000 * (termfreq * idfvec[t] + 0.0000005);
232 rec->relevance = relevance;
234 reclist_rewind(reclist);
241 * c-file-style: "Stroustrup"
242 * indent-tabs-mode: nil
244 * vim: shiftwidth=4 tabstop=8 expandtab