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;
45 struct word_entry *next;
48 static int word_entry_match(struct word_entry *entries, const char *norm_str,
49 const char *rank, int *mult)
51 for (; entries; entries = entries->next)
53 if (!strcmp(norm_str, entries->norm_str))
57 sscanf(rank, "%d%n", mult, &no_read);
61 if (no_read > 0 && (cp = strchr(rank, ' ')))
63 if ((cp - rank) == strlen(entries->ccl_field) &&
64 memcmp(entries->ccl_field, rank, cp - rank) == 0)
67 return entries->termno;
73 void relevance_countwords(struct relevance *r, struct record_cluster *cluster,
74 const char *words, const char *rank,
77 int *mult = cluster->term_frequency_vec_tmp;
80 pp2_charset_token_first(r->prt, words, 0);
81 for (i = 1; i < r->vec_len; i++)
85 while ((norm_str = pp2_charset_token_next(r->prt)))
88 int res = word_entry_match(r->entries, norm_str, rank, &local_mult);
91 assert(res < r->vec_len);
92 mult[res] += local_mult;
97 for (i = 1; i < r->vec_len; i++)
99 if (length > 0) /* only add if non-empty */
100 cluster->term_frequency_vecf[i] += (double) mult[i] / length;
101 cluster->term_frequency_vec[i] += mult[i];
104 cluster->term_frequency_vec[0] += length;
107 static void pull_terms(struct relevance *res, struct ccl_rpn_node *n)
120 pull_terms(res, n->u.p[0]);
121 pull_terms(res, n->u.p[1]);
124 nmem_strsplit(res->nmem, " ", n->u.t.term, &words, &numwords);
125 for (i = 0; i < numwords; i++)
127 const char *norm_str;
129 ccl_field = nmem_strdup_null(res->nmem, n->u.t.qual);
131 pp2_charset_token_first(res->prt, words[i], 0);
132 while ((norm_str = pp2_charset_token_next(res->prt)))
134 struct word_entry **e = &res->entries;
137 *e = nmem_malloc(res->nmem, sizeof(**e));
138 (*e)->norm_str = nmem_strdup(res->nmem, norm_str);
139 (*e)->ccl_field = ccl_field;
140 (*e)->termno = res->vec_len++;
150 struct relevance *relevance_create_ccl(pp2_charset_fact_t pft,
151 struct ccl_rpn_node *query,
154 NMEM nmem = nmem_create();
155 struct relevance *res = nmem_malloc(nmem, sizeof(*res));
161 res->rank_cluster = rank_cluster;
162 res->prt = pp2_charset_token_create(pft, "relevance");
164 pull_terms(res, query);
166 res->doc_frequency_vec = nmem_malloc(nmem, res->vec_len * sizeof(int));
167 for (i = 0; i < res->vec_len; i++)
168 res->doc_frequency_vec[i] = 0;
172 void relevance_destroy(struct relevance **rp)
176 pp2_charset_token_destroy((*rp)->prt);
177 nmem_destroy((*rp)->nmem);
182 void relevance_newrec(struct relevance *r, struct record_cluster *rec)
184 if (!rec->term_frequency_vec)
188 // term frequency [1,..] . [0] is total length of all fields
189 rec->term_frequency_vec =
191 r->vec_len * sizeof(*rec->term_frequency_vec));
192 for (i = 0; i < r->vec_len; i++)
193 rec->term_frequency_vec[i] = 0;
195 // term frequency divided by length of field [1,...]
196 rec->term_frequency_vecf =
198 r->vec_len * sizeof(*rec->term_frequency_vecf));
199 for (i = 0; i < r->vec_len; i++)
200 rec->term_frequency_vecf[i] = 0.0;
202 // for relevance_countwords (so we don't have to xmalloc/xfree)
203 rec->term_frequency_vec_tmp =
205 r->vec_len * sizeof(*rec->term_frequency_vec_tmp));
209 void relevance_donerecord(struct relevance *r, struct record_cluster *cluster)
213 for (i = 1; i < r->vec_len; i++)
214 if (cluster->term_frequency_vec[i] > 0)
215 r->doc_frequency_vec[i]++;
217 r->doc_frequency_vec[0]++;
220 // Prepare for a relevance-sorted read
221 void relevance_prepare_read(struct relevance *rel, struct reclist *reclist)
224 float *idfvec = xmalloc(rel->vec_len * sizeof(float));
226 reclist_enter(reclist);
227 // Calculate document frequency vector for each term.
228 for (i = 1; i < rel->vec_len; i++)
230 if (!rel->doc_frequency_vec[i])
234 /* add one to nominator idf(t,D) to ensure a value > 0 */
235 idfvec[i] = log((float) (1 + rel->doc_frequency_vec[0]) /
236 rel->doc_frequency_vec[i]);
239 // Calculate relevance for each document
244 struct record_cluster *rec = reclist_read_record(reclist);
247 for (t = 1; t < rel->vec_len; t++)
249 float termfreq = (float) rec->term_frequency_vecf[t];
250 relevance += 100000 * termfreq * idfvec[t];
252 if (!rel->rank_cluster)
254 struct record *record;
255 int cluster_size = 0;
257 for (record = rec->records; record; record = record->next)
260 relevance /= cluster_size;
262 rec->relevance_score = relevance;
264 reclist_leave(reclist);
271 * c-file-style: "Stroustrup"
272 * indent-tabs-mode: nil
274 * vim: shiftwidth=4 tabstop=8 expandtab