added more statistics to rank function, renamed variables to better names
[idzebra-moved-to-github.git] / index / ranksimilarity.c
1 /* $Id: ranksimilarity.c,v 1.3 2006-05-04 10:11:09 marc Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include <stdio.h>
24 #include <assert.h>
25 #include <limits.h>
26 #ifdef WIN32
27 #include <io.h>
28 #endif
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32
33 #include "index.h"
34 #include "rank.h"
35
36 static int log_level = 0;
37 static int log_initialized = 0;
38
39 struct ranksimilarity_class_info {
40   int dummy;
41 };
42
43 /** term specific info and statistics to be used under ranking */
44 struct ranksimilarity_term_info {
45
46   /** frequency of term within document field */
47   int freq_term_docfield;
48
49   /** frequency of term within result set of given term */
50   zint freq_term_resset;
51
52   /** number of docs within result set */
53   zint no_docs_resset;
54
55   /** number of terms in this field */
56   zint no_terms_field;
57
58   /** number of docs with this field in database*/
59   zint no_docs_field;
60
61   /** rank flag is one if term is to be included in ranking */
62   int rank_flag;
63
64   /** relative ranking weight of term */
65   int term_weight;
66
67   /** term id used to access term name and other info */
68   TERMID term;
69
70   /** index number in terms[i] array */
71   int term_index;
72 };
73
74 struct ranksimilarity_set_info {
75   int last_pos;
76
77   /** number of terms in query */
78   int no_terms_query;
79
80   /** number of terms in query which are included in ranking */
81   int no_ranked_terms_query;
82
83   /** number of documents in entire collection */
84   zint no_docs_database;
85
86   /** array of size no_terms_query with statistics gathered per term */
87   struct ranksimilarity_term_info *entries;
88
89   NMEM nmem;
90 };
91
92
93 /* local clean-up function */
94 static void  ranksimilar_rec_reset(struct ranksimilarity_set_info *si)
95 {
96   int i;
97   
98   for (i = 0; i < si->no_terms_query; i++)
99     {
100       si->entries[i].freq_term_docfield = 0;
101     }
102 }
103
104
105 /*
106  * create: Creates/Initialises this rank handler. This routine is 
107  *  called exactly once. The routine returns the class_handle.
108  */
109 static void *create (ZebraHandle zh)
110 {
111   struct ranksimilarity_class_info *ci = 
112     (struct ranksimilarity_class_info *) xmalloc (sizeof(*ci));
113
114   if (!log_initialized)
115     {
116       log_level = yaz_log_module_level("rank-similarity");
117       log_initialized = 1;
118     }
119   yaz_log(log_level, "create()");
120   return 0;
121 }
122
123 /*
124  * destroy: Destroys this rank handler. This routine is called
125  *  when the handler is no longer needed - i.e. when the server
126  *  dies. The class_handle was previously returned by create.
127  */
128 static void destroy (struct zebra_register *reg, void *class_handle)
129 {
130   struct ranksimilarity_class_info *ci 
131     = (struct ranksimilarity_class_info *) class_handle;
132   yaz_log(log_level, "destroy()");
133   xfree (ci);
134 }
135
136
137 /**
138  * begin: Prepares beginning of "real" ranking. Called once for
139  *  each result set. The returned handle is a "set handle" and
140  *  will be used in each of the handlers below.
141  */
142 static void *begin (struct zebra_register *reg, 
143                     void *class_handle, RSET rset, NMEM nmem,
144                     TERMID *terms, int numterms)
145 {
146   struct ranksimilarity_set_info *si = 
147     (struct ranksimilarity_set_info *) nmem_malloc (nmem, sizeof(*si));
148   int i;
149     
150   yaz_log(log_level, "begin() numterms=%d", numterms);
151  
152   /* setting database global statistics */
153    si->no_docs_database = -1;  /* TODO */
154
155   /* setting query statistics */
156    si->no_terms_query = numterms;
157    si->no_ranked_terms_query = 0;
158
159   /* setting internal data structures */
160   si->nmem=nmem;
161   si->entries = (struct ranksimilarity_term_info *)
162     nmem_malloc (si->nmem, sizeof(*si->entries)*numterms); 
163
164   /* reset the counts for the next term */
165   ranksimilar_rec_reset(si);
166
167
168   /* looping all terms in a specific field of query */
169   for (i = 0; i < numterms; i++)
170     {
171       struct ord_list *ol = NULL;
172
173
174       /* adding to number of rank entries */
175       if (strncmp (terms[i]->flags, "rank,", 5)) 
176         {
177           si->entries[i].rank_flag = 0;
178           yaz_log(log_level, "begin() terms[%d]: '%s' flags=%s not ranked", 
179                   i, terms[i]->name, terms[i]->flags);
180         } 
181       else 
182         {
183           const char *cp = strstr(terms[i]->flags+4, ",w=");
184
185           (si->no_ranked_terms_query)++;
186           ol = terms[i]->ol;
187           si->entries[i].rank_flag = 1;
188
189           /* notice that the call to rset_count(rset) has he side-effect of setting
190              rset->hits_limit = rset_count(rset) ??? */
191           si->entries[i].freq_term_resset = rset_count(terms[i]->rset);
192           /* si->entries[i].freq_term_resset = terms[i]->rset->hits_count; */
193
194           
195           yaz_log(log_level, "begin() rset_count(terms[%d]->rset) = %d", 
196             i, rset_count(terms[i]->rset)); 
197           yaz_log(log_level, "begin() terms[%d]->rset->hits_limit = %d", 
198                   i, terms[i]->rset->hits_limit); 
199           yaz_log(log_level, "begin() terms[%d]->rset->hits_count = %d", 
200                   i, terms[i]->rset->hits_count); 
201           yaz_log(log_level, "begin() terms[%d]->rset->hits_round = %d", 
202                   i, terms[i]->rset->hits_round); 
203           yaz_log(log_level, "begin() terms[%d]->rset->hits_approx = %d", 
204                   i, terms[i]->rset->hits_approx); 
205           
206
207           si->entries[i].no_docs_resset = -1; /*TODO*/
208           si->entries[i].no_docs_field = -1;   /*TODO*/
209           si->entries[i].no_terms_field = -1;   /*TODO*/
210           
211          if (cp)
212             si->entries[i].term_weight = atoi (cp+3);
213           else
214             si->entries[i].term_weight = 34; /* sqrroot of 1000 */
215
216           yaz_log(log_level, "begin() terms[%d]: '%s' flags=%s", 
217                   i, terms[i]->name, terms[i]->flags);
218           
219           /* looping indexes where term terms[i] is found */
220           for (; ol; ol = ol->next)
221             {
222               int index_type = 0;
223               const char *db = 0;
224               const char *string_index = 0;
225               int set = -1;
226               int use = -1;
227               
228               zebraExplain_lookup_ord(reg->zei,
229                                       ol->ord, &index_type, &db, &set, &use,
230                                       &string_index);
231               
232               if (string_index)
233                 yaz_log(log_level, 
234                         "begin() index: ord=%d type=%c db=%s str-index=%s",
235                         ol->ord, index_type, db, string_index);
236               else
237                 yaz_log(log_level, 
238                         "begin() index: ord=%d type=%c db=%s set=%d use=%d",
239                         ol->ord, index_type, db, set, use);
240             }
241      
242         }
243         
244       si->entries[i].term = terms[i];
245       si->entries[i].term_index=i;
246         
247       /* setting next entry in term */
248       terms[i]->rankpriv = &(si->entries[i]);
249     }
250     
251   return si;
252 }
253
254 /*
255  * end: Terminates ranking process. Called after a result set
256  *  has been ranked.
257  */
258 static void end (struct zebra_register *reg, void *set_handle)
259 {
260   yaz_log(log_level, "end()");
261 }
262
263
264 /**
265  * add: Called for each word occurence in a result set. This routine
266  *  should be as fast as possible. This routine should "incrementally"
267  *  update the score.
268  */
269 static void add (void *set_handle, int seqno, TERMID term)
270 {
271   struct ranksimilarity_set_info *si 
272     = (struct ranksimilarity_set_info *) set_handle;
273   struct ranksimilarity_term_info *ti; 
274   assert(si);
275   if (!term)
276     {
277       /* yaz_log(log_level, "add() seqno=%d NULL term", seqno); */
278       return;
279     }
280
281   ti= (struct ranksimilarity_term_info *) term->rankpriv;
282   assert(ti);
283   si->last_pos = seqno;
284   ti->freq_term_docfield++;
285   /* yaz_log(log_level, "add() seqno=%d term=%s freq_term_docfield=%d", 
286      seqno, term->name, ti->freq_term_docfield); */
287 }
288
289 /*
290  * calc: Called for each document in a result. This handler should 
291  *  produce a score based on previous call(s) to the add handler. The
292  *  score should be between 0 and 1000. If score cannot be obtained
293  *  -1 should be returned.
294  */
295 static int calc (void *set_handle, zint sysno, zint staticrank,
296                  int *stop_flag)
297 {
298   int i, score = 0;
299   struct ranksimilarity_set_info *si 
300     = (struct ranksimilarity_set_info *) set_handle;
301   
302   
303   yaz_log(log_level, "calc() sysno =      %d", sysno);
304   yaz_log(log_level, "calc() staticrank = %d", staticrank);
305         
306   yaz_log(log_level, "calc() si->no_terms_query = %d", 
307           si->no_terms_query);
308   yaz_log(log_level, "calc() si->no_ranked_terms_query = %d", 
309           si->no_ranked_terms_query);
310   yaz_log(log_level, "calc() si->no_docs_database = %d", 
311           si->no_docs_database); 
312
313   
314   if (!si->no_ranked_terms_query)
315     return -1;   /* ranking not enabled for any terms */
316
317   
318   /* if we set *stop_flag = 1, we stop processing (of result set list) */
319
320
321   /* here goes your formula to compute a scoring function */
322   /* you may use all the gathered statistics here */
323   for (i = 0; i < si->no_terms_query; i++)
324     {
325       yaz_log(log_level, "calc() entries[%d] termid %d", 
326               i, si->entries[i].term);
327       if (si->entries[i].term){
328         yaz_log(log_level, "calc() entries[%d] term '%s' flags=%s", 
329                 i, si->entries[i].term->name, si->entries[i].term->flags);
330         yaz_log(log_level, "calc() entries[%d] rank_flag %d", 
331                 i, si->entries[i].rank_flag );
332         yaz_log(log_level, "calc() entries[%d] term_weight %d", 
333                 i, si->entries[i].term_weight );
334         yaz_log(log_level, "calc() entries[%d] freq_term_docfield %d", 
335                 i, si->entries[i].freq_term_docfield );
336         yaz_log(log_level, "calc() entries[%d] freq_term_resset %d", 
337                 i, si->entries[i].freq_term_resset );
338         yaz_log(log_level, "calc() entries[%d] no_docs_resset %d", 
339                 i, si->entries[i].no_docs_resset );
340         yaz_log(log_level, "calc() entries[%d] no_docs_field %d", 
341                 i, si->entries[i].no_docs_field );
342         yaz_log(log_level, "calc() entries[%d] no_terms_field %d", 
343                 i, si->entries[i].no_terms_field );
344       }
345     }
346   
347
348   /* reset the counts for the next term */
349   ranksimilar_rec_reset(si);
350  
351
352   /* staticrank = 0 is highest, MAXINT lowest */
353   score = INT_MAX - staticrank;  /* but score is reverse (logical) */
354
355
356   /* debugging statistics output */
357   yaz_log(log_level, "calc() statistics: score = %d", score); 
358
359   return score;
360 }
361
362 /*
363  * Pseudo-meta code with sequence of calls as they occur in a
364  * server. Handlers are prefixed by --:
365  *
366  *     server init
367  *     -- create
368  *     foreach search
369  *        rank result set
370  *        -- begin
371  *        foreach record
372  *           foreach word
373  *              -- add
374  *           -- calc
375  *        -- end
376  *     -- destroy
377  *     server close
378  */
379
380 static struct rank_control rank_control = {
381   "rank-similarity",
382   create,
383   destroy,
384   begin,
385   end,
386   calc,
387   add,
388 };
389  
390 struct rank_control *rank_similarity_class = &rank_control;