Skip to content

Commit

Permalink
fuzzy matching uses smart case
Browse files Browse the repository at this point in the history
  • Loading branch information
NightMachinery committed Jan 12, 2024
1 parent d266336 commit 189b4f9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
6 changes: 3 additions & 3 deletions pdf_viewer/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ bool MySortFilterProxyModel::filterAcceptsRow(int source_row,
if (filterString.size() == 0) return true;
std::wstring s1 = filterString.toStdWString();
std::wstring s2 = key.toStdWString();
int score = static_cast<int>(rapidfuzz::fuzz::partial_ratio(s1, s2));
int score = calculate_partial_ratio(s1, s2);

return score > 50;
}
Expand Down Expand Up @@ -160,8 +160,8 @@ bool MySortFilterProxyModel::lessThan(const QModelIndex& left,
QString leftData = sourceModel()->data(left).toString();
QString rightData = sourceModel()->data(right).toString();

int left_score = static_cast<int>(rapidfuzz::fuzz::partial_ratio(filterString.toStdWString(), leftData.toStdWString()));
int right_score = static_cast<int>(rapidfuzz::fuzz::partial_ratio(filterString.toStdWString(), rightData.toStdWString()));
int left_score = calculate_partial_ratio(filterString.toStdWString(), leftData.toStdWString());
int right_score = calculate_partial_ratio(filterString.toStdWString(), rightData.toStdWString());
return left_score > right_score;
}
else {
Expand Down
32 changes: 26 additions & 6 deletions pdf_viewer/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
#include <qnetworkreply.h>
#include <qscreen.h>

#include "rapidfuzz_amalgamated.hpp"

#include <algorithm>
#include <locale>

#include <mupdf/pdf.h>

extern std::wstring LIBGEN_ADDRESS;
Expand All @@ -42,12 +47,11 @@ extern bool NUMERIC_TAGS;
#endif


std::wstring to_lower(const std::wstring& inp) {
std::wstring res;
for (char c : inp) {
res.push_back(::tolower(c));
}
return res;
std::wstring to_lower(const std::wstring& input) {
std::wstring output = input;
std::transform(output.begin(), output.end(), output.begin(),
[](wchar_t c) { return std::tolower(c, std::locale()); });
return output;
}

void get_flat_toc(const std::vector<TocNode*>& roots, std::vector<std::wstring>& output, std::vector<int>& pages) {
Expand Down Expand Up @@ -2256,3 +2260,19 @@ void convert_color4(float* in_color, int* out_color) {
out_color[2] = (int)(in_color[2] * 255);
out_color[3] = (int)(in_color[3] * 255);
}

// Your function with smart_case_p parameter
int calculate_partial_ratio(const std::wstring& filterString, const std::wstring& key, bool smart_case_p) {
std::wstring s1 = filterString;
std::wstring s2 = key;

// Convert strings to lowercase if smart_case_p is true and filterString is all lowercase
if (smart_case_p && is_all_lower(s1)) {
s1 = to_lower(s1);
s2 = to_lower(s2);
}

// Calculate the partial ratio score
int score = static_cast<int>(rapidfuzz::fuzz::partial_ratio(s1, s2));
return score;
}
4 changes: 4 additions & 0 deletions pdf_viewer/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,7 @@ void matmul(float m1[], float m2[], float result[]) {

void convert_color4(float* in_color, int* out_color);
std::string get_aplph_tag(int n, int max_n);

bool shouldTriggerDelete(QKeyEvent *key_event);

int calculate_partial_ratio(const std::wstring& filterString, const std::wstring& key, bool smart_case_p = true);

0 comments on commit 189b4f9

Please sign in to comment.