diff transform/TransformFactory.cpp @ 1842:19fa7bf208d8

Ensure search results that match the whole "phrase" get higher scores than any others
author Chris Cannam
date Fri, 17 Apr 2020 17:45:15 +0100
parents 5f8fbbde08ff
children 5b1b03c1d8d4
line wrap: on
line diff
--- a/transform/TransformFactory.cpp	Tue Apr 14 10:12:07 2020 +0100
+++ b/transform/TransformFactory.cpp	Fri Apr 17 17:45:15 2020 +0100
@@ -1110,11 +1110,36 @@
 {
     populateTransforms();
 
+    SearchResults results = searchUnadjusted(keywords);
+    
     if (keywords.size() > 1) {
-        // Additional score for all keywords in a row
-        keywords.push_back(keywords.join(" "));
+
+        // If there are any hits for all keywords in a row, put them
+        // in (replacing previous hits for the same transforms) but
+        // ensure they score more than any of the others
+
+        int maxScore = 0;
+        for (auto r: results) {
+            if (r.second.score > maxScore) {
+                maxScore = r.second.score;
+            }
+        }
+
+        QStringList oneBigKeyword;
+        oneBigKeyword << keywords.join(" ");
+        SearchResults oneBigKeywordResults = searchUnadjusted(oneBigKeyword);
+        for (auto r: oneBigKeywordResults) {
+            results[r.first] = r.second;
+            results[r.first].score += maxScore;
+        }
     }
 
+    return results;
+}
+
+TransformFactory::SearchResults
+TransformFactory::searchUnadjusted(QStringList keywords)
+{
     SearchResults results;
     TextMatcher matcher;
 
@@ -1177,6 +1202,26 @@
         if (match.score > 0) results[i->first] = match;
     }
 
+#ifdef DEBUG_TRANSFORM_FACTORY
+    SVCERR << "TransformFactory::search: keywords are: " << keywords.join(", ")
+           << endl;
+    int n = int(results.size()), i = 1;
+    SVCERR << "TransformFactory::search: results (" << n << "):" << endl;
+    
+    for (const auto &r: results) {
+        QStringList frags;
+        for (const auto &f: r.second.fragments) {
+            frags << QString("{\"%1\": \"%2\"}").arg(f.first).arg(f.second);
+        }
+        SVCERR << "[" << i << "/" << n << "] id " << r.first
+               << ": score " << r.second.score
+               << ", key " << r.second.key << ", fragments "
+               << frags.join(";") << endl;
+        ++i;
+    }
+    SVCERR << endl;
+#endif
+    
     return results;
 }