| 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> |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 } | 238 } |
| 239 | 239 |
| 240 // URLs that have been visited more often are better. | 240 // URLs that have been visited more often are better. |
| 241 if (m1.url_info.visit_count() != m2.url_info.visit_count()) | 241 if (m1.url_info.visit_count() != m2.url_info.visit_count()) |
| 242 return m1.url_info.visit_count() > m2.url_info.visit_count(); | 242 return m1.url_info.visit_count() > m2.url_info.visit_count(); |
| 243 | 243 |
| 244 // URLs that have been visited more recently are better. | 244 // URLs that have been visited more recently are better. |
| 245 return m1.url_info.last_visit() > m2.url_info.last_visit(); | 245 return m1.url_info.last_visit() > m2.url_info.last_visit(); |
| 246 } | 246 } |
| 247 | 247 |
| 248 // static |
| 249 TermMatches ScoredHistoryMatch::FilterTermMatchesByWordStarts( |
| 250 const TermMatches& term_matches, |
| 251 const WordStarts& word_starts, |
| 252 const size_t start_pos) { |
| 253 if (start_pos == std::string::npos) |
| 254 return term_matches; |
| 255 TermMatches filtered_matches; |
| 256 WordStarts::const_iterator next_word_starts = word_starts.begin(); |
| 257 WordStarts::const_iterator end_word_starts = word_starts.end(); |
| 258 for (TermMatches::const_iterator iter = term_matches.begin(); |
| 259 iter != term_matches.end(); ++iter) { |
| 260 // Advance next_word_starts until it's >= the position of the term |
| 261 // we're considering. |
| 262 while ((next_word_starts != end_word_starts) && |
| 263 (*next_word_starts < iter->offset)) |
| 264 ++next_word_starts; |
| 265 // Add the match if it's before the position we start filtering at or |
| 266 // if it's at a word boundary. |
| 267 if ((iter->offset < start_pos) || |
| 268 ((next_word_starts != end_word_starts) && |
| 269 (*next_word_starts == iter->offset))) |
| 270 filtered_matches.push_back(*iter); |
| 271 } |
| 272 return filtered_matches; |
| 273 } |
| 274 |
| 248 float ScoredHistoryMatch::GetTopicalityScore( | 275 float ScoredHistoryMatch::GetTopicalityScore( |
| 249 const int num_terms, | 276 const int num_terms, |
| 250 const string16& url, | 277 const string16& url, |
| 251 const RowWordStarts& word_starts) { | 278 const RowWordStarts& word_starts) { |
| 252 // Because the below thread is not thread safe, we check that we're | 279 // Because the below thread is not thread safe, we check that we're |
| 253 // only calling it from one thread: the UI thread. Specifically, | 280 // only calling it from one thread: the UI thread. Specifically, |
| 254 // we check "if we've heard of the UI thread then we'd better | 281 // we check "if we've heard of the UI thread then we'd better |
| 255 // be on it." The first part is necessary so unit tests pass. (Many | 282 // be on it." The first part is necessary so unit tests pass. (Many |
| 256 // unit tests don't set up the threading naming system; hence | 283 // unit tests don't set up the threading naming system; hence |
| 257 // CurrentlyOn(UI thread) will fail.) | 284 // CurrentlyOn(UI thread) will fail.) |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 (term_scores[i] >= kMaxRawTermScore) ? (kMaxRawTermScore - 1) : | 394 (term_scores[i] >= kMaxRawTermScore) ? (kMaxRawTermScore - 1) : |
| 368 term_scores[i]]; | 395 term_scores[i]]; |
| 369 } | 396 } |
| 370 // TODO(mpearson): If there are multiple terms, consider taking the | 397 // TODO(mpearson): If there are multiple terms, consider taking the |
| 371 // geometric mean of per-term scores rather than the arithmetic mean. | 398 // geometric mean of per-term scores rather than the arithmetic mean. |
| 372 | 399 |
| 373 return topicality_score / num_terms; | 400 return topicality_score / num_terms; |
| 374 } | 401 } |
| 375 | 402 |
| 376 // static | 403 // static |
| 377 TermMatches ScoredHistoryMatch::FilterTermMatchesByWordStarts( | |
| 378 const TermMatches& term_matches, | |
| 379 const WordStarts& word_starts, | |
| 380 const size_t start_pos) { | |
| 381 if (start_pos == std::string::npos) | |
| 382 return term_matches; | |
| 383 TermMatches filtered_matches; | |
| 384 WordStarts::const_iterator next_word_starts = word_starts.begin(); | |
| 385 WordStarts::const_iterator end_word_starts = word_starts.end(); | |
| 386 for (TermMatches::const_iterator iter = term_matches.begin(); | |
| 387 iter != term_matches.end(); ++iter) { | |
| 388 // Advance next_word_starts until it's >= the position of the term | |
| 389 // we're considering. | |
| 390 while ((next_word_starts != end_word_starts) && | |
| 391 (*next_word_starts < iter->offset)) | |
| 392 ++next_word_starts; | |
| 393 // Add the match if it's before the position we start filtering at or | |
| 394 // if it's at a word boundary. | |
| 395 if ((iter->offset < start_pos) || | |
| 396 ((next_word_starts != end_word_starts) && | |
| 397 (*next_word_starts == iter->offset))) | |
| 398 filtered_matches.push_back(*iter); | |
| 399 } | |
| 400 return filtered_matches; | |
| 401 } | |
| 402 | |
| 403 // static | |
| 404 void ScoredHistoryMatch::FillInTermScoreToTopicalityScoreArray() { | 404 void ScoredHistoryMatch::FillInTermScoreToTopicalityScoreArray() { |
| 405 for (int term_score = 0; term_score < kMaxRawTermScore; ++term_score) { | 405 for (int term_score = 0; term_score < kMaxRawTermScore; ++term_score) { |
| 406 float topicality_score; | 406 float topicality_score; |
| 407 if (term_score < 10) { | 407 if (term_score < 10) { |
| 408 // If the term scores less than 10 points (no full-credit hit, or | 408 // If the term scores less than 10 points (no full-credit hit, or |
| 409 // no combination of hits that score that well), then the topicality | 409 // no combination of hits that score that well), then the topicality |
| 410 // score is linear in the term score. | 410 // score is linear in the term score. |
| 411 topicality_score = 0.1 * term_score; | 411 topicality_score = 0.1 * term_score; |
| 412 } else { | 412 } else { |
| 413 // For term scores of at least ten points, pass them through a log | 413 // For term scores of at least ten points, pass them through a log |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 559 // results to preserve this behavior. | 559 // results to preserve this behavior. |
| 560 if (also_do_hup_like_scoring_) { | 560 if (also_do_hup_like_scoring_) { |
| 561 max_assigned_score_for_non_inlineable_matches_ = | 561 max_assigned_score_for_non_inlineable_matches_ = |
| 562 HistoryURLProvider::kScoreForBestInlineableResult - 1; | 562 HistoryURLProvider::kScoreForBestInlineableResult - 1; |
| 563 } | 563 } |
| 564 bookmark_value_ = OmniboxFieldTrial::HQPBookmarkValue(); | 564 bookmark_value_ = OmniboxFieldTrial::HQPBookmarkValue(); |
| 565 initialized_ = true; | 565 initialized_ = true; |
| 566 } | 566 } |
| 567 | 567 |
| 568 } // namespace history | 568 } // namespace history |
| OLD | NEW |