*** empty log message ***
[idzebra-moved-to-github.git] / rset / rsrel.c
index f52cc26..2f7af4c 100644 (file)
@@ -4,7 +4,34 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: rsrel.c,v $
- * Revision 1.2  1995-09-11 13:09:41  adam
+ * Revision 1.9  1995-12-11 09:15:26  adam
+ * New set types: sand/sor/snot - ranked versions of and/or/not in
+ * ranked/semi-ranked result sets.
+ * Note: the snot not finished yet.
+ * New rset member: flag.
+ * Bug fix: r_delete in rsrel.c did free bad memory block.
+ *
+ * Revision 1.8  1995/12/05  11:25:45  adam
+ * Doesn't include math.h.
+ *
+ * Revision 1.7  1995/10/12  12:41:57  adam
+ * Private info (buf) moved from struct rset_control to struct rset.
+ * Bug fixes in relevance.
+ *
+ * Revision 1.6  1995/10/10  14:00:04  adam
+ * Function rset_open changed its wflag parameter to general flags.
+ *
+ * Revision 1.5  1995/10/06  14:38:06  adam
+ * New result set method: r_score.
+ * Local no (sysno) and score is transferred to retrieveCtrl.
+ *
+ * Revision 1.4  1995/09/14  07:48:56  adam
+ * Other score calculation.
+ *
+ * Revision 1.3  1995/09/11  15:23:40  adam
+ * More work on relevance search.
+ *
+ * Revision 1.2  1995/09/11  13:09:41  adam
  * More work on relevance feedback.
  *
  * Revision 1.1  1995/09/08  14:52:42  adam
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <math.h>
 #include <assert.h>
 
 #include <isam.h>
 #include <rsrel.h>
 #include <alexutil.h>
 
-static rset_control *r_create(const struct rset_control *sel, void *parms);
-static RSFD r_open (rset_control *ct, int wflag);
+static void *r_create(const struct rset_control *sel, void *parms,
+                      int *flags);
+static RSFD r_open (RSET ct, int flag);
 static void r_close (RSFD rfd);
-static void r_delete (rset_control *ct);
+static void r_delete (RSET ct);
 static void r_rewind (RSFD rfd);
-static int r_count (rset_control *ct);
+static int r_count (RSET ct);
 static int r_read (RSFD rfd, void *buf);
 static int r_write (RSFD rfd, const void *buf);
+static int r_score (RSFD rfd, int *score);
 
 static const rset_control control = 
 {
-    "relevance set type",
-    0,
+    "relevance",
     r_create,
     r_open,
     r_close,
@@ -41,7 +68,8 @@ static const rset_control control =
     r_rewind,
     r_count,
     r_read,
-    r_write
+    r_write,
+    r_score
 };
 
 const rset_control *rset_kind_relevance = &control;
@@ -51,18 +79,65 @@ struct rset_rel_info {
     int     max_rec;
     int     no_rec;
     int     (*cmp)(const void *p1, const void *p2);
-    void    *key_buf;
-    int     *score_buf;
-
+    char    *key_buf;                   /* key buffer */
+    float   *score_buf;                 /* score buffer */
+    int     *sort_idx;                  /* score sorted index */
+    int     *sysno_idx;                /* sysno sorted index (ring buffer) */
     struct rset_rel_rfd *rfd_list;
 };
 
 struct rset_rel_rfd {
+    int     last_read_pos;
     int     position;
+    int     flag;
     struct rset_rel_rfd *next;
     struct rset_rel_info *info;
 };
 
+static void add_rec (struct rset_rel_info *info, double score, void *key)
+{
+    int idx, i, j;
+
+    for (i = 0; i<info->no_rec; i++)
+    {
+        idx = info->sort_idx[i];
+        if (score <= info->score_buf[idx])
+            break;
+    }
+    if (info->no_rec < info->max_rec)
+    {                                        /* there is room for this entry */
+        for (j = info->no_rec; j > i; --j)
+            info->sort_idx[j] = info->sort_idx[j-1];
+        idx = info->sort_idx[j] = info->no_rec;
+        ++(info->no_rec);
+    }
+    else if (i == 0)
+        return;                              /* score too low */
+    else
+    {
+        idx = info->sort_idx[0];             /* remove this entry */
+
+        --i;
+        for (j = 0; j < i; ++j)              /* make room */
+            info->sort_idx[j] = info->sort_idx[j+1];
+        info->sort_idx[j] = idx;             /* allocate sort entry */
+    }
+    memcpy (info->key_buf + idx*info->key_size, key, info->key_size);
+    info->score_buf[idx] = score;
+}
+
+
+static struct rset_rel_info *qsort_info;
+
+static int qcomp (const void *p1, const void *p2)
+{
+    int i1 = *(int*) p1;
+    int i2 = *(int*) p2;
+
+    return qsort_info->cmp (qsort_info->key_buf + i1*qsort_info->key_size,
+                            qsort_info->key_buf + i2*qsort_info->key_size);
+}
+
 static void relevance (struct rset_rel_info *info, rset_relevance_parms *parms)
 {
     char **isam_buf;
@@ -92,24 +167,21 @@ static void relevance (struct rset_rel_info *info, rset_relevance_parms *parms)
     while (1)
     {
         int min = -1, i;
-        double length, similarity;
+        double score;
 
         /* find min with lowest sysno */
         for (i = 0; i<parms->no_isam_positions; i++)
             if (isam_r[i] && 
-               (min < 0 || (*parms->cmp)(isam_buf[i], isam_buf[min]) < 1))
+               (min < 0 || (*parms->cmp)(isam_buf[i], isam_buf[min]) < 2))
                 min = i;
         if (min < 0)
             break;
         memcpy (isam_tmp_buf, isam_buf[min], info->key_size);
-        logf (LOG_LOG, "calc rel for");
-        key_logdump (LOG_LOG, isam_tmp_buf);
         /* calculate for all with those sysno */
-        length = 0.0;
         for (i = 0; i<parms->no_isam_positions; i++)
         {
             int r;
-
+            
             if (isam_r[i])
                 r = (*parms->cmp)(isam_buf[i], isam_tmp_buf);
             else 
@@ -125,19 +197,20 @@ static void relevance (struct rset_rel_info *info, rset_relevance_parms *parms)
                     isam_r[i] = is_readkey (isam_pt[i], isam_buf[i]);
                 } while (isam_r[i] && 
                          (*parms->cmp)(isam_buf[i], isam_tmp_buf) <= 1);
-                logf (LOG_DEBUG, "tf%d = %d", i, tf);
-                wgt[i] = 0.5+tf*0.5/max_tf[i];
-                length += wgt[i] * wgt[i];
+                wgt[i] = 0.1+tf*0.9/max_tf[i];
             }
         }
         /* calculate relevance value */
-        length = sqrt (length);
-        similarity = 0.0;
+        score = 0.0;
         for (i = 0; i<parms->no_isam_positions; i++)
-             similarity += wgt[i]/length;
-        logf (LOG_LOG, " %f", similarity);
+            score += wgt[i];
         /* if value is in the top score, then save it - don't emit yet */
+        add_rec (info, score/parms->no_isam_positions, isam_tmp_buf);
     }
+    for (i = 0; i<info->no_rec; i++)
+        info->sysno_idx[i] = i;
+    qsort_info = info;
+    qsort (info->sysno_idx, info->no_rec, sizeof(*info->sysno_idx), qcomp);
     for (i = 0; i<parms->no_isam_positions; i++)
     {
         is_pt_free (isam_pt[i]);
@@ -151,17 +224,14 @@ static void relevance (struct rset_rel_info *info, rset_relevance_parms *parms)
     xfree (wgt);
 }
 
-static rset_control *r_create (const struct rset_control *sel, void *parms)
+static void *r_create (const struct rset_control *sel, void *parms,
+                       int *flags)
 {
-    rset_control *newct;
     rset_relevance_parms *r_parms = parms;
     struct rset_rel_info *info;
 
-    newct = xmalloc(sizeof(*newct));
-    memcpy(newct, sel, sizeof(*sel));
-    newct->buf = xmalloc (sizeof(struct rset_rel_info));
-
-    info = newct->buf;
+    *flags |= RSET_FLAG_RANKED;
+    info = xmalloc (sizeof(struct rset_rel_info));
     info->key_size = r_parms->key_size;
     assert (info->key_size > 1);
     info->max_rec = r_parms->max_rec;
@@ -170,28 +240,31 @@ static rset_control *r_create (const struct rset_control *sel, void *parms)
 
     info->key_buf = xmalloc (info->key_size * info->max_rec);
     info->score_buf = xmalloc (sizeof(*info->score_buf) * info->max_rec);
+    info->sort_idx = xmalloc (sizeof(*info->sort_idx) * info->max_rec);
+    info->sysno_idx = xmalloc (sizeof(*info->sysno_idx) * info->max_rec);
     info->no_rec = 0;
     info->rfd_list = NULL;
 
     relevance (info, r_parms);
-    return newct;
+    return info;
 }
 
-static RSFD r_open (rset_control *ct, int wflag)
+static RSFD r_open (RSET ct, int flag)
 {
     struct rset_rel_rfd *rfd;
     struct rset_rel_info *info = ct->buf;
 
-    if (wflag)
+    if (flag & RSETF_WRITE)
     {
        logf (LOG_FATAL, "relevance set type is read-only");
        return NULL;
     }
     rfd = xmalloc (sizeof(*rfd));
+    rfd->flag = flag;
     rfd->next = info->rfd_list;
-    info->rfd_list = rfd;
-    rfd->position = 0;
     rfd->info = info;
+    info->rfd_list = rfd;
+    r_rewind (rfd);
     return rfd;
 }
 
@@ -211,23 +284,30 @@ static void r_close (RSFD rfd)
     assert (0);
 }
 
-static void r_delete (rset_control *ct)
+static void r_delete (RSET ct)
 {
     struct rset_rel_info *info = ct->buf;
 
     assert (info->rfd_list == NULL);
     xfree (info->key_buf);
     xfree (info->score_buf);
+    xfree (info->sort_idx);
+    xfree (info->sysno_idx);
     xfree (info);
-    xfree (ct);
 }
 
 static void r_rewind (RSFD rfd)
 {
-    ((struct rset_rel_rfd*) rfd)->position = 0;
+    struct rset_rel_rfd *p = rfd;
+    struct rset_rel_info *info = p->info;
+
+    if (p->flag & RSETF_SORT_RANK)
+        p->position = info->no_rec;
+    else
+        p->position = 0;
 }
 
-static int r_count (rset_control *ct)
+static int r_count (RSET ct)
 {
     struct rset_rel_info *info = ct->buf;
 
@@ -239,14 +319,32 @@ static int r_read (RSFD rfd, void *buf)
     struct rset_rel_rfd *p = rfd;
     struct rset_rel_info *info = p->info;
 
-    if (p->position >= info->max_rec)
-        return 0;
-    memcpy ((char*) buf + sizeof(*info->score_buf),
-            (char*) info->key_buf + info->key_size * p->position,
-            info->key_size);
+    if (p->flag & RSETF_SORT_RANK)
+    {
+        if (p->position <= 0)
+            return 0;
+        --(p->position);
+        p->last_read_pos = info->sort_idx[p->position];
+    }
+    else
+    {
+        if (p->position == info->no_rec)
+            return 0;
+        p->last_read_pos = info->sysno_idx[p->position];
+        ++(p->position);
+    }
     memcpy ((char*) buf,
-            info->score_buf + p->position, sizeof(*info->score_buf));
-    ++(p->position);
+            info->key_buf + info->key_size * p->last_read_pos,
+            info->key_size);
+    return 1;
+}
+
+static int r_score (RSFD rfd, int *score)
+{
+    struct rset_rel_rfd *p = rfd;
+    struct rset_rel_info *info = p->info;
+
+    *score = (int) (1000*info->score_buf[p->last_read_pos]);
     return 1;
 }