1 /* $Id: rank1.c,v 1.27 2005-08-19 11:04:23 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
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
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
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
34 static int log_level = 0;
35 static int log_initialized = 0;
37 struct rank_class_info {
41 struct rank_term_info {
51 struct rank_set_info {
55 struct rank_term_info *entries;
59 static int log2_int (zint g)
70 * create: Creates/Initialises this rank handler. This routine is
71 * called exactly once. The routine returns the class_handle.
73 static void *create (ZebraHandle zh)
75 struct rank_class_info *ci =
76 (struct rank_class_info *) xmalloc (sizeof(*ci));
80 log_level = yaz_log_module_level("rank1");
83 yaz_log(log_level, "rank-1 create");
88 * destroy: Destroys this rank handler. This routine is called
89 * when the handler is no longer needed - i.e. when the server
90 * dies. The class_handle was previously returned by create.
92 static void destroy (struct zebra_register *reg, void *class_handle)
94 struct rank_class_info *ci = (struct rank_class_info *) class_handle;
96 yaz_log(log_level, "rank-1 destroy");
102 * begin: Prepares beginning of "real" ranking. Called once for
103 * each result set. The returned handle is a "set handle" and
104 * will be used in each of the handlers below.
106 static void *begin (struct zebra_register *reg,
107 void *class_handle, RSET rset, NMEM nmem,
108 TERMID *terms, int numterms)
110 struct rank_set_info *si =
111 (struct rank_set_info *) nmem_malloc (nmem,sizeof(*si));
114 yaz_log(log_level, "rank-1 begin");
115 si->no_entries = numterms;
116 si->no_rank_entries = 0;
118 si->entries = (struct rank_term_info *)
119 nmem_malloc (si->nmem, sizeof(*si->entries)*numterms);
120 for (i = 0; i < numterms; i++)
122 zint g = rset_count(terms[i]->rset);
123 yaz_log(log_level, "i=%d flags=%s '%s'", i,
124 terms[i]->flags, terms[i]->name );
125 if (!strncmp (terms[i]->flags, "rank,", 5))
127 const char *cp = strstr(terms[i]->flags+4, ",w=");
128 si->entries[i].rank_flag = 1;
130 si->entries[i].rank_weight = atoi (cp+3);
132 si->entries[i].rank_weight = 34;
133 yaz_log(log_level, " i=%d weight=%d g="ZINT_FORMAT, i,
134 si->entries[i].rank_weight, g);
135 (si->no_rank_entries)++;
138 si->entries[i].rank_flag = 0;
139 si->entries[i].local_occur = 0; /* FIXME */
140 si->entries[i].global_occur = g;
141 si->entries[i].global_inv = 32 - log2_int (g);
142 yaz_log(log_level, " global_inv = %d g = " ZINT_FORMAT,
143 (int) (32-log2_int (g)), g);
144 si->entries[i].term = terms[i];
145 si->entries[i].term_index=i;
146 terms[i]->rankpriv = &(si->entries[i]);
152 * end: Terminates ranking process. Called after a result set
155 static void end (struct zebra_register *reg, void *set_handle)
157 yaz_log(log_level, "rank-1 end");
158 /* no need to free anything, they are in nmems */
163 * add: Called for each word occurence in a result set. This routine
164 * should be as fast as possible. This routine should "incrementally"
167 static void add (void *set_handle, int seqno, TERMID term)
169 struct rank_set_info *si = (struct rank_set_info *) set_handle;
170 struct rank_term_info *ti;
174 yaz_log(log_level, "rank-1 add NULL term");
177 ti= (struct rank_term_info *) term->rankpriv;
179 si->last_pos = seqno;
181 yaz_log(log_level, "rank-1 add seqno=%d term=%s count=%d",
182 seqno, term->name,ti->local_occur);
186 * calc: Called for each document in a result. This handler should
187 * produce a score based on previous call(s) to the add handler. The
188 * score should be between 0 and 1000. If score cannot be obtained
189 * -1 should be returned.
191 static int calc (void *set_handle, zint sysno, zint staticrank,
194 int i, lo, divisor, score = 0;
195 struct rank_set_info *si = (struct rank_set_info *) set_handle;
197 if (!si->no_rank_entries)
198 return -1; /* ranking not enabled for any terms */
200 for (i = 0; i < si->no_entries; i++)
202 yaz_log(log_level, "calc: i=%d rank_flag=%d lo=%d",
203 i, si->entries[i].rank_flag, si->entries[i].local_occur);
204 if (si->entries[i].rank_flag && (lo = si->entries[i].local_occur))
205 score += (8+log2_int (lo)) * si->entries[i].global_inv *
206 si->entries[i].rank_weight;
208 divisor = si->no_rank_entries * (8+log2_int (si->last_pos/si->no_entries));
209 score = score / divisor;
210 yaz_log(log_level, "calc sysno=" ZINT_FORMAT " score=%d", sysno, score);
213 /* reset the counts for the next term */
214 for (i = 0; i < si->no_entries; i++)
215 si->entries[i].local_occur = 0;
220 * Pseudo-meta code with sequence of calls as they occur in a
221 * server. Handlers are prefixed by --:
237 static struct rank_control rank_control = {
247 struct rank_control *rank_1_class = &rank_control;