comparison 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
comparison
equal deleted inserted replaced
1841:627a7d7ada45 1842:19fa7bf208d8
1108 TransformFactory::SearchResults 1108 TransformFactory::SearchResults
1109 TransformFactory::search(QStringList keywords) 1109 TransformFactory::search(QStringList keywords)
1110 { 1110 {
1111 populateTransforms(); 1111 populateTransforms();
1112 1112
1113 SearchResults results = searchUnadjusted(keywords);
1114
1113 if (keywords.size() > 1) { 1115 if (keywords.size() > 1) {
1114 // Additional score for all keywords in a row 1116
1115 keywords.push_back(keywords.join(" ")); 1117 // If there are any hits for all keywords in a row, put them
1116 } 1118 // in (replacing previous hits for the same transforms) but
1117 1119 // ensure they score more than any of the others
1120
1121 int maxScore = 0;
1122 for (auto r: results) {
1123 if (r.second.score > maxScore) {
1124 maxScore = r.second.score;
1125 }
1126 }
1127
1128 QStringList oneBigKeyword;
1129 oneBigKeyword << keywords.join(" ");
1130 SearchResults oneBigKeywordResults = searchUnadjusted(oneBigKeyword);
1131 for (auto r: oneBigKeywordResults) {
1132 results[r.first] = r.second;
1133 results[r.first].score += maxScore;
1134 }
1135 }
1136
1137 return results;
1138 }
1139
1140 TransformFactory::SearchResults
1141 TransformFactory::searchUnadjusted(QStringList keywords)
1142 {
1118 SearchResults results; 1143 SearchResults results;
1119 TextMatcher matcher; 1144 TextMatcher matcher;
1120 1145
1121 for (TransformDescriptionMap::const_iterator i = m_transforms.begin(); 1146 for (TransformDescriptionMap::const_iterator i = m_transforms.begin();
1122 i != m_transforms.end(); ++i) { 1147 i != m_transforms.end(); ++i) {
1175 matcher.test(match, keywords, i->second.units, tr("Units"), 5); 1200 matcher.test(match, keywords, i->second.units, tr("Units"), 5);
1176 1201
1177 if (match.score > 0) results[i->first] = match; 1202 if (match.score > 0) results[i->first] = match;
1178 } 1203 }
1179 1204
1205 #ifdef DEBUG_TRANSFORM_FACTORY
1206 SVCERR << "TransformFactory::search: keywords are: " << keywords.join(", ")
1207 << endl;
1208 int n = int(results.size()), i = 1;
1209 SVCERR << "TransformFactory::search: results (" << n << "):" << endl;
1210
1211 for (const auto &r: results) {
1212 QStringList frags;
1213 for (const auto &f: r.second.fragments) {
1214 frags << QString("{\"%1\": \"%2\"}").arg(f.first).arg(f.second);
1215 }
1216 SVCERR << "[" << i << "/" << n << "] id " << r.first
1217 << ": score " << r.second.score
1218 << ", key " << r.second.key << ", fragments "
1219 << frags.join(";") << endl;
1220 ++i;
1221 }
1222 SVCERR << endl;
1223 #endif
1224
1180 return results; 1225 return results;
1181 } 1226 }
1182 1227