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;
34 int *term_frequency_vec_tmp;
36 struct word_entry *entries;
37 pp2_charset_token_t prt;
47 const char *display_str;
51 struct word_entry *next;
54 static struct word_entry *word_entry_match(struct relevance *r,
56 const char *rank, int *mult)
59 struct word_entry *entries = r->entries;
60 for (; entries; entries = entries->next, i++)
62 if (*norm_str && !strcmp(norm_str, entries->norm_str))
64 int extra = r->follow_boost;
65 struct word_entry *e_follow = entries;
68 sscanf(rank, "%d%n", mult, &no_read);
72 if (no_read > 0 && (cp = strchr(rank, ' ')))
74 if ((cp - rank) == strlen(entries->ccl_field) &&
75 memcmp(entries->ccl_field, rank, cp - rank) == 0)
78 (*mult) += entries->follow_boost;
79 while ((e_follow = e_follow->next) != 0 && extra > 0)
81 e_follow->follow_boost = extra--;
85 entries->follow_boost = 0;
90 void relevance_countwords(struct relevance *r, struct record_cluster *cluster,
91 const char *words, const char *rank,
94 int *mult = r->term_frequency_vec_tmp;
97 double lead_decay = r->lead_decay;
99 WRBUF w = cluster->relevance_explain1;
101 pp2_charset_token_first(r->prt, words, 0);
102 for (e = r->entries, i = 1; i < r->vec_len; i++, e = e->next)
109 while ((norm_str = pp2_charset_token_next(r->prt)))
112 e = word_entry_match(r, norm_str, rank, &local_mult);
116 assert(res < r->vec_len);
117 mult[res] += local_mult / (1 + log2(1 + lead_decay * length));
118 wrbuf_printf(w, "%s: mult[%d] += local_mult(%d) / (1+log2(1+lead_decay(%f) * length(%d)));\n", e->display_str, res, local_mult, lead_decay, length);
123 for (e = r->entries, i = 1; i < r->vec_len; i++, e = e->next)
125 if (length == 0 || mult[i] == 0)
127 wrbuf_printf(w, "%s: field=%s vecf[%d] += mult[%d](%d)",
128 e->display_str, name, i, i, mult[i]);
129 switch (r->length_divide)
132 wrbuf_printf(w, ";\n");
133 cluster->term_frequency_vecf[i] += (double) mult[i];
136 wrbuf_printf(w, " / log2(1+length(%d));\n", length);
137 cluster->term_frequency_vecf[i] +=
138 (double) mult[i] / log2(1 + length);
141 wrbuf_printf(w, " / length(%d);\n", length);
142 cluster->term_frequency_vecf[i] += (double) mult[i] / length;
144 cluster->term_frequency_vec[i] += mult[i];
147 cluster->term_frequency_vec[0] += length;
150 static void pull_terms(struct relevance *res, struct ccl_rpn_node *n)
163 pull_terms(res, n->u.p[0]);
164 pull_terms(res, n->u.p[1]);
167 nmem_strsplit(res->nmem, " ", n->u.t.term, &words, &numwords);
168 for (i = 0; i < numwords; i++)
170 const char *norm_str;
172 ccl_field = nmem_strdup_null(res->nmem, n->u.t.qual);
174 pp2_charset_token_first(res->prt, words[i], 0);
175 while ((norm_str = pp2_charset_token_next(res->prt)))
177 struct word_entry **e = &res->entries;
180 *e = nmem_malloc(res->nmem, sizeof(**e));
181 (*e)->norm_str = nmem_strdup(res->nmem, norm_str);
182 (*e)->ccl_field = ccl_field;
183 (*e)->termno = res->vec_len++;
184 (*e)->display_str = nmem_strdup(res->nmem, words[i]);
194 struct relevance *relevance_create_ccl(pp2_charset_fact_t pft,
195 struct ccl_rpn_node *query,
197 int follow_boost, double lead_decay,
200 NMEM nmem = nmem_create();
201 struct relevance *res = nmem_malloc(nmem, sizeof(*res));
207 res->rank_cluster = rank_cluster;
208 res->follow_boost = follow_boost;
209 res->lead_decay = lead_decay;
210 res->length_divide = length_divide;
211 res->prt = pp2_charset_token_create(pft, "relevance");
213 pull_terms(res, query);
215 res->doc_frequency_vec = nmem_malloc(nmem, res->vec_len * sizeof(int));
216 for (i = 0; i < res->vec_len; i++)
217 res->doc_frequency_vec[i] = 0;
220 res->term_frequency_vec_tmp =
221 nmem_malloc(res->nmem,
222 res->vec_len * sizeof(*res->term_frequency_vec_tmp));
226 void relevance_destroy(struct relevance **rp)
230 pp2_charset_token_destroy((*rp)->prt);
231 nmem_destroy((*rp)->nmem);
236 void relevance_newrec(struct relevance *r, struct record_cluster *rec)
238 if (!rec->term_frequency_vec)
242 // term frequency [1,..] . [0] is total length of all fields
243 rec->term_frequency_vec =
245 r->vec_len * sizeof(*rec->term_frequency_vec));
246 for (i = 0; i < r->vec_len; i++)
247 rec->term_frequency_vec[i] = 0;
249 // term frequency divided by length of field [1,...]
250 rec->term_frequency_vecf =
252 r->vec_len * sizeof(*rec->term_frequency_vecf));
253 for (i = 0; i < r->vec_len; i++)
254 rec->term_frequency_vecf[i] = 0.0;
258 void relevance_donerecord(struct relevance *r, struct record_cluster *cluster)
262 for (i = 1; i < r->vec_len; i++)
263 if (cluster->term_frequency_vec[i] > 0)
264 r->doc_frequency_vec[i]++;
266 r->doc_frequency_vec[0]++;
269 // Prepare for a relevance-sorted read
270 void relevance_prepare_read(struct relevance *rel, struct reclist *reclist)
273 float *idfvec = xmalloc(rel->vec_len * sizeof(float));
275 reclist_enter(reclist);
276 // Calculate document frequency vector for each term.
277 for (i = 1; i < rel->vec_len; i++)
279 if (!rel->doc_frequency_vec[i])
283 /* add one to nominator idf(t,D) to ensure a value > 0 */
284 idfvec[i] = log((float) (1 + rel->doc_frequency_vec[0]) /
285 rel->doc_frequency_vec[i]);
288 // Calculate relevance for each document
293 struct word_entry *e = rel->entries;
294 struct record_cluster *rec = reclist_read_record(reclist);
297 w = rec->relevance_explain2;
299 for (i = 1; i < rel->vec_len; i++)
301 float termfreq = (float) rec->term_frequency_vecf[i];
302 int add = 100000 * termfreq * idfvec[i];
304 wrbuf_printf(w, "idf[%d] = log(((1 + total(%d))/termoccur(%d));\n",
305 i, rel->doc_frequency_vec[0],
306 rel->doc_frequency_vec[i]);
307 wrbuf_printf(w, "%s: relevance += 100000 * vecf[%d](%f) * "
308 "idf[%d](%f) (%d);\n",
309 e->display_str, i, termfreq, i, idfvec[i], add);
313 if (!rel->rank_cluster)
315 struct record *record;
316 int cluster_size = 0;
318 for (record = rec->records; record; record = record->next)
321 wrbuf_printf(w, "score = relevance(%d)/cluster_size(%d);\n",
322 relevance, cluster_size);
323 relevance /= cluster_size;
327 wrbuf_printf(w, "score = relevance(%d);\n", relevance);
329 rec->relevance_score = relevance;
331 reclist_leave(reclist);
338 * c-file-style: "Stroustrup"
339 * indent-tabs-mode: nil
341 * vim: shiftwidth=4 tabstop=8 expandtab