Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/history/scored_history_match.h" | 5 #include "chrome/browser/history/scored_history_match.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <functional> | 8 #include <functional> |
| 9 #include <iterator> | 9 #include <iterator> |
| 10 #include <numeric> | 10 #include <numeric> |
| 11 #include <set> | 11 #include <set> |
| 12 | 12 |
| 13 #include <math.h> | 13 #include <math.h> |
| 14 | 14 |
| 15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "chrome/browser/autocomplete/history_url_provider.h" | 18 #include "chrome/browser/autocomplete/history_url_provider.h" |
| 19 #include "chrome/browser/autocomplete/url_prefix.h" | 19 #include "chrome/browser/autocomplete/url_prefix.h" |
| 20 #include "chrome/browser/bookmarks/bookmark_service.h" | 20 #include "chrome/browser/bookmarks/bookmark_service.h" |
| 21 #include "chrome/browser/omnibox/omnibox_field_trial.h" | |
| 21 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
| 22 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 23 | 24 |
| 24 namespace history { | 25 namespace history { |
| 25 | 26 |
| 26 // ScoredHistoryMatch ---------------------------------------------------------- | 27 // ScoredHistoryMatch ---------------------------------------------------------- |
| 27 | 28 |
| 28 // static | 29 // static |
| 29 const size_t ScoredHistoryMatch::kMaxVisitsToScore = 10u; | 30 const size_t ScoredHistoryMatch::kMaxVisitsToScore = 10u; |
| 30 const int ScoredHistoryMatch::kDaysToPrecomputeRecencyScoresFor = 366; | 31 const int ScoredHistoryMatch::kDaysToPrecomputeRecencyScoresFor = 366; |
| 31 const int ScoredHistoryMatch::kMaxRawTermScore = 30; | 32 const int ScoredHistoryMatch::kMaxRawTermScore = 30; |
| 32 float* ScoredHistoryMatch::raw_term_score_to_topicality_score_ = NULL; | 33 float* ScoredHistoryMatch::raw_term_score_to_topicality_score_ = NULL; |
| 33 float* ScoredHistoryMatch::days_ago_to_recency_score_ = NULL; | 34 float* ScoredHistoryMatch::days_ago_to_recency_score_ = NULL; |
| 34 bool ScoredHistoryMatch::initialized_ = false; | 35 bool ScoredHistoryMatch::initialized_ = false; |
| 36 int ScoredHistoryMatch::bookmark_value_ = -1; | |
|
Peter Kasting
2013/11/26 02:33:48
Thought you were changing this to 1?
Mark P
2013/11/26 19:31:57
I did! I must've lost the change while rebasing.
| |
| 35 bool ScoredHistoryMatch::also_do_hup_like_scoring_ = false; | 37 bool ScoredHistoryMatch::also_do_hup_like_scoring_ = false; |
| 36 int ScoredHistoryMatch::max_assigned_score_for_non_inlineable_matches_ = -1; | 38 int ScoredHistoryMatch::max_assigned_score_for_non_inlineable_matches_ = -1; |
| 37 | 39 |
| 38 ScoredHistoryMatch::ScoredHistoryMatch() | 40 ScoredHistoryMatch::ScoredHistoryMatch() |
| 39 : raw_score_(0), | 41 : raw_score_(0), |
| 40 can_inline_(false) { | 42 can_inline_(false) { |
| 41 if (!initialized_) { | 43 if (!initialized_) { |
| 42 InitializeAlsoDoHUPLikeScoringFieldAndMaxScoreField(); | 44 InitializeAlsoDoHUPLikeScoringFieldAndMaxScoreField(); |
| 45 InitializeBookmarkValue(); | |
|
Peter Kasting
2013/11/26 02:33:48
Nit: Is there a lot of value in having multiple sh
Mark P
2013/11/26 19:31:57
No, there's practically no value.
| |
| 43 initialized_ = true; | 46 initialized_ = true; |
| 44 } | 47 } |
| 45 } | 48 } |
| 46 | 49 |
| 47 ScoredHistoryMatch::ScoredHistoryMatch(const URLRow& row, | 50 ScoredHistoryMatch::ScoredHistoryMatch(const URLRow& row, |
| 48 const VisitInfoVector& visits, | 51 const VisitInfoVector& visits, |
| 49 const std::string& languages, | 52 const std::string& languages, |
| 50 const string16& lower_string, | 53 const string16& lower_string, |
| 51 const String16Vector& terms, | 54 const String16Vector& terms, |
| 52 const RowWordStarts& word_starts, | 55 const RowWordStarts& word_starts, |
| 53 const base::Time now, | 56 const base::Time now, |
| 54 BookmarkService* bookmark_service) | 57 BookmarkService* bookmark_service) |
| 55 : HistoryMatch(row, 0, false, false), | 58 : HistoryMatch(row, 0, false, false), |
| 56 raw_score_(0), | 59 raw_score_(0), |
| 57 can_inline_(false) { | 60 can_inline_(false) { |
| 58 if (!initialized_) { | 61 if (!initialized_) { |
| 59 InitializeAlsoDoHUPLikeScoringFieldAndMaxScoreField(); | 62 InitializeAlsoDoHUPLikeScoringFieldAndMaxScoreField(); |
| 63 InitializeBookmarkValue(); | |
| 60 initialized_ = true; | 64 initialized_ = true; |
| 61 } | 65 } |
| 62 | 66 |
| 63 GURL gurl = row.url(); | 67 GURL gurl = row.url(); |
| 64 if (!gurl.is_valid()) | 68 if (!gurl.is_valid()) |
| 65 return; | 69 return; |
| 66 | 70 |
| 67 // Figure out where each search term appears in the URL and/or page title | 71 // Figure out where each search term appears in the URL and/or page title |
| 68 // so that we can score as well as provide autocomplete highlighting. | 72 // so that we can score as well as provide autocomplete highlighting. |
| 69 string16 url = CleanUpUrlForMatching(gurl, languages); | 73 string16 url = CleanUpUrlForMatching(gurl, languages); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 // makes it inlineable may be empty. | 147 // makes it inlineable may be empty. |
| 144 DCHECK(best_inlineable_prefix != NULL); | 148 DCHECK(best_inlineable_prefix != NULL); |
| 145 const int num_components_in_best_inlineable_prefix = | 149 const int num_components_in_best_inlineable_prefix = |
| 146 best_inlineable_prefix->num_components; | 150 best_inlineable_prefix->num_components; |
| 147 innermost_match = (num_components_in_best_inlineable_prefix == | 151 innermost_match = (num_components_in_best_inlineable_prefix == |
| 148 num_components_in_best_prefix); | 152 num_components_in_best_prefix); |
| 149 } | 153 } |
| 150 | 154 |
| 151 const float topicality_score = | 155 const float topicality_score = |
| 152 GetTopicalityScore(terms.size(), url, word_starts); | 156 GetTopicalityScore(terms.size(), url, word_starts); |
| 153 const float frecency_score = GetFrecency(now, visits); | 157 const float frecency_score = GetFrecency( |
| 158 now, (bookmark_service && bookmark_service->IsBookmarked(gurl)), visits); | |
| 154 raw_score_ = GetFinalRelevancyScore(topicality_score, frecency_score); | 159 raw_score_ = GetFinalRelevancyScore(topicality_score, frecency_score); |
| 155 raw_score_ = | 160 raw_score_ = |
| 156 (raw_score_ <= kint32max) ? static_cast<int>(raw_score_) : kint32max; | 161 (raw_score_ <= kint32max) ? static_cast<int>(raw_score_) : kint32max; |
| 157 | 162 |
| 158 if (also_do_hup_like_scoring_ && can_inline_) { | 163 if (also_do_hup_like_scoring_ && can_inline_) { |
| 159 // HistoryURL-provider-like scoring gives any match that is | 164 // HistoryURL-provider-like scoring gives any match that is |
| 160 // capable of being inlined a certain minimum score. Some of these | 165 // capable of being inlined a certain minimum score. Some of these |
| 161 // are given a higher score that lets them be shown in inline. | 166 // are given a higher score that lets them be shown in inline. |
| 162 // This test here derives from the test in | 167 // This test here derives from the test in |
| 163 // HistoryURLProvider::PromoteMatchForInlineAutocomplete(). | 168 // HistoryURLProvider::PromoteMatchForInlineAutocomplete(). |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 475 days_ago_to_recency_score_[days_ago] = unnormalized_recency_score / 100.0; | 480 days_ago_to_recency_score_[days_ago] = unnormalized_recency_score / 100.0; |
| 476 if (days_ago > 0) { | 481 if (days_ago > 0) { |
| 477 DCHECK_LE(days_ago_to_recency_score_[days_ago], | 482 DCHECK_LE(days_ago_to_recency_score_[days_ago], |
| 478 days_ago_to_recency_score_[days_ago - 1]); | 483 days_ago_to_recency_score_[days_ago - 1]); |
| 479 } | 484 } |
| 480 } | 485 } |
| 481 } | 486 } |
| 482 | 487 |
| 483 // static | 488 // static |
| 484 float ScoredHistoryMatch::GetFrecency(const base::Time& now, | 489 float ScoredHistoryMatch::GetFrecency(const base::Time& now, |
| 490 const bool bookmarked, | |
| 485 const VisitInfoVector& visits) { | 491 const VisitInfoVector& visits) { |
| 486 // Compute the weighted average |value_of_transition| over the last at | 492 // Compute the weighted average |value_of_transition| over the last at |
| 487 // most kMaxVisitsToScore visits, where each visit is weighted using | 493 // most kMaxVisitsToScore visits, where each visit is weighted using |
| 488 // GetRecencyScore() based on how many days ago it happened. Use | 494 // GetRecencyScore() based on how many days ago it happened. Use |
| 489 // kMaxVisitsToScore as the denominator for the average regardless of | 495 // kMaxVisitsToScore as the denominator for the average regardless of |
| 490 // how many visits there were in order to penalize a match that has | 496 // how many visits there were in order to penalize a match that has |
| 491 // fewer visits than kMaxVisitsToScore. | 497 // fewer visits than kMaxVisitsToScore. |
| 492 const int total_sampled_visits = std::min(visits.size(), kMaxVisitsToScore); | 498 const int total_sampled_visits = std::min(visits.size(), kMaxVisitsToScore); |
| 493 if (total_sampled_visits == 0) | 499 if (total_sampled_visits == 0) |
| 494 return 0.0f; | 500 return 0.0f; |
| 495 float summed_visit_points = 0; | 501 float summed_visit_points = 0; |
| 496 for (int i = 0; i < total_sampled_visits; ++i) { | 502 for (int i = 0; i < total_sampled_visits; ++i) { |
| 497 const int value_of_transition = | 503 int value_of_transition = |
| 498 (visits[i].second == content::PAGE_TRANSITION_TYPED) ? 20 : 1; | 504 (visits[i].second == content::PAGE_TRANSITION_TYPED) ? 20 : 1; |
| 505 if (bookmarked && (bookmark_value_ > value_of_transition)) | |
|
Peter Kasting
2013/11/26 02:33:48
Nit: Simpler?:
if (bookmarked)
value_of
Mark P
2013/11/26 19:31:57
I don't care either way, so I changed it as you su
| |
| 506 value_of_transition = bookmark_value_; | |
| 499 const float bucket_weight = | 507 const float bucket_weight = |
| 500 GetRecencyScore((now - visits[i].first).InDays()); | 508 GetRecencyScore((now - visits[i].first).InDays()); |
| 501 summed_visit_points += (value_of_transition * bucket_weight); | 509 summed_visit_points += (value_of_transition * bucket_weight); |
| 502 } | 510 } |
| 503 return visits.size() * summed_visit_points / total_sampled_visits; | 511 return visits.size() * summed_visit_points / total_sampled_visits; |
| 504 } | 512 } |
| 505 | 513 |
| 506 // static | 514 // static |
| 507 float ScoredHistoryMatch::GetFinalRelevancyScore(float topicality_score, | 515 float ScoredHistoryMatch::GetFinalRelevancyScore(float topicality_score, |
| 508 float frecency_score) { | 516 float frecency_score) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 554 // HUP-like-scoring, these results would actually come from the HUP | 562 // HUP-like-scoring, these results would actually come from the HUP |
| 555 // and not be demoted, thus outscoring the demoted HQP results. | 563 // and not be demoted, thus outscoring the demoted HQP results. |
| 556 // When the HQP provides these, we need to clamp the non-inlineable | 564 // When the HQP provides these, we need to clamp the non-inlineable |
| 557 // results to preserve this behavior. | 565 // results to preserve this behavior. |
| 558 if (also_do_hup_like_scoring_) { | 566 if (also_do_hup_like_scoring_) { |
| 559 max_assigned_score_for_non_inlineable_matches_ = | 567 max_assigned_score_for_non_inlineable_matches_ = |
| 560 HistoryURLProvider::kScoreForBestInlineableResult - 1; | 568 HistoryURLProvider::kScoreForBestInlineableResult - 1; |
| 561 } | 569 } |
| 562 } | 570 } |
| 563 | 571 |
| 572 void ScoredHistoryMatch::InitializeBookmarkValue() { | |
| 573 bookmark_value_ = OmniboxFieldTrial::HQPBookmarkValue(); | |
| 574 } | |
| 575 | |
| 564 } // namespace history | 576 } // namespace history |
| OLD | NEW |