Skip to content

Commit

Permalink
fix: Using doble to store weights
Browse files Browse the repository at this point in the history
Using doble to store weights
  • Loading branch information
Clauszy committed May 22, 2024
1 parent 1d161b3 commit 02e2890
Show file tree
Hide file tree
Showing 13 changed files with 26 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/grand-search/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,9 @@ bool Utils::setWeightMethod(MatchedItem &item)
* @param keys 输入关键字列表
* @return 文件权重
*/
int Utils::calcFileWeight(const QString &path, const QString &name, const QStringList &keys)
double Utils::calcFileWeight(const QString &path, const QString &name, const QStringList &keys)
{
int weight = 0;
double weight = 0;
for (const QString &key : keys) {
if (name.contains(key)) {
weight += 43;
Expand Down Expand Up @@ -396,7 +396,7 @@ qint64 Utils::calcDateDiff(const QDateTime &date1, const QDateTime &date2)
* @param type 计算类型,创建/修改/访问
* @return 天数的权重
*/
int Utils::calcWeightByDateDiff(const qint64 &diff, const int &type)
double Utils::calcWeightByDateDiff(const qint64 &diff, const int &type)
{
switch (type) {
case CreateDateType :
Expand Down
4 changes: 2 additions & 2 deletions src/grand-search/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class Utils
static bool setWeightMethod(MatchedItem &item);

// 计算文件类的权重
static int calcFileWeight(const QString &path, const QString &name, const QStringList &keys);
static double calcFileWeight(const QString &path, const QString &name, const QStringList &keys);
static qint64 calcDateDiff(const QDateTime &date1, const QDateTime &date2);
static int calcWeightByDateDiff(const qint64 &diff, const int &type);
static double calcWeightByDateDiff(const qint64 &diff, const int &type);

// 计算应用和设置的权重
static double calcAppWeight(const MatchedItem &item, const QStringList &keys);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ bool AnythingQueryPrivate::timeToPush() const
return (m_time.elapsed() - m_lastPush) > 100;
}

int AnythingQueryPrivate::calcItemWeight(const QString &name)
double AnythingQueryPrivate::calcItemWeight(const QString &name)
{
int w = 0;
double w = 0;
for ( const QString &key : m_entity.keys) {
if (name.contains(key, Qt::CaseInsensitive))
w += 20;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AnythingQueryPrivate
QFileInfoList traverseDirAndFile(const QString &path);
QString getRegExp() const;
bool timeToPush() const;
int calcItemWeight(const QString &name);
double calcItemWeight(const QString &name);
public:
ComDeepinAnythingInterface *m_anythingInterface = nullptr;
SemanticEntity m_entity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ bool FeatureQueryPrivate::timeToPush() const
return (m_time.elapsed() - m_lastPush) > 100;
}

int FeatureQueryPrivate::matchedWeight(const QSet<QString> &back)
double FeatureQueryPrivate::matchedWeight(const QSet<QString> &back)
{
int w = 0;
double w = 0;
auto keys = m_entity.keys;
for (const QString &str : back) {
if (keys.isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class FeatureQueryPrivate
static bool processResult(const QString &file, const QSet<QString> &match, void *pdata);
FeatureLibEngine::QueryConditons translateConditons();
bool timeToPush() const;
int matchedWeight(const QSet<QString> &back);
double matchedWeight(const QSet<QString> &back);
public:
SemanticEntity m_entity;
FileResultsHandler *m_handler = nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ bool FullTextQueryPrivate::timeToPush() const
return (m_time.elapsed() - m_lastPush) > 100;
}

int FullTextQueryPrivate::matchedWeight(const QSet<QString> &back)
double FullTextQueryPrivate::matchedWeight(const QSet<QString> &back)
{
int w = 0;
double w = 0;
auto keys = m_entity.keys;
for (const QString &str : back) {
if (keys.isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class FullTextQueryPrivate
}
static bool processResult(const QString &file, void *pdata, void *ctx);
bool timeToPush() const;
int matchedWeight(const QSet<QString> &back);
double matchedWeight(const QSet<QString> &back);
public:
SemanticEntity m_entity;
FileResultsHandler *m_handler = nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ bool VectorQueryPrivate::timeToPush() const
return (m_time.elapsed() - m_lastPush) > 100;
}

int VectorQueryPrivate::matchedWeight(qreal distance)
double VectorQueryPrivate::matchedWeight(qreal distance)
{
if (distance < 0 || distance >= 1)
if (distance < 0 || distance >= 2)
return 0;

return 40 * (1 - distance);
return 80 * (1 - distance / 2.0);
}

VectorQuery::VectorQuery(QObject *parent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class VectorQueryPrivate
public:
explicit VectorQueryPrivate(VectorQuery *qq);
bool timeToPush() const;
int matchedWeight(qreal distance);
double matchedWeight(qreal distance);
public:
FileResultsHandler *m_handler = nullptr;
SemanticParser *m_parser = nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ class FileResultsHandler
explicit FileResultsHandler();
bool appendTo(const QString &file, MatchedItemMap &container);
bool isResultLimit() const;
inline int itemWeight(const QString &file) const{
inline double itemWeight(const QString &file) const{
QReadLocker lk(&m_lock);
return m_resultWeight.value(file, 0);
}

inline void setItemWeight(const QString &file, int w) {
inline void setItemWeight(const QString &file, double w) {
QWriteLocker lk(&m_lock);
m_resultWeight[file] = w;
}

inline QHash<QString, int> allItemWeight() const {
inline QHash<QString, double> allItemWeight() const {
QReadLocker lk(&m_lock);
return m_resultWeight;
}
Expand All @@ -47,7 +47,7 @@ class FileResultsHandler
protected:
mutable QReadWriteLock m_lock;
QSet<QString> m_tmpSearchResults; // 存储所有的搜索结果,用于去重
QHash<QString, int> m_resultWeight;
QHash<QString, double> m_resultWeight;
QHash<QString, QVariantHash> m_resultExtra;
QHash<FileSearchUtils::Group, quint32> m_resultCountHash; // 记录各类型文件搜索结果数量
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void SemanticWorkerPrivate::run(const QueryFunction &func)
func.function(func.object, &SemanticWorkerPrivate::pushItem, func.worker);
}

void SemanticWorkerPrivate::sortItems(MatchedItemMap &items, const QHash<QString, int> &weight)
void SemanticWorkerPrivate::sortItems(MatchedItemMap &items, const QHash<QString, double> &weight)
{
auto setExt = [](const MatchedItem &t, int w){
if (t.extra.isValid() &&
Expand All @@ -91,8 +91,8 @@ void SemanticWorkerPrivate::sortItems(MatchedItemMap &items, const QHash<QString
setExt(item, weight.value(item.item, 0));
} else {
std::stable_sort(list.begin(), list.end(), [&weight, setExt](const MatchedItem &t1, const MatchedItem &t2) {
int w1 = weight.value(t1.item, 0);
int w2 = weight.value(t2.item, 0);
double w1 = weight.value(t1.item, 0.0);
double w2 = weight.value(t2.item, 0.0);
setExt(t1, w1);
setExt(t2, w2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SemanticWorkerPrivate
//void tryNotify();
static bool pushItem(const MatchedItemMap &items, void *ptr);
static void run(const QueryFunction &func);
static void sortItems(MatchedItemMap &items, const QHash<QString, int> &weight);
static void sortItems(MatchedItemMap &items, const QHash<QString, double> &weight);
static void mergeExtra(MatchedItemMap &items, const QHash<QString, QVariantHash> &extra);
public:
QString m_context;
Expand Down

0 comments on commit 02e2890

Please sign in to comment.