Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1716)

Side by Side Diff: chrome/browser/autocomplete/scored_history_match.cc

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

Powered by Google App Engine
This is Rietveld 408576698