Skip to content

Commit e07f9cd

Browse files
authored
fuzzy: rank exact matches above popularity factor (#142)
1 parent 911eb30 commit e07f9cd

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/finders/Fuzzy.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <unistd.h>
88

99
#include <hyprutils/string/VarList2.hpp>
10+
#include <hyprutils/string/String.hpp>
1011

1112
using namespace Hyprutils::String;
1213

@@ -76,6 +77,7 @@ constexpr float MIN_SALIENT_MATCH = 0.3F;
7677
constexpr float MIN_TOKEN_MATCH = 0.15F;
7778
constexpr float POPULARITY_FACTOR = 0.08F;
7879
constexpr float NO_SALIENT_PENALTY = 0.01F;
80+
constexpr float EXACT_MATCH_SCORE = 2.0F;
7981

8082
//
8183
static float tokenBestMatch(std::string_view qt, std::string_view lastQ, const std::unordered_set<std::string_view>& cset, const std::vector<std::string_view>& cTok) {
@@ -115,6 +117,15 @@ static float tokenBestMatch(std::string_view qt, std::string_view lastQ, const s
115117
}
116118

117119
static float scoreCandidate(std::string_view query, std::string_view cand, float freq) {
120+
const float popFactor = 1.F + (POPULARITY_FACTOR * std::log1p(std::max(0.F, freq)));
121+
122+
// exact matches occupy a reserved band above any achievable fuzzy score, so a popular
123+
// partial match can never outrank them; popularity only orders matches within a band
124+
std::string queryLower{query};
125+
std::ranges::transform(queryLower, queryLower.begin(), ::tolower);
126+
if (trim(queryLower) == trim(std::string{cand}))
127+
return EXACT_MATCH_SCORE + popFactor;
128+
118129
CVarList2 qTokens(std::string{query}, 0, 's', true, false);
119130
CVarList2 cTokens(std::string{cand}, 0, 's', true, false);
120131

@@ -168,8 +179,6 @@ static float scoreCandidate(std::string_view query, std::string_view cand, float
168179
float lenDiff = float(std::abs(int(query.size()) - int(cand.size())));
169180
float lenFactor = std::exp(-lenDiff / 25.f);
170181

171-
float popFactor = 1.F + (POPULARITY_FACTOR * std::log1p(std::max(0.F, freq)));
172-
173182
return base * lenFactor * popFactor;
174183
}
175184

0 commit comments

Comments
 (0)