2 * Copyright (C) 1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.2 1998-03-05 13:03:29 adam
10 * Revision 1.1 1998/03/05 08:45:12 adam
11 * New result set model and modular ranking system. Moved towards
12 * descent server API. System information stored as "SGML" records.
26 struct rank_class_info {
30 struct rank_term_info {
37 struct rank_set_info {
41 struct rank_term_info *entries;
44 static int log2_int (unsigned g)
53 * create: Creates/Initialises this rank handler. This routine is
54 * called exactly once. The routine returns the class_handle.
56 static void *create (ZebraHandle zh)
58 struct rank_class_info *ci = xmalloc (sizeof(*ci));
60 logf (LOG_DEBUG, "rank-1 create");
65 * destroy: Destroys this rank handler. This routine is called
66 * when the handler is no longer needed - i.e. when the server
67 * dies. The class_handle was previously returned by create.
69 static void destroy (ZebraHandle zh, void *class_handle)
71 struct rank_class_info *ci = class_handle;
73 logf (LOG_DEBUG, "rank-1 destroy");
79 * begin: Prepares beginning of "real" ranking. Called once for
80 * each result set. The returned handle is a "set handle" and
81 * will be used in each of the handlers below.
83 static void *begin (ZebraHandle zh, void *class_handle, RSET rset)
85 struct rank_set_info *si = xmalloc (sizeof(*si));
88 logf (LOG_DEBUG, "rank-1 begin");
89 si->no_entries = rset->no_rset_terms;
90 si->no_rank_entries = 0;
91 si->entries = xmalloc (sizeof(*si->entries)*si->no_entries);
92 for (i = 0; i < si->no_entries; i++)
94 int g = rset->rset_terms[i]->nn;
95 if (!strcmp (rset->rset_terms[i]->flags, "rank"))
97 si->entries[i].rank_flag = 1;
98 (si->no_rank_entries)++;
101 si->entries[i].rank_flag = 0;
102 si->entries[i].local_occur = 0;
103 si->entries[i].global_occur = g;
104 si->entries[i].global_inv = 32 - log2_int (g);
105 logf (LOG_DEBUG, "-------- %d ------", 32 - log2_int (g));
111 * end: Terminates ranking process. Called after a result set
114 static void end (ZebraHandle zh, void *set_handle)
116 struct rank_set_info *si = set_handle;
117 logf (LOG_DEBUG, "rank-1 end");
122 * add: Called for each word occurence in a result set. This routine
123 * should be as fast as possible. This routine should "incrementally"
126 static void add (void *set_handle, int seqno, int term_index)
128 struct rank_set_info *si = set_handle;
129 logf (LOG_DEBUG, "rank-1 add seqno=%d term_index=%d", seqno, term_index);
130 si->last_pos = seqno;
131 si->entries[term_index].local_occur++;
135 * calc: Called for each document in a result. This handler should
136 * produce a score based on previous call(s) to the add handler. The
137 * score should be between 0 and 1000. If score cannot be obtained
138 * -1 should be returned.
140 static int calc (void *set_handle, int sysno)
142 int i, lo, divisor, score = 0;
143 struct rank_set_info *si = set_handle;
145 logf (LOG_DEBUG, "rank-1 calc sysno=%d", sysno);
147 if (!si->no_rank_entries)
149 for (i = 0; i < si->no_entries; i++)
150 if (si->entries[i].rank_flag && (lo = si->entries[i].local_occur))
151 score += (8+log2_int (lo)) * si->entries[i].global_inv;
153 divisor = si->no_rank_entries * (8+log2_int (si->last_pos/si->no_entries));
154 score = score / divisor;
157 for (i = 0; i < si->no_entries; i++)
158 si->entries[i].local_occur = 0;
163 * Pseudo-meta code with sequence of calls as they occur in a
164 * server. Handlers are prefixed by --:
180 static struct rank_control rank_control = {
190 struct rank_control *rank1_class = &rank_control;