Index: chrome/browser/autocomplete/scored_history_match.h |
diff --git a/chrome/browser/autocomplete/scored_history_match.h b/chrome/browser/autocomplete/scored_history_match.h |
index a73b67d9eadde72412eac3d96c1f02b232a6dc16..76a55f84c58bf925413e59f5c91e5b3b4b661f28 100644 |
--- a/chrome/browser/autocomplete/scored_history_match.h |
+++ b/chrome/browser/autocomplete/scored_history_match.h |
@@ -13,62 +13,46 @@ |
#include "chrome/browser/autocomplete/in_memory_url_index_types.h" |
#include "components/history/core/browser/history_match.h" |
#include "components/history/core/browser/history_types.h" |
+#include "testing/gtest/include/gtest/gtest_prod.h" |
+ |
+class ScoredHistoryMatchTest; |
// An HistoryMatch that has a score as well as metrics defining where in the |
// history item's URL and/or page title matches have occurred. |
struct ScoredHistoryMatch : public history::HistoryMatch { |
- // The Builder inner class allows the embedder to control how matches are |
- // scored (we cannot use a base::Callback<> as base::Bind() is limited to 6 |
- // parameters). |
- // TODO(sdefresne): remove this since ScoredHistoryMatch can now depends on |
- // chrome/browser/autocomplete and components/bookmarks |
- // http://crbug.com/462645 |
- class Builder { |
- public: |
- Builder() {} |
- virtual ~Builder() {} |
- |
- // Creates a new match with a raw score calculated for the history item |
- // given in |row| with recent visits as indicated in |visits|. First |
- // determines if the row qualifies by seeing if all of the terms in |
- // |terms_vector| occur in |row|. If so, calculates a raw score. This raw |
- // score is in part determined by whether the matches occur at word |
- // boundaries, the locations of which are stored in |word_starts|. For some |
- // terms, it's appropriate to look for the word boundary within the term. |
- // For instance, the term ".net" should look for a word boundary at the "n". |
- // These offsets (".net" should have an offset of 1) come from |
- // |terms_to_word_starts_offsets|. |history_client| is used to determine |
- // if the match's URL is referenced by any bookmarks, which can also affect |
- // the raw score. The raw score allows the matches to be ordered and can be |
- // used to influence the final score calculated by the client of this index. |
- // If the row does not qualify the raw score will be 0. |languages| is used |
- // to help parse/format the URL before looking for the terms. |
- virtual ScoredHistoryMatch Build( |
- const history::URLRow& row, |
- const VisitInfoVector& visits, |
- const std::string& languages, |
- const base::string16& lower_string, |
- const String16Vector& terms_vector, |
- const WordStarts& terms_to_word_starts_offsets, |
- const RowWordStarts& word_starts, |
- const base::Time now) const = 0; |
- }; |
+ // ScoreMaxRelevance maps from an intermediate-score to the maximum |
+ // final-relevance score given to a URL for this intermediate score. |
+ // This is used to store the score ranges of HQP relevance buckets. |
+ // Please see GetFinalRelevancyScore() for details. |
+ typedef std::pair<double, int> ScoreMaxRelevance; |
// Required for STL, we don't use this directly. |
ScoredHistoryMatch(); |
- // Initialize the ScoredHistoryMatch, passing |url_info|, |input_location|, |
- // |match_in_scheme| and |innermost_match| to HistoryMatch constructor, and |
- // using |raw_score|, |url_matches|, |title_matches| and |can_inline| to |
- // initialize the corresponding properties of this class. |
- ScoredHistoryMatch(const history::URLRow& url_info, |
- size_t input_location, |
- bool match_in_scheme, |
- bool innermost_match, |
- int raw_score, |
- const TermMatches& url_matches, |
- const TermMatches& title_matches, |
- bool can_inline); |
+ // Initializes the ScoredHistoryMatch with a raw score calculated for the |
+ // history item given in |row| with recent visits as indicated in |visits|. It |
+ // first determines if the row qualifies by seeing if all of the terms in |
+ // |terms_vector| occur in |row|. If so, calculates a raw score. This raw |
+ // score is in part determined by whether the matches occur at word |
+ // boundaries, the locations of which are stored in |word_starts|. For some |
+ // terms, it's appropriate to look for the word boundary within the term. For |
+ // instance, the term ".net" should look for a word boundary at the "n". These |
+ // offsets (".net" should have an offset of 1) come from |
+ // |terms_to_word_starts_offsets|. |is_url_bookmarked| indicates whether the |
+ // match's URL is referenced by any bookmarks, which can also affect the raw |
+ // score. The raw score allows the matches to be ordered and can be/ used to |
Mark P
2015/03/10 00:28:12
nit: extra "/"
sdefresne
2015/03/10 10:43:28
Done.
|
+ // influence the final score calculated by the client of this index. If the |
+ // row does not qualify the raw score will be 0. |languages| is used to help |
+ // parse/format the URL before looking for the terms. |
+ ScoredHistoryMatch(const history::URLRow& row, |
+ const VisitInfoVector& visits, |
+ const std::string& languages, |
+ const base::string16& lower_string, |
+ const String16Vector& terms_vector, |
+ const WordStarts& terms_to_word_starts_offsets, |
+ const RowWordStarts& word_starts, |
+ bool is_url_bookmarked, |
+ base::Time now); |
~ScoredHistoryMatch(); |
@@ -78,6 +62,18 @@ struct ScoredHistoryMatch : public history::HistoryMatch { |
static bool MatchScoreGreater(const ScoredHistoryMatch& m1, |
const ScoredHistoryMatch& m2); |
+ // Returns |term_matches| after removing all matches that are not at a |
+ // word break that are in the range [|start_pos|, |end_pos|). |
+ // start_pos == string::npos is treated as start_pos = length of string. |
+ // (In other words, no matches will be filtered.) |
+ // end_pos == string::npos is treated as end_pos = length of string. |
+ static TermMatches FilterTermMatchesByWordStarts( |
+ const TermMatches& term_matches, |
+ const WordStarts& terms_to_word_starts_offsets, |
+ const WordStarts& word_starts, |
+ size_t start_pos, |
+ size_t end_pos); |
+ |
// The maximum number of recent visits to examine in GetFrequency(). |
// Public so url_index_private_data.cc knows how many visits it is |
// expected to deliver (at minimum) to this class. |
@@ -101,6 +97,88 @@ struct ScoredHistoryMatch : public history::HistoryMatch { |
// True if this is a candidate for in-line autocompletion. |
bool can_inline; |
+ |
+ private: |
Mark P
2015/03/10 00:28:12
Did you modify anything in this large block?
sdefresne
2015/03/10 10:43:28
I did only replace ScoredHistoryMatchBuilderImpl b
|
+ friend class ScoredHistoryMatchTest; |
+ FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchTest, GetFinalRelevancyScore); |
+ FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchTest, GetHQPBucketsFromString); |
+ FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchTest, ScoringBookmarks); |
+ FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchTest, ScoringScheme); |
+ FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchTest, ScoringTLD); |
+ |
+ // Initialize ScoredHistoryMatch statics. Must be called before any other |
+ // method of ScoredHistoryMatch and before creating any instances. |
+ static void Init(); |
+ |
+ // Return a topicality score based on how many matches appear in the url and |
+ // the page's title and where they are (e.g., at word boundaries). Revises |
+ // url_matches and title_matches of |scored_history_match| in the process so |
+ // they only reflect matches used for scoring. (For instance, some mid-word |
+ // matches are not given credit in scoring.) |
+ float GetTopicalityScore(const int num_terms, |
+ const base::string16& cleaned_up_url, |
+ const WordStarts& terms_to_word_starts_offsets, |
+ const RowWordStarts& word_starts); |
+ |
+ // Returns a recency score based on |last_visit_days_ago|, which is |
+ // how many days ago the page was last visited. |
+ static float GetRecencyScore(int last_visit_days_ago); |
+ |
+ // Examines the first kMaxVisitsToScore and return a score (higher is |
+ // better) based the rate of visits, whether the page is bookmarked, and |
+ // how often those visits are typed navigations (i.e., explicitly |
+ // invoked by the user). |now| is passed in to avoid unnecessarily |
+ // recomputing it frequently. |
+ static float GetFrequency(const base::Time& now, |
+ const bool bookmarked, |
+ const VisitInfoVector& visits); |
+ |
+ // Combines the two component scores into a final score that's |
+ // an appropriate value to use as a relevancy score. Scoring buckets are |
+ // specified through |hqp_relevance_buckets|. Please see the function |
+ // implementation for more details. |
+ static float GetFinalRelevancyScore( |
+ float topicality_score, |
+ float frequency_score, |
+ const std::vector<ScoreMaxRelevance>& hqp_relevance_buckets); |
+ |
+ // Initializes the HQP experimental params: |hqp_relevance_buckets_| |
+ // to default buckets. If hqp experimental scoring is enabled, it |
+ // fetches the |hqp_experimental_scoring_enabled_|, |topicality_threshold_| |
+ // and |hqp_relevance_buckets_| from omnibox field trials. |
+ static void InitHQPExperimentalParams(); |
+ |
+ // Helper function to parse the string containing the scoring buckets. |
+ // For example, |
+ // String: "0.0:400,1.5:600,12.0:1300,20.0:1399" |
+ // Buckets: vector[(0.0, 400),(1.5,600),(12.0,1300),(20.0,1399)] |
+ // Returns false, in case if it fail to parse the string. |
+ static bool GetHQPBucketsFromString( |
+ const std::string& buckets_str, |
+ std::vector<ScoreMaxRelevance>* hqp_buckets); |
+ |
+ // Untyped visits to bookmarked pages score this, compared to 1 for |
+ // untyped visits to non-bookmarked pages and 20 for typed visits. |
+ static int bookmark_value_; |
+ |
+ // If true, we allow input terms to match in the TLD (e.g., ".com"). |
+ static bool allow_tld_matches_; |
+ |
+ // If true, we allow input terms to match in the scheme (e.g., "http://"). |
+ static bool allow_scheme_matches_; |
+ |
+ // True, if hqp experimental scoring is enabled. |
+ static bool hqp_experimental_scoring_enabled_; |
+ |
+ // |topicality_threshold_| is used to control the topicality scoring. |
+ // If |topicality_threshold_| > 0, then URLs with topicality-score < threshold |
+ // are given topicality score of 0. By default it is initalized to -1. |
+ static float topicality_threshold_; |
+ |
+ // |hqp_relevance_buckets_| gives mapping from (topicality*frequency) |
+ // to the final relevance scoring. Please see GetFinalRelevancyScore() |
+ // for more details and scoring method. |
+ static std::vector<ScoreMaxRelevance>* hqp_relevance_buckets_; |
}; |
typedef std::vector<ScoredHistoryMatch> ScoredHistoryMatches; |