This source file includes following definitions.
- Calculate
- Calculate
#include "chrome/browser/ui/app_list/search/tokenized_string_match.h"
#include "chrome/browser/ui/app_list/search/tokenized_string_char_iterator.h"
namespace app_list {
namespace {
const double kIsPrefixMultiplier = 1.0;
const double kIsFrontOfWordMultipler = 0.8;
const double kIsWeakHitMultiplier = 0.6;
const double kNoMatchScore = 0.0;
}
TokenizedStringMatch::TokenizedStringMatch()
: relevance_(kNoMatchScore) {}
TokenizedStringMatch::~TokenizedStringMatch() {}
bool TokenizedStringMatch::Calculate(const TokenizedString& query,
const TokenizedString& text) {
relevance_ = kNoMatchScore;
hits_.clear();
gfx::Range hit = gfx::Range::InvalidRange();
TokenizedStringCharIterator query_iter(query);
TokenizedStringCharIterator text_iter(text);
while (!query_iter.end() && !text_iter.end()) {
if (query_iter.Get() == text_iter.Get()) {
if (query_iter.GetArrayPos() == text_iter.GetArrayPos())
relevance_ += kIsPrefixMultiplier;
else if (text_iter.IsFirstCharOfToken())
relevance_ += kIsFrontOfWordMultipler;
else
relevance_ += kIsWeakHitMultiplier;
if (!hit.IsValid())
hit.set_start(text_iter.GetArrayPos());
hit.set_end(text_iter.GetArrayPos() + text_iter.GetCharSize());
query_iter.NextChar();
text_iter.NextChar();
} else {
if (hit.IsValid()) {
hits_.push_back(hit);
hit = gfx::Range::InvalidRange();
}
text_iter.NextToken();
}
}
if (!query_iter.end()) {
relevance_ = kNoMatchScore;
hits_.clear();
return false;
}
if (hit.IsValid())
hits_.push_back(hit);
if (text.text().length())
relevance_ /= text.text().length();
return relevance_ > kNoMatchScore;
}
bool TokenizedStringMatch::Calculate(const base::string16& query,
const base::string16& text) {
const TokenizedString tokenized_query(query);
const TokenizedString tokenized_text(text);
return Calculate(tokenized_query, tokenized_text);
}
}