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