OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_LANGUAGE_MODEL_H_ | 5 #ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_LANGUAGE_MODEL_H_ |
6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_LANGUAGE_MODEL_H_ | 6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_LANGUAGE_MODEL_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "components/keyed_service/core/keyed_service.h" | 12 #include "components/keyed_service/core/keyed_service.h" |
13 | 13 |
14 class PrefRegistrySimple; | 14 class PrefRegistrySimple; |
15 class PrefService; | 15 class PrefService; |
16 | 16 |
17 namespace translate { | 17 namespace translate { |
18 | 18 |
19 // Collects data about languages in which the user reads the web and provides | 19 // Collects data about languages in which the user reads the web and provides |
20 // access to current estimated language preferences. The past behaviour is | 20 // access to current estimated language preferences. The past behaviour is |
21 // discounted so that this model reflects changes in browsing habits. This model | 21 // discounted so that this model reflects changes in browsing habits. This model |
22 // does not have to contain all languages that ever appeared in user's browsing, | 22 // does not have to contain all languages that ever appeared in user's browsing, |
23 // languages with insignificant frequency are removed, eventually. | 23 // languages with insignificant frequency are removed, eventually. |
24 class LanguageModel : public KeyedService { | 24 class LanguageModel : public KeyedService { |
25 public: | 25 public: |
26 struct LanguageInfo { | 26 struct LanguageInfo { |
| 27 LanguageInfo() = default; |
| 28 LanguageInfo(const std::string& language_code, float frequency) |
| 29 : language_code(language_code), frequency(frequency) {} |
| 30 |
27 // The ISO 639 language code. | 31 // The ISO 639 language code. |
28 std::string language_code; | 32 std::string language_code; |
29 | 33 |
30 // The current estimated frequency of the language share, a number between 0 | 34 // The current estimated frequency of the language share, a number between 0 |
31 // and 1 (can be understood as the probability that the next page the user | 35 // and 1 (can be understood as the probability that the next page the user |
32 // opens is in this language). Frequencies over all LanguageInfos from | 36 // opens is in this language). Frequencies over all LanguageInfos from |
33 // GetTopLanguages() sum to 1 (unless there are no top languages, yet). | 37 // GetTopLanguages() sum to 1 (unless there are no top languages, yet). |
34 float frequency; | 38 float frequency = 0.0f; |
35 | |
36 bool operator==(const LanguageInfo& m) const { | |
37 return language_code == m.language_code; | |
38 } | |
39 }; | 39 }; |
40 | 40 |
41 explicit LanguageModel(PrefService* pref_service); | 41 explicit LanguageModel(PrefService* pref_service); |
42 ~LanguageModel() override; | 42 ~LanguageModel() override; |
43 | 43 |
44 // Registers profile prefs for the model. | 44 // Registers profile prefs for the model. |
45 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 45 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
46 | 46 |
47 // Returns a list of the languages currently tracked by the model, sorted by | 47 // Returns a list of the languages currently tracked by the model, sorted by |
48 // frequency in decreasing order. The list is empty, if the model has not | 48 // frequency in decreasing order. The list is empty, if the model has not |
49 // enough data points. | 49 // enough data points. |
50 std::vector<LanguageInfo> GetTopLanguages() const; | 50 std::vector<LanguageInfo> GetTopLanguages() const; |
51 | 51 |
52 // Returns the estimated frequency for the given language or 0 if the language | 52 // Returns the estimated frequency for the given language or 0 if the language |
53 // is not among the top languages kept in the model. | 53 // is not among the top languages kept in the model. |
54 float GetLanguageFrequency(const std::string& language_code) const; | 54 float GetLanguageFrequency(const std::string& language_code) const; |
55 | 55 |
56 // Informs the model that a page with the given language has been visited. | 56 // Informs the model that a page with the given language has been visited. |
57 void OnPageVisited(const std::string& language_code); | 57 void OnPageVisited(const std::string& language_code); |
58 | 58 |
59 private: | 59 private: |
60 PrefService* pref_service_; | 60 PrefService* pref_service_; |
61 | 61 |
62 DISALLOW_COPY_AND_ASSIGN(LanguageModel); | 62 DISALLOW_COPY_AND_ASSIGN(LanguageModel); |
63 }; | 63 }; |
64 | 64 |
65 } // namespace translate | 65 } // namespace translate |
66 | 66 |
67 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_LANGUAGE_MODEL_H_ | 67 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_LANGUAGE_MODEL_H_ |
OLD | NEW |