2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1995-10-06 14:38:06 adam
8 * New result set method: r_score.
9 * Local no (sysno) and score is transferred to retrieveCtrl.
11 * Revision 1.4 1995/09/14 07:48:56 adam
12 * Other score calculation.
14 * Revision 1.3 1995/09/11 15:23:40 adam
15 * More work on relevance search.
17 * Revision 1.2 1995/09/11 13:09:41 adam
18 * More work on relevance feedback.
20 * Revision 1.1 1995/09/08 14:52:42 adam
21 * Work on relevance feedback.
34 static rset_control *r_create(const struct rset_control *sel, void *parms);
35 static RSFD r_open (rset_control *ct, int wflag);
36 static void r_close (RSFD rfd);
37 static void r_delete (rset_control *ct);
38 static void r_rewind (RSFD rfd);
39 static int r_count (rset_control *ct);
40 static int r_read (RSFD rfd, void *buf);
41 static int r_write (RSFD rfd, const void *buf);
42 static int r_score (RSFD rfd, int *score);
44 static const rset_control control =
59 const rset_control *rset_kind_relevance = &control;
61 struct rset_rel_info {
65 int (*cmp)(const void *p1, const void *p2);
70 struct rset_rel_rfd *rfd_list;
75 struct rset_rel_rfd *next;
76 struct rset_rel_info *info;
79 static void add_rec (struct rset_rel_info *info, double score, void *key)
83 logf (LOG_DEBUG, "add %f", score);
84 for (i = 0; i<info->no_rec; i++)
86 idx = info->sort_idx[i];
87 if (score <= info->score_buf[idx])
90 if (info->no_rec < info->max_rec)
92 for (j = info->no_rec; j > i; --j)
93 info->sort_idx[j] = info->sort_idx[j-1];
94 idx = info->sort_idx[j] = info->no_rec;
101 idx = info->sort_idx[0];
104 for (j = 0; j < i; ++j)
105 info->sort_idx[j] = info->sort_idx[j+1];
106 info->sort_idx[j] = idx;
108 memcpy (info->key_buf + idx*info->key_size, key, info->key_size);
109 info->score_buf[idx] = score;
112 static void relevance (struct rset_rel_info *info, rset_relevance_parms *parms)
122 logf (LOG_DEBUG, "relevance");
123 isam_buf = xmalloc (parms->no_isam_positions * sizeof(*isam_buf));
124 isam_r = xmalloc (sizeof (*isam_r) * parms->no_isam_positions);
125 isam_pt = xmalloc (sizeof (*isam_pt) * parms->no_isam_positions);
126 isam_tmp_buf = xmalloc (info->key_size);
127 max_tf = xmalloc (sizeof (*max_tf) * parms->no_isam_positions);
128 wgt = xmalloc (sizeof (*wgt) * parms->no_isam_positions);
130 for (i = 0; i<parms->no_isam_positions; i++)
132 isam_buf[i] = xmalloc (info->key_size);
133 isam_pt[i] = is_position (parms->is, parms->isam_positions[i]);
134 max_tf [i] = is_numkeys (isam_pt[i]);
135 isam_r[i] = is_readkey (isam_pt[i], isam_buf[i]);
136 logf (LOG_DEBUG, "max tf %d = %d", i, max_tf[i]);
143 /* find min with lowest sysno */
144 for (i = 0; i<parms->no_isam_positions; i++)
146 (min < 0 || (*parms->cmp)(isam_buf[i], isam_buf[min]) < 1))
150 memcpy (isam_tmp_buf, isam_buf[min], info->key_size);
151 /* calculate for all with those sysno */
152 for (i = 0; i<parms->no_isam_positions; i++)
157 r = (*parms->cmp)(isam_buf[i], isam_tmp_buf);
168 isam_r[i] = is_readkey (isam_pt[i], isam_buf[i]);
169 } while (isam_r[i] &&
170 (*parms->cmp)(isam_buf[i], isam_tmp_buf) <= 1);
171 wgt[i] = 0.1+tf*0.9/max_tf[i];
174 /* calculate relevance value */
176 for (i = 0; i<parms->no_isam_positions; i++)
178 /* if value is in the top score, then save it - don't emit yet */
179 add_rec (info, score, isam_tmp_buf);
181 for (i = 0; i<parms->no_isam_positions; i++)
183 is_pt_free (isam_pt[i]);
187 xfree (isam_tmp_buf);
194 static rset_control *r_create (const struct rset_control *sel, void *parms)
197 rset_relevance_parms *r_parms = parms;
198 struct rset_rel_info *info;
200 newct = xmalloc(sizeof(*newct));
201 memcpy(newct, sel, sizeof(*sel));
202 newct->buf = xmalloc (sizeof(struct rset_rel_info));
205 info->key_size = r_parms->key_size;
206 assert (info->key_size > 1);
207 info->max_rec = r_parms->max_rec;
208 assert (info->max_rec > 1);
209 info->cmp = r_parms->cmp;
211 info->key_buf = xmalloc (info->key_size * info->max_rec);
212 info->score_buf = xmalloc (sizeof(*info->score_buf) * info->max_rec);
213 info->sort_idx = xmalloc (sizeof(*info->sort_idx) * info->max_rec);
215 info->rfd_list = NULL;
217 relevance (info, r_parms);
221 static RSFD r_open (rset_control *ct, int wflag)
223 struct rset_rel_rfd *rfd;
224 struct rset_rel_info *info = ct->buf;
228 logf (LOG_FATAL, "relevance set type is read-only");
231 rfd = xmalloc (sizeof(*rfd));
232 rfd->next = info->rfd_list;
233 info->rfd_list = rfd;
234 rfd->position = info->no_rec;
239 static void r_close (RSFD rfd)
241 struct rset_rel_info *info = ((struct rset_rel_rfd*)rfd)->info;
242 struct rset_rel_rfd **rfdp;
244 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
247 *rfdp = (*rfdp)->next;
251 logf (LOG_FATAL, "r_close but no rfd match!");
255 static void r_delete (rset_control *ct)
257 struct rset_rel_info *info = ct->buf;
259 assert (info->rfd_list == NULL);
260 xfree (info->key_buf);
261 xfree (info->score_buf);
262 xfree (info->sort_idx);
267 static void r_rewind (RSFD rfd)
269 struct rset_rel_rfd *p = rfd;
270 struct rset_rel_info *info = p->info;
272 p->position = info->no_rec;
275 static int r_count (rset_control *ct)
277 struct rset_rel_info *info = ct->buf;
282 static int r_read (RSFD rfd, void *buf)
284 struct rset_rel_rfd *p = rfd;
285 struct rset_rel_info *info = p->info;
287 if (p->position <= 0)
291 info->key_buf + info->key_size * info->sort_idx[p->position],
296 static int r_score (RSFD rfd, int *score)
298 struct rset_rel_rfd *p = rfd;
299 struct rset_rel_info *info = p->info;
303 *score = (int) (1000*info->score_buf[info->sort_idx[p->position]]);
307 static int r_write (RSFD rfd, const void *buf)
309 logf (LOG_FATAL, "relevance set type is read-only");