Removed livcode ranking
[idzebra-moved-to-github.git] / index / rank1.c
1 /* $Id: rank1.c,v 1.19 2004-10-28 10:37:15 heikki 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     TERMID term;
48     int term_index;
49 };
50
51 struct rank_set_info {
52     int last_pos;
53     int no_entries;
54     int no_rank_entries;
55     struct rank_term_info *entries;
56     NMEM nmem;
57 };
58
59 static int log2_int (unsigned g)
60 {
61     int n = 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     yaz_log (LOG_DEBUG, "rank-1 create");
77     return ci;
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 rank_class_info *ci = (struct rank_class_info *) class_handle;
88
89     yaz_log (LOG_DEBUG, "rank-1 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 rank_set_info *si = 
104         (struct rank_set_info *) nmem_malloc (nmem,sizeof(*si));
105     int i;
106
107 #if DEBUG_RANK
108     yaz_log (LOG_LOG, "rank-1 begin");
109 #endif
110     si->no_entries = numterms;
111     si->no_rank_entries = 0;
112     si->nmem=nmem;
113     si->entries = (struct rank_term_info *)
114         nmem_malloc (si->nmem, sizeof(*si->entries)*numterms); 
115     for (i = 0; i < numterms; i++)
116     {
117         zint g = rset_count(terms[i]->rset);
118 #if DEBUG_RANK
119         yaz_log(LOG_LOG, "i=%d flags=%s '%s'", i, 
120                 terms[i]->flags, terms[i]->name );
121 #endif
122         if  (!strncmp (terms[i]->flags, "rank,", 5)) 
123         {
124             const char *cp = strstr(terms[i]->flags+4, ",w=");
125             si->entries[i].rank_flag = 1;
126             if (cp)
127                 si->entries[i].rank_weight = atoi (cp+3);
128             else
129                 si->entries[i].rank_weight = 34;
130 #if DEBUG_RANK
131             yaz_log (LOG_LOG, " i=%d weight=%d g="ZINT_FORMAT, i,
132                      si->entries[i].rank_weight, g);
133 #endif
134             (si->no_rank_entries)++;
135         }
136         else
137             si->entries[i].rank_flag = 0;
138         si->entries[i].local_occur = 0;  /* FIXME */
139         si->entries[i].global_occur = g;
140         si->entries[i].global_inv = 32 - log2_int (g);
141         yaz_log (LOG_DEBUG, " global_inv = %d g = " ZINT_FORMAT, 
142                 (int) (32-log2_int (g)), g);
143         si->entries[i].term=terms[i];
144         si->entries[i].term_index=i;
145         terms[i]->rankpriv=&(si->entries[i]);
146     }
147     return si;
148 }
149
150 /*
151  * end: Terminates ranking process. Called after a result set
152  *  has been ranked.
153  */
154 static void end (struct zebra_register *reg, void *set_handle)
155 {
156     yaz_log (LOG_DEBUG, "rank-1 end");
157     /* no need to free anything, they are in nmems */
158 }
159
160
161 /**
162  * add: Called for each word occurence in a result set. This routine
163  *  should be as fast as possible. This routine should "incrementally"
164  *  update the score.
165  */
166 static void add (void *set_handle, int seqno, TERMID term)
167 {
168     struct rank_set_info *si = (struct rank_set_info *) set_handle;
169     struct rank_term_info *ti= (struct rank_term_info *) term->rankpriv;
170     assert(si);
171     assert(term);
172     assert(ti);
173 #if DEBUG_RANK
174     yaz_log (LOG_LOG, "rank-1 add seqno=%d term=%s", seqno, term->name);
175 #endif
176     si->last_pos = seqno;
177     ti->local_occur++;
178 }
179
180 /*
181  * calc: Called for each document in a result. This handler should 
182  *  produce a score based on previous call(s) to the add handler. The
183  *  score should be between 0 and 1000. If score cannot be obtained
184  *  -1 should be returned.
185  */
186 static int calc (void *set_handle, zint sysno)
187 {
188     int i, lo, divisor, score = 0;
189     struct rank_set_info *si = (struct rank_set_info *) set_handle;
190
191     if (!si->no_rank_entries)
192         return -1;
193
194 #if DEBUG_RANK
195     yaz_log(LOG_LOG, "calc");
196 #endif
197     for (i = 0; i < si->no_entries; i++)
198     {
199 #if DEBUG_RANK
200         yaz_log(LOG_LOG, "calc: i=%d rank_flag=%d lo=%d",
201                 i, si->entries[i].rank_flag, si->entries[i].local_occur);
202 #endif
203         if (si->entries[i].rank_flag && (lo = si->entries[i].local_occur))
204             score += (8+log2_int (lo)) * si->entries[i].global_inv *
205                 si->entries[i].rank_weight;
206     }
207     divisor = si->no_rank_entries * (8+log2_int (si->last_pos/si->no_entries));
208     score = score / divisor;
209 #if DEBUG_RANK
210     yaz_log (LOG_LOG, "calc sysno=" ZINT_FORMAT " score=%d", sysno, score);
211 #endif
212     if (score > 1000)
213         score = 1000;
214     for (i = 0; i < si->no_entries; i++)
215         si->entries[i].local_occur = 0;
216     return score;
217 }
218
219 /*
220  * Pseudo-meta code with sequence of calls as they occur in a
221  * server. Handlers are prefixed by --:
222  *
223  *     server init
224  *     -- create
225  *     foreach search
226  *        rank result set
227  *        -- begin
228  *        foreach record
229  *           foreach word
230  *              -- add
231  *           -- calc
232  *        -- end
233  *     -- destroy
234  *     server close
235  */
236
237 static struct rank_control rank_control = {
238     "rank-1",
239     create,
240     destroy,
241     begin,
242     end,
243     calc,
244     add,
245 };
246  
247 struct rank_control *rank1_class = &rank_control;