summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2019-06-25 22:59:58 +0200
committerLudovic Courtès <ludo@gnu.org>2019-06-27 11:14:40 +0200
commit8874faaaac665100a095ef25e39c9a389f5a397f (patch)
tree2637ca9b5f10a73f799c87792b3ba00d03921ad2
parentc25b44d640f709599e3c484a458ae452d99108e1 (diff)
ui: 'relevance' considers regexps connected with a logical and.
* guix/ui.scm (relevance)[score]: Change to return 0 when one of REGEXPS doesn't match. * tests/ui.scm ("package-relevance"): New test.
-rw-r--r--guix/ui.scm25
-rw-r--r--tests/ui.scm27
2 files changed, 40 insertions, 12 deletions
diff --git a/guix/ui.scm b/guix/ui.scm
index 0b4fe144b69..d9dbe4a652c 100644
--- a/guix/ui.scm
+++ b/guix/ui.scm
@@ -1256,17 +1256,20 @@ weight of this field in the final score.
1256A score of zero means that OBJ does not match any of REGEXPS. The higher the 1256A score of zero means that OBJ does not match any of REGEXPS. The higher the
1257score, the more relevant OBJ is to REGEXPS." 1257score, the more relevant OBJ is to REGEXPS."
1258 (define (score str) 1258 (define (score str)
1259 (let ((counts (map (lambda (regexp) 1259 (define scores
1260 (match (fold-matches regexp str '() cons) 1260 (map (lambda (regexp)
1261 (() 0) 1261 (fold-matches regexp str 0
1262 ((m) (if (string=? (match:substring m) str) 1262 (lambda (m score)
1263 5 ;exact match 1263 (+ score
1264 1)) 1264 (if (string=? (match:substring m) str)
1265 (lst (length lst)))) 1265 5 ;exact match
1266 regexps))) 1266 1)))))
1267 ;; Compute a score that's proportional to the number of regexps matched 1267 regexps))
1268 ;; and to the number of matches for each regexp. 1268
1269 (* (length counts) (reduce + 0 counts)))) 1269 ;; Return zero if one of REGEXPS doesn't match.
1270 (if (any zero? scores)
1271 0
1272 (reduce + 0 scores)))
1270 1273
1271 (fold (lambda (metric relevance) 1274 (fold (lambda (metric relevance)
1272 (match metric 1275 (match metric
diff --git a/tests/ui.scm b/tests/ui.scm
index 1e98e3534be..2138e23369b 100644
--- a/tests/ui.scm
+++ b/tests/ui.scm
@@ -1,5 +1,5 @@
1;;; GNU Guix --- Functional package management for GNU 1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org> 2;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2019 Ludovic Courtès <ludo@gnu.org>
3;;; 3;;;
4;;; This file is part of GNU Guix. 4;;; This file is part of GNU Guix.
5;;; 5;;;
@@ -22,10 +22,12 @@
22 #:use-module (guix profiles) 22 #:use-module (guix profiles)
23 #:use-module (guix store) 23 #:use-module (guix store)
24 #:use-module (guix derivations) 24 #:use-module (guix derivations)
25 #:use-module ((gnu packages) #:select (specification->package))
25 #:use-module (guix tests) 26 #:use-module (guix tests)
26 #:use-module (srfi srfi-1) 27 #:use-module (srfi srfi-1)
27 #:use-module (srfi srfi-11) 28 #:use-module (srfi srfi-11)
28 #:use-module (srfi srfi-19) 29 #:use-module (srfi srfi-19)
30 #:use-module (srfi srfi-26)
29 #:use-module (srfi srfi-64) 31 #:use-module (srfi srfi-64)
30 #:use-module (ice-9 regex)) 32 #:use-module (ice-9 regex))
31 33
@@ -260,4 +262,27 @@ Second line" 24))
260 "ISO-8859-1") 262 "ISO-8859-1")
261 (show-manifest-transaction store m t)))))))) 263 (show-manifest-transaction store m t))))))))
262 264
265(test-assert "package-relevance"
266 (let ((guile (specification->package "guile"))
267 (gcrypt (specification->package "guile-gcrypt"))
268 (go (specification->package "go"))
269 (gnugo (specification->package "gnugo"))
270 (rx (cut make-regexp <> regexp/icase))
271 (>0 (cut > <> 0))
272 (=0 zero?))
273 (and (>0 (package-relevance guile
274 (map rx '("scheme"))))
275 (>0 (package-relevance guile
276 (map rx '("scheme" "implementation"))))
277 (>0 (package-relevance gcrypt
278 (map rx '("guile" "crypto"))))
279 (=0 (package-relevance guile
280 (map rx '("guile" "crypto"))))
281 (>0 (package-relevance go
282 (map rx '("go"))))
283 (=0 (package-relevance go
284 (map rx '("go" "game"))))
285 (>0 (package-relevance gnugo
286 (map rx '("go" "game")))))))
287
263(test-end "ui") 288(test-end "ui")