Skip to content

Commit 4ea2e24

Browse files
committed
feat: Add fuzzy search support for applications
add fuzzy search. PMS-BUG-288459
1 parent 65086ca commit 4ea2e24

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

src/models/searchfilterproxymodel.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,32 @@ bool SearchFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &
2626
const QString & displayName = modelIndex.data(Qt::DisplayRole).toString();
2727
const QString & name = modelIndex.data(AppsModel::NameRole).toString();
2828
const QString & transliterated = modelIndex.data(AppsModel::AllTransliteratedRole).toString();
29-
const QString & jianpin = Dtk::Core::firstLetters(displayName).join(',');
3029

31-
auto nameCopy = name;
32-
nameCopy = nameCopy.toLower();
33-
nameCopy.replace(" ", "");
34-
35-
return displayName.contains(searchPattern) || nameCopy.contains(searchPattern) || transliterated.contains(searchPattern) || jianpin.contains(searchPattern);
30+
QString pattern = searchPattern.pattern().toLower().remove(" ");
31+
32+
// 模糊匹配函数
33+
auto fuzzyMatch = [this](const QString & modelData, const QString & pattern) -> bool {
34+
if (modelData.contains(pattern, Qt::CaseInsensitive))
35+
{
36+
return true;
37+
}
38+
QString processedText = modelData.toLower().simplified();
39+
int textLen = processedText.length();
40+
int patternLen = pattern.length();
41+
std::vector<std::vector<int>> dp(textLen + 1, std::vector<int>(patternLen + 1, 0));
42+
for (int i = 1; i <= textLen; i++)
43+
{
44+
for (int j = 1; j <= patternLen; j++) {
45+
if (processedText[i - 1] == pattern[j - 1]) {
46+
dp[i][j] = dp[i - 1][j - 1] + 1;
47+
} else {
48+
dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);
49+
}
50+
}
51+
}
52+
53+
float matchScore = static_cast<float>(dp[textLen][patternLen]) / patternLen;
54+
return matchScore >= m_fuzzyThreshold;
55+
};
56+
return fuzzyMatch(displayName, pattern) || fuzzyMatch(name, pattern) || fuzzyMatch(transliterated, pattern);
3657
}

src/models/searchfilterproxymodel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ class SearchFilterProxyModel : public QSortFilterProxyModel
3131
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
3232
private:
3333
explicit SearchFilterProxyModel(QObject *parent = nullptr);
34+
float m_fuzzyThreshold = 0.8f;
3435
};

0 commit comments

Comments
 (0)