From cba7dbdafc512991d1689d2097ca4e7135d7dc6f Mon Sep 17 00:00:00 2001 From: Guido Ranzuglia Date: Thu, 30 Mar 2017 09:10:24 +1100 Subject: [PATCH] - search engine now ranks the results giving precedence to the matching words contained in the action title --- src/common/searcher.cpp | 30 ++++++++++++++++++++++-------- src/common/searcher.h | 3 +-- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/common/searcher.cpp b/src/common/searcher.cpp index 4264c2d73..a69b5c978 100644 --- a/src/common/searcher.cpp +++ b/src/common/searcher.cpp @@ -80,29 +80,43 @@ void WordActionsMapAccessor::addSubStrings( QStringList& res ) const } RankedMatches::RankedMatches() -:wordmatchesperaction(),ranking() +:ranking() { } -int RankedMatches::computeRankedMatches( const QStringList& inputst,const WordActionsMap& map ) +int RankedMatches::computeRankedMatches( const QStringList& inputst,const WordActionsMap& map, bool matchesontitlearemoreimportant) { - wordmatchesperaction.clear(); + QMap wordmatchesperaction; ranking.clear(); - ranking.resize(inputst.size()); + int inputstsize = inputst.size(); + ranking.resize(inputstsize); + float bonuspertitleword = 0.0f; + if (matchesontitlearemoreimportant) + bonuspertitleword = 1.0f / pow(10,inputstsize); foreach(const QString& st,inputst) { QList res; bool found = map.getActionsPerWord(st,res); if (found) { - foreach(QAction* act,res) + foreach(QAction* act, res) + { ++wordmatchesperaction[act]; + QString title = act->text().toLower().trimmed(); + if (title.contains(st)) + wordmatchesperaction[act] = wordmatchesperaction[act] + bonuspertitleword; + } } } + + QMap > rankedmatches; + for (QMap::iterator it = wordmatchesperaction.begin(); it != wordmatchesperaction.end(); ++it) + rankedmatches[it.value()].push_back(it.key()); + int maxindex = -1; - for(QMap::iterator it = wordmatchesperaction.begin();it != wordmatchesperaction.end();++it) + for(QMap >::iterator it = rankedmatches.end() - 1;it != rankedmatches.begin() - 1;--it) { - int index = it.value() - 1; + int index = std::floor(it.key()) - 1; if (index >= ranking.size()) { throw InvalidInvariantException("WARNING! Index contained in wordmatchesperaction it's out-of-bound."); @@ -110,7 +124,7 @@ int RankedMatches::computeRankedMatches( const QStringList& inputst,const WordAc } if (index > maxindex) maxindex = index; - ranking[index].push_back(it.key()); + ranking[index].append(it.value()); } return maxindex + 1; } diff --git a/src/common/searcher.h b/src/common/searcher.h index 003361587..eb25dea4d 100644 --- a/src/common/searcher.h +++ b/src/common/searcher.h @@ -49,8 +49,7 @@ public: void getActionsWithNMatches(const int n,QList& res); private: friend int WordActionsMapAccessor::rankedMatchesPerInputString(const QString& input,RankedMatches& rm) const; - int computeRankedMatches(const QStringList& inputst,const WordActionsMap& map); - QMap wordmatchesperaction; + int computeRankedMatches(const QStringList& inputst,const WordActionsMap& map,bool matchesontitlearemoreimportant = true); QVector > ranking; };