Give names to all utility processes.
[chromium-blink-merge.git] / chrome / browser / autocomplete / scored_history_match.cc
blobbf689ec70ffe43c82cd98013473a8e61a004dac9
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/autocomplete/scored_history_match.h"
7 #include <math.h>
9 #include <algorithm>
10 #include <vector>
12 #include "base/logging.h"
13 #include "base/numerics/safe_conversions.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_offset_string_conversions.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "chrome/browser/autocomplete/history_url_provider.h"
20 #include "components/bookmarks/browser/bookmark_utils.h"
21 #include "components/omnibox/omnibox_field_trial.h"
22 #include "components/omnibox/url_prefix.h"
23 #include "content/public/browser/browser_thread.h"
25 namespace {
27 // The number of days of recency scores to precompute.
28 const int kDaysToPrecomputeRecencyScoresFor = 366;
30 // The number of raw term score buckets use; raw term scores greater this are
31 // capped at the score of the largest bucket.
32 const int kMaxRawTermScore = 30;
34 // If true, assign raw scores to be max(whatever it normally would be, a score
35 // that's similar to the score HistoryURL provider would assign). This variable
36 // is set in the constructor by examining the field trial state.
37 const bool kAlsoDoHupLikeScoring = false;
39 // Pre-computed information to speed up calculating recency scores.
40 // |days_ago_to_recency_score| is a simple array mapping how long ago a page was
41 // visited (in days) to the recency score we should assign it. This allows easy
42 // lookups of scores without requiring math. This is initialized by
43 // InitDaysAgoToRecencyScoreArray called by
44 // ScoredHistoryMatch::Init().
45 float days_ago_to_recency_score[kDaysToPrecomputeRecencyScoresFor];
47 // Pre-computed information to speed up calculating topicality scores.
48 // |raw_term_score_to_topicality_score| is a simple array mapping how raw terms
49 // scores (a weighted sum of the number of hits for the term, weighted by how
50 // important the hit is: hostname, path, etc.) to the topicality score we should
51 // assign it. This allows easy lookups of scores without requiring math. This
52 // is initialized by InitRawTermScoreToTopicalityScoreArray() called from
53 // ScoredHistoryMatch::Init().
54 float raw_term_score_to_topicality_score[kMaxRawTermScore];
56 // The maximum score that can be assigned to non-inlineable matches. This is
57 // useful because often we want inlineable matches to come first (even if they
58 // don't sometimes score as well as non-inlineable matches) because if a
59 // non-inlineable match comes first than all matches will get demoted later in
60 // HistoryQuickProvider to non-inlineable scores. Set to -1 to indicate no
61 // maximum score.
62 int max_assigned_score_for_non_inlineable_matches = -1;
64 // Whether ScoredHistoryMatch::Init() has been called.
65 bool initialized = false;
67 // Precalculates raw_term_score_to_topicality_score, used in
68 // GetTopicalityScore().
69 void InitRawTermScoreToTopicalityScoreArray() {
70 for (int term_score = 0; term_score < kMaxRawTermScore; ++term_score) {
71 float topicality_score;
72 if (term_score < 10) {
73 // If the term scores less than 10 points (no full-credit hit, or
74 // no combination of hits that score that well), then the topicality
75 // score is linear in the term score.
76 topicality_score = 0.1 * term_score;
77 } else {
78 // For term scores of at least ten points, pass them through a log
79 // function so a score of 10 points gets a 1.0 (to meet up exactly
80 // with the linear component) and increases logarithmically until
81 // maxing out at 30 points, with computes to a score around 2.1.
82 topicality_score = (1.0 + 2.25 * log10(0.1 * term_score));
84 raw_term_score_to_topicality_score[term_score] = topicality_score;
88 // Pre-calculates days_ago_to_recency_score, used in GetRecencyScore().
89 void InitDaysAgoToRecencyScoreArray() {
90 for (int days_ago = 0; days_ago < kDaysToPrecomputeRecencyScoresFor;
91 days_ago++) {
92 int unnormalized_recency_score;
93 if (days_ago <= 4) {
94 unnormalized_recency_score = 100;
95 } else if (days_ago <= 14) {
96 // Linearly extrapolate between 4 and 14 days so 14 days has a score
97 // of 70.
98 unnormalized_recency_score = 70 + (14 - days_ago) * (100 - 70) / (14 - 4);
99 } else if (days_ago <= 31) {
100 // Linearly extrapolate between 14 and 31 days so 31 days has a score
101 // of 50.
102 unnormalized_recency_score = 50 + (31 - days_ago) * (70 - 50) / (31 - 14);
103 } else if (days_ago <= 90) {
104 // Linearly extrapolate between 30 and 90 days so 90 days has a score
105 // of 30.
106 unnormalized_recency_score = 30 + (90 - days_ago) * (50 - 30) / (90 - 30);
107 } else {
108 // Linearly extrapolate between 90 and 365 days so 365 days has a score
109 // of 10.
110 unnormalized_recency_score =
111 10 + (365 - days_ago) * (20 - 10) / (365 - 90);
113 days_ago_to_recency_score[days_ago] = unnormalized_recency_score / 100.0;
114 if (days_ago > 0) {
115 DCHECK_LE(days_ago_to_recency_score[days_ago],
116 days_ago_to_recency_score[days_ago - 1]);
121 } // namespace
123 // static
124 const size_t ScoredHistoryMatch::kMaxVisitsToScore = 10;
125 int ScoredHistoryMatch::bookmark_value_ = 1;
126 bool ScoredHistoryMatch::fix_frequency_bugs_ = false;
127 bool ScoredHistoryMatch::allow_tld_matches_ = false;
128 bool ScoredHistoryMatch::allow_scheme_matches_ = false;
129 size_t ScoredHistoryMatch::num_title_words_to_allow_ = 10u;
130 bool ScoredHistoryMatch::hqp_experimental_scoring_enabled_ = false;
131 float ScoredHistoryMatch::topicality_threshold_ = -1;
132 std::vector<ScoredHistoryMatch::ScoreMaxRelevance>*
133 ScoredHistoryMatch::hqp_relevance_buckets_ = nullptr;
135 ScoredHistoryMatch::ScoredHistoryMatch() : raw_score(0), can_inline(false) {
138 ScoredHistoryMatch::ScoredHistoryMatch(
139 const history::URLRow& row,
140 const VisitInfoVector& visits,
141 const std::string& languages,
142 const base::string16& lower_string,
143 const String16Vector& terms_vector,
144 const WordStarts& terms_to_word_starts_offsets,
145 const RowWordStarts& word_starts,
146 bool is_url_bookmarked,
147 base::Time now)
148 : HistoryMatch(row, 0, false, false), raw_score(0), can_inline(false) {
149 GURL gurl = row.url();
150 if (!gurl.is_valid())
151 return;
153 ScoredHistoryMatch::Init();
155 // Figure out where each search term appears in the URL and/or page title
156 // so that we can score as well as provide autocomplete highlighting.
157 base::OffsetAdjuster::Adjustments adjustments;
158 base::string16 url =
159 bookmarks::CleanUpUrlForMatching(gurl, languages, &adjustments);
160 base::string16 title = bookmarks::CleanUpTitleForMatching(row.title());
161 int term_num = 0;
162 for (const auto& term : terms_vector) {
163 TermMatches url_term_matches = MatchTermInString(term, url, term_num);
164 TermMatches title_term_matches = MatchTermInString(term, title, term_num);
165 if (url_term_matches.empty() && title_term_matches.empty()) {
166 // A term was not found in either URL or title - reject.
167 return;
169 url_matches.insert(url_matches.end(), url_term_matches.begin(),
170 url_term_matches.end());
171 title_matches.insert(title_matches.end(), title_term_matches.begin(),
172 title_term_matches.end());
173 ++term_num;
176 // Sort matches by offset and eliminate any which overlap.
177 // TODO(mpearson): Investigate whether this has any meaningful
178 // effect on scoring. (It's necessary at some point: removing
179 // overlaps and sorting is needed to decide what to highlight in the
180 // suggestion string. But this sort and de-overlap doesn't have to
181 // be done before scoring.)
182 url_matches = SortAndDeoverlapMatches(url_matches);
183 title_matches = SortAndDeoverlapMatches(title_matches);
185 // We can inline autocomplete a match if:
186 // 1) there is only one search term
187 // 2) AND the match begins immediately after one of the prefixes in
188 // URLPrefix such as http://www and https:// (note that one of these
189 // is the empty prefix, for cases where the user has typed the scheme)
190 // 3) AND the search string does not end in whitespace (making it look to
191 // the IMUI as though there is a single search term when actually there
192 // is a second, empty term).
193 // |best_inlineable_prefix| stores the inlineable prefix computed in
194 // clause (2) or NULL if no such prefix exists. (The URL is not inlineable.)
195 // Note that using the best prefix here means that when multiple
196 // prefixes match, we'll choose to inline following the longest one.
197 // For a URL like "http://www.washingtonmutual.com", this means
198 // typing "w" will inline "ashington..." instead of "ww.washington...".
199 if (!url_matches.empty() && (terms_vector.size() == 1) &&
200 !IsWhitespace(*lower_string.rbegin())) {
201 const base::string16 gurl_spec = base::UTF8ToUTF16(gurl.spec());
202 const URLPrefix* best_inlineable_prefix =
203 URLPrefix::BestURLPrefix(gurl_spec, terms_vector[0]);
204 if (best_inlineable_prefix) {
205 // Initialize innermost_match.
206 // The idea here is that matches that occur in the scheme or
207 // "www." are worse than matches which don't. For the URLs
208 // "http://www.google.com" and "http://wellsfargo.com", we want
209 // the omnibox input "w" to cause the latter URL to rank higher
210 // than the former. Note that this is not the same as checking
211 // whether one match's inlinable prefix has more components than
212 // the other match's, since in this example, both matches would
213 // have an inlinable prefix of "http://", which is one component.
215 // Instead, we look for the overall best (i.e., most components)
216 // prefix of the current URL, and then check whether the inlinable
217 // prefix has that many components. If it does, this is an
218 // "innermost" match, and should be boosted. In the example
219 // above, the best prefixes for the two URLs have two and one
220 // components respectively, while the inlinable prefixes each
221 // have one component; this means the first match is not innermost
222 // and the second match is innermost, resulting in us boosting the
223 // second match.
225 // Now, the code that implements this.
226 // The deepest prefix for this URL regardless of where the match is.
227 const URLPrefix* best_prefix =
228 URLPrefix::BestURLPrefix(gurl_spec, base::string16());
229 DCHECK(best_prefix);
230 // If the URL is inlineable, we must have a match. Note the prefix that
231 // makes it inlineable may be empty.
232 can_inline = true;
233 innermost_match =
234 best_inlineable_prefix->num_components == best_prefix->num_components;
238 const float topicality_score = GetTopicalityScore(
239 terms_vector.size(), url, terms_to_word_starts_offsets, word_starts);
240 const float frequency_score = GetFrequency(now, is_url_bookmarked, visits);
241 raw_score = base::saturated_cast<int>(GetFinalRelevancyScore(
242 topicality_score, frequency_score, *hqp_relevance_buckets_));
244 if (kAlsoDoHupLikeScoring && can_inline) {
245 // HistoryURL-provider-like scoring gives any match that is
246 // capable of being inlined a certain minimum score. Some of these
247 // are given a higher score that lets them be shown in inline.
248 // This test here derives from the test in
249 // HistoryURLProvider::PromoteMatchForInlineAutocomplete().
250 const bool promote_to_inline =
251 (row.typed_count() > 1) || (IsHostOnly() && (row.typed_count() == 1));
252 int hup_like_score =
253 promote_to_inline
254 ? HistoryURLProvider::kScoreForBestInlineableResult
255 : HistoryURLProvider::kBaseScoreForNonInlineableResult;
257 // Also, if the user types the hostname of a host with a typed
258 // visit, then everything from that host get given inlineable scores
259 // (because the URL-that-you-typed will go first and everything
260 // else will be assigned one minus the previous score, as coded
261 // at the end of HistoryURLProvider::DoAutocomplete().
262 if (base::UTF8ToUTF16(gurl.host()) == terms_vector[0])
263 hup_like_score = HistoryURLProvider::kScoreForBestInlineableResult;
265 // HistoryURLProvider has the function PromoteOrCreateShorterSuggestion()
266 // that's meant to promote prefixes of the best match (if they've
267 // been visited enough related to the best match) or
268 // create/promote host-only suggestions (even if they've never
269 // been typed). The code is complicated and we don't try to
270 // duplicate the logic here. Instead, we handle a simple case: in
271 // low-typed-count ranges, give host-only matches (i.e.,
272 // http://www.foo.com/ vs. http://www.foo.com/bar.html) a boost so
273 // that the host-only match outscores all the other matches that
274 // would normally have the same base score. This behavior is not
275 // identical to what happens in HistoryURLProvider even in these
276 // low typed count ranges--sometimes it will create/promote when
277 // this test does not (indeed, we cannot create matches like HUP
278 // can) and vice versa--but the underlying philosophy is similar.
279 if (!promote_to_inline && IsHostOnly())
280 hup_like_score++;
282 // All the other logic to goes into hup-like-scoring happens in
283 // the tie-breaker case of MatchScoreGreater().
285 // Incorporate hup_like_score into raw_score.
286 raw_score = std::max(raw_score, hup_like_score);
289 // If this match is not inlineable and there's a cap on the maximum
290 // score that can be given to non-inlineable matches, apply the cap.
291 if (!can_inline && (max_assigned_score_for_non_inlineable_matches != -1)) {
292 raw_score =
293 std::min(raw_score, max_assigned_score_for_non_inlineable_matches);
296 // Now that we're done processing this entry, correct the offsets of the
297 // matches in |url_matches| so they point to offsets in the original URL
298 // spec, not the cleaned-up URL string that we used for matching.
299 std::vector<size_t> offsets = OffsetsFromTermMatches(url_matches);
300 base::OffsetAdjuster::UnadjustOffsets(adjustments, &offsets);
301 url_matches = ReplaceOffsetsInTermMatches(url_matches, offsets);
304 ScoredHistoryMatch::~ScoredHistoryMatch() {
307 // Comparison function for sorting ScoredMatches by their scores with
308 // intelligent tie-breaking.
309 bool ScoredHistoryMatch::MatchScoreGreater(const ScoredHistoryMatch& m1,
310 const ScoredHistoryMatch& m2) {
311 if (m1.raw_score != m2.raw_score)
312 return m1.raw_score > m2.raw_score;
314 // This tie-breaking logic is inspired by / largely copied from the
315 // ordering logic in history_url_provider.cc CompareHistoryMatch().
317 // A URL that has been typed at all is better than one that has never been
318 // typed. (Note "!"s on each side.)
319 if (!m1.url_info.typed_count() != !m2.url_info.typed_count())
320 return m1.url_info.typed_count() > m2.url_info.typed_count();
322 // Innermost matches (matches after any scheme or "www.") are better than
323 // non-innermost matches.
324 if (m1.innermost_match != m2.innermost_match)
325 return m1.innermost_match;
327 // URLs that have been typed more often are better.
328 if (m1.url_info.typed_count() != m2.url_info.typed_count())
329 return m1.url_info.typed_count() > m2.url_info.typed_count();
331 // For URLs that have each been typed once, a host (alone) is better
332 // than a page inside.
333 if (m1.url_info.typed_count() == 1) {
334 if (m1.IsHostOnly() != m2.IsHostOnly())
335 return m1.IsHostOnly();
338 // URLs that have been visited more often are better.
339 if (m1.url_info.visit_count() != m2.url_info.visit_count())
340 return m1.url_info.visit_count() > m2.url_info.visit_count();
342 // URLs that have been visited more recently are better.
343 return m1.url_info.last_visit() > m2.url_info.last_visit();
346 // static
347 TermMatches ScoredHistoryMatch::FilterTermMatchesByWordStarts(
348 const TermMatches& term_matches,
349 const WordStarts& terms_to_word_starts_offsets,
350 const WordStarts& word_starts,
351 size_t start_pos,
352 size_t end_pos) {
353 // Return early if no filtering is needed.
354 if (start_pos == std::string::npos)
355 return term_matches;
356 TermMatches filtered_matches;
357 WordStarts::const_iterator next_word_starts = word_starts.begin();
358 WordStarts::const_iterator end_word_starts = word_starts.end();
359 for (const auto& term_match : term_matches) {
360 const size_t term_offset =
361 terms_to_word_starts_offsets[term_match.term_num];
362 // Advance next_word_starts until it's >= the position of the term we're
363 // considering (adjusted for where the word begins within the term).
364 while ((next_word_starts != end_word_starts) &&
365 (*next_word_starts < (term_match.offset + term_offset)))
366 ++next_word_starts;
367 // Add the match if it's before the position we start filtering at or
368 // after the position we stop filtering at (assuming we have a position
369 // to stop filtering at) or if it's at a word boundary.
370 if ((term_match.offset < start_pos) ||
371 ((end_pos != std::string::npos) && (term_match.offset >= end_pos)) ||
372 ((next_word_starts != end_word_starts) &&
373 (*next_word_starts == term_match.offset + term_offset)))
374 filtered_matches.push_back(term_match);
376 return filtered_matches;
379 // static
380 void ScoredHistoryMatch::Init() {
381 // Because the code below is not thread safe, we check that we're only calling
382 // it from one thread: the UI thread. Specifically, we check "if we've heard
383 // of the UI thread then we'd better be on it." The first part is necessary
384 // so unit tests pass. (Many unit tests don't set up the threading naming
385 // system; hence CurrentlyOn(UI thread) will fail.)
386 using content::BrowserThread;
387 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
388 BrowserThread::CurrentlyOn(BrowserThread::UI));
390 if (initialized)
391 return;
393 initialized = true;
395 // When doing HUP-like scoring, don't allow a non-inlineable match
396 // to beat the score of good inlineable matches. This is a problem
397 // because if a non-inlineable match ends up with the highest score
398 // from HistoryQuick provider, all HistoryQuick matches get demoted
399 // to non-inlineable scores (scores less than 1200). Without
400 // HUP-like-scoring, these results would actually come from the HUP
401 // and not be demoted, thus outscoring the demoted HQP results.
402 // When the HQP provides these, we need to clamp the non-inlineable
403 // results to preserve this behavior.
404 if (kAlsoDoHupLikeScoring) {
405 max_assigned_score_for_non_inlineable_matches =
406 HistoryURLProvider::kScoreForBestInlineableResult - 1;
408 bookmark_value_ = OmniboxFieldTrial::HQPBookmarkValue();
409 fix_frequency_bugs_ = OmniboxFieldTrial::HQPFixFrequencyScoringBugs();
410 allow_tld_matches_ = OmniboxFieldTrial::HQPAllowMatchInTLDValue();
411 allow_scheme_matches_ = OmniboxFieldTrial::HQPAllowMatchInSchemeValue();
412 num_title_words_to_allow_ = OmniboxFieldTrial::HQPNumTitleWordsToAllow();
414 InitRawTermScoreToTopicalityScoreArray();
415 InitDaysAgoToRecencyScoreArray();
416 InitHQPExperimentalParams();
419 float ScoredHistoryMatch::GetTopicalityScore(
420 const int num_terms,
421 const base::string16& url,
422 const WordStarts& terms_to_word_starts_offsets,
423 const RowWordStarts& word_starts) {
424 ScoredHistoryMatch::Init();
425 // A vector that accumulates per-term scores. The strongest match--a
426 // match in the hostname at a word boundary--is worth 10 points.
427 // Everything else is less. In general, a match that's not at a word
428 // boundary is worth about 1/4th or 1/5th of a match at the word boundary
429 // in the same part of the URL/title.
430 DCHECK_GT(num_terms, 0);
431 std::vector<int> term_scores(num_terms, 0);
432 WordStarts::const_iterator next_word_starts =
433 word_starts.url_word_starts_.begin();
434 WordStarts::const_iterator end_word_starts =
435 word_starts.url_word_starts_.end();
436 const size_t question_mark_pos = url.find('?');
437 const size_t colon_pos = url.find(':');
438 // The + 3 skips the // that probably appears in the protocol
439 // after the colon. If the protocol doesn't have two slashes after
440 // the colon, that's okay--all this ends up doing is starting our
441 // search for the next / a few characters into the hostname. The
442 // only times this can cause problems is if we have a protocol without
443 // a // after the colon and the hostname is only one or two characters.
444 // This isn't worth worrying about.
445 const size_t end_of_hostname_pos = (colon_pos != std::string::npos)
446 ? url.find('/', colon_pos + 3)
447 : url.find('/');
448 size_t last_part_of_hostname_pos = (end_of_hostname_pos != std::string::npos)
449 ? url.rfind('.', end_of_hostname_pos)
450 : url.rfind('.');
451 // Loop through all URL matches and score them appropriately.
452 // First, filter all matches not at a word boundary and in the path (or
453 // later).
454 url_matches = FilterTermMatchesByWordStarts(
455 url_matches, terms_to_word_starts_offsets, word_starts.url_word_starts_,
456 end_of_hostname_pos, std::string::npos);
457 if (colon_pos != std::string::npos) {
458 // Also filter matches not at a word boundary and in the scheme.
459 url_matches = FilterTermMatchesByWordStarts(
460 url_matches, terms_to_word_starts_offsets, word_starts.url_word_starts_,
461 0, colon_pos);
463 for (const auto& url_match : url_matches) {
464 const size_t term_offset = terms_to_word_starts_offsets[url_match.term_num];
465 // Advance next_word_starts until it's >= the position of the term we're
466 // considering (adjusted for where the word begins within the term).
467 while ((next_word_starts != end_word_starts) &&
468 (*next_word_starts < (url_match.offset + term_offset))) {
469 ++next_word_starts;
471 const bool at_word_boundary =
472 (next_word_starts != end_word_starts) &&
473 (*next_word_starts == url_match.offset + term_offset);
474 if ((question_mark_pos != std::string::npos) &&
475 (url_match.offset > question_mark_pos)) {
476 // The match is in a CGI ?... fragment.
477 DCHECK(at_word_boundary);
478 term_scores[url_match.term_num] += 5;
479 } else if ((end_of_hostname_pos != std::string::npos) &&
480 (url_match.offset > end_of_hostname_pos)) {
481 // The match is in the path.
482 DCHECK(at_word_boundary);
483 term_scores[url_match.term_num] += 8;
484 } else if ((colon_pos == std::string::npos) ||
485 (url_match.offset > colon_pos)) {
486 // The match is in the hostname.
487 if ((last_part_of_hostname_pos == std::string::npos) ||
488 (url_match.offset < last_part_of_hostname_pos)) {
489 // Either there are no dots in the hostname or this match isn't
490 // the last dotted component.
491 term_scores[url_match.term_num] += at_word_boundary ? 10 : 2;
492 } else {
493 // The match is in the last part of a dotted hostname (usually this
494 // is the top-level domain .com, .net, etc.).
495 if (allow_tld_matches_)
496 term_scores[url_match.term_num] += at_word_boundary ? 10 : 0;
498 } else {
499 // The match is in the protocol (a.k.a. scheme).
500 // Matches not at a word boundary should have been filtered already.
501 DCHECK(at_word_boundary);
502 match_in_scheme = true;
503 if (allow_scheme_matches_)
504 term_scores[url_match.term_num] += 10;
507 // Now do the analogous loop over all matches in the title.
508 next_word_starts = word_starts.title_word_starts_.begin();
509 end_word_starts = word_starts.title_word_starts_.end();
510 size_t word_num = 0;
511 title_matches = FilterTermMatchesByWordStarts(
512 title_matches, terms_to_word_starts_offsets,
513 word_starts.title_word_starts_, 0, std::string::npos);
514 for (const auto& title_match : title_matches) {
515 const size_t term_offset =
516 terms_to_word_starts_offsets[title_match.term_num];
517 // Advance next_word_starts until it's >= the position of the term we're
518 // considering (adjusted for where the word begins within the term).
519 while ((next_word_starts != end_word_starts) &&
520 (*next_word_starts < (title_match.offset + term_offset))) {
521 ++next_word_starts;
522 ++word_num;
524 if (word_num >= num_title_words_to_allow_)
525 break; // only count the first ten words
526 DCHECK(next_word_starts != end_word_starts);
527 DCHECK_EQ(*next_word_starts, title_match.offset + term_offset)
528 << "not at word boundary";
529 term_scores[title_match.term_num] += 8;
531 // TODO(mpearson): Restore logic for penalizing out-of-order matches.
532 // (Perhaps discount them by 0.8?)
533 // TODO(mpearson): Consider: if the earliest match occurs late in the string,
534 // should we discount it?
535 // TODO(mpearson): Consider: do we want to score based on how much of the
536 // input string the input covers? (I'm leaning toward no.)
538 // Compute the topicality_score as the sum of transformed term_scores.
539 float topicality_score = 0;
540 for (int term_score : term_scores) {
541 // Drop this URL if it seems like a term didn't appear or, more precisely,
542 // didn't appear in a part of the URL or title that we trust enough
543 // to give it credit for. For instance, terms that appear in the middle
544 // of a CGI parameter get no credit. Almost all the matches dropped
545 // due to this test would look stupid if shown to the user.
546 if (term_score == 0)
547 return 0;
548 topicality_score += raw_term_score_to_topicality_score[std::min(
549 term_score, kMaxRawTermScore - 1)];
551 // TODO(mpearson): If there are multiple terms, consider taking the
552 // geometric mean of per-term scores rather than the arithmetic mean.
554 const float final_topicality_score = topicality_score / num_terms;
556 // Demote the URL if the topicality score is less than threshold.
557 if (hqp_experimental_scoring_enabled_ &&
558 (final_topicality_score < topicality_threshold_)) {
559 return 0.0;
562 return final_topicality_score;
565 // static
566 float ScoredHistoryMatch::GetRecencyScore(int last_visit_days_ago) {
567 ScoredHistoryMatch::Init();
568 // Lookup the score in days_ago_to_recency_score, treating
569 // everything older than what we've precomputed as the oldest thing
570 // we've precomputed. The std::max is to protect against corruption
571 // in the database (in case last_visit_days_ago is negative).
572 return days_ago_to_recency_score[std::max(
573 std::min(last_visit_days_ago, kDaysToPrecomputeRecencyScoresFor - 1), 0)];
576 // static
577 float ScoredHistoryMatch::GetFrequency(const base::Time& now,
578 const bool bookmarked,
579 const VisitInfoVector& visits) {
580 // Compute the weighted average |value_of_transition| over the last at
581 // most kMaxVisitsToScore visits, where each visit is weighted using
582 // GetRecencyScore() based on how many days ago it happened. Use
583 // kMaxVisitsToScore as the denominator for the average regardless of
584 // how many visits there were in order to penalize a match that has
585 // fewer visits than kMaxVisitsToScore.
586 float summed_visit_points = 0;
587 const size_t max_visit_to_score =
588 std::min(visits.size(), ScoredHistoryMatch::kMaxVisitsToScore);
589 for (size_t i = 0; i < max_visit_to_score; ++i) {
590 const ui::PageTransition page_transition = fix_frequency_bugs_ ?
591 ui::PageTransitionStripQualifier(visits[i].second) : visits[i].second;
592 int value_of_transition =
593 (page_transition == ui::PAGE_TRANSITION_TYPED) ? 20 : 1;
594 if (bookmarked)
595 value_of_transition = std::max(value_of_transition, bookmark_value_);
596 const float bucket_weight =
597 GetRecencyScore((now - visits[i].first).InDays());
598 summed_visit_points += (value_of_transition * bucket_weight);
600 if (fix_frequency_bugs_)
601 return summed_visit_points / ScoredHistoryMatch::kMaxVisitsToScore;
602 return visits.size() * summed_visit_points /
603 ScoredHistoryMatch::kMaxVisitsToScore;
606 // static
607 float ScoredHistoryMatch::GetFinalRelevancyScore(
608 float topicality_score,
609 float frequency_score,
610 const std::vector<ScoreMaxRelevance>& hqp_relevance_buckets) {
611 DCHECK(hqp_relevance_buckets.size() > 0);
612 DCHECK_EQ(hqp_relevance_buckets[0].first, 0.0);
614 if (topicality_score == 0)
615 return 0;
616 // Here's how to interpret intermediate_score: Suppose the omnibox
617 // has one input term. Suppose we have a URL for which the omnibox
618 // input term has a single URL hostname hit at a word boundary. (This
619 // implies topicality_score = 1.0.). Then the intermediate_score for
620 // this URL will depend entirely on the frequency_score with
621 // this interpretation:
622 // - a single typed visit more than three months ago, no other visits -> 0.2
623 // - a visit every three days, no typed visits -> 0.706
624 // - a visit every day, no typed visits -> 0.916
625 // - a single typed visit yesterday, no other visits -> 2.0
626 // - a typed visit once a week -> 11.77
627 // - a typed visit every three days -> 14.12
628 // - at least ten typed visits today -> 20.0 (maximum score)
630 // The below code maps intermediate_score to the range [0, 1399].
631 // For example:
632 // HQP default scoring buckets: "0.0:400,1.5:600,12.0:1300,20.0:1399"
633 // We will linearly interpolate the scores between:
634 // 0 to 1.5 --> 400 to 600
635 // 1.5 to 12.0 --> 600 to 1300
636 // 12.0 to 20.0 --> 1300 to 1399
637 // >= 20.0 --> 1399
639 // The score maxes out at 1399 (i.e., cannot beat a good inlineable result
640 // from HistoryURL provider).
641 const float intermediate_score = topicality_score * frequency_score;
643 // Find the threshold where intermediate score is greater than bucket.
644 size_t i = 1;
645 for (; i < hqp_relevance_buckets.size(); ++i) {
646 const ScoreMaxRelevance& hqp_bucket = hqp_relevance_buckets[i];
647 if (intermediate_score >= hqp_bucket.first) {
648 continue;
650 const ScoreMaxRelevance& previous_bucket = hqp_relevance_buckets[i - 1];
651 const float slope = ((hqp_bucket.second - previous_bucket.second) /
652 (hqp_bucket.first - previous_bucket.first));
653 return (previous_bucket.second +
654 (slope * (intermediate_score - previous_bucket.first)));
656 // It will reach this stage when the score is > highest bucket score.
657 // Return the highest bucket score.
658 return hqp_relevance_buckets[i - 1].second;
661 // static
662 void ScoredHistoryMatch::InitHQPExperimentalParams() {
663 // These are default HQP relevance scoring buckets.
664 // See GetFinalRelevancyScore() for details.
665 std::string hqp_relevance_buckets_str = "0.0:400,1.5:600,12.0:1300,20.0:1399";
667 // Fetch the experiment params if they are any.
668 hqp_experimental_scoring_enabled_ =
669 OmniboxFieldTrial::HQPExperimentalScoringEnabled();
671 if (hqp_experimental_scoring_enabled_) {
672 // Add the topicality threshold from experiment params.
673 float hqp_experimental_topicality_threhold =
674 OmniboxFieldTrial::HQPExperimentalTopicalityThreshold();
675 topicality_threshold_ = hqp_experimental_topicality_threhold;
677 // Add the HQP experimental scoring buckets.
678 std::string hqp_experimental_scoring_buckets =
679 OmniboxFieldTrial::HQPExperimentalScoringBuckets();
680 if (!hqp_experimental_scoring_buckets.empty())
681 hqp_relevance_buckets_str = hqp_experimental_scoring_buckets;
684 // Parse the hqp_relevance_buckets_str string once and store them in vector
685 // which is easy to access.
686 hqp_relevance_buckets_ =
687 new std::vector<ScoredHistoryMatch::ScoreMaxRelevance>();
689 bool is_valid_bucket_str = GetHQPBucketsFromString(hqp_relevance_buckets_str,
690 hqp_relevance_buckets_);
691 DCHECK(is_valid_bucket_str);
694 // static
695 bool ScoredHistoryMatch::GetHQPBucketsFromString(
696 const std::string& buckets_str,
697 std::vector<ScoreMaxRelevance>* hqp_buckets) {
698 DCHECK(hqp_buckets != NULL);
699 DCHECK(!buckets_str.empty());
701 base::StringPairs kv_pairs;
702 if (base::SplitStringIntoKeyValuePairs(buckets_str, ':', ',', &kv_pairs)) {
703 for (base::StringPairs::const_iterator it = kv_pairs.begin();
704 it != kv_pairs.end(); ++it) {
705 ScoreMaxRelevance bucket;
706 bool is_valid_intermediate_score =
707 base::StringToDouble(it->first, &bucket.first);
708 DCHECK(is_valid_intermediate_score);
709 bool is_valid_hqp_score = base::StringToInt(it->second, &bucket.second);
710 DCHECK(is_valid_hqp_score);
711 hqp_buckets->push_back(bucket);
713 return true;
715 return false;