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