[libdazzle] fuzzy: make penalty a float for better priority control



commit ded3f004471b3f4bc895c6d089a9712232ad47a2
Author: Christian Hergert <chergert redhat com>
Date:   Sun Jun 4 20:06:12 2017 -0700

    fuzzy: make penalty a float for better priority control

 src/fuzzy/dzl-fuzzy-index-cursor.c  |    8 ++++----
 src/fuzzy/dzl-fuzzy-index-private.h |    2 +-
 src/fuzzy/dzl-fuzzy-index.c         |    8 +++++---
 3 files changed, 10 insertions(+), 8 deletions(-)
---
diff --git a/src/fuzzy/dzl-fuzzy-index-cursor.c b/src/fuzzy/dzl-fuzzy-index-cursor.c
index 243a2cf..5815158 100644
--- a/src/fuzzy/dzl-fuzzy-index-cursor.c
+++ b/src/fuzzy/dzl-fuzzy-index-cursor.c
@@ -416,7 +416,7 @@ dzl_fuzzy_index_cursor_worker (GTask        *task,
         {
           const DzlFuzzyIndexItem *item = &lookup.tables[0][i];
           DzlFuzzyMatch match;
-          guint penalty;
+          gfloat penalty;
 
           if (item->lookaside_id != last_id)
             {
@@ -429,7 +429,7 @@ dzl_fuzzy_index_cursor_worker (GTask        *task,
                                                         &penalty))
                 continue;
 
-              match.score = 1.0 / (strlen (match.key) + item->position + penalty);
+              match.score = 1.0 / (strlen (match.key) + item->position) * penalty;
 
               g_array_append_val (self->matches, match);
             }
@@ -451,7 +451,7 @@ dzl_fuzzy_index_cursor_worker (GTask        *task,
       guint score = GPOINTER_TO_UINT (value);
       gpointer other_score;
       DzlFuzzyMatch match;
-      guint penalty;
+      gfloat penalty;
 
       if G_UNLIKELY (!_dzl_fuzzy_index_resolve (self->index,
                                                 lookaside_id,
@@ -460,7 +460,7 @@ dzl_fuzzy_index_cursor_worker (GTask        *task,
                                                 &penalty))
         continue;
 
-      match.score = 1.0 / (strlen (match.key) + score + penalty);
+      match.score = 1.0 / (strlen (match.key) + score) * penalty;
 
       if (g_hash_table_lookup_extended (by_document,
                                         GUINT_TO_POINTER (match.document_id),
diff --git a/src/fuzzy/dzl-fuzzy-index-private.h b/src/fuzzy/dzl-fuzzy-index-private.h
index e324761..2dbc8be 100644
--- a/src/fuzzy/dzl-fuzzy-index-private.h
+++ b/src/fuzzy/dzl-fuzzy-index-private.h
@@ -29,7 +29,7 @@ gboolean     _dzl_fuzzy_index_resolve         (DzlFuzzyIndex  *self,
                                                guint           lookaside_id,
                                                guint          *document_id,
                                                const gchar   **key,
-                                               guint          *penalty);
+                                               gfloat         *penalty);
 
 G_END_DECLS
 
diff --git a/src/fuzzy/dzl-fuzzy-index.c b/src/fuzzy/dzl-fuzzy-index.c
index a690f95..50898ff 100644
--- a/src/fuzzy/dzl-fuzzy-index.c
+++ b/src/fuzzy/dzl-fuzzy-index.c
@@ -460,7 +460,7 @@ _dzl_fuzzy_index_resolve (DzlFuzzyIndex  *self,
                           guint           lookaside_id,
                           guint          *document_id,
                           const gchar   **key,
-                          guint          *penalty)
+                          gfloat         *penalty)
 {
   const LookasideEntry *entry;
   const gchar *local_key = NULL;
@@ -492,8 +492,10 @@ _dzl_fuzzy_index_resolve (DzlFuzzyIndex  *self,
 
   if (penalty != NULL)
     {
-      *penalty = (entry->key_id & 0xFF000000) >> 24;
-      *penalty += strlen (local_key);
+      guint p = (entry->key_id & 0xFF000000) >> 24;
+
+      /* Use the penalty to force categorization by importance. */
+      *penalty = (255 - p) / 255.0;
     }
 
   return TRUE;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]