2 * Copyright (C) 1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.1 1998-03-05 08:45:12 adam
8 * New result set model and modular ranking system. Moved towards
9 * descent server API. System information stored as "SGML" records.
23 struct rank_class_info {
27 struct rank_term_info {
34 struct rank_set_info {
38 struct rank_term_info *entries;
41 static int log2_int (unsigned g)
50 * create: Creates/Initialises this rank handler. This routine is
51 * called exactly once. The routine returns the class_handle.
53 static void *create (ZebraHandle zh)
55 struct rank_class_info *ci = xmalloc (sizeof(*ci));
57 logf (LOG_DEBUG, "rank-1 create");
62 * destroy: Destroys this rank handler. This routine is called
63 * when the handler is no longer needed - i.e. when the server
64 * dies. The class_handle was previously returned by create.
66 static void destroy (ZebraHandle zh, void *class_handle)
68 struct rank_class_info *ci = class_handle;
70 logf (LOG_DEBUG, "rank-1 destroy");
76 * begin: Prepares beginning of "real" ranking. Called once for
77 * each result set. The returned handle is a "set handle" and
78 * will be used in each of the handlers below.
80 static void *begin (ZebraHandle zh, void *class_handle, RSET rset)
82 struct rank_set_info *si = xmalloc (sizeof(*si));
85 logf (LOG_DEBUG, "rank-1 begin");
86 si->no_entries = rset->no_rset_terms;
87 si->no_rank_entries = 0;
88 si->entries = xmalloc (sizeof(*si->entries)*si->no_entries);
89 for (i = 0; i < si->no_entries; i++)
91 int g = rset->rset_terms[i]->nn;
92 if (!strcmp (rset->rset_terms[i]->flags, "rank"))
94 si->entries[i].rank_flag = 1;
95 (si->no_rank_entries)++;
98 si->entries[i].rank_flag = 0;
99 si->entries[i].local_occur = 0;
100 si->entries[i].global_occur = g;
101 si->entries[i].global_inv = 32 - log2_int (g);
102 logf (LOG_DEBUG, "-------- %d ------", 32 - log2_int (g));
108 * end: Terminates ranking process. Called after a result set
111 static void end (ZebraHandle zh, void *set_handle)
113 struct rank_set_info *si = set_handle;
114 logf (LOG_DEBUG, "rank-1 end");
119 * add: Called for each word occurence in a result set. This routine
120 * should be as fast as possible. This routine should "incrementally"
123 static void add (void *set_handle, int seqno, int term_index)
125 struct rank_set_info *si = set_handle;
126 logf (LOG_DEBUG, "rank-1 add seqno=%d term_index=%d", seqno, term_index);
127 si->last_pos = seqno;
128 si->entries[term_index].local_occur++;
132 * calc: Called for each document in a result. This handler should
133 * produce a score based on previous call(s) to the add handler. The
134 * score should be between 0 and 1000. If score cannot be obtained
135 * -1 should be returned.
137 static int calc (void *set_handle, int sysno)
139 int i, lu, score = 0;
140 struct rank_set_info *si = set_handle;
142 logf (LOG_DEBUG, "rank-1 calc sysno=%d", sysno);
144 if (!si->no_rank_entries)
146 for (i = 0; i < si->no_entries; i++)
147 if (si->entries[i].rank_flag && (lu = si->entries[i].local_occur))
148 score += (2+log2_int (lu)) * si->entries[i].global_inv;
149 logf (LOG_DEBUG, " dividend=%d", 60*score);
150 logf (LOG_DEBUG, " divisor=%d", si->no_rank_entries * log2_int (4+si->last_pos));
151 score = (60 * score)/(si->no_rank_entries * log2_int (4+si->last_pos));
152 for (i = 0; i < si->no_entries; i++)
153 si->entries[i].local_occur = 0;
158 * Pseudo-meta code with sequence of calls as they occur in a
159 * server. Handlers are prefixed by --:
175 static struct rank_control rank_control = {
185 struct rank_control *rank1_class = &rank_control;