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_AUTOCOMPLETE_SCORED_HISTORY_MATCH_H_ | 5 #ifndef CHROME_BROWSER_AUTOCOMPLETE_SCORED_HISTORY_MATCH_H_ |
6 #define CHROME_BROWSER_AUTOCOMPLETE_SCORED_HISTORY_MATCH_H_ | 6 #define CHROME_BROWSER_AUTOCOMPLETE_SCORED_HISTORY_MATCH_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "chrome/browser/autocomplete/in_memory_url_index_types.h" | 13 #include "chrome/browser/autocomplete/in_memory_url_index_types.h" |
14 #include "components/history/core/browser/history_match.h" | 14 #include "components/history/core/browser/history_match.h" |
15 #include "components/history/core/browser/history_types.h" | 15 #include "components/history/core/browser/history_types.h" |
16 #include "testing/gtest/include/gtest/gtest_prod.h" | |
17 | |
18 class ScoredHistoryMatchTest; | |
16 | 19 |
17 // An HistoryMatch that has a score as well as metrics defining where in the | 20 // An HistoryMatch that has a score as well as metrics defining where in the |
18 // history item's URL and/or page title matches have occurred. | 21 // history item's URL and/or page title matches have occurred. |
19 struct ScoredHistoryMatch : public history::HistoryMatch { | 22 struct ScoredHistoryMatch : public history::HistoryMatch { |
20 // The Builder inner class allows the embedder to control how matches are | 23 // ScoreMaxRelevance maps from an intermediate-score to the maximum |
21 // scored (we cannot use a base::Callback<> as base::Bind() is limited to 6 | 24 // final-relevance score given to a URL for this intermediate score. |
22 // parameters). | 25 // This is used to store the score ranges of HQP relevance buckets. |
23 // TODO(sdefresne): remove this since ScoredHistoryMatch can now depends on | 26 // Please see GetFinalRelevancyScore() for details. |
24 // chrome/browser/autocomplete and components/bookmarks | 27 typedef std::pair<double, int> ScoreMaxRelevance; |
25 // http://crbug.com/462645 | |
26 class Builder { | |
27 public: | |
28 Builder() {} | |
29 virtual ~Builder() {} | |
30 | |
31 // Creates a new match with a raw score calculated for the history item | |
32 // given in |row| with recent visits as indicated in |visits|. First | |
33 // determines if the row qualifies by seeing if all of the terms in | |
34 // |terms_vector| occur in |row|. If so, calculates a raw score. This raw | |
35 // score is in part determined by whether the matches occur at word | |
36 // boundaries, the locations of which are stored in |word_starts|. For some | |
37 // terms, it's appropriate to look for the word boundary within the term. | |
38 // For instance, the term ".net" should look for a word boundary at the "n". | |
39 // These offsets (".net" should have an offset of 1) come from | |
40 // |terms_to_word_starts_offsets|. |history_client| is used to determine | |
41 // if the match's URL is referenced by any bookmarks, which can also affect | |
42 // the raw score. The raw score allows the matches to be ordered and can be | |
43 // used to influence the final score calculated by the client of this index. | |
44 // If the row does not qualify the raw score will be 0. |languages| is used | |
45 // to help parse/format the URL before looking for the terms. | |
46 virtual ScoredHistoryMatch Build( | |
47 const history::URLRow& row, | |
48 const VisitInfoVector& visits, | |
49 const std::string& languages, | |
50 const base::string16& lower_string, | |
51 const String16Vector& terms_vector, | |
52 const WordStarts& terms_to_word_starts_offsets, | |
53 const RowWordStarts& word_starts, | |
54 const base::Time now) const = 0; | |
55 }; | |
56 | 28 |
57 // Required for STL, we don't use this directly. | 29 // Required for STL, we don't use this directly. |
58 ScoredHistoryMatch(); | 30 ScoredHistoryMatch(); |
59 | 31 |
60 // Initialize the ScoredHistoryMatch, passing |url_info|, |input_location|, | 32 // Initializes the ScoredHistoryMatch with a raw score calculated for the |
61 // |match_in_scheme| and |innermost_match| to HistoryMatch constructor, and | 33 // history item given in |row| with recent visits as indicated in |visits|. It |
62 // using |raw_score|, |url_matches|, |title_matches| and |can_inline| to | 34 // first determines if the row qualifies by seeing if all of the terms in |
63 // initialize the corresponding properties of this class. | 35 // |terms_vector| occur in |row|. If so, calculates a raw score. This raw |
64 ScoredHistoryMatch(const history::URLRow& url_info, | 36 // score is in part determined by whether the matches occur at word |
65 size_t input_location, | 37 // boundaries, the locations of which are stored in |word_starts|. For some |
66 bool match_in_scheme, | 38 // terms, it's appropriate to look for the word boundary within the term. For |
67 bool innermost_match, | 39 // instance, the term ".net" should look for a word boundary at the "n". These |
68 int raw_score, | 40 // offsets (".net" should have an offset of 1) come from |
69 const TermMatches& url_matches, | 41 // |terms_to_word_starts_offsets|. |is_url_bookmarked| indicates whether the |
70 const TermMatches& title_matches, | 42 // match's URL is referenced by any bookmarks, which can also affect the raw |
71 bool can_inline); | 43 // score. The raw score allows the matches to be ordered and can be used to |
44 // influence the final score calculated by the client of this index. If the | |
45 // row does not qualify the raw score will be 0. |languages| is used to help | |
46 // parse/format the URL before looking for the terms. | |
47 ScoredHistoryMatch(const history::URLRow& row, | |
48 const VisitInfoVector& visits, | |
49 const std::string& languages, | |
50 const base::string16& lower_string, | |
51 const String16Vector& terms_vector, | |
52 const WordStarts& terms_to_word_starts_offsets, | |
53 const RowWordStarts& word_starts, | |
54 bool is_url_bookmarked, | |
55 base::Time now); | |
72 | 56 |
73 ~ScoredHistoryMatch(); | 57 ~ScoredHistoryMatch(); |
74 | 58 |
75 // Compares two matches by score. Functor supporting URLIndexPrivateData's | 59 // Compares two matches by score. Functor supporting URLIndexPrivateData's |
76 // HistoryItemsForTerms function. Looks at particular fields within | 60 // HistoryItemsForTerms function. Looks at particular fields within |
77 // with url_info to make tie-breaking a bit smarter. | 61 // with url_info to make tie-breaking a bit smarter. |
78 static bool MatchScoreGreater(const ScoredHistoryMatch& m1, | 62 static bool MatchScoreGreater(const ScoredHistoryMatch& m1, |
79 const ScoredHistoryMatch& m2); | 63 const ScoredHistoryMatch& m2); |
80 | 64 |
65 // Returns |term_matches| after removing all matches that are not at a | |
66 // word break that are in the range [|start_pos|, |end_pos|). | |
67 // start_pos == string::npos is treated as start_pos = length of string. | |
68 // (In other words, no matches will be filtered.) | |
69 // end_pos == string::npos is treated as end_pos = length of string. | |
70 static TermMatches FilterTermMatchesByWordStarts( | |
71 const TermMatches& term_matches, | |
72 const WordStarts& terms_to_word_starts_offsets, | |
73 const WordStarts& word_starts, | |
74 size_t start_pos, | |
75 size_t end_pos); | |
76 | |
81 // The maximum number of recent visits to examine in GetFrequency(). | 77 // The maximum number of recent visits to examine in GetFrequency(). |
82 // Public so url_index_private_data.cc knows how many visits it is | 78 // Public so url_index_private_data.cc knows how many visits it is |
83 // expected to deliver (at minimum) to this class. | 79 // expected to deliver (at minimum) to this class. |
84 static const size_t kMaxVisitsToScore; | 80 static const size_t kMaxVisitsToScore; |
85 | 81 |
86 // An interim score taking into consideration location and completeness | 82 // An interim score taking into consideration location and completeness |
87 // of the match. | 83 // of the match. |
88 int raw_score; | 84 int raw_score; |
89 | 85 |
90 // Both these TermMatches contain the set of matches that are considered | 86 // Both these TermMatches contain the set of matches that are considered |
91 // important. At this time, that means they exclude mid-word matches | 87 // important. At this time, that means they exclude mid-word matches |
92 // except in the hostname of the URL. (Technically, during early | 88 // except in the hostname of the URL. (Technically, during early |
93 // construction of ScoredHistoryMatch, they may contain all matches, but | 89 // construction of ScoredHistoryMatch, they may contain all matches, but |
94 // unimportant matches are eliminated by GetTopicalityScore(), called | 90 // unimportant matches are eliminated by GetTopicalityScore(), called |
95 // during construction.) | 91 // during construction.) |
96 | 92 |
97 // Term matches within the URL. | 93 // Term matches within the URL. |
98 TermMatches url_matches; | 94 TermMatches url_matches; |
99 // Term matches within the page title. | 95 // Term matches within the page title. |
100 TermMatches title_matches; | 96 TermMatches title_matches; |
101 | 97 |
102 // True if this is a candidate for in-line autocompletion. | 98 // True if this is a candidate for in-line autocompletion. |
103 bool can_inline; | 99 bool can_inline; |
100 | |
101 private: | |
102 friend class ScoredHistoryMatchTest; | |
103 FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchTest, GetFinalRelevancyScore); | |
104 FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchTest, GetHQPBucketsFromString); | |
105 FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchTest, ScoringBookmarks); | |
106 FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchTest, ScoringScheme); | |
107 FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchTest, ScoringTLD); | |
108 | |
109 // Initialize ScoredHistoryMatch statics. Must be called before any other | |
110 // method of ScoredHistoryMatch and before creating any instances. | |
111 static void Init(); | |
112 | |
113 // Return a topicality score based on how many matches appear in the url and | |
114 // the page's title and where they are (e.g., at word boundaries). Revises | |
115 // url_matches and title_matches of |scored_history_match| in the process so | |
Mark P
2015/03/12 21:31:37
update this comment. This variable no longer exis
sdefresne
2015/03/12 21:36:21
Done.
| |
116 // they only reflect matches used for scoring. (For instance, some mid-word | |
117 // matches are not given credit in scoring.) | |
118 float GetTopicalityScore(const int num_terms, | |
119 const base::string16& cleaned_up_url, | |
120 const WordStarts& terms_to_word_starts_offsets, | |
121 const RowWordStarts& word_starts); | |
122 | |
123 // Returns a recency score based on |last_visit_days_ago|, which is | |
124 // how many days ago the page was last visited. | |
125 static float GetRecencyScore(int last_visit_days_ago); | |
126 | |
127 // Examines the first kMaxVisitsToScore and return a score (higher is | |
128 // better) based the rate of visits, whether the page is bookmarked, and | |
129 // how often those visits are typed navigations (i.e., explicitly | |
130 // invoked by the user). |now| is passed in to avoid unnecessarily | |
131 // recomputing it frequently. | |
132 static float GetFrequency(const base::Time& now, | |
133 const bool bookmarked, | |
134 const VisitInfoVector& visits); | |
135 | |
136 // Combines the two component scores into a final score that's | |
137 // an appropriate value to use as a relevancy score. Scoring buckets are | |
138 // specified through |hqp_relevance_buckets|. Please see the function | |
139 // implementation for more details. | |
140 static float GetFinalRelevancyScore( | |
141 float topicality_score, | |
142 float frequency_score, | |
143 const std::vector<ScoreMaxRelevance>& hqp_relevance_buckets); | |
144 | |
145 // Initializes the HQP experimental params: |hqp_relevance_buckets_| | |
146 // to default buckets. If hqp experimental scoring is enabled, it | |
147 // fetches the |hqp_experimental_scoring_enabled_|, |topicality_threshold_| | |
148 // and |hqp_relevance_buckets_| from omnibox field trials. | |
149 static void InitHQPExperimentalParams(); | |
150 | |
151 // Helper function to parse the string containing the scoring buckets. | |
152 // For example, | |
153 // String: "0.0:400,1.5:600,12.0:1300,20.0:1399" | |
154 // Buckets: vector[(0.0, 400),(1.5,600),(12.0,1300),(20.0,1399)] | |
155 // Returns false, in case if it fail to parse the string. | |
156 static bool GetHQPBucketsFromString( | |
157 const std::string& buckets_str, | |
158 std::vector<ScoreMaxRelevance>* hqp_buckets); | |
159 | |
160 // Untyped visits to bookmarked pages score this, compared to 1 for | |
161 // untyped visits to non-bookmarked pages and 20 for typed visits. | |
162 static int bookmark_value_; | |
163 | |
164 // If true, we allow input terms to match in the TLD (e.g., ".com"). | |
165 static bool allow_tld_matches_; | |
166 | |
167 // If true, we allow input terms to match in the scheme (e.g., "http://"). | |
168 static bool allow_scheme_matches_; | |
169 | |
170 // True, if hqp experimental scoring is enabled. | |
171 static bool hqp_experimental_scoring_enabled_; | |
172 | |
173 // |topicality_threshold_| is used to control the topicality scoring. | |
174 // If |topicality_threshold_| > 0, then URLs with topicality-score < threshold | |
175 // are given topicality score of 0. By default it is initalized to -1. | |
176 static float topicality_threshold_; | |
177 | |
178 // |hqp_relevance_buckets_| gives mapping from (topicality*frequency) | |
179 // to the final relevance scoring. Please see GetFinalRelevancyScore() | |
180 // for more details and scoring method. | |
181 static std::vector<ScoreMaxRelevance>* hqp_relevance_buckets_; | |
104 }; | 182 }; |
105 typedef std::vector<ScoredHistoryMatch> ScoredHistoryMatches; | 183 typedef std::vector<ScoredHistoryMatch> ScoredHistoryMatches; |
106 | 184 |
107 #endif // CHROME_BROWSER_AUTOCOMPLETE_SCORED_HISTORY_MATCH_H_ | 185 #endif // CHROME_BROWSER_AUTOCOMPLETE_SCORED_HISTORY_MATCH_H_ |
OLD | NEW |