2 * Copyright (C) 1998-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1999-02-02 14:51:01 adam
8 * Updated WIN32 code specific sections. Changed header.
10 * Revision 1.3 1998/06/12 12:21:53 adam
13 * Revision 1.2 1998/03/05 13:03:29 adam
16 * Revision 1.1 1998/03/05 08:45:12 adam
17 * New result set model and modular ranking system. Moved towards
18 * descent server API. System information stored as "SGML" records.
32 struct rank_class_info {
36 struct rank_term_info {
43 struct rank_set_info {
47 struct rank_term_info *entries;
50 static int log2_int (unsigned g)
59 * create: Creates/Initialises this rank handler. This routine is
60 * called exactly once. The routine returns the class_handle.
62 static void *create (ZebraHandle zh)
64 struct rank_class_info *ci = xmalloc (sizeof(*ci));
66 logf (LOG_DEBUG, "rank-1 create");
71 * destroy: Destroys this rank handler. This routine is called
72 * when the handler is no longer needed - i.e. when the server
73 * dies. The class_handle was previously returned by create.
75 static void destroy (ZebraHandle zh, void *class_handle)
77 struct rank_class_info *ci = class_handle;
79 logf (LOG_DEBUG, "rank-1 destroy");
85 * begin: Prepares beginning of "real" ranking. Called once for
86 * each result set. The returned handle is a "set handle" and
87 * will be used in each of the handlers below.
89 static void *begin (ZebraHandle zh, void *class_handle, RSET rset)
91 struct rank_set_info *si = xmalloc (sizeof(*si));
94 logf (LOG_DEBUG, "rank-1 begin");
95 si->no_entries = rset->no_rset_terms;
96 si->no_rank_entries = 0;
97 si->entries = xmalloc (sizeof(*si->entries)*si->no_entries);
98 for (i = 0; i < si->no_entries; i++)
100 int g = rset->rset_terms[i]->nn;
101 if (!strcmp (rset->rset_terms[i]->flags, "rank"))
103 si->entries[i].rank_flag = 1;
104 (si->no_rank_entries)++;
107 si->entries[i].rank_flag = 0;
108 si->entries[i].local_occur = 0;
109 si->entries[i].global_occur = g;
110 si->entries[i].global_inv = 32 - log2_int (g);
111 logf (LOG_DEBUG, "-------- %d ------", 32 - log2_int (g));
117 * end: Terminates ranking process. Called after a result set
120 static void end (ZebraHandle zh, void *set_handle)
122 struct rank_set_info *si = set_handle;
123 logf (LOG_DEBUG, "rank-1 end");
129 * add: Called for each word occurence in a result set. This routine
130 * should be as fast as possible. This routine should "incrementally"
133 static void add (void *set_handle, int seqno, int term_index)
135 struct rank_set_info *si = set_handle;
136 logf (LOG_DEBUG, "rank-1 add seqno=%d term_index=%d", seqno, term_index);
137 si->last_pos = seqno;
138 si->entries[term_index].local_occur++;
142 * calc: Called for each document in a result. This handler should
143 * produce a score based on previous call(s) to the add handler. The
144 * score should be between 0 and 1000. If score cannot be obtained
145 * -1 should be returned.
147 static int calc (void *set_handle, int sysno)
149 int i, lo, divisor, score = 0;
150 struct rank_set_info *si = set_handle;
152 logf (LOG_DEBUG, "rank-1 calc sysno=%d", sysno);
154 if (!si->no_rank_entries)
156 for (i = 0; i < si->no_entries; i++)
157 if (si->entries[i].rank_flag && (lo = si->entries[i].local_occur))
158 score += (8+log2_int (lo)) * si->entries[i].global_inv;
160 divisor = si->no_rank_entries * (8+log2_int (si->last_pos/si->no_entries));
161 score = score / divisor;
164 for (i = 0; i < si->no_entries; i++)
165 si->entries[i].local_occur = 0;
170 * Pseudo-meta code with sequence of calls as they occur in a
171 * server. Handlers are prefixed by --:
187 static struct rank_control rank_control = {
197 struct rank_control *rank1_class = &rank_control;