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

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

Issue 2200493002: using ulp to improve TranslateManager GetTargetLanguage() and InitiateTranslation() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix global state problem in unittests Created 4 years, 4 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/translate_prefs.cc
diff --git a/components/translate/core/browser/translate_prefs.cc b/components/translate/core/browser/translate_prefs.cc
index 98419c0c91474d127ccef774ead2cf8f0f123a91..ed57e985d6a84ce90cf3491b52e7cd6a0d1db949 100644
--- a/components/translate/core/browser/translate_prefs.cc
+++ b/components/translate/core/browser/translate_prefs.cc
@@ -20,6 +20,7 @@
namespace translate {
+const char TranslatePrefs::kPrefLanguageProfile[] = "language_profile";
const char TranslatePrefs::kPrefTranslateSiteBlacklist[] =
"translate_site_blacklist";
const char TranslatePrefs::kPrefTranslateWhitelists[] = "translate_whitelists";
@@ -35,10 +36,18 @@ const char TranslatePrefs::kPrefTranslateLastDeniedTimeForLanguage[] =
"translate_last_denied_time_for_language";
const char TranslatePrefs::kPrefTranslateTooOftenDeniedForLanguage[] =
"translate_too_often_denied_for_language";
+
const char kTranslateUI2016Q2TrialName[] = "TranslateUI2016Q2";
const char kAlwaysTranslateOfferThreshold[] =
"always_translate_offer_threshold";
+// For reading ULP prefs.
+const char kConfidence[] = "confidence";
+const char kLanguage[] = "language";
+const char kPreference[] = "preference";
+const char kProbability[] = "probability";
+const char kReading[] = "reading";
+
// The below properties used to be used but now are deprecated. Don't use them
// since an old profile might have some values there.
//
@@ -474,6 +483,8 @@ void TranslatePrefs::RegisterProfilePrefs(
registry->RegisterDictionaryPref(
kPrefTranslateTooOftenDeniedForLanguage,
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
+ registry->RegisterDictionaryPref(
+ kPrefLanguageProfile, user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
}
// static
@@ -569,4 +580,58 @@ bool TranslatePrefs::IsDictionaryEmpty(const char* pref_id) const {
return (dict == NULL || dict->empty());
}
+double TranslatePrefs::GetReadingFromUserLanguageProfile(
+ LanguageAndProbabilityList* out_value) const {
+ const base::DictionaryValue* dict =
+ prefs_->GetDictionary(kPrefLanguageProfile);
+ const base::DictionaryValue* entries = nullptr;
+
+ // Return 0.0 if no ULP prefs.
+ if (!dict)
+ return 0.0;
+
+ // Return 0.0 if no such list.
+ if (!dict->GetDictionary(kReading, &entries))
+ return 0.0;
+
+ double confidence = 0.0;
+ // Return 0.0 if cannot find confidence.
+ if (!entries->GetDouble(kConfidence, &confidence))
+ return 0.0;
+
+ const base::ListValue* preference = nullptr;
+ // Return the confidence if there are no item on the 'preference' field.
+ if (!entries->GetList(kPreference, &preference))
+ return confidence;
+
+ // Use a map to fold the probability of all the same normalized language
+ // code together.
+ std::map<std::string, double> probability_map;
+ // Iterate through the preference.
+ for (const auto& entry : *preference) {
+ const base::DictionaryValue* item = nullptr;
+ std::string language;
+ double probability = 0.0;
+ if (entry->GetAsDictionary(&item) &&
+ item->GetString(kLanguage, &language) &&
+ item->GetDouble(kProbability, &probability)) {
+ // Normalize the the language code known and supported by
+ // Translate.
+ translate::ToTranslateLanguageSynonym(&language);
+ // Discard if the normalized version is unsupported.
+ if (TranslateDownloadManager::IsSupportedLanguage(language)) {
+ probability_map[language] += probability;
+ }
+ }
+ }
+ for (const auto& it : probability_map)
+ out_value->push_back(it);
+ std::sort(out_value->begin(), out_value->end(),
+ [](const LanguageAndProbability& left,
+ const LanguageAndProbability& right) {
+ return left.second > right.second;
+ });
+ return confidence;
+}
+
} // namespace translate

Powered by Google App Engine
This is Rietveld 408576698