2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1995-12-05 11:25:45 adam
8 * Doesn't include math.h.
10 * Revision 1.7 1995/10/12 12:41:57 adam
11 * Private info (buf) moved from struct rset_control to struct rset.
12 * Bug fixes in relevance.
14 * Revision 1.6 1995/10/10 14:00:04 adam
15 * Function rset_open changed its wflag parameter to general flags.
17 * Revision 1.5 1995/10/06 14:38:06 adam
18 * New result set method: r_score.
19 * Local no (sysno) and score is transferred to retrieveCtrl.
21 * Revision 1.4 1995/09/14 07:48:56 adam
22 * Other score calculation.
24 * Revision 1.3 1995/09/11 15:23:40 adam
25 * More work on relevance search.
27 * Revision 1.2 1995/09/11 13:09:41 adam
28 * More work on relevance feedback.
30 * Revision 1.1 1995/09/08 14:52:42 adam
31 * Work on relevance feedback.
43 static void *r_create(const struct rset_control *sel, void *parms);
44 static RSFD r_open (RSET ct, int flag);
45 static void r_close (RSFD rfd);
46 static void r_delete (RSET ct);
47 static void r_rewind (RSFD rfd);
48 static int r_count (RSET ct);
49 static int r_read (RSFD rfd, void *buf);
50 static int r_write (RSFD rfd, const void *buf);
51 static int r_score (RSFD rfd, int *score);
53 static const rset_control control =
67 const rset_control *rset_kind_relevance = &control;
69 struct rset_rel_info {
73 int (*cmp)(const void *p1, const void *p2);
74 char *key_buf; /* key buffer */
75 float *score_buf; /* score buffer */
76 int *sort_idx; /* score sorted index */
77 int *sysno_idx; /* sysno sorted index (ring buffer) */
78 struct rset_rel_rfd *rfd_list;
85 struct rset_rel_rfd *next;
86 struct rset_rel_info *info;
89 static void add_rec (struct rset_rel_info *info, double score, void *key)
93 for (i = 0; i<info->no_rec; i++)
95 idx = info->sort_idx[i];
96 if (score <= info->score_buf[idx])
99 if (info->no_rec < info->max_rec)
100 { /* there is room for this entry */
101 for (j = info->no_rec; j > i; --j)
102 info->sort_idx[j] = info->sort_idx[j-1];
103 idx = info->sort_idx[j] = info->no_rec;
107 return; /* score too low */
110 idx = info->sort_idx[0]; /* remove this entry */
113 for (j = 0; j < i; ++j) /* make room */
114 info->sort_idx[j] = info->sort_idx[j+1];
115 info->sort_idx[j] = idx; /* allocate sort entry */
117 memcpy (info->key_buf + idx*info->key_size, key, info->key_size);
118 info->score_buf[idx] = score;
122 static struct rset_rel_info *qsort_info;
124 static int qcomp (const void *p1, const void *p2)
129 return qsort_info->cmp (qsort_info->key_buf + i1*qsort_info->key_size,
130 qsort_info->key_buf + i2*qsort_info->key_size);
133 static void relevance (struct rset_rel_info *info, rset_relevance_parms *parms)
143 logf (LOG_DEBUG, "relevance");
144 isam_buf = xmalloc (parms->no_isam_positions * sizeof(*isam_buf));
145 isam_r = xmalloc (sizeof (*isam_r) * parms->no_isam_positions);
146 isam_pt = xmalloc (sizeof (*isam_pt) * parms->no_isam_positions);
147 isam_tmp_buf = xmalloc (info->key_size);
148 max_tf = xmalloc (sizeof (*max_tf) * parms->no_isam_positions);
149 wgt = xmalloc (sizeof (*wgt) * parms->no_isam_positions);
151 for (i = 0; i<parms->no_isam_positions; i++)
153 isam_buf[i] = xmalloc (info->key_size);
154 isam_pt[i] = is_position (parms->is, parms->isam_positions[i]);
155 max_tf [i] = is_numkeys (isam_pt[i]);
156 isam_r[i] = is_readkey (isam_pt[i], isam_buf[i]);
157 logf (LOG_DEBUG, "max tf %d = %d", i, max_tf[i]);
164 /* find min with lowest sysno */
165 for (i = 0; i<parms->no_isam_positions; i++)
167 (min < 0 || (*parms->cmp)(isam_buf[i], isam_buf[min]) < 1))
171 memcpy (isam_tmp_buf, isam_buf[min], info->key_size);
172 /* calculate for all with those sysno */
173 for (i = 0; i<parms->no_isam_positions; i++)
178 r = (*parms->cmp)(isam_buf[i], isam_tmp_buf);
189 isam_r[i] = is_readkey (isam_pt[i], isam_buf[i]);
190 } while (isam_r[i] &&
191 (*parms->cmp)(isam_buf[i], isam_tmp_buf) <= 1);
192 wgt[i] = 0.1+tf*0.9/max_tf[i];
195 /* calculate relevance value */
197 for (i = 0; i<parms->no_isam_positions; i++)
199 /* if value is in the top score, then save it - don't emit yet */
200 add_rec (info, score, isam_tmp_buf);
202 for (i = 0; i<info->no_rec; i++)
203 info->sysno_idx[i] = i;
205 qsort (info->sysno_idx, info->no_rec, sizeof(*info->sysno_idx), qcomp);
206 for (i = 0; i<parms->no_isam_positions; i++)
208 is_pt_free (isam_pt[i]);
212 xfree (isam_tmp_buf);
219 static void *r_create (const struct rset_control *sel, void *parms)
221 rset_relevance_parms *r_parms = parms;
222 struct rset_rel_info *info;
224 info = xmalloc (sizeof(struct rset_rel_info));
225 info->key_size = r_parms->key_size;
226 assert (info->key_size > 1);
227 info->max_rec = r_parms->max_rec;
228 assert (info->max_rec > 1);
229 info->cmp = r_parms->cmp;
231 info->key_buf = xmalloc (info->key_size * info->max_rec);
232 info->score_buf = xmalloc (sizeof(*info->score_buf) * info->max_rec);
233 info->sort_idx = xmalloc (sizeof(*info->sort_idx) * info->max_rec);
234 info->sysno_idx = xmalloc (sizeof(*info->sysno_idx) * info->max_rec);
236 info->rfd_list = NULL;
238 relevance (info, r_parms);
242 static RSFD r_open (RSET ct, int flag)
244 struct rset_rel_rfd *rfd;
245 struct rset_rel_info *info = ct->buf;
247 if (flag & RSETF_WRITE)
249 logf (LOG_FATAL, "relevance set type is read-only");
252 rfd = xmalloc (sizeof(*rfd));
254 rfd->next = info->rfd_list;
256 info->rfd_list = rfd;
261 static void r_close (RSFD rfd)
263 struct rset_rel_info *info = ((struct rset_rel_rfd*)rfd)->info;
264 struct rset_rel_rfd **rfdp;
266 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
269 *rfdp = (*rfdp)->next;
273 logf (LOG_FATAL, "r_close but no rfd match!");
277 static void r_delete (RSET ct)
279 struct rset_rel_info *info = ct->buf;
281 assert (info->rfd_list == NULL);
282 xfree (info->key_buf);
283 xfree (info->score_buf);
284 xfree (info->sort_idx);
285 xfree (info->sysno_idx);
290 static void r_rewind (RSFD rfd)
292 struct rset_rel_rfd *p = rfd;
293 struct rset_rel_info *info = p->info;
295 if (p->flag & RSETF_SORT_RANK)
296 p->position = info->no_rec;
301 static int r_count (RSET ct)
303 struct rset_rel_info *info = ct->buf;
308 static int r_read (RSFD rfd, void *buf)
310 struct rset_rel_rfd *p = rfd;
311 struct rset_rel_info *info = p->info;
313 if (p->flag & RSETF_SORT_RANK)
315 if (p->position <= 0)
318 p->last_read_pos = info->sort_idx[p->position];
322 if (p->position == info->no_rec)
324 p->last_read_pos = info->sysno_idx[p->position];
328 info->key_buf + info->key_size * p->last_read_pos,
333 static int r_score (RSFD rfd, int *score)
335 struct rset_rel_rfd *p = rfd;
336 struct rset_rel_info *info = p->info;
338 *score = (int) (1000*info->score_buf[p->last_read_pos]);
342 static int r_write (RSFD rfd, const void *buf)
344 logf (LOG_FATAL, "relevance set type is read-only");