Metadata 'skiparticle works for ICU normalization
[pazpar2-moved-to-github.git] / src / relevance.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2009 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17
18 */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <assert.h>
25 #include <math.h>
26 #include <stdlib.h>
27
28 #include "relevance.h"
29 #include "pazpar2.h"
30
31 struct relevance
32 {
33     int *doc_frequency_vec;
34     int vec_len;
35     struct word_entry *entries;
36     pp2_charset_t pct;
37     NMEM nmem;
38 };
39
40
41 struct word_entry {
42     const char *norm_str;
43     int termno;
44     struct word_entry *next;
45 };
46
47 static void add_word_entry(NMEM nmem, 
48                            struct word_entry **entries,
49                            const char *norm_str,
50                            int term_no)
51 {
52     struct word_entry *ne = nmem_malloc(nmem, sizeof(*ne));
53     ne->norm_str = nmem_strdup(nmem, norm_str);
54     ne->termno = term_no;
55     
56     ne->next = *entries;
57     *entries = ne;
58 }
59
60
61 int word_entry_match(struct word_entry *entries, const char *norm_str)
62 {
63     for (; entries; entries = entries->next)
64     {
65         if (!strcmp(norm_str, entries->norm_str))
66             return entries->termno;
67     }
68     return 0;
69 }
70
71 static struct word_entry *build_word_entries(pp2_charset_t pct, NMEM nmem,
72                                              const char **terms)
73 {
74     int termno = 1; /* >0 signals THERE is an entry */
75     struct word_entry *entries = 0;
76     const char **p = terms;
77
78     for (; *p; p++)
79     {
80         pp2_relevance_token_t prt = pp2_relevance_tokenize(pct, *p, 0);
81         const char *norm_str;
82
83         while ((norm_str = pp2_relevance_token_next(prt)))
84             add_word_entry(nmem, &entries, norm_str, termno);
85
86         pp2_relevance_token_destroy(prt);
87
88         termno++;
89     }
90     return entries;
91 }
92
93 void relevance_countwords(struct relevance *r, struct record_cluster *cluster,
94                           const char *words, int multiplier, const char *name)
95 {
96     pp2_relevance_token_t prt = pp2_relevance_tokenize(r->pct, words, 0);
97     int *mult = cluster->term_frequency_vec_tmp;
98     const char *norm_str;
99     int i, length = 0;
100
101     for (i = 1; i < r->vec_len; i++)
102         mult[i] = 0;
103
104     while ((norm_str = pp2_relevance_token_next(prt)))
105     {
106         int res = word_entry_match(r->entries, norm_str);
107         if (res)
108         {
109             assert(res < r->vec_len);
110             mult[res] += multiplier;
111         }
112         length++;
113     }
114
115     for (i = 1; i < r->vec_len; i++)
116     {
117         if (length > 0) /* only add if non-empty */
118             cluster->term_frequency_vecf[i] += (double) mult[i] / length;
119         cluster->term_frequency_vec[i] += mult[i];
120     }
121
122     cluster->term_frequency_vec[0] += length;
123     pp2_relevance_token_destroy(prt);
124 }
125
126 struct relevance *relevance_create(pp2_charset_t pct,
127                                    NMEM nmem, const char **terms)
128 {
129     struct relevance *res = nmem_malloc(nmem, sizeof(struct relevance));
130     const char **p;
131     int i;
132
133     for (p = terms, i = 0; *p; p++, i++)
134         ;
135     res->vec_len = ++i;
136     res->doc_frequency_vec = nmem_malloc(nmem, res->vec_len * sizeof(int));
137     memset(res->doc_frequency_vec, 0, res->vec_len * sizeof(int));
138     res->nmem = nmem;
139     res->entries = build_word_entries(pct, nmem, terms);
140     res->pct = pct;
141     return res;
142 }
143
144 void relevance_newrec(struct relevance *r, struct record_cluster *rec)
145 {
146     if (!rec->term_frequency_vec)
147     {
148         int i;
149
150         // term frequency [1,..] . [0] is total length of all fields
151         rec->term_frequency_vec =
152             nmem_malloc(r->nmem,
153                         r->vec_len * sizeof(*rec->term_frequency_vec));
154         for (i = 0; i < r->vec_len; i++)
155             rec->term_frequency_vec[i] = 0;
156         
157         // term frequency divided by length of field [1,...]
158         rec->term_frequency_vecf =
159             nmem_malloc(r->nmem,
160                         r->vec_len * sizeof(*rec->term_frequency_vecf));
161         for (i = 0; i < r->vec_len; i++)
162             rec->term_frequency_vecf[i] = 0.0;
163         
164         // for relevance_countwords (so we don't have to xmalloc/xfree)
165         rec->term_frequency_vec_tmp =
166             nmem_malloc(r->nmem,
167                         r->vec_len * sizeof(*rec->term_frequency_vec_tmp));
168     }
169 }
170
171
172 void relevance_donerecord(struct relevance *r, struct record_cluster *cluster)
173 {
174     int i;
175
176     for (i = 1; i < r->vec_len; i++)
177         if (cluster->term_frequency_vec[i] > 0)
178             r->doc_frequency_vec[i]++;
179
180     r->doc_frequency_vec[0]++;
181 }
182
183 // Prepare for a relevance-sorted read
184 void relevance_prepare_read(struct relevance *rel, struct reclist *reclist)
185 {
186     int i;
187     float *idfvec = xmalloc(rel->vec_len * sizeof(float));
188
189     reclist_rewind(reclist);
190     // Calculate document frequency vector for each term.
191     for (i = 1; i < rel->vec_len; i++)
192     {
193         if (!rel->doc_frequency_vec[i])
194             idfvec[i] = 0;
195         else
196         {
197             // This conditional may be terribly wrong
198             // It was there to address the situation where vec[0] == vec[i]
199             // which leads to idfvec[i] == 0... not sure about this
200             // Traditional TF-IDF may assume that a word that occurs in every
201             // record is irrelevant, but this is actually something we will
202             // see a lot
203             if ((idfvec[i] = log((float) rel->doc_frequency_vec[0] /
204                             rel->doc_frequency_vec[i])) < 0.0000001)
205                 idfvec[i] = 1;
206         }
207     }
208     // Calculate relevance for each document
209
210     while (1)
211     {
212         int t;
213         int relevance = 0;
214         struct record_cluster *rec = reclist_read_record(reclist);
215         if (!rec)
216             break;
217         for (t = 1; t < rel->vec_len; t++)
218         {
219             float termfreq;
220 #if 1
221             termfreq = (float) rec->term_frequency_vecf[t];
222 #else
223             if (rec->term_frequency_vec[0])
224             {
225                 termfreq = (float)
226                     rec->term_frequency_vec[t] / rec->term_frequency_vec[0] ;
227             }
228             else
229                 termfreq = 0.0;
230 #endif
231             relevance += 100000 * (termfreq * idfvec[t] + 0.0000005);  
232         }
233         rec->relevance = relevance;
234     }
235     reclist_rewind(reclist);
236     xfree(idfvec);
237 }
238
239 /*
240  * Local variables:
241  * c-basic-offset: 4
242  * c-file-style: "Stroustrup"
243  * indent-tabs-mode: nil
244  * End:
245  * vim: shiftwidth=4 tabstop=8 expandtab
246  */
247