2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.2 1995-09-11 13:09:41 adam
8 * More work on relevance feedback.
10 * Revision 1.1 1995/09/08 14:52:42 adam
11 * Work on relevance feedback.
24 static rset_control *r_create(const struct rset_control *sel, void *parms);
25 static RSFD r_open (rset_control *ct, int wflag);
26 static void r_close (RSFD rfd);
27 static void r_delete (rset_control *ct);
28 static void r_rewind (RSFD rfd);
29 static int r_count (rset_control *ct);
30 static int r_read (RSFD rfd, void *buf);
31 static int r_write (RSFD rfd, const void *buf);
33 static const rset_control control =
47 const rset_control *rset_kind_relevance = &control;
49 struct rset_rel_info {
53 int (*cmp)(const void *p1, const void *p2);
57 struct rset_rel_rfd *rfd_list;
62 struct rset_rel_rfd *next;
63 struct rset_rel_info *info;
66 static void relevance (struct rset_rel_info *info, rset_relevance_parms *parms)
76 logf (LOG_DEBUG, "relevance");
77 isam_buf = xmalloc (parms->no_isam_positions * sizeof(*isam_buf));
78 isam_r = xmalloc (sizeof (*isam_r) * parms->no_isam_positions);
79 isam_pt = xmalloc (sizeof (*isam_pt) * parms->no_isam_positions);
80 isam_tmp_buf = xmalloc (info->key_size);
81 max_tf = xmalloc (sizeof (*max_tf) * parms->no_isam_positions);
82 wgt = xmalloc (sizeof (*wgt) * parms->no_isam_positions);
84 for (i = 0; i<parms->no_isam_positions; i++)
86 isam_buf[i] = xmalloc (info->key_size);
87 isam_pt[i] = is_position (parms->is, parms->isam_positions[i]);
88 max_tf [i] = is_numkeys (isam_pt[i]);
89 isam_r[i] = is_readkey (isam_pt[i], isam_buf[i]);
90 logf (LOG_DEBUG, "max tf %d = %d", i, max_tf[i]);
95 double length, similarity;
97 /* find min with lowest sysno */
98 for (i = 0; i<parms->no_isam_positions; i++)
100 (min < 0 || (*parms->cmp)(isam_buf[i], isam_buf[min]) < 1))
104 memcpy (isam_tmp_buf, isam_buf[min], info->key_size);
105 logf (LOG_LOG, "calc rel for");
106 key_logdump (LOG_LOG, isam_tmp_buf);
107 /* calculate for all with those sysno */
109 for (i = 0; i<parms->no_isam_positions; i++)
114 r = (*parms->cmp)(isam_buf[i], isam_tmp_buf);
125 isam_r[i] = is_readkey (isam_pt[i], isam_buf[i]);
126 } while (isam_r[i] &&
127 (*parms->cmp)(isam_buf[i], isam_tmp_buf) <= 1);
128 logf (LOG_DEBUG, "tf%d = %d", i, tf);
129 wgt[i] = 0.5+tf*0.5/max_tf[i];
130 length += wgt[i] * wgt[i];
133 /* calculate relevance value */
134 length = sqrt (length);
136 for (i = 0; i<parms->no_isam_positions; i++)
137 similarity += wgt[i]/length;
138 logf (LOG_LOG, " %f", similarity);
139 /* if value is in the top score, then save it - don't emit yet */
141 for (i = 0; i<parms->no_isam_positions; i++)
143 is_pt_free (isam_pt[i]);
147 xfree (isam_tmp_buf);
154 static rset_control *r_create (const struct rset_control *sel, void *parms)
157 rset_relevance_parms *r_parms = parms;
158 struct rset_rel_info *info;
160 newct = xmalloc(sizeof(*newct));
161 memcpy(newct, sel, sizeof(*sel));
162 newct->buf = xmalloc (sizeof(struct rset_rel_info));
165 info->key_size = r_parms->key_size;
166 assert (info->key_size > 1);
167 info->max_rec = r_parms->max_rec;
168 assert (info->max_rec > 1);
169 info->cmp = r_parms->cmp;
171 info->key_buf = xmalloc (info->key_size * info->max_rec);
172 info->score_buf = xmalloc (sizeof(*info->score_buf) * info->max_rec);
174 info->rfd_list = NULL;
176 relevance (info, r_parms);
180 static RSFD r_open (rset_control *ct, int wflag)
182 struct rset_rel_rfd *rfd;
183 struct rset_rel_info *info = ct->buf;
187 logf (LOG_FATAL, "relevance set type is read-only");
190 rfd = xmalloc (sizeof(*rfd));
191 rfd->next = info->rfd_list;
192 info->rfd_list = rfd;
198 static void r_close (RSFD rfd)
200 struct rset_rel_info *info = ((struct rset_rel_rfd*)rfd)->info;
201 struct rset_rel_rfd **rfdp;
203 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
206 *rfdp = (*rfdp)->next;
210 logf (LOG_FATAL, "r_close but no rfd match!");
214 static void r_delete (rset_control *ct)
216 struct rset_rel_info *info = ct->buf;
218 assert (info->rfd_list == NULL);
219 xfree (info->key_buf);
220 xfree (info->score_buf);
225 static void r_rewind (RSFD rfd)
227 ((struct rset_rel_rfd*) rfd)->position = 0;
230 static int r_count (rset_control *ct)
232 struct rset_rel_info *info = ct->buf;
237 static int r_read (RSFD rfd, void *buf)
239 struct rset_rel_rfd *p = rfd;
240 struct rset_rel_info *info = p->info;
242 if (p->position >= info->max_rec)
244 memcpy ((char*) buf + sizeof(*info->score_buf),
245 (char*) info->key_buf + info->key_size * p->position,
248 info->score_buf + p->position, sizeof(*info->score_buf));
253 static int r_write (RSFD rfd, const void *buf)
255 logf (LOG_FATAL, "relevance set type is read-only");