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/sequence_checker.h" |
| 13 #include "components/keyed_service/core/keyed_service.h" |
| 14 #include "components/translate/core/browser/ranker_model_loader.h" |
| 15 #include "components/translate/core/browser/translate_ranker.h" |
| 16 |
| 17 namespace base { |
| 18 class TaskRunner; |
| 19 } // namespace base |
| 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(const TranslatePrefs& prefs, |
| 43 const std::string& src, |
| 44 const std::string& dst, |
| 45 const std::string& locale); |
| 46 |
| 47 ~TranslateRankerFeatures(); |
| 48 |
| 49 void WriteTo(std::ostream& stream) const; |
| 50 |
| 51 // Input value used to generate the features. |
| 52 double accepted_count; |
| 53 double denied_count; |
| 54 double ignored_count; |
| 55 double total_count; |
| 56 |
| 57 // Used for inference. |
| 58 std::string src_lang; |
| 59 std::string dst_lang; |
| 60 std::string country; |
| 61 std::string app_locale; |
| 62 double accepted_ratio; |
| 63 double denied_ratio; |
| 64 double ignored_ratio; |
| 65 }; |
| 66 |
| 67 // If enabled, downloads a translate ranker model and uses it to determine |
| 68 // whether the user should be given a translation prompt or not. |
| 69 class TranslateRankerImpl : public TranslateRanker, public RankerModelObserver { |
| 70 public: |
| 71 enum { |
| 72 DO_NOT_CACHE_MODELS = -1, |
| 73 ACCEPT_ANY_MODEL_VERSION = 0, |
| 74 }; |
| 75 |
| 76 TranslateRankerImpl(const base::FilePath& model_path, const GURL& model_url); |
| 77 ~TranslateRankerImpl() override; |
| 78 |
| 79 // Get the file path of the translate ranker model, by default with a fixed |
| 80 // name within |data_dir|. |
| 81 static base::FilePath GetModelPath(const base::FilePath& data_dir); |
| 82 |
| 83 // Get the URL from which the download the translate ranker model, by default |
| 84 // from Finch. |
| 85 static GURL GetModelURL(); |
| 86 |
| 87 // TranslateRanker... |
| 88 bool IsEnabled() override; |
| 89 bool IsLoggingEnabled() override; |
| 90 bool IsQueryEnabled() override; |
| 91 bool IsEnforcementEnabled() override; |
| 92 int GetModelVersion() const override; |
| 93 bool ShouldOfferTranslation(const TranslatePrefs& translate_prefs, |
| 94 const std::string& src_lang, |
| 95 const std::string& dst_lang) override; |
| 96 void AddTranslateEvent( |
| 97 std::unique_ptr<metrics::TranslateEventProto> translate_event) override; |
| 98 void FlushTranslateEvents( |
| 99 std::vector<std::unique_ptr<metrics::TranslateEventProto>>* events) |
| 100 override; |
| 101 |
| 102 // RankerModelObserver... |
| 103 RankerModelStatus Validate( |
| 104 const chrome_intelligence::RankerModel& model) const override; |
| 105 void OnModelAvailable( |
| 106 std::unique_ptr<chrome_intelligence::RankerModel> model) override; |
| 107 |
| 108 // Calculate the score given to |features| by the |model|. |
| 109 double CalculateScore(const TranslateRankerFeatures& features); |
| 110 |
| 111 private: |
| 112 void OnModelAvailableImpl( |
| 113 std::unique_ptr<chrome_intelligence::RankerModel> model); |
| 114 |
| 115 // Used to sanity check the threading of this ranker. |
| 116 base::SequenceChecker sequence_checker_; |
| 117 |
| 118 // Remembers the task runner associated with this ranker. This is used to |
| 119 // ensure that updates to |model_| are posted back to the correct thread. |
| 120 scoped_refptr<base::TaskRunner> task_runner_; |
| 121 |
| 122 // A helper to load the translate ranker model from disk cache or a URL. |
| 123 RankerModelLoader model_loader_; |
| 124 |
| 125 // The translation ranker model. |
| 126 std::unique_ptr<chrome_intelligence::RankerModel> model_; |
| 127 |
| 128 // Saved cache of translate event protos. |
| 129 std::vector<std::unique_ptr<metrics::TranslateEventProto>> event_cache_; |
| 130 |
| 131 DISALLOW_COPY_AND_ASSIGN(TranslateRankerImpl); |
| 132 }; |
| 133 |
| 134 } // namespace translate |
| 135 |
| 136 std::ostream& operator<<(std::ostream& stream, |
| 137 const translate::TranslateRankerFeatures& features); |
| 138 |
| 139 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_IMPL_H_ |
OLD | NEW |