1 /* $Id: ranksimilarity.c,v 1.9 2006-05-19 23:20:24 adam 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;
232 zebraExplain_lookup_ord(reg->zei,
233 ol->ord, &index_type, &db,
237 += zebraExplain_ord_get_doc_occurrences(reg->zei, ol->ord);
239 += zebraExplain_ord_get_term_occurrences(reg->zei, ol->ord);
243 "begin() index: ord=%d type=%c db=%s str-index=%s",
244 ol->ord, index_type, db, string_index);
247 "begin() index: ord=%d type=%c db=%s",
248 ol->ord, index_type, db);
251 si->entries[i].no_docs_fieldindex = no_docs_fieldindex;
252 si->entries[i].no_terms_fieldindex = no_terms_fieldindex;
255 si->entries[i].term = terms[i];
256 si->entries[i].term_index=i;
258 /* setting next entry in term */
259 terms[i]->rankpriv = &(si->entries[i]);
266 * end: Terminates ranking process. Called after a result set
269 static void end (struct zebra_register *reg, void *set_handle)
271 yaz_log(log_level, "end()");
277 * add: Called for each word occurence in a result set. This routine
278 * should be as fast as possible. This routine should "incrementally"
281 static void add (void *set_handle, int seqno, TERMID term)
283 struct ranksimilarity_set_info *si
284 = (struct ranksimilarity_set_info *) set_handle;
285 struct ranksimilarity_term_info *ti;
289 /* yaz_log(log_level, "add() seqno=%d NULL term", seqno); */
293 ti= (struct ranksimilarity_term_info *) term->rankpriv;
295 si->last_pos = seqno;
296 ti->freq_term_docfield++;
297 /*yaz_log(log_level, "add() seqno=%d term=%s freq_term_docfield=%d",
298 seqno, term->name, ti->freq_term_docfield); */
302 * calc: Called for each document in a result. This handler should
303 * produce a score based on previous call(s) to the add handler. The
304 * score should be between 0 and 1000. If score cannot be obtained
305 * -1 should be returned.
307 static int calc (void *set_handle, zint sysno, zint staticrank,
311 struct ranksimilarity_set_info *si
312 = (struct ranksimilarity_set_info *) set_handle;
315 yaz_log(log_level, "calc() sysno = " ZINT_FORMAT, sysno);
316 yaz_log(log_level, "calc() staticrank = " ZINT_FORMAT, staticrank);
318 yaz_log(log_level, "calc() si->no_terms_query = %d",
320 yaz_log(log_level, "calc() si->no_ranked_terms_query = %d",
321 si->no_ranked_terms_query);
322 yaz_log(log_level, "calc() si->no_docs_database = " ZINT_FORMAT,
323 si->no_docs_database);
324 yaz_log(log_level, "calc() si->no_terms_database = " ZINT_FORMAT,
325 si->no_terms_database);
328 if (!si->no_ranked_terms_query)
329 return -1; /* ranking not enabled for any terms */
332 /* if we set *stop_flag = 1, we stop processing (of result set list) */
335 /* here goes your formula to compute a scoring function */
336 /* you may use all the gathered statistics here */
337 for (i = 0; i < si->no_terms_query; i++)
339 yaz_log(log_level, "calc() entries[%d] termid %p",
340 i, si->entries[i].term);
341 if (si->entries[i].term){
342 yaz_log(log_level, "calc() entries[%d] term '%s' flags=%s",
343 i, si->entries[i].term->name, si->entries[i].term->flags);
344 yaz_log(log_level, "calc() entries[%d] rank_flag %d",
345 i, si->entries[i].rank_flag );
346 yaz_log(log_level, "calc() entries[%d] fieldindex_weight %d",
347 i, si->entries[i].fieldindex_weight );
348 yaz_log(log_level, "calc() entries[%d] freq_term_docfield %d",
349 i, si->entries[i].freq_term_docfield );
350 yaz_log(log_level, "calc() entries[%d] freq_term_resset " ZINT_FORMAT,
351 i, si->entries[i].freq_term_resset );
352 yaz_log(log_level, "calc() entries[%d] no_docs_resset " ZINT_FORMAT,
353 i, si->entries[i].no_docs_resset );
354 yaz_log(log_level, "calc() entries[%d] no_docs_fieldindex "
356 i, si->entries[i].no_docs_fieldindex );
357 yaz_log(log_level, "calc() entries[%d] no_terms_fieldindex "
359 i, si->entries[i].no_terms_fieldindex );
364 /* reset the counts for the next term */
365 ranksimilar_rec_reset(si);
368 /* staticrank = 0 is highest, MAXINT lowest */
369 score = INT_MAX - staticrank; /* but score is reverse (logical) */
372 /* debugging statistics output */
373 yaz_log(log_level, "calc() statistics: score = %d", score);
379 * Pseudo-meta code with sequence of calls as they occur in a
380 * server. Handlers are prefixed by --:
396 static struct rank_control rank_control = {
406 struct rank_control *rank_similarity_class = &rank_control;
410 * indent-tabs-mode: nil
412 * vim: shiftwidth=4 tabstop=8 expandtab