Package: guix-patches;
Reported by: aurtzy <aurtzy <at> gmail.com>
Date: Fri, 13 Sep 2024 07:04:02 UTC
Severity: normal
Tags: patch
Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):
From: aurtzy <aurtzy <at> gmail.com> To: guix-patches <at> gnu.org Cc: aurtzy <aurtzy <at> gmail.com> Subject: [PATCH] ui: Add more nuance to relevance scoring. Date: Fri, 13 Sep 2024 03:02:25 -0400
Fixes <https://issues.guix.gnu.org/70689>. * guix/ui.scm (char-set:word-border): New variable. (relevance): Update docstring. [whole-word-score, exact-match-score]: New variables. [score]: Score whole words such that matching a whole word will always put an object higher than another if the other does not match any whole words. Exact matches are given similar treatment. Score matches slightly higher than the baseline if they have one word boundary, with the assumption that they are more likely to be part of compound words rather than simply substrings. Only count a maximum of one scored match per field to limit putting too much weight on terms that happen to be very common. [score][string-ref-border?]: New procedure. Change-Id: I8e3d7a20bf296485355d1c191fe3fee5ef6490c8 --- Hello! This is an attempt to improve guix's search functionality for cases like the linked issue. Elaborating on some parts of my implementation: I opted to switch to counting a maximum of one match per field, which helps with cases where a common subword matches /many/ times in packages with longer descriptions, pushing more relevant packages down. In multi-term searches, the unique terms - which are naturally rarer - also contribute to a larger percentage of the score as a result of these changes. Having matches with only one word boundary be scored as 2 instead of 1 was done with the reasoning that a term is more likely to be part of a compound word name (and thus more relevant) if it is a prefix or suffix; for example, "gl" in OpenGL, "borg" in borgmatic, and "tor" in torbrowser. In an effort to minimize regressions in scoring, I've compiled a set of test cases and their expected results, which - if useful - might also be usable in future work: | Keyword(s) with poor | Expectations | | results before | | |-----------------------+-----------------------------------------------| | dig | ~bind~ near top. | | rsh | ~inetutils~ near top. | | c | C language related. | | c compiler | Compiler-related C stuff. | | r | R language related. | | tor | Tor related; ~torbrowser~ somewhere near top. | | gcc | ~gcc-toolchain~ near top. | |-----------------------+-----------------------------------------------| | Keyword(s) with mixed | | | results before | | |-----------------------+-----------------------------------------------| | gl | GL related. | | sh | Shell-related. | |-----------------------+-----------------------------------------------| | Keywords(s) with good | | | results before | | |-----------------------+-----------------------------------------------| | gcc toolchain | ~gcc-toolchain~ near top. | | python | ~python~ at top. | | python language | ~python~ at top. | | python minimal | ~python{,2}-minimal~ and friends near top. | | sync files | File synchronization related. | | sdl2 | ~sdl2~ at top. | However, some of these cases might be a bit too abstract, so I'm not sure how sufficient this testing is. Note that I only did minimal testing with =guix system search= and =guix home search= which - while seemingly fine - could be more rigorous (am I forgetting any other commands?). Going over the results of these changes on the test cases: There were notable improvements searching: - =rsh=: ~inetutils~ now shows up at the top when searching =rsh=, with another relevant (but previously buried) ~emacs-tramp~ at second place. - =c=: Searches for =c= return results related to the language now, whereas before it was a lot of unrelated packages that simply had the most =c= characters. - =dig=: While not the first result, ~bind~ is now displayed as tied for 3rd in relevance score, showing up within 10 packages. - =r=: Previously in a similar situation as C. Now ~r~ shows up at the top, with other R-related packages under it. - =gl=: The =gl= test case's results are slightly improved. Before, there were some non-relevant packages with the =gl= substring near the top, which is no longer the case. - =sh=: As a common subword, searching =sh= led to a mix of relevant and less relevant results at the top. A good majority are now shell-related. - =tor=: ~tor~ shows up on the top in both cases, but before with lots of non-relevant packages under it; the previously buried ~torbrowser~ now accompanies other more relevant results near the top. - =gcc=: ~gcc-toolchain~ is now a top result, compared to ~gccgo~ at the top before (and even ~gdc-toolchain~ also being higher; upstream name being "gcc" seems to have caused that). There are slight regressions with searching: - =sync files=: The new algorithm has a few less relevant results at the top compared to before, but otherwise seems like a shuffling of the old results. - =sdl2=: ~sdl2~'s top rank is overtaken by two libraries. If I didn't mention a test case from the table, it's probably because results were at least consistent or better (and I think I've written too much to read already). Closing this message on an unrelated note for future work: I stumbled on an interesting idea while looking for test cases which suggested reducing the score of a programming library when its language is not included in search terms. It's out of scope for the current issue, but I thought I'd mention it anyways for potential further improvements. Cheers, aurtzy guix/ui.scm | 54 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/guix/ui.scm b/guix/ui.scm index 966f0611f6..420f1f7501 100644 --- a/guix/ui.scm +++ b/guix/ui.scm @@ -19,6 +19,7 @@ ;;; Copyright © 2018 Steve Sprang <scs <at> stevesprang.com> ;;; Copyright © 2022 Taiju HIGASHI <higashi <at> taiju.info> ;;; Copyright © 2022 Liliana Marie Prikler <liliana.prikler <at> gmail.com> +;;; Copyright © 2024 aurtzy <aurtzy <at> gmail.com> ;;; ;;; This file is part of GNU Guix. ;;; @@ -1678,22 +1679,57 @@ (define* (package->recutils p port #:optional (width (terminal-columns)) ;;; Searching. ;;; +(define char-set:word-border (char-set-union char-set:digit + 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 -field/weight pairs, where FIELD is a procedure that returns a string or list -of strings describing OBJ, and WEIGHT is a positive integer denoting the -weight of this field in the final score. + "Compute a \"relevance score\" for OBJ as a function of its matches of REGEXPS and +accordingly to METRICS. METRICS is list of field/weight pairs, where FIELD is a +procedure that returns a string or list of strings describing OBJ, and WEIGHT is a +positive integer denoting the 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." + ;; Ensure that objects with whole word matches always score greater than (or equal + ;; to) objects that only match substrings. + (define whole-word-score (apply + (map (match-lambda + ((_ . weight) weight)) + metrics))) + (define exact-match-score (* whole-word-score 2)) + (define (score regexp str) + (define (string-ref-border? k) + (if (<= 0 k (1- (string-length str))) + (char-set-contains? char-set:word-border (string-ref str k)) + #t)) + (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) + exact-match-score) + ((>= score whole-word-score) + ;; No need to compute further if score is already max + ;; possible score + score) + (else + (let ((start-border? + (string-ref-border? (1- (match:start m)))) + (end-border? + (string-ref-border? (match:end m)))) + (max score + (cond + ((and start-border? end-border?) + whole-word-score) + ((or start-border? end-border?) + ;; If the match only has one border, it could still be + ;; part of a compound word, and thus be more likely to + ;; be relevant than if it was just a substring. + 2) + (else + 1))))))))) (define (regexp->score regexp) (let ((score-regexp (lambda (str) (score regexp str)))) base-commit: b6d5a7f5836739dab884b49a64ca354794dd845f -- 2.45.2
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.