[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
01/01: ui: Increase relevance score for exact matches.
From: |
Ludovic Courtès |
Subject: |
01/01: ui: Increase relevance score for exact matches. |
Date: |
Fri, 29 Jun 2018 17:08:48 -0400 (EDT) |
civodul pushed a commit to branch master
in repository guix.
commit fd1395c4982dd8cf7fe9843317d1ebb1e5d78bee
Author: Ludovic Courtès <address@hidden>
Date: Fri Jun 29 12:17:41 2018 +0200
ui: Increase relevance score for exact matches.
Previously "guix package -s python" would have 'python2-zope-interface' as
its first result (relevance: 10), followed by many other python-*
packages with the same score, while 'python' itself would come
later (relevance: 7).
This change makes 'python' the first result (relevance: 27).
Reported by Gábor Boskovits.
* guix/ui.scm (relevance)[score]: Use 'fold-matches' instead of
'match:count' to counter the number of maches. Give more weight to
exact matches.
---
guix/ui.scm | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/guix/ui.scm b/guix/ui.scm
index ec70945..6996b7f 100644
--- a/guix/ui.scm
+++ b/guix/ui.scm
@@ -1222,11 +1222,14 @@ field in the final score.
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 (score str)
- (let ((counts (filter-map (lambda (regexp)
- (match (regexp-exec regexp str)
- (#f #f)
- (m (match:count m))))
- regexps)))
+ (let ((counts (map (lambda (regexp)
+ (match (fold-matches regexp str '() cons)
+ (() 0)
+ ((m) (if (string=? (match:substring m) str)
+ 5 ;exact match
+ 1))
+ (lst (length lst))))
+ regexps)))
;; Compute a score that's proportional to the number of regexps matched
;; and to the number of matches for each regexp.
(* (length counts) (reduce + 0 counts))))