Subject: [bug#73220] [PATCH v2] ui: Add partial match relevance scoring.

From: Simon Tournier <[email-masked]>
Date: Fri, 13 Sep 2024 16:24:06 +0200

* guix/ui.scm (char-set:delimiters): New variable.
(revelance)[string-match-term?]: New procedure.
[score]: Use it.

Change-Id: If2edc0e08b338a0064f73425db60d688c0535fb0
---
 guix/ui.scm | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)

diff --git a/guix/ui.scm b/guix/ui.scm
index 966f0611f6..a8d1d120a4 100644
--- a/guix/ui.scm
+++ b/guix/ui.scm
@@ -1678,6 +1678,14 @@ (define* (package->recutils p port #:optional (width (terminal-columns))
 ;;; Searching.
 ;;;
 
+(define char-set:delimiters (char-set-xor
+                             (char-set #\-) ;remove from punctuation
+                             (char-set-union (char-set #\nul)
+                                             (char-set #\newline)
+                                             char-set:punctuation
+                                             char-set:symbol
+                                             char-set:whitespace)))
+
 (define (relevance obj regexps metrics)
   "Compute a \"relevance score\" for OBJ as a function of its number of
 matches of REGEXPS and accordingly to METRICS.  METRICS is list of
@@ -1687,13 +1695,28 @@ (define (relevance obj regexps metrics)
 
 A score of zero means that OBJ does not match any of REGEXPS.  The higher the
 score, the more relevant OBJ is to REGEXPS."
+  (define (string-match-term? regex-match str)
+    (let* ((start (match:start regex-match))
+           (char:start (if (= 0 start)
+                           #\nul
+                           (string-ref str (1- start))))
+           (end (match:end regex-match))
+           (char:end (if (= end (string-length str))
+                         #\nul
+                         (string-ref str end))))
+      (and (char-set-contains? char-set:delimiters char:start)
+           (char-set-contains? char-set:delimiters char:end))))
+
   (define (score regexp str)
     (fold-matches regexp str 0
                   (lambda (m score)
                     (+ score
-                       (if (string=? (match:substring m) str)
-                           5             ;exact match
-                           1)))))
+                       (cond
+                        ((string=? (match:substring m) str)
+                         5)             ;exact match
+                        ((string-match-term? m str)
+                         3)             ;XXX
+                        (else 1))))))
 
   (define (regexp->score regexp)
     (let ((score-regexp (lambda (str) (score regexp str))))

base-commit: 98bc13b9ea5f22a60de6c289d59072638001e08e
prerequisite-patch-id: 912de410e3d8a0796e83bfa50047debb0030b624
prerequisite-patch-id: 9c72d45734a13bd80021b14b562ed1b6238aa7ca
prerequisite-patch-id: 952cbe8dad322348d00f15125b512d34aaad8009
prerequisite-patch-id: fa6543fd5e6ec54a5036335aa5fa2b3a52675610
prerequisite-patch-id: cd68729ed441ec8235fde738e1f19669b570b099
prerequisite-patch-id: 53c5439602662bd61a3729aedf9327dfee5e9956
prerequisite-patch-id: a7edcd751c7a127f76b9c8e33ee425b6e800cfd7
prerequisite-patch-id: 29c1b2b9fcc017cff904ff3c1a32f65a6d54bad8
prerequisite-patch-id: 71757f95077bb7812f9d5a4e942c15b152ec7ac9
-- 
2.45.2