2 * Copyright (C) 1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.3 1998-06-12 12:21:53 adam
10 * Revision 1.2 1998/03/05 13:03:29 adam
13 * Revision 1.1 1998/03/05 08:45:12 adam
14 * New result set model and modular ranking system. Moved towards
15 * descent server API. System information stored as "SGML" records.
29 struct rank_class_info {
33 struct rank_term_info {
40 struct rank_set_info {
44 struct rank_term_info *entries;
47 static int log2_int (unsigned g)
56 * create: Creates/Initialises this rank handler. This routine is
57 * called exactly once. The routine returns the class_handle.
59 static void *create (ZebraHandle zh)
61 struct rank_class_info *ci = xmalloc (sizeof(*ci));
63 logf (LOG_DEBUG, "rank-1 create");
68 * destroy: Destroys this rank handler. This routine is called
69 * when the handler is no longer needed - i.e. when the server
70 * dies. The class_handle was previously returned by create.
72 static void destroy (ZebraHandle zh, void *class_handle)
74 struct rank_class_info *ci = class_handle;
76 logf (LOG_DEBUG, "rank-1 destroy");
82 * begin: Prepares beginning of "real" ranking. Called once for
83 * each result set. The returned handle is a "set handle" and
84 * will be used in each of the handlers below.
86 static void *begin (ZebraHandle zh, void *class_handle, RSET rset)
88 struct rank_set_info *si = xmalloc (sizeof(*si));
91 logf (LOG_DEBUG, "rank-1 begin");
92 si->no_entries = rset->no_rset_terms;
93 si->no_rank_entries = 0;
94 si->entries = xmalloc (sizeof(*si->entries)*si->no_entries);
95 for (i = 0; i < si->no_entries; i++)
97 int g = rset->rset_terms[i]->nn;
98 if (!strcmp (rset->rset_terms[i]->flags, "rank"))
100 si->entries[i].rank_flag = 1;
101 (si->no_rank_entries)++;
104 si->entries[i].rank_flag = 0;
105 si->entries[i].local_occur = 0;
106 si->entries[i].global_occur = g;
107 si->entries[i].global_inv = 32 - log2_int (g);
108 logf (LOG_DEBUG, "-------- %d ------", 32 - log2_int (g));
114 * end: Terminates ranking process. Called after a result set
117 static void end (ZebraHandle zh, void *set_handle)
119 struct rank_set_info *si = set_handle;
120 logf (LOG_DEBUG, "rank-1 end");
126 * add: Called for each word occurence in a result set. This routine
127 * should be as fast as possible. This routine should "incrementally"
130 static void add (void *set_handle, int seqno, int term_index)
132 struct rank_set_info *si = set_handle;
133 logf (LOG_DEBUG, "rank-1 add seqno=%d term_index=%d", seqno, term_index);
134 si->last_pos = seqno;
135 si->entries[term_index].local_occur++;
139 * calc: Called for each document in a result. This handler should
140 * produce a score based on previous call(s) to the add handler. The
141 * score should be between 0 and 1000. If score cannot be obtained
142 * -1 should be returned.
144 static int calc (void *set_handle, int sysno)
146 int i, lo, divisor, score = 0;
147 struct rank_set_info *si = set_handle;
149 logf (LOG_DEBUG, "rank-1 calc sysno=%d", sysno);
151 if (!si->no_rank_entries)
153 for (i = 0; i < si->no_entries; i++)
154 if (si->entries[i].rank_flag && (lo = si->entries[i].local_occur))
155 score += (8+log2_int (lo)) * si->entries[i].global_inv;
157 divisor = si->no_rank_entries * (8+log2_int (si->last_pos/si->no_entries));
158 score = score / divisor;
161 for (i = 0; i < si->no_entries; i++)
162 si->entries[i].local_occur = 0;
167 * Pseudo-meta code with sequence of calls as they occur in a
168 * server. Handlers are prefixed by --:
184 static struct rank_control rank_control = {
194 struct rank_control *rank1_class = &rank_control;