1 /* $Id: ranksimilarity.c,v 1.8 2006-05-11 10:26:13 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 docs with this fieldindex in database */
56 zint no_docs_fieldindex;
58 /** number of terms in this fieldindex */
59 zint no_terms_fieldindex;
61 /** rank flag is one if term is to be included in ranking */
64 /** relative ranking weight of term fieldindex */
65 int fieldindex_weight;
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 database */
84 zint no_docs_database;
86 /** number of terms in entire database */
87 zint no_terms_database;
89 /** array of size no_terms_query with statistics gathered per term */
90 struct ranksimilarity_term_info *entries;
96 /* local clean-up function */
97 static void ranksimilar_rec_reset(struct ranksimilarity_set_info *si)
101 for (i = 0; i < si->no_terms_query; i++)
103 si->entries[i].freq_term_docfield = 0;
109 * create: Creates/Initialises this rank handler. This routine is
110 * called exactly once. The routine returns the class_handle.
112 static void *create (ZebraHandle zh)
114 struct ranksimilarity_class_info *ci =
115 (struct ranksimilarity_class_info *) xmalloc (sizeof(*ci));
117 if (!log_initialized)
119 log_level = yaz_log_module_level("rank-similarity");
122 yaz_log(log_level, "create()");
127 * destroy: Destroys this rank handler. This routine is called
128 * when the handler is no longer needed - i.e. when the server
129 * dies. The class_handle was previously returned by create.
131 static void destroy (struct zebra_register *reg, void *class_handle)
133 struct ranksimilarity_class_info *ci
134 = (struct ranksimilarity_class_info *) class_handle;
135 yaz_log(log_level, "destroy()");
141 * begin: Prepares beginning of "real" ranking. Called once for
142 * each result set. The returned handle is a "set handle" and
143 * will be used in each of the handlers below.
145 static void *begin (struct zebra_register *reg,
146 void *class_handle, RSET rset, NMEM nmem,
147 TERMID *terms, int numterms)
149 struct ranksimilarity_set_info *si =
150 (struct ranksimilarity_set_info *) nmem_malloc (nmem, sizeof(*si));
153 yaz_log(log_level, "begin() numterms=%d", numterms);
155 /* setting database global statistics */
156 si->no_docs_database = -1; /* TODO */
157 si->no_terms_database = -1; /* TODO */
159 /* setting query statistics */
160 si->no_terms_query = numterms;
161 si->no_ranked_terms_query = 0;
163 /* setting internal data structures */
165 si->entries = (struct ranksimilarity_term_info *)
166 nmem_malloc (si->nmem, sizeof(*si->entries)*numterms);
168 /* reset the counts for the next term */
169 ranksimilar_rec_reset(si);
172 /* looping all terms in a specific fieldindex of query */
173 for (i = 0; i < numterms; i++)
175 struct ord_list *ol = NULL;
178 /* adding to number of rank entries */
179 if (strncmp (terms[i]->flags, "rank,", 5))
181 si->entries[i].rank_flag = 0;
182 yaz_log(log_level, "begin() terms[%d]: '%s' flags=%s not ranked",
183 i, terms[i]->name, terms[i]->flags);
187 const char *cp = strstr(terms[i]->flags+4, ",w=");
189 zint no_docs_fieldindex = 0;
190 zint no_terms_fieldindex = 0;
192 yaz_log(log_level, "begin() terms[%d]: '%s' flags=%s",
193 i, terms[i]->name, terms[i]->flags);
195 (si->no_ranked_terms_query)++;
198 si->entries[i].rank_flag = 1;
199 /* notice that the call to rset_count(rset) has he side-effect
200 of setting rset->hits_limit = rset_count(rset) ??? */
201 si->entries[i].freq_term_resset = rset_count(terms[i]->rset);
202 si->entries[i].no_docs_resset = terms[i]->rset->hits_count;
206 si->entries[i].fieldindex_weight = atoi (cp+3);
208 si->entries[i].fieldindex_weight = 34; /* sqrroot of 1000 */
212 yaz_log(log_level, "begin() rset_count(terms[%d]->rset) = "
213 ZINT_FORMAT, i, rset_count(terms[i]->rset));
214 yaz_log(log_level, "begin() terms[%d]->rset->hits_limit = "
215 ZINT_FORMAT, i, terms[i]->rset->hits_limit);
216 yaz_log(log_level, "begin() terms[%d]->rset->hits_count = "
217 ZINT_FORMAT, i, terms[i]->rset->hits_count);
218 yaz_log(log_level, "begin() terms[%d]->rset->hits_round = "
219 ZINT_FORMAT, i, terms[i]->rset->hits_round);
220 yaz_log(log_level, "begin() terms[%d]->rset->hits_approx = %d",
221 i, terms[i]->rset->hits_approx);
224 /* looping indexes where term terms[i] is found */
226 for (; ol; ol = ol->next)
230 const char *string_index = 0;
234 zebraExplain_lookup_ord(reg->zei,
235 ol->ord, &index_type, &db, &set, &use,
239 += zebraExplain_ord_get_doc_occurrences(reg->zei, ol->ord);
241 += zebraExplain_ord_get_term_occurrences(reg->zei, ol->ord);
245 "begin() index: ord=%d type=%c db=%s str-index=%s",
246 ol->ord, index_type, db, string_index);
249 "begin() index: ord=%d type=%c db=%s set=%d use=%d",
250 ol->ord, index_type, db, set, use);
253 si->entries[i].no_docs_fieldindex = no_docs_fieldindex;
254 si->entries[i].no_terms_fieldindex = no_terms_fieldindex;
257 si->entries[i].term = terms[i];
258 si->entries[i].term_index=i;
260 /* setting next entry in term */
261 terms[i]->rankpriv = &(si->entries[i]);
268 * end: Terminates ranking process. Called after a result set
271 static void end (struct zebra_register *reg, void *set_handle)
273 yaz_log(log_level, "end()");
279 * add: Called for each word occurence in a result set. This routine
280 * should be as fast as possible. This routine should "incrementally"
283 static void add (void *set_handle, int seqno, TERMID term)
285 struct ranksimilarity_set_info *si
286 = (struct ranksimilarity_set_info *) set_handle;
287 struct ranksimilarity_term_info *ti;
291 /* yaz_log(log_level, "add() seqno=%d NULL term", seqno); */
295 ti= (struct ranksimilarity_term_info *) term->rankpriv;
297 si->last_pos = seqno;
298 ti->freq_term_docfield++;
299 /*yaz_log(log_level, "add() seqno=%d term=%s freq_term_docfield=%d",
300 seqno, term->name, ti->freq_term_docfield); */
304 * calc: Called for each document in a result. This handler should
305 * produce a score based on previous call(s) to the add handler. The
306 * score should be between 0 and 1000. If score cannot be obtained
307 * -1 should be returned.
309 static int calc (void *set_handle, zint sysno, zint staticrank,
313 struct ranksimilarity_set_info *si
314 = (struct ranksimilarity_set_info *) set_handle;
317 yaz_log(log_level, "calc() sysno = " ZINT_FORMAT, sysno);
318 yaz_log(log_level, "calc() staticrank = " ZINT_FORMAT, staticrank);
320 yaz_log(log_level, "calc() si->no_terms_query = %d",
322 yaz_log(log_level, "calc() si->no_ranked_terms_query = %d",
323 si->no_ranked_terms_query);
324 yaz_log(log_level, "calc() si->no_docs_database = " ZINT_FORMAT,
325 si->no_docs_database);
326 yaz_log(log_level, "calc() si->no_terms_database = " ZINT_FORMAT,
327 si->no_terms_database);
330 if (!si->no_ranked_terms_query)
331 return -1; /* ranking not enabled for any terms */
334 /* if we set *stop_flag = 1, we stop processing (of result set list) */
337 /* here goes your formula to compute a scoring function */
338 /* you may use all the gathered statistics here */
339 for (i = 0; i < si->no_terms_query; i++)
341 yaz_log(log_level, "calc() entries[%d] termid %p",
342 i, si->entries[i].term);
343 if (si->entries[i].term){
344 yaz_log(log_level, "calc() entries[%d] term '%s' flags=%s",
345 i, si->entries[i].term->name, si->entries[i].term->flags);
346 yaz_log(log_level, "calc() entries[%d] rank_flag %d",
347 i, si->entries[i].rank_flag );
348 yaz_log(log_level, "calc() entries[%d] fieldindex_weight %d",
349 i, si->entries[i].fieldindex_weight );
350 yaz_log(log_level, "calc() entries[%d] freq_term_docfield %d",
351 i, si->entries[i].freq_term_docfield );
352 yaz_log(log_level, "calc() entries[%d] freq_term_resset " ZINT_FORMAT,
353 i, si->entries[i].freq_term_resset );
354 yaz_log(log_level, "calc() entries[%d] no_docs_resset " ZINT_FORMAT,
355 i, si->entries[i].no_docs_resset );
356 yaz_log(log_level, "calc() entries[%d] no_docs_fieldindex "
358 i, si->entries[i].no_docs_fieldindex );
359 yaz_log(log_level, "calc() entries[%d] no_terms_fieldindex "
361 i, si->entries[i].no_terms_fieldindex );
366 /* reset the counts for the next term */
367 ranksimilar_rec_reset(si);
370 /* staticrank = 0 is highest, MAXINT lowest */
371 score = INT_MAX - staticrank; /* but score is reverse (logical) */
374 /* debugging statistics output */
375 yaz_log(log_level, "calc() statistics: score = %d", score);
381 * Pseudo-meta code with sequence of calls as they occur in a
382 * server. Handlers are prefixed by --:
398 static struct rank_control rank_control = {
408 struct rank_control *rank_similarity_class = &rank_control;
412 * indent-tabs-mode: nil
414 * vim: shiftwidth=4 tabstop=8 expandtab