OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #include "components/translate/core/browser/translate_ranker_impl.h" |
| 6 |
| 7 #include <cmath> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" |
| 11 #include "base/command_line.h" |
| 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" |
| 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/metrics/histogram_macros.h" |
| 16 #include "base/profiler/scoped_tracker.h" |
| 17 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/string_util.h" |
| 19 #include "base/task_runner.h" |
| 20 #include "base/threading/thread_task_runner_handle.h" |
| 21 #include "components/metrics/proto/translate_event.pb.h" |
| 22 #include "components/translate/core/browser/proto/ranker_model.pb.h" |
| 23 #include "components/translate/core/browser/proto/translate_ranker_model.pb.h" |
| 24 #include "components/translate/core/browser/ranker_model.h" |
| 25 #include "components/translate/core/browser/translate_download_manager.h" |
| 26 #include "components/translate/core/browser/translate_prefs.h" |
| 27 #include "components/translate/core/browser/translate_url_fetcher.h" |
| 28 #include "components/translate/core/common/translate_switches.h" |
| 29 #include "components/variations/variations_associated_data.h" |
| 30 #include "url/gurl.h" |
| 31 |
| 32 namespace translate { |
| 33 |
| 34 namespace { |
| 35 |
| 36 using chrome_intelligence::RankerModel; |
| 37 using chrome_intelligence::RankerModelProto; |
| 38 using chrome_intelligence::TranslateRankerModel; |
| 39 |
| 40 const double kTranslationOfferThreshold = 0.5; |
| 41 |
| 42 const char kTranslateRankerModelFileName[] = "Translate Ranker Model"; |
| 43 const char kUmaPrefix[] = "Translate.Ranker"; |
| 44 const char kUnknown[] = "UNKNOWN"; |
| 45 |
| 46 double Sigmoid(double x) { |
| 47 return 1.0 / (1.0 + exp(-x)); |
| 48 } |
| 49 |
| 50 double SafeRatio(int numerator, int denominator) { |
| 51 return denominator ? (numerator / static_cast<double>(denominator)) : 0.0; |
| 52 } |
| 53 |
| 54 double ScoreComponent(const google::protobuf::Map<std::string, float>& weights, |
| 55 const std::string& key) { |
| 56 auto i = weights.find(base::ToLowerASCII(key)); |
| 57 if (i == weights.end()) |
| 58 i = weights.find(kUnknown); |
| 59 return i == weights.end() ? 0.0 : i->second; |
| 60 } |
| 61 |
| 62 RankerModelStatus ValidateModel(const RankerModel& model) { |
| 63 if (model.proto().model_case() != RankerModelProto::kTranslate) |
| 64 return RankerModelStatus::VALIDATION_FAILED; |
| 65 |
| 66 if (model.proto().translate().model_revision_case() != |
| 67 TranslateRankerModel::kLogisticRegressionModel) { |
| 68 return RankerModelStatus::INCOMPATIBLE; |
| 69 } |
| 70 |
| 71 return RankerModelStatus::OK; |
| 72 } |
| 73 |
| 74 } // namespace |
| 75 |
| 76 const base::Feature kTranslateRankerQuery{"TranslateRankerQuery", |
| 77 base::FEATURE_DISABLED_BY_DEFAULT}; |
| 78 |
| 79 const base::Feature kTranslateRankerEnforcement{ |
| 80 "TranslateRankerEnforcement", base::FEATURE_DISABLED_BY_DEFAULT}; |
| 81 |
| 82 const base::Feature kTranslateRankerLogging{"TranslateRankerLogging", |
| 83 base::FEATURE_DISABLED_BY_DEFAULT}; |
| 84 |
| 85 TranslateRankerFeatures::TranslateRankerFeatures() {} |
| 86 |
| 87 TranslateRankerFeatures::TranslateRankerFeatures(int accepted, |
| 88 int denied, |
| 89 int ignored, |
| 90 const std::string& src, |
| 91 const std::string& dst, |
| 92 const std::string& cntry, |
| 93 const std::string& locale) |
| 94 : accepted_count(accepted), |
| 95 denied_count(denied), |
| 96 ignored_count(ignored), |
| 97 total_count(accepted_count + denied_count + ignored_count), |
| 98 src_lang(src), |
| 99 dst_lang(dst), |
| 100 country(cntry), |
| 101 app_locale(locale), |
| 102 accepted_ratio(SafeRatio(accepted_count, total_count)), |
| 103 denied_ratio(SafeRatio(denied_count, total_count)), |
| 104 ignored_ratio(SafeRatio(ignored_count, total_count)) {} |
| 105 |
| 106 TranslateRankerFeatures::TranslateRankerFeatures(const TranslatePrefs& prefs, |
| 107 const std::string& src, |
| 108 const std::string& dst, |
| 109 const std::string& locale) |
| 110 : TranslateRankerFeatures(prefs.GetTranslationAcceptedCount(src), |
| 111 prefs.GetTranslationDeniedCount(src), |
| 112 prefs.GetTranslationIgnoredCount(src), |
| 113 src, |
| 114 dst, |
| 115 prefs.GetCountry(), |
| 116 locale) {} |
| 117 |
| 118 TranslateRankerFeatures::~TranslateRankerFeatures() {} |
| 119 |
| 120 void TranslateRankerFeatures::WriteTo(std::ostream& stream) const { |
| 121 stream << "src_lang='" << src_lang << "', " |
| 122 << "dst_lang='" << dst_lang << "', " |
| 123 << "country='" << country << "', " |
| 124 << "app_locale='" << app_locale << "', " |
| 125 << "accept_count=" << accepted_count << ", " |
| 126 << "denied_count=" << denied_count << ", " |
| 127 << "ignored_count=" << ignored_count << ", " |
| 128 << "total_count=" << total_count << ", " |
| 129 << "accept_ratio=" << accepted_ratio << ", " |
| 130 << "decline_ratio=" << denied_ratio << ", " |
| 131 << "ignore_ratio=" << ignored_ratio; |
| 132 } |
| 133 |
| 134 TranslateRankerImpl::TranslateRankerImpl(const base::FilePath& model_path, |
| 135 const GURL& model_url) |
| 136 : weak_ptr_factory_(this) { |
| 137 model_loader_ = base::MakeUnique<RankerModelLoader>( |
| 138 base::Bind(&ValidateModel), |
| 139 base::Bind(&TranslateRankerImpl::OnModelAvailable, |
| 140 weak_ptr_factory_.GetWeakPtr()), |
| 141 model_path, model_url, kUmaPrefix); |
| 142 model_loader_->Start(); |
| 143 } |
| 144 |
| 145 TranslateRankerImpl::~TranslateRankerImpl() {} |
| 146 |
| 147 // static |
| 148 base::FilePath TranslateRankerImpl::GetModelPath( |
| 149 const base::FilePath& data_dir) { |
| 150 if (data_dir.empty()) |
| 151 return base::FilePath(); |
| 152 |
| 153 // Otherwise, look for the file in data dir. |
| 154 return data_dir.AppendASCII(kTranslateRankerModelFileName); |
| 155 } |
| 156 |
| 157 // static |
| 158 GURL TranslateRankerImpl::GetModelURL() { |
| 159 // Allow override of the ranker model URL from the command line. |
| 160 std::string raw_url; |
| 161 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 162 if (command_line->HasSwitch(switches::kTranslateRankerModelURL)) { |
| 163 raw_url = |
| 164 command_line->GetSwitchValueASCII(switches::kTranslateRankerModelURL); |
| 165 } else { |
| 166 // Otherwise take the ranker model URL from the ranker query variation. |
| 167 raw_url = variations::GetVariationParamValueByFeature( |
| 168 kTranslateRankerQuery, switches::kTranslateRankerModelURL); |
| 169 } |
| 170 |
| 171 DVLOG(3) << switches::kTranslateRankerModelURL << " = " << raw_url; |
| 172 |
| 173 return GURL(raw_url); |
| 174 } |
| 175 |
| 176 bool TranslateRankerImpl::IsLoggingEnabled() { |
| 177 return base::FeatureList::IsEnabled(kTranslateRankerLogging); |
| 178 } |
| 179 |
| 180 bool TranslateRankerImpl::IsQueryEnabled() { |
| 181 return base::FeatureList::IsEnabled(kTranslateRankerQuery); |
| 182 } |
| 183 |
| 184 bool TranslateRankerImpl::IsEnforcementEnabled() { |
| 185 return base::FeatureList::IsEnabled(kTranslateRankerEnforcement); |
| 186 } |
| 187 |
| 188 int TranslateRankerImpl::GetModelVersion() const { |
| 189 return model_ ? model_->proto().translate().version() : 0; |
| 190 } |
| 191 |
| 192 bool TranslateRankerImpl::ShouldOfferTranslation( |
| 193 const TranslatePrefs& translate_prefs, |
| 194 const std::string& src_lang, |
| 195 const std::string& dst_lang) { |
| 196 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 197 // The ranker is a gate in the "show a translation prompt" flow. To retain |
| 198 // the pre-existing functionality, it defaults to returning true in the |
| 199 // absence of a model or if enforcement is disabled. As this is ranker is |
| 200 // subsumed into a more general assist ranker, this default will go away |
| 201 // (or become False). |
| 202 const bool kDefaultResponse = true; |
| 203 |
| 204 // If we don't have a model, request one and return the default. |
| 205 if (model_ == nullptr) { |
| 206 return kDefaultResponse; |
| 207 } |
| 208 |
| 209 SCOPED_UMA_HISTOGRAM_TIMER("Translate.Ranker.Timer.ShouldOfferTranslation"); |
| 210 |
| 211 // TODO(rogerm): Remove ScopedTracker below once crbug.com/646711 is closed. |
| 212 tracked_objects::ScopedTracker tracking_profile( |
| 213 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 214 "646711 translate::TranslateRankerImpl::ShouldOfferTranslation")); |
| 215 |
| 216 TranslateRankerFeatures features( |
| 217 translate_prefs, src_lang, dst_lang, |
| 218 TranslateDownloadManager::GetInstance()->application_locale()); |
| 219 |
| 220 double score = CalculateScore(features); |
| 221 |
| 222 DVLOG(2) << "TranslateRankerImpl::ShouldOfferTranslation: " |
| 223 << "Score = " << score << ", Features=[" << features << "]"; |
| 224 |
| 225 bool result = (score >= kTranslationOfferThreshold); |
| 226 |
| 227 UMA_HISTOGRAM_BOOLEAN("Translate.Ranker.QueryResult", result); |
| 228 |
| 229 return result; |
| 230 } |
| 231 |
| 232 double TranslateRankerImpl::CalculateScore( |
| 233 const TranslateRankerFeatures& features) { |
| 234 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 235 SCOPED_UMA_HISTOGRAM_TIMER("Translate.Ranker.Timer.CalculateScore"); |
| 236 DCHECK(model_ != nullptr); |
| 237 const TranslateRankerModel::LogisticRegressionModel& logit = |
| 238 model_->proto().translate().logistic_regression_model(); |
| 239 |
| 240 double dot_product = |
| 241 (features.accepted_count * logit.accept_count_weight()) + |
| 242 (features.denied_count * logit.decline_count_weight()) + |
| 243 (features.ignored_count * logit.ignore_count_weight()) + |
| 244 (features.accepted_ratio * logit.accept_ratio_weight()) + |
| 245 (features.denied_ratio * logit.decline_ratio_weight()) + |
| 246 (features.ignored_ratio * logit.ignore_ratio_weight()) + |
| 247 ScoreComponent(logit.source_language_weight(), features.src_lang) + |
| 248 ScoreComponent(logit.dest_language_weight(), features.dst_lang) + |
| 249 ScoreComponent(logit.country_weight(), features.country) + |
| 250 ScoreComponent(logit.locale_weight(), features.app_locale); |
| 251 |
| 252 return Sigmoid(dot_product + logit.bias()); |
| 253 } |
| 254 |
| 255 void TranslateRankerImpl::FlushTranslateEvents( |
| 256 std::vector<metrics::TranslateEventProto>* events) { |
| 257 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 258 DVLOG(3) << "Flushing translate ranker events."; |
| 259 events->swap(event_cache_); |
| 260 event_cache_.clear(); |
| 261 } |
| 262 |
| 263 void TranslateRankerImpl::AddTranslateEvent( |
| 264 const metrics::TranslateEventProto& event) { |
| 265 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 266 DVLOG(3) << "Adding translate ranker event."; |
| 267 if (IsLoggingEnabled()) |
| 268 event_cache_.push_back(event); |
| 269 } |
| 270 |
| 271 void TranslateRankerImpl::OnModelAvailable(std::unique_ptr<RankerModel> model) { |
| 272 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 273 model_ = std::move(model); |
| 274 } |
| 275 |
| 276 } // namespace translate |
| 277 |
| 278 std::ostream& operator<<(std::ostream& stream, |
| 279 const translate::TranslateRankerFeatures& features) { |
| 280 features.WriteTo(stream); |
| 281 return stream; |
| 282 } |
OLD | NEW |