2 * Copyright (C) 1998-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 2000-03-15 15:00:30 adam
8 * First work on threaded version.
10 * Revision 1.5 1999/05/26 07:49:13 adam
13 * Revision 1.4 1999/02/02 14:51:01 adam
14 * Updated WIN32 code specific sections. Changed header.
16 * Revision 1.3 1998/06/12 12:21:53 adam
19 * Revision 1.2 1998/03/05 13:03:29 adam
22 * Revision 1.1 1998/03/05 08:45:12 adam
23 * New result set model and modular ranking system. Moved towards
24 * descent server API. System information stored as "SGML" records.
38 struct rank_class_info {
42 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 (ZebraService zh)
70 struct rank_class_info *ci = (struct rank_class_info *)
71 xmalloc (sizeof(*ci));
73 logf (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 (ZebraService zh, void *class_handle)
84 struct rank_class_info *ci = (struct rank_class_info *) class_handle;
86 logf (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 (ZebraHandle zh, void *class_handle, RSET rset)
98 struct rank_set_info *si = (struct rank_set_info *) xmalloc (sizeof(*si));
101 logf (LOG_DEBUG, "rank-1 begin");
102 si->no_entries = rset->no_rset_terms;
103 si->no_rank_entries = 0;
104 si->entries = (struct rank_term_info *)
105 xmalloc (sizeof(*si->entries)*si->no_entries);
106 for (i = 0; i < si->no_entries; i++)
108 int g = rset->rset_terms[i]->nn;
109 if (!strcmp (rset->rset_terms[i]->flags, "rank"))
111 si->entries[i].rank_flag = 1;
112 (si->no_rank_entries)++;
115 si->entries[i].rank_flag = 0;
116 si->entries[i].local_occur = 0;
117 si->entries[i].global_occur = g;
118 si->entries[i].global_inv = 32 - log2_int (g);
119 logf (LOG_DEBUG, "-------- %d ------", 32 - log2_int (g));
125 * end: Terminates ranking process. Called after a result set
128 static void end (ZebraHandle zh, void *set_handle)
130 struct rank_set_info *si = (struct rank_set_info *) set_handle;
131 logf (LOG_DEBUG, "rank-1 end");
137 * add: Called for each word occurence in a result set. This routine
138 * should be as fast as possible. This routine should "incrementally"
141 static void add (void *set_handle, int seqno, int term_index)
143 struct rank_set_info *si = (struct rank_set_info *) set_handle;
144 logf (LOG_DEBUG, "rank-1 add seqno=%d term_index=%d", seqno, term_index);
145 si->last_pos = seqno;
146 si->entries[term_index].local_occur++;
150 * calc: Called for each document in a result. This handler should
151 * produce a score based on previous call(s) to the add handler. The
152 * score should be between 0 and 1000. If score cannot be obtained
153 * -1 should be returned.
155 static int calc (void *set_handle, int sysno)
157 int i, lo, divisor, score = 0;
158 struct rank_set_info *si = (struct rank_set_info *) set_handle;
160 logf (LOG_DEBUG, "rank-1 calc sysno=%d", sysno);
162 if (!si->no_rank_entries)
164 for (i = 0; i < si->no_entries; i++)
165 if (si->entries[i].rank_flag && (lo = si->entries[i].local_occur))
166 score += (8+log2_int (lo)) * si->entries[i].global_inv;
168 divisor = si->no_rank_entries * (8+log2_int (si->last_pos/si->no_entries));
169 score = score / divisor;
172 for (i = 0; i < si->no_entries; i++)
173 si->entries[i].local_occur = 0;
178 * Pseudo-meta code with sequence of calls as they occur in a
179 * server. Handlers are prefixed by --:
195 static struct rank_control rank_control = {
205 struct rank_control *rank1_class = &rank_control;