added new ranking function rank-similar in ranksimilar.c
[idzebra-moved-to-github.git] / index / ranksimilarity.c
1 /* $Id: ranksimilarity.c,v 1.1 2006-05-03 09:31:26 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 struct ranksimilarity_term_info {
44     int local_occur;
45     zint global_occur;
46     int global_inv;
47     int rank_flag;
48     int rank_weight;
49     TERMID term;
50     int term_index;
51 };
52
53 struct ranksimilarity_set_info {
54     int last_pos;
55     int no_entries;
56     int no_rank_entries;
57     struct ranksimilarity_term_info *entries;
58     NMEM nmem;
59 };
60
61
62 /*
63  * create: Creates/Initialises this rank handler. This routine is 
64  *  called exactly once. The routine returns the class_handle.
65  */
66 static void *create (ZebraHandle zh)
67 {
68     struct ranksimilarity_class_info *ci = 
69         (struct ranksimilarity_class_info *) xmalloc (sizeof(*ci));
70
71     if (!log_initialized)
72     {
73         log_level = yaz_log_module_level("ranksimilarity");
74         log_initialized = 1;
75     }
76     yaz_log(log_level, "create()");
77     return 0;
78 }
79
80 /*
81  * destroy: Destroys this rank handler. This routine is called
82  *  when the handler is no longer needed - i.e. when the server
83  *  dies. The class_handle was previously returned by create.
84  */
85 static void destroy (struct zebra_register *reg, void *class_handle)
86 {
87     struct ranksimilarity_class_info *ci 
88       = (struct ranksimilarity_class_info *) class_handle;
89     yaz_log(log_level, "destroy()");
90     xfree (ci);
91 }
92
93
94 /**
95  * begin: Prepares beginning of "real" ranking. Called once for
96  *  each result set. The returned handle is a "set handle" and
97  *  will be used in each of the handlers below.
98  */
99 static void *begin (struct zebra_register *reg, 
100                     void *class_handle, RSET rset, NMEM nmem,
101                     TERMID *terms, int numterms)
102 {
103     struct ranksimilarity_set_info *si = 
104         (struct ranksimilarity_set_info *) nmem_malloc (nmem, sizeof(*si));
105     int i;
106
107     yaz_log(log_level, "begin()");
108
109     /* count how many terms are ranked (2=102 or similar) */
110     si->no_entries = numterms;
111     si->no_rank_entries = 0;
112     si->nmem=nmem;
113     si->entries = (struct ranksimilarity_term_info *)
114         nmem_malloc (si->nmem, sizeof(*si->entries)*numterms); 
115
116     /* looping all terms in a specific field of query */
117     for (i = 0; i < numterms; i++)
118     {
119         struct ord_list *ol = terms[i]->ol;
120
121         yaz_log(log_level, "begin() term i=%d flags=%s '%s'", i, 
122                 terms[i]->flags, terms[i]->name );
123
124         for (; ol; ol = ol->next)
125         {
126             int index_type = 0;
127             const char *db = 0;
128             const char *string_index = 0;
129             int set = -1;
130             int use = -1;
131
132             zebraExplain_lookup_ord(reg->zei,
133                                     ol->ord, &index_type, &db, &set, &use,
134                                     &string_index);
135
136             if (string_index)
137                 yaz_log(log_level, "begin() ord=%d index_type=%c db=%s str-index=%s",
138                     ol->ord, index_type, db, string_index);
139             else
140                 yaz_log(log_level, "begin() ord=%d index_type=%c db=%s set=%d use=%d",
141                     ol->ord, index_type, db, set, use);
142         }
143         if (!strncmp (terms[i]->flags, "rank,", 5)) 
144             (si->no_rank_entries)++;
145
146         /* setting next entry in term */
147         terms[i]->rankpriv = &(si->entries[i]);
148     }
149     return si;
150 }
151
152 /*
153  * end: Terminates ranking process. Called after a result set
154  *  has been ranked.
155  */
156 static void end (struct zebra_register *reg, void *set_handle)
157 {
158     yaz_log(log_level, "end()");
159 }
160
161
162 /**
163  * add: Called for each word occurence in a result set. This routine
164  *  should be as fast as possible. This routine should "incrementally"
165  *  update the score.
166  */
167 static void add (void *set_handle, int seqno, TERMID term)
168 {
169   struct ranksimilarity_set_info *si = (struct ranksimilarity_set_info *) set_handle;
170   struct ranksimilarity_term_info *ti; 
171     assert(si);
172     if (!term)
173     {
174       /* yaz_log(log_level, "add() NULL term"); */
175         return;
176     }
177
178     
179     ti= (struct ranksimilarity_term_info *) term->rankpriv;
180     assert(ti);
181     si->last_pos = seqno;
182     ti->local_occur++;
183     /* yaz_log(log_level, "add() seqno=%d term=%s count=%d", 
184        seqno, term->name,ti->local_occur); */
185 }
186
187 /*
188  * calc: Called for each document in a result. This handler should 
189  *  produce a score based on previous call(s) to the add handler. The
190  *  score should be between 0 and 1000. If score cannot be obtained
191  *  -1 should be returned.
192  */
193 static int calc (void *set_handle, zint sysno, zint staticrank,
194                  int *stop_flag)
195 {
196   int i, score = 0;
197   struct ranksimilarity_set_info *si 
198     = (struct ranksimilarity_set_info *) set_handle;
199   
200   yaz_log(log_level, "calc()");
201   
202   if (!si->no_rank_entries)
203     return -1;   /* ranking not enabled for any terms */
204
205   /* here you put in your own score function */
206   
207   
208   /* reset the counts for the next term */
209   for (i = 0; i < si->no_entries; i++)
210     si->entries[i].local_occur = 0;
211   
212   /* if we set *stop_flag = 1, we stop processing (of result set list) */
213   /* staticrank = 0 is highest, MAXINT lowest */
214
215
216   /* here goes your formula to compute a scoring function */
217   /* you may use all the gathered statistics here */
218
219   score = INT_MAX - staticrank;  /* but score is reverse (logical) */
220
221
222
223   return score;
224 }
225
226 /*
227  * Pseudo-meta code with sequence of calls as they occur in a
228  * server. Handlers are prefixed by --:
229  *
230  *     server init
231  *     -- create
232  *     foreach search
233  *        rank result set
234  *        -- begin
235  *        foreach record
236  *           foreach word
237  *              -- add
238  *           -- calc
239  *        -- end
240  *     -- destroy
241  *     server close
242  */
243
244 static struct rank_control rank_control = {
245     "rank-similarity",
246     create,
247     destroy,
248     begin,
249     end,
250     calc,
251     add,
252 };
253  
254 struct rank_control *rank_similarity_class = &rank_control;