Omit CVS Id. Update copyright year.
[idzebra-moved-to-github.git] / index / rank1.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1995-2008 Index Data
3
4 Zebra 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 Zebra 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 #include <stdio.h>
21 #include <assert.h>
22 #ifdef WIN32
23 #include <io.h>
24 #endif
25 #if HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28
29 #include "index.h"
30 #include "rank.h"
31
32 static int log_level = 0;
33 static int log_initialized = 0;
34
35 struct rank_class_info {
36     int dummy;
37 };
38
39 struct rank_term_info {
40     int local_occur;
41     zint global_occur;
42     int global_inv;
43     int rank_flag;
44     int rank_weight;
45     TERMID term;
46     int term_index;
47 };
48
49 struct rank_set_info {
50     int last_pos;
51     int no_entries;
52     int no_rank_entries;
53     struct rank_term_info *entries;
54     NMEM nmem;
55 };
56
57 static int log2_int (zint g)
58 {
59     int n = 0;
60     if (g < 0)
61         return 0;
62     while ((g = g>>1))
63         n++;
64     return n;
65 }
66
67 /*
68  * create: Creates/Initialises this rank handler. This routine is 
69  *  called exactly once. The routine returns the class_handle.
70  */
71 static void *create (ZebraHandle zh)
72 {
73     struct rank_class_info *ci = 
74         (struct rank_class_info *) xmalloc (sizeof(*ci));
75
76     if (!log_initialized)
77     {
78         log_level = yaz_log_module_level("rank1");
79         log_initialized = 1;
80     }
81     yaz_log(log_level, "rank-1 create");
82     return ci;
83 }
84
85 /*
86  * destroy: Destroys this rank handler. This routine is called
87  *  when the handler is no longer needed - i.e. when the server
88  *  dies. The class_handle was previously returned by create.
89  */
90 static void destroy (struct zebra_register *reg, void *class_handle)
91 {
92     struct rank_class_info *ci = (struct rank_class_info *) class_handle;
93
94     yaz_log(log_level, "rank-1 destroy");
95     xfree (ci);
96 }
97
98
99 /**
100  * begin: Prepares beginning of "real" ranking. Called once for
101  *  each result set. The returned handle is a "set handle" and
102  *  will be used in each of the handlers below.
103  */
104 static void *begin (struct zebra_register *reg, 
105                     void *class_handle, RSET rset, NMEM nmem,
106                     TERMID *terms, int numterms)
107 {
108     struct rank_set_info *si = 
109         (struct rank_set_info *) nmem_malloc (nmem,sizeof(*si));
110     int i;
111
112     yaz_log(log_level, "rank-1 begin");
113     si->no_entries = numterms;
114     si->no_rank_entries = 0;
115     si->nmem=nmem;
116     si->entries = (struct rank_term_info *)
117         nmem_malloc (si->nmem, sizeof(*si->entries)*numterms); 
118     for (i = 0; i < numterms; i++)
119     {
120         zint g = rset_count(terms[i]->rset);
121         yaz_log(log_level, "i=%d flags=%s '%s'", i, 
122                 terms[i]->flags, terms[i]->name );
123         if  (!strncmp (terms[i]->flags, "rank,", 5)) 
124         {
125             const char *cp = strstr(terms[i]->flags+4, ",w=");
126             si->entries[i].rank_flag = 1;
127             if (cp)
128                 si->entries[i].rank_weight = atoi (cp+3);
129             else
130               si->entries[i].rank_weight = 34; /* sqrroot of 1000 */
131             yaz_log(log_level, " i=%d weight=%d g="ZINT_FORMAT, i,
132                      si->entries[i].rank_weight, g);
133             (si->no_rank_entries)++;
134         }
135         else
136             si->entries[i].rank_flag = 0;
137         si->entries[i].local_occur = 0;  /* FIXME */
138         si->entries[i].global_occur = g;
139         si->entries[i].global_inv = 32 - log2_int (g);
140         yaz_log(log_level, " global_inv = %d g = " ZINT_FORMAT, 
141                 (int) (32-log2_int (g)), g);
142         si->entries[i].term = terms[i];
143         si->entries[i].term_index=i;
144         terms[i]->rankpriv = &(si->entries[i]);
145     }
146     return si;
147 }
148
149 /*
150  * end: Terminates ranking process. Called after a result set
151  *  has been ranked.
152  */
153 static void end (struct zebra_register *reg, void *set_handle)
154 {
155     yaz_log(log_level, "rank-1 end");
156     /* no need to free anything, they are in nmems */
157 }
158
159
160 /**
161  * add: Called for each word occurence in a result set. This routine
162  *  should be as fast as possible. This routine should "incrementally"
163  *  update the score.
164  */
165 static void add (void *set_handle, int seqno, TERMID term)
166 {
167     struct rank_set_info *si = (struct rank_set_info *) set_handle;
168     struct rank_term_info *ti;
169     assert(si);
170     if (!term)
171     {
172         yaz_log(log_level, "rank-1 add NULL term");
173         return;
174     }
175     ti= (struct rank_term_info *) term->rankpriv;
176     assert(ti);
177     si->last_pos = seqno;
178     ti->local_occur++;
179     yaz_log(log_level, "rank-1 add seqno=%d term=%s count=%d", 
180             seqno, term->name,ti->local_occur);
181 }
182
183 /*
184  * calc: Called for each document in a result. This handler should 
185  *  produce a score based on previous call(s) to the add handler. The
186  *  score should be between 0 and 1000. If score cannot be obtained
187  *  -1 should be returned.
188  */
189 static int calc (void *set_handle, zint sysno, zint staticrank,
190                  int *stop_flag)
191 {
192     int i, lo, divisor, score = 0;
193     struct rank_set_info *si = (struct rank_set_info *) set_handle;
194
195     if (!si->no_rank_entries) 
196         return -1;   /* ranking not enabled for any terms */
197
198     for (i = 0; i < si->no_entries; i++)
199     {
200         yaz_log(log_level, "calc: i=%d rank_flag=%d lo=%d",
201                 i, si->entries[i].rank_flag, si->entries[i].local_occur);
202         if (si->entries[i].rank_flag && (lo = si->entries[i].local_occur))
203             score += (8+log2_int (lo)) * si->entries[i].global_inv *
204                 si->entries[i].rank_weight;
205     }
206     divisor = si->no_rank_entries * (8+log2_int (si->last_pos/si->no_entries));
207     score = score / divisor;
208     yaz_log(log_level, "calc sysno=" ZINT_FORMAT " score=%d", sysno, score);
209     if (score > 1000)
210         score = 1000;
211     /* reset the counts for the next term */
212     for (i = 0; i < si->no_entries; i++)
213         si->entries[i].local_occur = 0;
214     return score;
215 }
216
217 /*
218  * Pseudo-meta code with sequence of calls as they occur in a
219  * server. Handlers are prefixed by --:
220  *
221  *     server init
222  *     -- create
223  *     foreach search
224  *        rank result set
225  *        -- begin
226  *        foreach record
227  *           foreach word
228  *              -- add
229  *           -- calc
230  *        -- end
231  *     -- destroy
232  *     server close
233  */
234
235 static struct rank_control rank_control = {
236     "rank-1",
237     create,
238     destroy,
239     begin,
240     end,
241     calc,
242     add,
243 };
244  
245 struct rank_control *rank_1_class = &rank_control;
246 /*
247  * Local variables:
248  * c-basic-offset: 4
249  * indent-tabs-mode: nil
250  * End:
251  * vim: shiftwidth=4 tabstop=8 expandtab
252  */
253