1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2013 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"
32 #define log2(x) (log(x)/log(2))
37 int *doc_frequency_vec;
38 int *term_frequency_vec_tmp;
41 struct word_entry *entries;
42 pp2_charset_token_t prt;
52 const char *display_str;
55 struct word_entry *next;
58 static struct word_entry *word_entry_match(struct relevance *r,
60 const char *rank, int *weight)
63 struct word_entry *entries = r->entries;
64 for (; entries; entries = entries->next, i++)
66 if (*norm_str && !strcmp(norm_str, entries->norm_str))
70 sscanf(rank, "%d%n", weight, &no_read);
74 if (no_read > 0 && (cp = strchr(rank, ' ')))
76 if ((cp - rank) == strlen(entries->ccl_field) &&
77 memcmp(entries->ccl_field, rank, cp - rank) == 0)
78 *weight = atoi(cp + 1);
86 void relevance_countwords(struct relevance *r, struct record_cluster *cluster,
87 const char *words, const char *rank,
90 int *w = r->term_frequency_vec_tmp;
93 double lead_decay = r->lead_decay;
95 WRBUF wr = cluster->relevance_explain1;
96 int printed_about_field = 0;
98 pp2_charset_token_first(r->prt, words, 0);
99 for (e = r->entries, i = 1; i < r->vec_len; i++, e = e->next)
106 while ((norm_str = pp2_charset_token_next(r->prt)))
108 int local_weight = 0;
109 e = word_entry_match(r, norm_str, rank, &local_weight);
115 if (!printed_about_field)
117 printed_about_field = 1;
118 wrbuf_printf(wr, "field=%s content=", name);
119 if (strlen(words) > 50)
121 wrbuf_xmlputs_n(wr, words, 49);
122 wrbuf_puts(wr, " ...");
125 wrbuf_xmlputs(wr, words);
126 wrbuf_puts(wr, ";\n");
128 assert(res < r->vec_len);
129 w[res] += local_weight / (1 + log2(1 + lead_decay * length));
130 wrbuf_printf(wr, "%s: w[%d] += w(%d) / "
131 "(1+log2(1+lead_decay(%f) * length(%d)));\n",
132 e->display_str, res, local_weight, lead_decay, length);
134 if (j > 0 && r->term_pos[j])
136 int d = length + 1 - r->term_pos[j];
137 wrbuf_printf(wr, "%s: w[%d] += w[%d](%d) * follow(%f) / "
139 e->display_str, res, res, w[res],
140 r->follow_factor, d);
141 w[res] += w[res] * r->follow_factor / (1 + log2(d));
143 for (j = 0; j < r->vec_len; j++)
144 r->term_pos[j] = j < res ? 0 : length + 1;
149 for (e = r->entries, i = 1; i < r->vec_len; i++, e = e->next)
151 if (length == 0 || w[i] == 0)
153 wrbuf_printf(wr, "%s: tf[%d] += w[%d](%d)", e->display_str, i, i, w[i]);
154 switch (r->length_divide)
157 cluster->term_frequency_vecf[i] += (double) w[i];
160 wrbuf_printf(wr, " / log2(1+length(%d))", length);
161 cluster->term_frequency_vecf[i] +=
162 (double) w[i] / log2(1 + length);
165 wrbuf_printf(wr, " / length(%d)", length);
166 cluster->term_frequency_vecf[i] += (double) w[i] / length;
168 cluster->term_frequency_vec[i] += w[i];
169 wrbuf_printf(wr, " (%f);\n", cluster->term_frequency_vecf[i]);
172 cluster->term_frequency_vec[0] += length;
175 static void pull_terms(struct relevance *res, struct ccl_rpn_node *n)
188 pull_terms(res, n->u.p[0]);
189 pull_terms(res, n->u.p[1]);
192 nmem_strsplit(res->nmem, " ", n->u.t.term, &words, &numwords);
193 for (i = 0; i < numwords; i++)
195 const char *norm_str;
197 ccl_field = nmem_strdup_null(res->nmem, n->u.t.qual);
199 pp2_charset_token_first(res->prt, words[i], 0);
200 while ((norm_str = pp2_charset_token_next(res->prt)))
202 struct word_entry **e = &res->entries;
205 *e = nmem_malloc(res->nmem, sizeof(**e));
206 (*e)->norm_str = nmem_strdup(res->nmem, norm_str);
207 (*e)->ccl_field = ccl_field;
208 (*e)->termno = res->vec_len++;
209 (*e)->display_str = nmem_strdup(res->nmem, words[i]);
219 struct relevance *relevance_create_ccl(pp2_charset_fact_t pft,
220 struct ccl_rpn_node *query,
222 double follow_factor, double lead_decay,
225 NMEM nmem = nmem_create();
226 struct relevance *res = nmem_malloc(nmem, sizeof(*res));
232 res->rank_cluster = rank_cluster;
233 res->follow_factor = follow_factor;
234 res->lead_decay = lead_decay;
235 res->length_divide = length_divide;
236 res->prt = pp2_charset_token_create(pft, "relevance");
238 pull_terms(res, query);
240 res->doc_frequency_vec = nmem_malloc(nmem, res->vec_len * sizeof(int));
241 for (i = 0; i < res->vec_len; i++)
242 res->doc_frequency_vec[i] = 0;
245 res->term_frequency_vec_tmp =
246 nmem_malloc(res->nmem,
247 res->vec_len * sizeof(*res->term_frequency_vec_tmp));
250 nmem_malloc(res->nmem, res->vec_len * sizeof(*res->term_pos));
255 void relevance_destroy(struct relevance **rp)
259 pp2_charset_token_destroy((*rp)->prt);
260 nmem_destroy((*rp)->nmem);
265 void relevance_newrec(struct relevance *r, struct record_cluster *rec)
267 if (!rec->term_frequency_vec)
271 // term frequency [1,..] . [0] is total length of all fields
272 rec->term_frequency_vec =
274 r->vec_len * sizeof(*rec->term_frequency_vec));
275 for (i = 0; i < r->vec_len; i++)
276 rec->term_frequency_vec[i] = 0;
278 // term frequency divided by length of field [1,...]
279 rec->term_frequency_vecf =
281 r->vec_len * sizeof(*rec->term_frequency_vecf));
282 for (i = 0; i < r->vec_len; i++)
283 rec->term_frequency_vecf[i] = 0.0;
287 void relevance_donerecord(struct relevance *r, struct record_cluster *cluster)
291 for (i = 1; i < r->vec_len; i++)
292 if (cluster->term_frequency_vec[i] > 0)
293 r->doc_frequency_vec[i]++;
295 r->doc_frequency_vec[0]++;
298 // Prepare for a relevance-sorted read
299 void relevance_prepare_read(struct relevance *rel, struct reclist *reclist)
302 float *idfvec = xmalloc(rel->vec_len * sizeof(float));
304 reclist_enter(reclist);
305 // Calculate document frequency vector for each term.
306 for (i = 1; i < rel->vec_len; i++)
308 if (!rel->doc_frequency_vec[i])
312 /* add one to nominator idf(t,D) to ensure a value > 0 */
313 idfvec[i] = log((float) (1 + rel->doc_frequency_vec[0]) /
314 rel->doc_frequency_vec[i]);
317 // Calculate relevance for each document
322 struct word_entry *e = rel->entries;
323 struct record_cluster *rec = reclist_read_record(reclist);
326 w = rec->relevance_explain2;
328 wrbuf_puts(w, "relevance = 0;\n");
329 for (i = 1; i < rel->vec_len; i++)
331 float termfreq = (float) rec->term_frequency_vecf[i];
332 int add = 100000 * termfreq * idfvec[i];
334 wrbuf_printf(w, "idf[%d] = log(((1 + total(%d))/termoccur(%d));\n",
335 i, rel->doc_frequency_vec[0],
336 rel->doc_frequency_vec[i]);
337 wrbuf_printf(w, "%s: relevance += 100000 * tf[%d](%f) * "
338 "idf[%d](%f) (%d);\n",
339 e->display_str, i, termfreq, i, idfvec[i], add);
343 if (!rel->rank_cluster)
345 struct record *record;
346 int cluster_size = 0;
348 for (record = rec->records; record; record = record->next)
351 wrbuf_printf(w, "score = relevance(%d)/cluster_size(%d);\n",
352 relevance, cluster_size);
353 relevance /= cluster_size;
357 wrbuf_printf(w, "score = relevance(%d);\n", relevance);
359 rec->relevance_score = relevance;
361 reclist_leave(reclist);
368 * c-file-style: "Stroustrup"
369 * indent-tabs-mode: nil
371 * vim: shiftwidth=4 tabstop=8 expandtab