1 /* $Id: ranksimilarity.c,v 1.3 2006-05-04 10:11:09 marc Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
36 static int log_level = 0;
37 static int log_initialized = 0;
39 struct ranksimilarity_class_info {
43 /** term specific info and statistics to be used under ranking */
44 struct ranksimilarity_term_info {
46 /** frequency of term within document field */
47 int freq_term_docfield;
49 /** frequency of term within result set of given term */
50 zint freq_term_resset;
52 /** number of docs within result set */
55 /** number of terms in this field */
58 /** number of docs with this field in database*/
61 /** rank flag is one if term is to be included in ranking */
64 /** relative ranking weight of term */
67 /** term id used to access term name and other info */
70 /** index number in terms[i] array */
74 struct ranksimilarity_set_info {
77 /** number of terms in query */
80 /** number of terms in query which are included in ranking */
81 int no_ranked_terms_query;
83 /** number of documents in entire collection */
84 zint no_docs_database;
86 /** array of size no_terms_query with statistics gathered per term */
87 struct ranksimilarity_term_info *entries;
93 /* local clean-up function */
94 static void ranksimilar_rec_reset(struct ranksimilarity_set_info *si)
98 for (i = 0; i < si->no_terms_query; i++)
100 si->entries[i].freq_term_docfield = 0;
106 * create: Creates/Initialises this rank handler. This routine is
107 * called exactly once. The routine returns the class_handle.
109 static void *create (ZebraHandle zh)
111 struct ranksimilarity_class_info *ci =
112 (struct ranksimilarity_class_info *) xmalloc (sizeof(*ci));
114 if (!log_initialized)
116 log_level = yaz_log_module_level("rank-similarity");
119 yaz_log(log_level, "create()");
124 * destroy: Destroys this rank handler. This routine is called
125 * when the handler is no longer needed - i.e. when the server
126 * dies. The class_handle was previously returned by create.
128 static void destroy (struct zebra_register *reg, void *class_handle)
130 struct ranksimilarity_class_info *ci
131 = (struct ranksimilarity_class_info *) class_handle;
132 yaz_log(log_level, "destroy()");
138 * begin: Prepares beginning of "real" ranking. Called once for
139 * each result set. The returned handle is a "set handle" and
140 * will be used in each of the handlers below.
142 static void *begin (struct zebra_register *reg,
143 void *class_handle, RSET rset, NMEM nmem,
144 TERMID *terms, int numterms)
146 struct ranksimilarity_set_info *si =
147 (struct ranksimilarity_set_info *) nmem_malloc (nmem, sizeof(*si));
150 yaz_log(log_level, "begin() numterms=%d", numterms);
152 /* setting database global statistics */
153 si->no_docs_database = -1; /* TODO */
155 /* setting query statistics */
156 si->no_terms_query = numterms;
157 si->no_ranked_terms_query = 0;
159 /* setting internal data structures */
161 si->entries = (struct ranksimilarity_term_info *)
162 nmem_malloc (si->nmem, sizeof(*si->entries)*numterms);
164 /* reset the counts for the next term */
165 ranksimilar_rec_reset(si);
168 /* looping all terms in a specific field of query */
169 for (i = 0; i < numterms; i++)
171 struct ord_list *ol = NULL;
174 /* adding to number of rank entries */
175 if (strncmp (terms[i]->flags, "rank,", 5))
177 si->entries[i].rank_flag = 0;
178 yaz_log(log_level, "begin() terms[%d]: '%s' flags=%s not ranked",
179 i, terms[i]->name, terms[i]->flags);
183 const char *cp = strstr(terms[i]->flags+4, ",w=");
185 (si->no_ranked_terms_query)++;
187 si->entries[i].rank_flag = 1;
189 /* notice that the call to rset_count(rset) has he side-effect of setting
190 rset->hits_limit = rset_count(rset) ??? */
191 si->entries[i].freq_term_resset = rset_count(terms[i]->rset);
192 /* si->entries[i].freq_term_resset = terms[i]->rset->hits_count; */
195 yaz_log(log_level, "begin() rset_count(terms[%d]->rset) = %d",
196 i, rset_count(terms[i]->rset));
197 yaz_log(log_level, "begin() terms[%d]->rset->hits_limit = %d",
198 i, terms[i]->rset->hits_limit);
199 yaz_log(log_level, "begin() terms[%d]->rset->hits_count = %d",
200 i, terms[i]->rset->hits_count);
201 yaz_log(log_level, "begin() terms[%d]->rset->hits_round = %d",
202 i, terms[i]->rset->hits_round);
203 yaz_log(log_level, "begin() terms[%d]->rset->hits_approx = %d",
204 i, terms[i]->rset->hits_approx);
207 si->entries[i].no_docs_resset = -1; /*TODO*/
208 si->entries[i].no_docs_field = -1; /*TODO*/
209 si->entries[i].no_terms_field = -1; /*TODO*/
212 si->entries[i].term_weight = atoi (cp+3);
214 si->entries[i].term_weight = 34; /* sqrroot of 1000 */
216 yaz_log(log_level, "begin() terms[%d]: '%s' flags=%s",
217 i, terms[i]->name, terms[i]->flags);
219 /* looping indexes where term terms[i] is found */
220 for (; ol; ol = ol->next)
224 const char *string_index = 0;
228 zebraExplain_lookup_ord(reg->zei,
229 ol->ord, &index_type, &db, &set, &use,
234 "begin() index: ord=%d type=%c db=%s str-index=%s",
235 ol->ord, index_type, db, string_index);
238 "begin() index: ord=%d type=%c db=%s set=%d use=%d",
239 ol->ord, index_type, db, set, use);
244 si->entries[i].term = terms[i];
245 si->entries[i].term_index=i;
247 /* setting next entry in term */
248 terms[i]->rankpriv = &(si->entries[i]);
255 * end: Terminates ranking process. Called after a result set
258 static void end (struct zebra_register *reg, void *set_handle)
260 yaz_log(log_level, "end()");
265 * add: Called for each word occurence in a result set. This routine
266 * should be as fast as possible. This routine should "incrementally"
269 static void add (void *set_handle, int seqno, TERMID term)
271 struct ranksimilarity_set_info *si
272 = (struct ranksimilarity_set_info *) set_handle;
273 struct ranksimilarity_term_info *ti;
277 /* yaz_log(log_level, "add() seqno=%d NULL term", seqno); */
281 ti= (struct ranksimilarity_term_info *) term->rankpriv;
283 si->last_pos = seqno;
284 ti->freq_term_docfield++;
285 /* yaz_log(log_level, "add() seqno=%d term=%s freq_term_docfield=%d",
286 seqno, term->name, ti->freq_term_docfield); */
290 * calc: Called for each document in a result. This handler should
291 * produce a score based on previous call(s) to the add handler. The
292 * score should be between 0 and 1000. If score cannot be obtained
293 * -1 should be returned.
295 static int calc (void *set_handle, zint sysno, zint staticrank,
299 struct ranksimilarity_set_info *si
300 = (struct ranksimilarity_set_info *) set_handle;
303 yaz_log(log_level, "calc() sysno = %d", sysno);
304 yaz_log(log_level, "calc() staticrank = %d", staticrank);
306 yaz_log(log_level, "calc() si->no_terms_query = %d",
308 yaz_log(log_level, "calc() si->no_ranked_terms_query = %d",
309 si->no_ranked_terms_query);
310 yaz_log(log_level, "calc() si->no_docs_database = %d",
311 si->no_docs_database);
314 if (!si->no_ranked_terms_query)
315 return -1; /* ranking not enabled for any terms */
318 /* if we set *stop_flag = 1, we stop processing (of result set list) */
321 /* here goes your formula to compute a scoring function */
322 /* you may use all the gathered statistics here */
323 for (i = 0; i < si->no_terms_query; i++)
325 yaz_log(log_level, "calc() entries[%d] termid %d",
326 i, si->entries[i].term);
327 if (si->entries[i].term){
328 yaz_log(log_level, "calc() entries[%d] term '%s' flags=%s",
329 i, si->entries[i].term->name, si->entries[i].term->flags);
330 yaz_log(log_level, "calc() entries[%d] rank_flag %d",
331 i, si->entries[i].rank_flag );
332 yaz_log(log_level, "calc() entries[%d] term_weight %d",
333 i, si->entries[i].term_weight );
334 yaz_log(log_level, "calc() entries[%d] freq_term_docfield %d",
335 i, si->entries[i].freq_term_docfield );
336 yaz_log(log_level, "calc() entries[%d] freq_term_resset %d",
337 i, si->entries[i].freq_term_resset );
338 yaz_log(log_level, "calc() entries[%d] no_docs_resset %d",
339 i, si->entries[i].no_docs_resset );
340 yaz_log(log_level, "calc() entries[%d] no_docs_field %d",
341 i, si->entries[i].no_docs_field );
342 yaz_log(log_level, "calc() entries[%d] no_terms_field %d",
343 i, si->entries[i].no_terms_field );
348 /* reset the counts for the next term */
349 ranksimilar_rec_reset(si);
352 /* staticrank = 0 is highest, MAXINT lowest */
353 score = INT_MAX - staticrank; /* but score is reverse (logical) */
356 /* debugging statistics output */
357 yaz_log(log_level, "calc() statistics: score = %d", score);
363 * Pseudo-meta code with sequence of calls as they occur in a
364 * server. Handlers are prefixed by --:
380 static struct rank_control rank_control = {
390 struct rank_control *rank_similarity_class = &rank_control;