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