| 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 #ifndef CHROME_BROWSER_HISTORY_SCORED_HISTORY_MATCH_H_ | 5 #ifndef CHROME_BROWSER_HISTORY_SCORED_HISTORY_MATCH_H_ |
| 6 #define CHROME_BROWSER_HISTORY_SCORED_HISTORY_MATCH_H_ | 6 #define CHROME_BROWSER_HISTORY_SCORED_HISTORY_MATCH_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 13 #include "chrome/browser/autocomplete/history_provider_util.h" | 13 #include "chrome/browser/autocomplete/history_provider_util.h" |
| 14 #include "chrome/browser/history/history_types.h" | 14 #include "chrome/browser/history/history_types.h" |
| 15 #include "chrome/browser/history/in_memory_url_index_types.h" | 15 #include "chrome/browser/history/in_memory_url_index_types.h" |
| 16 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 16 | 17 |
| 17 class BookmarkService; | 18 class BookmarkService; |
| 18 | 19 |
| 19 namespace history { | 20 namespace history { |
| 20 | 21 |
| 21 class ScoredHistoryMatchTest; | 22 class ScoredHistoryMatchTest; |
| 22 | 23 |
| 23 // An HistoryMatch that has a score as well as metrics defining where in the | 24 // An HistoryMatch that has a score as well as metrics defining where in the |
| 24 // history item's URL and/or page title matches have occurred. | 25 // history item's URL and/or page title matches have occurred. |
| 25 class ScoredHistoryMatch : public history::HistoryMatch { | 26 class ScoredHistoryMatch : public history::HistoryMatch { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 | 85 |
| 85 // Helper function for GetTopicalityScore(). | 86 // Helper function for GetTopicalityScore(). |
| 86 // Returns |term_matches| after removing all matches that are not at a | 87 // Returns |term_matches| after removing all matches that are not at a |
| 87 // word break that starts after position |start_pos|. If |start_pos| is | 88 // word break that starts after position |start_pos|. If |start_pos| is |
| 88 // string::npos, does no filtering and simply returns |term_matches|. | 89 // string::npos, does no filtering and simply returns |term_matches|. |
| 89 static TermMatches FilterTermMatchesByWordStarts( | 90 static TermMatches FilterTermMatchesByWordStarts( |
| 90 const TermMatches& term_matches, | 91 const TermMatches& term_matches, |
| 91 const WordStarts& word_starts, | 92 const WordStarts& word_starts, |
| 92 const size_t start_pos); | 93 const size_t start_pos); |
| 93 | 94 |
| 94 // Precalculates raw_term_score_to_topicality_score, used in | 95 // Precalculates raw_term_score_to_topicality_score_, used in |
| 95 // GetTopicalityScore(). | 96 // GetTopicalityScore(). |
| 96 static void FillInTermScoreToTopicalityScoreArray(); | 97 static void FillInTermScoreToTopicalityScoreArray(); |
| 97 | 98 |
| 98 // Returns a recency score based on |last_visit_days_ago|, which is | 99 // Returns a recency score based on |last_visit_days_ago|, which is |
| 99 // how many days ago the page was last visited. | 100 // how many days ago the page was last visited. |
| 100 static float GetRecencyScore(int last_visit_days_ago); | 101 static float GetRecencyScore(int last_visit_days_ago); |
| 101 | 102 |
| 102 // Pre-calculates days_ago_to_recency_numerator_, used in | 103 // Pre-calculates days_ago_to_recency_numerator_, used in |
| 103 // GetRecencyScore(). | 104 // GetRecencyScore(). |
| 104 static void FillInDaysAgoToRecencyScoreArray(); | 105 static void FillInDaysAgoToRecencyScoreArray(); |
| 105 | 106 |
| 106 // Examines the first kMaxVisitsToScore and return a score (higher is | 107 // Examines the first kMaxVisitsToScore and return a score (higher is |
| 107 // better) based the rate of visits and how often those visits are | 108 // better) based the rate of visits, whether the page is bookmarked, and |
| 108 // typed navigations (i.e., explicitly invoked by the user). | 109 // how often those visits are typed navigations (i.e., explicitly |
| 109 // |now| is passed in to avoid unnecessarily recomputing it frequently. | 110 // invoked by the user). |now| is passed in to avoid unnecessarily |
| 111 // recomputing it frequently. |
| 110 static float GetFrecency(const base::Time& now, | 112 static float GetFrecency(const base::Time& now, |
| 113 const bool bookmarked, |
| 111 const VisitInfoVector& visits); | 114 const VisitInfoVector& visits); |
| 112 | 115 |
| 113 // Combines the two component scores into a final score that's | 116 // Combines the two component scores into a final score that's |
| 114 // an appropriate value to use as a relevancy score. | 117 // an appropriate value to use as a relevancy score. |
| 115 static float GetFinalRelevancyScore( | 118 static float GetFinalRelevancyScore( |
| 116 float topicality_score, | 119 float topicality_score, |
| 117 float frecency_score); | 120 float frecency_score); |
| 118 | 121 |
| 119 // Sets also_do_hup_like_scoring and | 122 // Sets |also_do_hup_like_scoring_|, |
| 120 // max_assigned_score_for_non_inlineable_matches based on the field | 123 // |max_assigned_score_for_non_inlineable_matches_| and |bookmark_value_| |
| 121 // trial state. | 124 // based on the field trial state. |
| 122 static void InitializeAlsoDoHUPLikeScoringFieldAndMaxScoreField(); | 125 static void Init(); |
| 123 | 126 |
| 124 // An interim score taking into consideration location and completeness | 127 // An interim score taking into consideration location and completeness |
| 125 // of the match. | 128 // of the match. |
| 126 int raw_score_; | 129 int raw_score_; |
| 127 | 130 |
| 128 // Both these TermMatches contain the set of matches that are considered | 131 // Both these TermMatches contain the set of matches that are considered |
| 129 // important. At this time, that means they exclude mid-word matches | 132 // important. At this time, that means they exclude mid-word matches |
| 130 // except in the hostname of the URL. (Technically, during early | 133 // except in the hostname of the URL. (Technically, during early |
| 131 // construction of ScoredHistoryMatch, they may contain all matches, but | 134 // construction of ScoredHistoryMatch, they may contain all matches, but |
| 132 // unimportant matches are eliminated by GetTopicalityScore(), called | 135 // unimportant matches are eliminated by GetTopicalityScore(), called |
| 133 // during construction.) | 136 // during construction.) |
| 134 // Term matches within the URL. | 137 // Term matches within the URL. |
| 135 TermMatches url_matches_; | 138 TermMatches url_matches_; |
| 136 // Term matches within the page title. | 139 // Term matches within the page title. |
| 137 TermMatches title_matches_; | 140 TermMatches title_matches_; |
| 138 | 141 |
| 139 // True if this is a candidate for in-line autocompletion. | 142 // True if this is a candidate for in-line autocompletion. |
| 140 bool can_inline_; | 143 bool can_inline_; |
| 141 | 144 |
| 142 // Pre-computed information to speed up calculating recency scores. | 145 // Pre-computed information to speed up calculating recency scores. |
| 143 // |days_ago_to_recency_score| is a simple array mapping how long | 146 // |days_ago_to_recency_score_| is a simple array mapping how long |
| 144 // ago a page was visited (in days) to the recency score we should | 147 // ago a page was visited (in days) to the recency score we should |
| 145 // assign it. This allows easy lookups of scores without requiring | 148 // assign it. This allows easy lookups of scores without requiring |
| 146 // math. This is initialized upon first use of GetRecencyScore(), | 149 // math. This is initialized upon first use of GetRecencyScore(), |
| 147 // which calls FillInDaysAgoToRecencyScoreArray(), | 150 // which calls FillInDaysAgoToRecencyScoreArray(), |
| 148 static float* days_ago_to_recency_score_; | 151 static float* days_ago_to_recency_score_; |
| 149 | 152 |
| 150 // Pre-computed information to speed up calculating topicality | 153 // Pre-computed information to speed up calculating topicality |
| 151 // scores. |raw_term_score_to_topicality_score| is a simple array | 154 // scores. |raw_term_score_to_topicality_score_| is a simple array |
| 152 // mapping how raw terms scores (a weighted sum of the number of | 155 // mapping how raw terms scores (a weighted sum of the number of |
| 153 // hits for the term, weighted by how important the hit is: | 156 // hits for the term, weighted by how important the hit is: |
| 154 // hostname, path, etc.) to the topicality score we should assign | 157 // hostname, path, etc.) to the topicality score we should assign |
| 155 // it. This allows easy lookups of scores without requiring math. | 158 // it. This allows easy lookups of scores without requiring math. |
| 156 // This is initialized upon first use of GetTopicalityScore(), | 159 // This is initialized upon first use of GetTopicalityScore(), |
| 157 // which calls FillInTermScoreToTopicalityScoreArray(). | 160 // which calls FillInTermScoreToTopicalityScoreArray(). |
| 158 static float* raw_term_score_to_topicality_score_; | 161 static float* raw_term_score_to_topicality_score_; |
| 159 | 162 |
| 160 // Used so we initialize static variables only once (on first use). | 163 // Used so we initialize static variables only once (on first use). |
| 161 static bool initialized_; | 164 static bool initialized_; |
| 162 | 165 |
| 166 // Untyped visits to bookmarked pages score this, compared to 1 for |
| 167 // untyped visits to non-bookmarked pages and 20 for typed visits. |
| 168 static int bookmark_value_; |
| 169 |
| 163 // If true, assign raw scores to be max(whatever it normally would be, | 170 // If true, assign raw scores to be max(whatever it normally would be, |
| 164 // a score that's similar to the score HistoryURL provider would assign). | 171 // a score that's similar to the score HistoryURL provider would assign). |
| 165 // This variable is set in the constructor by examining the field trial | 172 // This variable is set in the constructor by examining the field trial |
| 166 // state. | 173 // state. |
| 167 static bool also_do_hup_like_scoring_; | 174 static bool also_do_hup_like_scoring_; |
| 168 | 175 |
| 169 // The maximum score that can be assigned to non-inlineable matches. | 176 // The maximum score that can be assigned to non-inlineable matches. |
| 170 // This is useful because often we want inlineable matches to come | 177 // This is useful because often we want inlineable matches to come |
| 171 // first (even if they don't sometimes score as well as non-inlineable | 178 // first (even if they don't sometimes score as well as non-inlineable |
| 172 // matches) because if a non-inlineable match comes first than all matches | 179 // matches) because if a non-inlineable match comes first than all matches |
| 173 // will get demoted later in HistoryQuickProvider to non-inlineable scores. | 180 // will get demoted later in HistoryQuickProvider to non-inlineable scores. |
| 174 // Set to -1 to indicate no maximum score. | 181 // Set to -1 to indicate no maximum score. |
| 175 static int max_assigned_score_for_non_inlineable_matches_; | 182 static int max_assigned_score_for_non_inlineable_matches_; |
| 176 }; | 183 }; |
| 177 typedef std::vector<ScoredHistoryMatch> ScoredHistoryMatches; | 184 typedef std::vector<ScoredHistoryMatch> ScoredHistoryMatches; |
| 178 | 185 |
| 179 } // namespace history | 186 } // namespace history |
| 180 | 187 |
| 181 #endif // CHROME_BROWSER_HISTORY_SCORED_HISTORY_MATCH_H_ | 188 #endif // CHROME_BROWSER_HISTORY_SCORED_HISTORY_MATCH_H_ |
| OLD | NEW |