Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(940)

Unified Diff: components/translate/core/browser/language_model.cc

Issue 2391383005: [LanguageModel] Return top languages only with a reasonable sample set (Closed)
Patch Set: Fix the constructor Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/translate/core/browser/language_model.cc
diff --git a/components/translate/core/browser/language_model.cc b/components/translate/core/browser/language_model.cc
index 60c435a80d8ed83c0f0a24463852737a08678de1..6c2dd6c500103fb42037b308a8eb947df29eeb65 100644
--- a/components/translate/core/browser/language_model.cc
+++ b/components/translate/core/browser/language_model.cc
@@ -19,6 +19,7 @@ namespace {
const char kLanguageModelCounters[] = "language_model_counters";
const int kMaxCountersSum = 1000;
+const int kMinCountersSum = 100;
const float kCutoffRatio = 0.005f;
const float kDiscountFactor = 0.75f;
@@ -60,9 +61,13 @@ void DiscountAndCleanCounters(base::DictionaryValue* dict) {
std::vector<LanguageModel::LanguageInfo> GetAllLanguages(
const base::DictionaryValue& dict) {
- std::vector<LanguageModel::LanguageInfo> top_languages;
int counters_sum = GetCountersSum(dict);
+ // If the sample is not large enough yet, pretend there are no top languages.
+ if (counters_sum < kMinCountersSum)
+ return std::vector<LanguageModel::LanguageInfo>();
+
+ std::vector<LanguageModel::LanguageInfo> top_languages;
int counter_value = 0;
for (base::DictionaryValue::Iterator itr(dict); !itr.IsAtEnd();
itr.Advance()) {
@@ -103,12 +108,15 @@ float LanguageModel::GetLanguageFrequency(
const std::string& language_code) const {
const base::DictionaryValue* dict =
pref_service_->GetDictionary(kLanguageModelCounters);
+ int counters_sum = GetCountersSum(*dict);
+ // If the sample is not large enough yet, pretend there are no top languages.
+ if (counters_sum < kMinCountersSum)
+ return 0;
+
int counter_value = 0;
// If the key |language_code| does not exist, |counter_value| stays 0.
dict->GetInteger(language_code, &counter_value);
- int counters_sum = GetCountersSum(*dict);
-
return static_cast<float>(counter_value) / counters_sum;
}
« no previous file with comments | « components/translate/core/browser/language_model.h ('k') | components/translate/core/browser/language_model_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698