OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/autocomplete/network_action_predictor.h" | 5 #include "chrome/browser/autocomplete/network_action_predictor.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 17 matching lines...) Expand all Loading... |
28 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
29 #include "content/public/browser/notification_source.h" | 29 #include "content/public/browser/notification_source.h" |
30 | 30 |
31 namespace { | 31 namespace { |
32 | 32 |
33 const float kConfidenceCutoff[] = { | 33 const float kConfidenceCutoff[] = { |
34 0.8f, | 34 0.8f, |
35 0.5f | 35 0.5f |
36 }; | 36 }; |
37 | 37 |
38 const size_t kMinimumUserTextLength = 2; | 38 const size_t kMinimumUserTextLength = 1; |
39 const int kMinimumNumberOfHits = 3; | 39 const int kMinimumNumberOfHits = 3; |
40 | 40 |
41 COMPILE_ASSERT(arraysize(kConfidenceCutoff) == | 41 COMPILE_ASSERT(arraysize(kConfidenceCutoff) == |
42 NetworkActionPredictor::LAST_PREDICT_ACTION, | 42 NetworkActionPredictor::LAST_PREDICT_ACTION, |
43 ConfidenceCutoff_count_mismatch); | 43 ConfidenceCutoff_count_mismatch); |
44 | 44 |
45 bool GetURLRowForAutocompleteMatch(Profile* profile, | 45 bool GetURLRowForAutocompleteMatch(Profile* profile, |
46 const AutocompleteMatch& match, | 46 const AutocompleteMatch& match, |
47 history::URLRow* url_row) { | 47 history::URLRow* url_row) { |
48 DCHECK(url_row); | 48 DCHECK(url_row); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 } | 124 } |
125 | 125 |
126 void NetworkActionPredictor::ClearTransitionalMatches() { | 126 void NetworkActionPredictor::ClearTransitionalMatches() { |
127 transitional_matches_.clear(); | 127 transitional_matches_.clear(); |
128 } | 128 } |
129 | 129 |
130 // Given a match, return a recommended action. | 130 // Given a match, return a recommended action. |
131 NetworkActionPredictor::Action NetworkActionPredictor::RecommendAction( | 131 NetworkActionPredictor::Action NetworkActionPredictor::RecommendAction( |
132 const string16& user_text, | 132 const string16& user_text, |
133 const AutocompleteMatch& match) const { | 133 const AutocompleteMatch& match) const { |
134 double confidence = 0.0; | 134 DCHECK(prerender::GetOmniboxHeuristicToUse() == |
| 135 prerender::OMNIBOX_HEURISTIC_EXACT || |
| 136 prerender::GetOmniboxHeuristicToUse() == |
| 137 prerender::OMNIBOX_HEURISTIC_EXACT_FULL); |
135 | 138 |
136 switch (prerender::GetOmniboxHeuristicToUse()) { | 139 bool is_in_db = false; |
137 case prerender::OMNIBOX_HEURISTIC_EXACT: | 140 const double confidence = CalculateConfidence(user_text, match, &is_in_db); |
138 case prerender::OMNIBOX_HEURISTIC_EXACT_FULL: | |
139 confidence = ExactAlgorithm(user_text, match); | |
140 break; | |
141 default: | |
142 NOTREACHED(); | |
143 break; | |
144 }; | |
145 | |
146 DCHECK(confidence >= 0.0 && confidence <= 1.0); | 141 DCHECK(confidence >= 0.0 && confidence <= 1.0); |
147 | 142 |
148 UMA_HISTOGRAM_COUNTS_100("NetworkActionPredictor.Confidence_" + | 143 if (is_in_db) { |
149 prerender::GetOmniboxHistogramSuffix(), | 144 UMA_HISTOGRAM_COUNTS_100("NetworkActionPredictor.Confidence_" + |
150 confidence * 100); | 145 prerender::GetOmniboxHistogramSuffix(), |
| 146 confidence * 100); |
| 147 } |
151 | 148 |
152 // Map the confidence to an action. | 149 // Map the confidence to an action. |
153 Action action = ACTION_NONE; | 150 Action action = ACTION_NONE; |
154 for (int i = 0; i < LAST_PREDICT_ACTION; ++i) { | 151 for (int i = 0; i < LAST_PREDICT_ACTION; ++i) { |
155 if (confidence >= kConfidenceCutoff[i]) { | 152 if (confidence >= kConfidenceCutoff[i]) { |
156 action = static_cast<Action>(i); | 153 action = static_cast<Action>(i); |
157 break; | 154 break; |
158 } | 155 } |
159 } | 156 } |
160 | 157 |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 return false; | 370 return false; |
374 | 371 |
375 history::URLDatabase* url_db = service->InMemoryDatabase(); | 372 history::URLDatabase* url_db = service->InMemoryDatabase(); |
376 if (!url_db) | 373 if (!url_db) |
377 return false; | 374 return false; |
378 | 375 |
379 DeleteOldEntries(url_db); | 376 DeleteOldEntries(url_db); |
380 return true; | 377 return true; |
381 } | 378 } |
382 | 379 |
383 double NetworkActionPredictor::ExactAlgorithm( | 380 double NetworkActionPredictor::CalculateConfidence( |
384 const string16& user_text, | 381 const string16& user_text, |
385 const AutocompleteMatch& match) const { | 382 const AutocompleteMatch& match, |
| 383 bool* is_in_db) const { |
386 const DBCacheKey key = { user_text, match.destination_url }; | 384 const DBCacheKey key = { user_text, match.destination_url }; |
387 | 385 |
| 386 *is_in_db = false; |
388 if (user_text.length() < kMinimumUserTextLength) | 387 if (user_text.length() < kMinimumUserTextLength) |
389 return 0.0; | 388 return 0.0; |
390 | 389 |
391 const DBCacheMap::const_iterator iter = db_cache_.find(key); | 390 const DBCacheMap::const_iterator iter = db_cache_.find(key); |
392 if (iter == db_cache_.end()) | 391 if (iter == db_cache_.end()) |
393 return 0.0; | 392 return 0.0; |
394 | 393 |
| 394 *is_in_db = true; |
395 const DBCacheValue& value = iter->second; | 395 const DBCacheValue& value = iter->second; |
396 if (value.number_of_hits < kMinimumNumberOfHits) | 396 if (value.number_of_hits < kMinimumNumberOfHits) |
397 return 0.0; | 397 return 0.0; |
398 | 398 |
399 return static_cast<double>(value.number_of_hits) / | 399 return static_cast<double>(value.number_of_hits) / |
400 (value.number_of_hits + value.number_of_misses); | 400 (value.number_of_hits + value.number_of_misses); |
401 } | 401 } |
402 | 402 |
403 void NetworkActionPredictor::AddRow( | 403 void NetworkActionPredictor::AddRow( |
404 const DBCacheKey& key, | 404 const DBCacheKey& key, |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 | 472 |
473 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE, | 473 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE, |
474 base::Bind(&NetworkActionPredictorDatabase::CommitTransaction, db_)); | 474 base::Bind(&NetworkActionPredictorDatabase::CommitTransaction, db_)); |
475 } | 475 } |
476 | 476 |
477 NetworkActionPredictor::TransitionalMatch::TransitionalMatch() { | 477 NetworkActionPredictor::TransitionalMatch::TransitionalMatch() { |
478 } | 478 } |
479 | 479 |
480 NetworkActionPredictor::TransitionalMatch::~TransitionalMatch() { | 480 NetworkActionPredictor::TransitionalMatch::~TransitionalMatch() { |
481 } | 481 } |
OLD | NEW |