From 8874faaaac665100a095ef25e39c9a389f5a397f Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Tue, 25 Jun 2019 22:59:58 +0200 Subject: 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. --- guix/ui.scm | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'guix') diff --git a/guix/ui.scm b/guix/ui.scm index 0b4fe144b6..d9dbe4a652 100644 --- a/guix/ui.scm +++ b/guix/ui.scm @@ -1256,17 +1256,20 @@ weight of this 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 (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)))) + (define scores + (map (lambda (regexp) + (fold-matches regexp str 0 + (lambda (m score) + (+ score + (if (string=? (match:substring m) str) + 5 ;exact match + 1))))) + regexps)) + + ;; Return zero if one of REGEXPS doesn't match. + (if (any zero? scores) + 0 + (reduce + 0 scores))) (fold (lambda (metric relevance) (match metric -- cgit v1.2.3