Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_AUTOCOMPLETE_SCORED_HISTORY_MATCH_BUILDER_H_ | |
| 6 #define CHROME_BROWSER_AUTOCOMPLETE_SCORED_HISTORY_MATCH_BUILDER_H_ | |
| 7 | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "base/strings/string16.h" | |
| 12 #include "components/history/core/browser/history_types.h" | |
| 13 #include "components/history/core/browser/in_memory_url_index_types.h" | |
| 14 #include "components/history/core/browser/scored_history_match.h" | |
| 15 #include "testing/gtest/include/gtest/gtest_prod.h" | |
| 16 | |
| 17 class ScoredHistoryMatchBuilderTest; | |
| 18 | |
| 19 // ScoredHistoryMatchBuilder creates new history matches with a raw score | |
| 20 // calculated for the history item given in |row| with recent visits as | |
| 21 // indicated in |visits|. | |
| 22 // | |
| 23 // First determines if the row qualifies by seeing if all of the terms in | |
| 24 // |terms_vector| occur in |row|. If so, calculates a raw score. This raw | |
| 25 // score is in part determined by whether the matches occur at word boundaries, | |
| 26 // the locations of which are stored in |word_starts|. For some terms, it's | |
| 27 // appropriate to look for the word boundary within the term. For instance, the | |
| 28 // term ".net" should look for a word boundary at the "n". These offsets (".net" | |
| 29 // should have an offset of 1) come from |terms_to_word_starts_offsets|. | |
| 30 // |is_bookmarked| is used to determine if the match's URL is referenced by any | |
| 31 // bookmarks, which can also affect the raw score. The raw score allows the | |
| 32 // matches to be ordered and can be/ used to influence the final score | |
| 33 // calculated by the client of this index. If the row does not qualify the raw | |
| 34 // score will be 0. |languages| is used to help parse/format the URL before | |
| 35 // looking for the terms. | |
| 36 class ScoredHistoryMatchBuilder : public history::ScoredHistoryMatch::Builder { | |
| 37 public: | |
| 38 // Returns whether |url| is bookmarked, used to affect the score. Must support | |
| 39 // being called multiple time. | |
|
Mark P
2015/02/04 19:54:04
nit: time->times
| |
| 40 typedef base::Callback<bool(const GURL& url)> IsBookmarkedCallback; | |
| 41 | |
| 42 explicit ScoredHistoryMatchBuilder(const IsBookmarkedCallback& is_bookmarked); | |
| 43 ~ScoredHistoryMatchBuilder() override; | |
| 44 | |
| 45 // Returns |term_matches| after removing all matches that are not at a | |
| 46 // word break that are in the range [|start_pos|, |end_pos|). | |
| 47 // start_pos == string::npos is treated as start_pos = length of string. | |
| 48 // (In other words, no matches will be filtered.) | |
| 49 // end_pos == string::npos is treated as end_pos = length of string. | |
| 50 static history::TermMatches FilterTermMatchesByWordStarts( | |
| 51 const history::TermMatches& term_matches, | |
| 52 const history::WordStarts& terms_to_word_starts_offsets, | |
| 53 const history::WordStarts& word_starts, | |
| 54 size_t start_pos, | |
| 55 size_t end_pos); | |
| 56 | |
| 57 private: | |
| 58 friend class ScoredHistoryMatchBuilderTest; | |
| 59 FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchBuilderTest, ScoringBookmarks); | |
| 60 FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchBuilderTest, ScoringScheme); | |
| 61 FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchBuilderTest, ScoringTLD); | |
| 62 | |
| 63 // Initialize ScoredHistoryMatchBuilder statics. | |
| 64 void Init(); | |
| 65 | |
| 66 // Return a topicality score based on how many matches appear in the | |
| 67 // url and the page's title and where they are (e.g., at word | |
| 68 // boundaries). Revises url_matches and title_matches of | |
| 69 // |scored_history_match| | |
| 70 // in the process so they only reflect matches used for scoring. (For | |
| 71 // instance, | |
| 72 // some mid-word matches are not given credit in scoring.) | |
| 73 static float GetTopicalityScore( | |
| 74 const int num_terms, | |
| 75 const base::string16& cleaned_up_url, | |
| 76 const history::WordStarts& terms_to_word_starts_offsets, | |
| 77 const history::RowWordStarts& word_starts, | |
| 78 history::ScoredHistoryMatch* scored_history_match); | |
| 79 | |
| 80 // Returns a recency score based on |last_visit_days_ago|, which is | |
| 81 // how many days ago the page was last visited. | |
| 82 static float GetRecencyScore(int last_visit_days_ago); | |
| 83 | |
| 84 // Examines the first kMaxVisitsToScore and return a score (higher is | |
| 85 // better) based the rate of visits, whether the page is bookmarked, and | |
| 86 // how often those visits are typed navigations (i.e., explicitly | |
| 87 // invoked by the user). |now| is passed in to avoid unnecessarily | |
| 88 // recomputing it frequently. | |
| 89 static float GetFrequency(const base::Time& now, | |
| 90 const bool bookmarked, | |
| 91 const history::VisitInfoVector& visits); | |
| 92 | |
| 93 // Combines the two component scores into a final score that's | |
| 94 // an appropriate value to use as a relevancy score. | |
| 95 static float GetFinalRelevancyScore(float topicality_score, | |
| 96 float frequency_score); | |
| 97 | |
| 98 // history::ScoredHistoryMatch implementation. | |
| 99 history::ScoredHistoryMatch Build( | |
| 100 const history::URLRow& row, | |
| 101 const history::VisitInfoVector& visits, | |
| 102 const std::string& languages, | |
| 103 const base::string16& lower_string, | |
| 104 const history::String16Vector& terms_vector, | |
| 105 const history::WordStarts& terms_to_word_starts_offsets, | |
| 106 const history::RowWordStarts& word_starts, | |
| 107 const base::Time now) const override; | |
| 108 | |
| 109 // Untyped visits to bookmarked pages score this, compared to 1 for | |
| 110 // untyped visits to non-bookmarked pages and 20 for typed visits. | |
| 111 static int bookmark_value_; | |
| 112 | |
| 113 // If true, we allow input terms to match in the TLD (e.g., .com). | |
| 114 static bool allow_tld_matches_; | |
| 115 | |
| 116 // If true, we allow input terms to match in the scheme (e.g., http://). | |
| 117 static bool allow_scheme_matches_; | |
| 118 | |
| 119 // The IsBookmarkedCallback to use to check whether an URL is bookmarked. May | |
| 120 // be unset during testing. | |
| 121 IsBookmarkedCallback is_bookmarked_; | |
| 122 | |
| 123 DISALLOW_COPY_AND_ASSIGN(ScoredHistoryMatchBuilder); | |
| 124 }; | |
| 125 | |
| 126 #endif // CHROME_BROWSER_AUTOCOMPLETE_SCORED_HISTORY_MATCH_BUILDER_H_ | |
| OLD | NEW |