1 /* $Id: rank1.c,v 1.17 2004-08-20 14:44:46 heikki Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
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
37 struct rank_class_info {
41 struct rank_term_info {
49 struct rank_set_info {
53 struct rank_term_info *entries;
56 static int log2_int (unsigned g)
65 * create: Creates/Initialises this rank handler. This routine is
66 * called exactly once. The routine returns the class_handle.
68 static void *create (ZebraHandle zh)
70 struct rank_class_info *ci = (struct rank_class_info *)
71 xmalloc (sizeof(*ci));
73 yaz_log (LOG_DEBUG, "rank-1 create");
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.
82 static void destroy (struct zebra_register *reg, void *class_handle)
84 struct rank_class_info *ci = (struct rank_class_info *) class_handle;
86 yaz_log (LOG_DEBUG, "rank-1 destroy");
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.
96 static void *begin (struct zebra_register *reg, void *class_handle, RSET rset)
98 struct rank_set_info *si = (struct rank_set_info *) xmalloc (sizeof(*si));
102 yaz_log (LOG_LOG, "rank-1 begin");
104 si->no_entries = 0; /* rset->no_rset_terms; */ /* FIXME - what to do here*/
105 /* Now that we don't count term occurrences, ranking will have to */
107 si->no_rank_entries = 0;
108 si->entries = (struct rank_term_info *)
109 xmalloc (sizeof(*si->entries)*si->no_entries);
110 for (i = 0; i < si->no_entries; i++)
112 zint g = 0; /* rset->rset_terms[i]->nn; */ /* FIXME ??? */
114 yaz_log(LOG_LOG, "i=%d flags=%s", i, rset->rset_terms[i]->flags);
116 if (0) /* (!strncmp (rset->rset_terms[i]->flags, "rank,", 5)) */
117 /* FIXME */ /* ??? */
119 const char *cp = "w";
120 /*= strstr(rset->rset_terms[i]->flags+4, ",w=");*/
122 si->entries[i].rank_flag = 1;
124 si->entries[i].rank_weight = atoi (cp+3);
126 si->entries[i].rank_weight = 34;
128 yaz_log (LOG_LOG, " i=%d weight=%d", i,
129 si->entries[i].rank_weight);
131 (si->no_rank_entries)++;
134 si->entries[i].rank_flag = 0;
135 si->entries[i].local_occur = 0;
136 si->entries[i].global_occur = g;
137 si->entries[i].global_inv = 32 - log2_int (g);
138 yaz_log (LOG_DEBUG, " global_inv = %d g = " ZINT_FORMAT, (int) (32-log2_int (g)), g);
144 * end: Terminates ranking process. Called after a result set
147 static void end (struct zebra_register *reg, void *set_handle)
149 struct rank_set_info *si = (struct rank_set_info *) set_handle;
150 yaz_log (LOG_DEBUG, "rank-1 end");
156 * add: Called for each word occurence in a result set. This routine
157 * should be as fast as possible. This routine should "incrementally"
160 static void add (void *set_handle, int seqno, int term_index)
162 struct rank_set_info *si = (struct rank_set_info *) set_handle;
164 yaz_log (LOG_LOG, "rank-1 add seqno=%d term_index=%d", seqno, term_index);
166 si->last_pos = seqno;
167 si->entries[term_index].local_occur++;
171 * calc: Called for each document in a result. This handler should
172 * produce a score based on previous call(s) to the add handler. The
173 * score should be between 0 and 1000. If score cannot be obtained
174 * -1 should be returned.
176 static int calc (void *set_handle, zint sysno)
178 int i, lo, divisor, score = 0;
179 struct rank_set_info *si = (struct rank_set_info *) set_handle;
181 if (!si->no_rank_entries)
185 yaz_log(LOG_LOG, "calc");
187 for (i = 0; i < si->no_entries; i++)
190 yaz_log(LOG_LOG, "i=%d rank_flag=%d lo=%d",
191 i, si->entries[i].rank_flag, si->entries[i].local_occur);
193 if (si->entries[i].rank_flag && (lo = si->entries[i].local_occur))
194 score += (8+log2_int (lo)) * si->entries[i].global_inv *
195 si->entries[i].rank_weight;
197 divisor = si->no_rank_entries * (8+log2_int (si->last_pos/si->no_entries));
198 score = score / divisor;
200 yaz_log (LOG_LOG, "sysno=" ZINT_FORMAT " score=%d", sysno, score);
204 for (i = 0; i < si->no_entries; i++)
205 si->entries[i].local_occur = 0;
210 * Pseudo-meta code with sequence of calls as they occur in a
211 * server. Handlers are prefixed by --:
227 static struct rank_control rank_control = {
237 struct rank_control *rank1_class = &rank_control;