Commit f2123bad authored by Moe Ahmadi's avatar Moe Ahmadi Committed by Commit Bot

Moves frecency score formula to shared location

This is done in order for Frecency score to be used outside of
LocalHistoryZeroSuggestProvider for repeatable quereis.

Bug: 1110077
Change-Id: I7c077d7c5215b88cf565c30e84556dffca55182b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2422153Reviewed-by: default avatarJustin Donnelly <jdonnelly@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Commit-Queue: Moe Ahmadi <mahmadi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#809143}
parent de37f24c
...@@ -8,6 +8,17 @@ namespace history { ...@@ -8,6 +8,17 @@ namespace history {
NormalizedKeywordSearchTermVisit::~NormalizedKeywordSearchTermVisit() = default; NormalizedKeywordSearchTermVisit::~NormalizedKeywordSearchTermVisit() = default;
double NormalizedKeywordSearchTermVisit::GetFrecency(
base::Time now,
int recency_decay_unit_sec,
double frequency_exponent) const {
const double recency_sec =
base::TimeDelta(now - most_recent_visit_time).InSeconds();
const double recency_decayed =
recency_decay_unit_sec / (recency_sec + recency_decay_unit_sec);
const double frequency_powered = pow(visits, frequency_exponent);
return frequency_powered * recency_decayed;
}
KeywordSearchTermVisit::KeywordSearchTermVisit() : visits(0) {} KeywordSearchTermVisit::KeywordSearchTermVisit() : visits(0) {}
......
...@@ -20,6 +20,19 @@ struct NormalizedKeywordSearchTermVisit { ...@@ -20,6 +20,19 @@ struct NormalizedKeywordSearchTermVisit {
NormalizedKeywordSearchTermVisit() = default; NormalizedKeywordSearchTermVisit() = default;
~NormalizedKeywordSearchTermVisit(); ~NormalizedKeywordSearchTermVisit();
// Returns the frecency score of the visit based on the following formula:
// (frequency ^ frequency_exponent) * recency_decay_unit_in_seconds
// frecency = ————————————————————————————————————————————————————————————————
// recency_in_seconds + recency_decay_unit_in_seconds
// This score combines frequency and recency of the visit favoring ones that
// are more frequent and more recent (see go/local-zps-frecency-ranking).
// |recency_decay_unit_sec| is the number of seconds until the recency
// component of the score decays to half. |frequency_exponent| is factor by
// which the frequency of the visit is exponentiated.
double GetFrecency(base::Time now,
int recency_decay_unit_sec,
double frequency_exponent) const;
base::string16 normalized_term; // The search term, in lower case and with base::string16 normalized_term; // The search term, in lower case and with
// extra whitespaces collapsed. // extra whitespaces collapsed.
int visits{0}; // The visit count. int visits{0}; // The visit count.
......
...@@ -94,21 +94,6 @@ bool AllowLocalHistoryZeroSuggestSuggestions(const AutocompleteInput& input) { ...@@ -94,21 +94,6 @@ bool AllowLocalHistoryZeroSuggestSuggestions(const AutocompleteInput& input) {
#endif #endif
} }
// Helper function for calculating frecency of a visit based on this formula:
//            (frequency ^ frequency_exponent) * recency_decay_unit_in_seconds
// frecency = ————————————————————————————————————————————————————————————————
//                   recency_in_seconds + recency_decay_unit_in_seconds
// a frecency score combines frequency and recency of occurrences favoring ones
// that are more frequent and more recent (see go/local-zps-frecency-ranking).
double CalculateFrecency(const history::NormalizedKeywordSearchTermVisit& visit,
base::Time now) {
const double recency_sec =
base::TimeDelta(now - visit.most_recent_visit_time).InSeconds();
const double recency_decayed = 60 / (recency_sec + 60);
const double frequency_powered = pow(visit.visits, 1.15);
return frequency_powered * recency_decayed;
}
} // namespace } // namespace
// static // static
...@@ -249,12 +234,16 @@ void LocalHistoryZeroSuggestProvider::QueryURLDatabase( ...@@ -249,12 +234,16 @@ void LocalHistoryZeroSuggestProvider::QueryURLDatabase(
bool frecency_ranking = base::FeatureList::IsEnabled( bool frecency_ranking = base::FeatureList::IsEnabled(
omnibox::kOmniboxLocalZeroSuggestFrecencyRanking); omnibox::kOmniboxLocalZeroSuggestFrecencyRanking);
const base::Time now = base::Time::Now(); const base::Time now = base::Time::Now();
std::sort(results.begin(), results.end(), const int kRecencyDecayUnitSec = 60;
[frecency_ranking, now](const auto& a, const auto& b) { const double kFrequencyExponent = 1.15;
auto CompareByFrecency = [&](const auto& a, const auto& b) {
return frecency_ranking return frecency_ranking
? CalculateFrecency(a, now) > CalculateFrecency(b, now) ? a.GetFrecency(now, kRecencyDecayUnitSec, kFrequencyExponent) >
b.GetFrecency(now, kRecencyDecayUnitSec,
kFrequencyExponent)
: a.most_recent_visit_time > b.most_recent_visit_time; : a.most_recent_visit_time > b.most_recent_visit_time;
}); };
std::sort(results.begin(), results.end(), CompareByFrecency);
int relevance = kLocalHistoryZeroSuggestRelevance; int relevance = kLocalHistoryZeroSuggestRelevance;
for (const auto& result : results) { for (const auto& result : results) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment