1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2012 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_charset_token_t prt;
43 struct word_entry *next;
46 int word_entry_match(struct word_entry *entries, const char *norm_str)
48 for (; entries; entries = entries->next)
50 if (!strcmp(norm_str, entries->norm_str))
51 return entries->termno;
56 void relevance_countwords(struct relevance *r, struct record_cluster *cluster,
57 const char *words, int multiplier, const char *name)
59 int *mult = cluster->term_frequency_vec_tmp;
63 pp2_charset_token_first(r->prt, words, 0);
64 for (i = 1; i < r->vec_len; i++)
67 while ((norm_str = pp2_charset_token_next(r->prt)))
69 int res = word_entry_match(r->entries, norm_str);
72 assert(res < r->vec_len);
73 mult[res] += multiplier;
78 for (i = 1; i < r->vec_len; i++)
80 if (length > 0) /* only add if non-empty */
81 cluster->term_frequency_vecf[i] += (double) mult[i] / length;
82 cluster->term_frequency_vec[i] += mult[i];
85 cluster->term_frequency_vec[0] += length;
88 static void pull_terms(struct relevance *res, struct ccl_rpn_node *n)
100 pull_terms(res, n->u.p[0]);
101 pull_terms(res, n->u.p[1]);
104 nmem_strsplit(res->nmem, " ", n->u.t.term, &words, &numwords);
105 for (i = 0; i < numwords; i++)
107 const char *norm_str;
109 pp2_charset_token_first(res->prt, words[i], 0);
110 while ((norm_str = pp2_charset_token_next(res->prt)))
112 struct word_entry **e = &res->entries;
115 *e = nmem_malloc(res->nmem, sizeof(**e));
116 (*e)->norm_str = nmem_strdup(res->nmem, norm_str);
117 (*e)->termno = res->vec_len++;
127 struct relevance *relevance_create_ccl(pp2_charset_fact_t pft,
128 NMEM nmem, struct ccl_rpn_node *query)
130 struct relevance *res = nmem_malloc(nmem, sizeof(*res));
136 res->prt = pp2_charset_token_create(pft, "relevance");
138 pull_terms(res, query);
140 res->doc_frequency_vec = nmem_malloc(nmem, res->vec_len * sizeof(int));
141 for (i = 0; i < res->vec_len; i++)
142 res->doc_frequency_vec[i] = 0;
146 void relevance_destroy(struct relevance **rp)
150 pp2_charset_token_destroy((*rp)->prt);
155 void relevance_newrec(struct relevance *r, struct record_cluster *rec)
157 if (!rec->term_frequency_vec)
161 // term frequency [1,..] . [0] is total length of all fields
162 rec->term_frequency_vec =
164 r->vec_len * sizeof(*rec->term_frequency_vec));
165 for (i = 0; i < r->vec_len; i++)
166 rec->term_frequency_vec[i] = 0;
168 // term frequency divided by length of field [1,...]
169 rec->term_frequency_vecf =
171 r->vec_len * sizeof(*rec->term_frequency_vecf));
172 for (i = 0; i < r->vec_len; i++)
173 rec->term_frequency_vecf[i] = 0.0;
175 // for relevance_countwords (so we don't have to xmalloc/xfree)
176 rec->term_frequency_vec_tmp =
178 r->vec_len * sizeof(*rec->term_frequency_vec_tmp));
182 void relevance_donerecord(struct relevance *r, struct record_cluster *cluster)
186 for (i = 1; i < r->vec_len; i++)
187 if (cluster->term_frequency_vec[i] > 0)
188 r->doc_frequency_vec[i]++;
190 r->doc_frequency_vec[0]++;
193 // Prepare for a relevance-sorted read
194 void relevance_prepare_read(struct relevance *rel, struct reclist *reclist)
197 float *idfvec = xmalloc(rel->vec_len * sizeof(float));
199 reclist_enter(reclist);
200 // Calculate document frequency vector for each term.
201 for (i = 1; i < rel->vec_len; i++)
203 if (!rel->doc_frequency_vec[i])
207 // This conditional may be terribly wrong
208 // It was there to address the situation where vec[0] == vec[i]
209 // which leads to idfvec[i] == 0... not sure about this
210 // Traditional TF-IDF may assume that a word that occurs in every
211 // record is irrelevant, but this is actually something we will
213 if ((idfvec[i] = log((float) rel->doc_frequency_vec[0] /
214 rel->doc_frequency_vec[i])) < 0.0000001)
218 // Calculate relevance for each document
223 struct record_cluster *rec = reclist_read_record(reclist);
226 for (t = 1; t < rel->vec_len; t++)
228 float termfreq = (float) rec->term_frequency_vecf[t];
229 relevance += 100000 * (termfreq * idfvec[t] + 0.0000005);
231 rec->relevance_score = relevance;
233 reclist_leave(reclist);
240 * c-file-style: "Stroustrup"
241 * indent-tabs-mode: nil
243 * vim: shiftwidth=4 tabstop=8 expandtab