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 #ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_IMPL_H_ |
| 6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_IMPL_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <string> |
| 10 |
| 11 #include "base/feature_list.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/sequence_checker.h" |
| 15 #include "base/sequenced_task_runner.h" |
| 16 #include "components/keyed_service/core/keyed_service.h" |
| 17 #include "components/translate/core/browser/ranker_model_loader.h" |
| 18 #include "components/translate/core/browser/translate_ranker.h" |
| 19 #include "url/gurl.h" |
| 20 |
| 21 namespace chrome_intelligence { |
| 22 class RankerModel; |
| 23 } // namespace chrome_intelligence |
| 24 |
| 25 namespace metrics { |
| 26 class TranslateEventProto; |
| 27 } // namespace metrics |
| 28 |
| 29 namespace translate { |
| 30 |
| 31 class TranslatePrefs; |
| 32 |
| 33 // Features used to enable ranker query, enforcement and logging. Note that |
| 34 // enabling enforcement implies (forces) enabling queries. |
| 35 extern const base::Feature kTranslateRankerQuery; |
| 36 extern const base::Feature kTranslateRankerEnforcement; |
| 37 extern const base::Feature kTranslateRankerLogging; |
| 38 |
| 39 struct TranslateRankerFeatures { |
| 40 TranslateRankerFeatures(); |
| 41 |
| 42 TranslateRankerFeatures(int accepted, |
| 43 int denied, |
| 44 int ignored, |
| 45 const std::string& src, |
| 46 const std::string& dst, |
| 47 const std::string& cntry, |
| 48 const std::string& locale); |
| 49 |
| 50 TranslateRankerFeatures(const TranslatePrefs& prefs, |
| 51 const std::string& src, |
| 52 const std::string& dst, |
| 53 const std::string& locale); |
| 54 |
| 55 ~TranslateRankerFeatures(); |
| 56 |
| 57 void WriteTo(std::ostream& stream) const; |
| 58 |
| 59 // Input value used to generate the features. |
| 60 int accepted_count; |
| 61 int denied_count; |
| 62 int ignored_count; |
| 63 int total_count; |
| 64 |
| 65 // Used for inference. |
| 66 std::string src_lang; |
| 67 std::string dst_lang; |
| 68 std::string country; |
| 69 std::string app_locale; |
| 70 double accepted_ratio; |
| 71 double denied_ratio; |
| 72 double ignored_ratio; |
| 73 }; |
| 74 |
| 75 // If enabled, downloads a translate ranker model and uses it to determine |
| 76 // whether the user should be given a translation prompt or not. |
| 77 class TranslateRankerImpl : public TranslateRanker { |
| 78 public: |
| 79 TranslateRankerImpl(const base::FilePath& model_path, const GURL& model_url); |
| 80 ~TranslateRankerImpl() override; |
| 81 |
| 82 // Get the file path of the translate ranker model, by default with a fixed |
| 83 // name within |data_dir|. |
| 84 static base::FilePath GetModelPath(const base::FilePath& data_dir); |
| 85 |
| 86 // Get the URL from which the download the translate ranker model, by default |
| 87 // from Finch. |
| 88 static GURL GetModelURL(); |
| 89 |
| 90 // TranslateRanker... |
| 91 bool IsLoggingEnabled() override; |
| 92 bool IsQueryEnabled() override; |
| 93 bool IsEnforcementEnabled() override; |
| 94 int GetModelVersion() const override; |
| 95 bool ShouldOfferTranslation(const TranslatePrefs& translate_prefs, |
| 96 const std::string& src_lang, |
| 97 const std::string& dst_lang) override; |
| 98 void AddTranslateEvent( |
| 99 const metrics::TranslateEventProto& translate_event) override; |
| 100 void FlushTranslateEvents( |
| 101 std::vector<metrics::TranslateEventProto>* events) override; |
| 102 |
| 103 void OnModelAvailable( |
| 104 std::unique_ptr<chrome_intelligence::RankerModel> model); |
| 105 |
| 106 // Calculate the score given to |features| by the |model|. |
| 107 double CalculateScore(const TranslateRankerFeatures& features); |
| 108 |
| 109 private: |
| 110 // Used to sanity check the threading of this ranker. |
| 111 base::SequenceChecker sequence_checker_; |
| 112 |
| 113 // A helper to load the translate ranker model from disk cache or a URL. |
| 114 std::unique_ptr<RankerModelLoader> model_loader_; |
| 115 |
| 116 // The translation ranker model. |
| 117 std::unique_ptr<chrome_intelligence::RankerModel> model_; |
| 118 |
| 119 // Saved cache of translate event protos. |
| 120 std::vector<metrics::TranslateEventProto> event_cache_; |
| 121 |
| 122 base::WeakPtrFactory<TranslateRankerImpl> weak_ptr_factory_; |
| 123 |
| 124 DISALLOW_COPY_AND_ASSIGN(TranslateRankerImpl); |
| 125 }; |
| 126 |
| 127 } // namespace translate |
| 128 |
| 129 std::ostream& operator<<(std::ostream& stream, |
| 130 const translate::TranslateRankerFeatures& features); |
| 131 |
| 132 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_IMPL_H_ |
OLD | NEW |