From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id iOxCKk1F1F44PwAA0tVLHw (envelope-from ) for ; Mon, 01 Jun 2020 00:01:17 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id oC78JU1F1F6wRAAAbx9fmQ (envelope-from ) for ; Mon, 01 Jun 2020 00:01:17 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id A99AC9407C8 for ; Mon, 1 Jun 2020 00:01:16 +0000 (UTC) Received: from localhost ([::1]:37254 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jfXtD-0002oB-DP for larch@yhetil.org; Sun, 31 May 2020 20:01:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:38240) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jfXt2-0002l2-1o for guix-patches@gnu.org; Sun, 31 May 2020 20:01:04 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:50799) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jfXt1-00060r-NO for guix-patches@gnu.org; Sun, 31 May 2020 20:01:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1jfXt1-0003au-Mf for guix-patches@gnu.org; Sun, 31 May 2020 20:01:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#39258] [PATCH 2/4] ui: Use string matching with literal search strings. Resent-From: Arun Isaac Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Mon, 01 Jun 2020 00:01:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 39258 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: To: 39258@debbugs.gnu.org Cc: Arun Isaac Received: via spool by 39258-submit@debbugs.gnu.org id=B39258.159096964113753 (code B ref 39258); Mon, 01 Jun 2020 00:01:03 +0000 Received: (at 39258) by debbugs.gnu.org; 1 Jun 2020 00:00:41 +0000 Received: from localhost ([127.0.0.1]:34108 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jfXsf-0003Zf-85 for submit@debbugs.gnu.org; Sun, 31 May 2020 20:00:41 -0400 Received: from mugam.systemreboot.net ([139.59.75.54]:37186) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jfXsa-0003Yg-GY for 39258@debbugs.gnu.org; Sun, 31 May 2020 20:00:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=systemreboot.net; s=default; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=qZBdE0ZfKP/ze9QPewx5RIbs99IBZQjftJB+yWVWKGg=; b=C0W7eZewjmVG5gFSq8utnD0Zt1 5YPNbzIrscLxkKSLkqx1jRj4IMPBio7Cobpp+059lffHTWocwOJWytadxjU71IJ7xxZ1RMMAZ9w8S a0gRyX5EQ89skwtgm8MR5oXIyHxNDiT5Uppq2CV/ECvu1K/yEXa+hGK68oqM/1089otY=; Received: from [192.168.2.1] (helo=steel.lan) by systemreboot.net with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1jfXsX-000Zk2-5b; Mon, 01 Jun 2020 05:30:33 +0530 From: Arun Isaac Date: Mon, 1 Jun 2020 05:30:28 +0530 Message-Id: <20200601000030.7443-3-arunisaac@systemreboot.net> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200601000030.7443-1-arunisaac@systemreboot.net> References: <20200601000030.7443-1-arunisaac@systemreboot.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Score: 0.0 (/) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-Spam-Score: -1.0 (-) X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Scanner: scn0 Authentication-Results: aspmx1.migadu.com; dkim=fail (rsa verify failed) header.d=systemreboot.net header.s=default header.b=C0W7eZew; dmarc=fail reason="SPF not aligned (relaxed)" header.from=systemreboot.net (policy=none); spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Spam-Score: 5.09 X-TUID: CeyX/7tElB4e * guix/scripts/package.scm (process-query): Make search query a regexp only if it is not a literal search string. * guix/ui.scm (relevance): Use string matching with literal search strings and regexp matching with regexp search strings. --- guix/scripts/package.scm | 12 +++++++--- guix/ui.scm | 50 +++++++++++++++++++++++++--------------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/guix/scripts/package.scm b/guix/scripts/package.scm index 1246147798..1b637f7802 100644 --- a/guix/scripts/package.scm +++ b/guix/scripts/package.scm @@ -675,6 +675,11 @@ doesn't need it." (define (process-query opts) "Process any query specified by OPTS. Return #t when a query was actually processed, #f otherwise." + (define (regexp-pattern? str) + (string-any + (char-set #\. #\[ #\{ #\} #\( #\) #\\ #\* #\+ #\? #\| #\^ #\$) + str)) + (let* ((profiles (delete-duplicates (match (filter-map (match-lambda (('profile . p) p) @@ -781,11 +786,12 @@ processed, #f otherwise." (('search _) (let* ((patterns (filter-map (match-lambda - (('query 'search rx) rx) + (('query 'search (? regexp-pattern? rx)) + (make-regexp* rx regexp/icase)) + (('query 'search pattern) pattern) (_ #f)) opts)) - (regexps (map (cut make-regexp* <> regexp/icase) patterns)) - (matches (find-packages-by-description regexps))) + (matches (find-packages-by-description patterns))) (leave-on-EPIPE (display-search-results matches (current-output-port))) #t)) diff --git a/guix/ui.scm b/guix/ui.scm index 4a22358963..56754dba83 100644 --- a/guix/ui.scm +++ b/guix/ui.scm @@ -1489,41 +1489,53 @@ HYPERLINKS? is true, emit hyperlink escape sequences when appropriate." ;;; Searching. ;;; -(define (relevance obj regexps metrics) +(define (relevance obj patterns 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 +matches of PATTERNS 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." - (define (score regexp str) - (fold-matches regexp str 0 - (lambda (m score) - (+ score - (if (string=? (match:substring m) str) - 5 ;exact match - 1))))) - - (define (regexp->score regexp) - (let ((score-regexp (lambda (str) (score regexp str)))) +A score of zero means that OBJ does not match any of PATTERNS. The higher the +score, the more relevant OBJ is to PATTERNS." + (define (score pattern str) + (match pattern + ((? string? pattern) + (cond + ((string=? str pattern) 5) + (else + (let loop ((score 0) (start 0)) + (cond + ((string-contains-ci str pattern start) + => (lambda (index) + (loop (+ score 1) (+ index (string-length pattern))))) + (else score)))))) + ((? regexp? regexp) + (fold-matches regexp str 0 + (lambda (m score) + (+ score + (if (string=? (match:substring m) str) + 5 ;exact match + 1))))))) + + (define (pattern->score pattern) + (let ((score-pattern (lambda (str) (score pattern str)))) (fold (lambda (metric relevance) (match metric ((field . weight) (match (field obj) (#f relevance) ((? string? str) - (+ relevance (* (score-regexp str) weight))) + (+ relevance (* (score-pattern str) weight))) ((lst ...) - (+ relevance (* weight (apply + (map score-regexp lst))))))))) + (+ relevance (* weight (apply + (map score-pattern lst))))))))) 0 metrics))) - (let loop ((regexps regexps) + (let loop ((patterns patterns) (total-score 0)) - (match regexps + (match patterns ((head . tail) - (let ((score (regexp->score head))) + (let ((score (pattern->score head))) ;; Return zero if one of PATTERNS doesn't match. (cond ((zero? score) 0) -- 2.26.2