More type casts due to zint.
[idzebra-moved-to-github.git] / index / rank1.c
1 /* $Id: rank1.c,v 1.16 2004-08-06 13:36:23 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
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
24
25 #include <stdio.h>
26 #include <assert.h>
27 #ifdef WIN32
28 #include <io.h>
29 #else
30 #include <unistd.h>
31 #endif
32
33 #define DEBUG_RANK 0
34
35 #include "index.h"
36
37 struct rank_class_info {
38     int dummy;
39 };
40
41 struct rank_term_info {
42     int local_occur;
43     zint global_occur;
44     int global_inv;
45     int rank_flag;
46     int rank_weight;
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 };
55
56 static int log2_int (unsigned g)
57 {
58     int n = 0;
59     while ((g = g>>1))
60         n++;
61     return n;
62 }
63
64 /*
65  * create: Creates/Initialises this rank handler. This routine is 
66  *  called exactly once. The routine returns the class_handle.
67  */
68 static void *create (ZebraHandle zh)
69 {
70     struct rank_class_info *ci = (struct rank_class_info *)
71         xmalloc (sizeof(*ci));
72
73     yaz_log (LOG_DEBUG, "rank-1 create");
74     return ci;
75 }
76
77 /*
78  * destroy: Destroys this rank handler. This routine is called
79  *  when the handler is no longer needed - i.e. when the server
80  *  dies. The class_handle was previously returned by create.
81  */
82 static void destroy (struct zebra_register *reg, void *class_handle)
83 {
84     struct rank_class_info *ci = (struct rank_class_info *) class_handle;
85
86     yaz_log (LOG_DEBUG, "rank-1 destroy");
87     xfree (ci);
88 }
89
90
91 /*
92  * begin: Prepares beginning of "real" ranking. Called once for
93  *  each result set. The returned handle is a "set handle" and
94  *  will be used in each of the handlers below.
95  */
96 static void *begin (struct zebra_register *reg, void *class_handle, RSET rset)
97 {
98     struct rank_set_info *si = (struct rank_set_info *) xmalloc (sizeof(*si));
99     int i;
100
101 #if DEBUG_RANK
102     yaz_log (LOG_LOG, "rank-1 begin");
103 #endif
104     si->no_entries = rset->no_rset_terms;
105     si->no_rank_entries = 0;
106     si->entries = (struct rank_term_info *)
107         xmalloc (sizeof(*si->entries)*si->no_entries);
108     for (i = 0; i < si->no_entries; i++)
109     {
110         zint g = rset->rset_terms[i]->nn;
111 #if DEBUG_RANK
112         yaz_log(LOG_LOG, "i=%d flags=%s", i, rset->rset_terms[i]->flags);
113 #endif
114         if (!strncmp (rset->rset_terms[i]->flags, "rank,", 5))
115         {
116             const char *cp = strstr(rset->rset_terms[i]->flags+4, ",w=");
117             si->entries[i].rank_flag = 1;
118             if (cp)
119                 si->entries[i].rank_weight = atoi (cp+3);
120             else
121                 si->entries[i].rank_weight = 34;
122 #if DEBUG_RANK
123             yaz_log (LOG_LOG, " i=%d weight=%d", i,
124                      si->entries[i].rank_weight);
125 #endif
126             (si->no_rank_entries)++;
127         }
128         else
129             si->entries[i].rank_flag = 0;
130         si->entries[i].local_occur = 0;
131         si->entries[i].global_occur = g;
132         si->entries[i].global_inv = 32 - log2_int (g);
133         yaz_log (LOG_DEBUG, " global_inv = %d g = " ZINT_FORMAT, (int) (32-log2_int (g)), g);
134     }
135     return si;
136 }
137
138 /*
139  * end: Terminates ranking process. Called after a result set
140  *  has been ranked.
141  */
142 static void end (struct zebra_register *reg, void *set_handle)
143 {
144     struct rank_set_info *si = (struct rank_set_info *) set_handle;
145     yaz_log (LOG_DEBUG, "rank-1 end");
146     xfree (si->entries);
147     xfree (si);
148 }
149
150 /*
151  * add: Called for each word occurence in a result set. This routine
152  *  should be as fast as possible. This routine should "incrementally"
153  *  update the score.
154  */
155 static void add (void *set_handle, int seqno, int term_index)
156 {
157     struct rank_set_info *si = (struct rank_set_info *) set_handle;
158 #if DEBUG_RANK
159     yaz_log (LOG_LOG, "rank-1 add seqno=%d term_index=%d", seqno, term_index);
160 #endif
161     si->last_pos = seqno;
162     si->entries[term_index].local_occur++;
163 }
164
165 /*
166  * calc: Called for each document in a result. This handler should 
167  *  produce a score based on previous call(s) to the add handler. The
168  *  score should be between 0 and 1000. If score cannot be obtained
169  *  -1 should be returned.
170  */
171 static int calc (void *set_handle, zint sysno)
172 {
173     int i, lo, divisor, score = 0;
174     struct rank_set_info *si = (struct rank_set_info *) set_handle;
175
176     if (!si->no_rank_entries)
177         return -1;
178
179 #if DEBUG_RANK
180     yaz_log(LOG_LOG, "calc");
181 #endif
182     for (i = 0; i < si->no_entries; i++)
183     {
184 #if DEBUG_RANK
185         yaz_log(LOG_LOG, "i=%d rank_flag=%d lo=%d",
186                 i, si->entries[i].rank_flag, si->entries[i].local_occur);
187 #endif
188         if (si->entries[i].rank_flag && (lo = si->entries[i].local_occur))
189             score += (8+log2_int (lo)) * si->entries[i].global_inv *
190                 si->entries[i].rank_weight;
191     }
192     divisor = si->no_rank_entries * (8+log2_int (si->last_pos/si->no_entries));
193     score = score / divisor;
194 #if DEBUG_RANK
195     yaz_log (LOG_LOG, "sysno=" ZINT_FORMAT " score=%d", sysno, score);
196 #endif
197     if (score > 1000)
198         score = 1000;
199     for (i = 0; i < si->no_entries; i++)
200         si->entries[i].local_occur = 0;
201     return score;
202 }
203
204 /*
205  * Pseudo-meta code with sequence of calls as they occur in a
206  * server. Handlers are prefixed by --:
207  *
208  *     server init
209  *     -- create
210  *     foreach search
211  *        rank result set
212  *        -- begin
213  *        foreach record
214  *           foreach word
215  *              -- add
216  *           -- calc
217  *        -- end
218  *     -- destroy
219  *     server close
220  */
221
222 static struct rank_control rank_control = {
223     "rank-1",
224     create,
225     destroy,
226     begin,
227     end,
228     calc,
229     add,
230 };
231  
232 struct rank_control *rank1_class = &rank_control;