Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "extensions/renderer/i18n_custom_bindings.h" | 5 #include "extensions/renderer/i18n_custom_bindings.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 34 | 34 |
| 35 namespace extensions { | 35 namespace extensions { |
| 36 | 36 |
| 37 using namespace v8_helpers; | 37 using namespace v8_helpers; |
| 38 | 38 |
| 39 namespace { | 39 namespace { |
| 40 | 40 |
| 41 // Max number of languages to detect. | 41 // Max number of languages to detect. |
| 42 const int kCldNumLangs = 3; | 42 const int kCldNumLangs = 3; |
| 43 | 43 |
| 44 // CLD3 minimum reliable byte threshold. Predictions for inputs below this size | |
| 45 // in bytes will be considered unreliable. | |
| 46 const int kCld3MinimumByteThreshold = 50; | |
| 47 | |
| 44 struct DetectedLanguage { | 48 struct DetectedLanguage { |
| 45 DetectedLanguage(const std::string& language, int percentage) | 49 DetectedLanguage(const std::string& language, int percentage) |
| 46 : language(language), percentage(percentage) {} | 50 : language(language), percentage(percentage) {} |
| 47 ~DetectedLanguage() {} | 51 ~DetectedLanguage() {} |
| 48 | 52 |
| 49 // Returns a new v8::Local<v8::Value> representing the serialized form of | 53 // Returns a new v8::Local<v8::Value> representing the serialized form of |
| 50 // this DetectedLanguage object. | 54 // this DetectedLanguage object. |
| 51 std::unique_ptr<base::DictionaryValue> ToDictionary() const; | 55 std::unique_ptr<base::DictionaryValue> ToDictionary() const; |
| 52 | 56 |
| 53 std::string language; | 57 std::string language; |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 303 } | 307 } |
| 304 | 308 |
| 305 LanguageDetectionResult result(is_reliable); | 309 LanguageDetectionResult result(is_reliable); |
| 306 // populate LanguageDetectionResult with languages and percents | 310 // populate LanguageDetectionResult with languages and percents |
| 307 InitDetectedLanguages(languages, percents, &result.languages); | 311 InitDetectedLanguages(languages, percents, &result.languages); |
| 308 args.GetReturnValue().Set(result.ToValue(context())); | 312 args.GetReturnValue().Set(result.ToValue(context())); |
| 309 | 313 |
| 310 #elif BUILDFLAG(CLD_VERSION) == 3 | 314 #elif BUILDFLAG(CLD_VERSION) == 3 |
| 311 chrome_lang_id::NNetLanguageIdentifier nnet_lang_id(/*min_num_bytes=*/0, | 315 chrome_lang_id::NNetLanguageIdentifier nnet_lang_id(/*min_num_bytes=*/0, |
| 312 /*max_num_bytes=*/512); | 316 /*max_num_bytes=*/512); |
| 313 const std::vector<chrome_lang_id::NNetLanguageIdentifier::Result> | 317 std::vector<chrome_lang_id::NNetLanguageIdentifier::Result> lang_results = |
| 314 lang_results = nnet_lang_id.FindTopNMostFreqLangs(text, kCldNumLangs); | 318 nnet_lang_id.FindTopNMostFreqLangs(text, kCldNumLangs); |
| 319 | |
| 320 // is_reliable is set to false if we believe the input is too short to be | |
| 321 // accurately identified by the current model. | |
| 322 // | |
| 323 // Note that when is_reliable is false, the TranslateExtension .js code | |
|
Devlin
2017/04/03 20:46:10
This is a public API, so we shouldn't include impl
| |
| 324 // gathers additional surrounding context and will try the prediction once | |
| 325 // more. | |
| 326 if (text.size() < kCld3MinimumByteThreshold) { | |
| 327 for (auto& result : lang_results) { | |
| 328 result.is_reliable = false; | |
| 329 } | |
| 330 } | |
| 331 | |
| 315 LanguageDetectionResult result; | 332 LanguageDetectionResult result; |
| 316 | 333 |
| 317 // Populate LanguageDetectionResult with prediction reliability, languages, | 334 // Populate LanguageDetectionResult with prediction reliability, languages, |
| 318 // and the corresponding percentages. | 335 // and the corresponding percentages. |
| 319 InitDetectedLanguages(lang_results, &result); | 336 InitDetectedLanguages(lang_results, &result); |
| 320 args.GetReturnValue().Set(result.ToValue(context())); | 337 args.GetReturnValue().Set(result.ToValue(context())); |
| 321 #else | 338 #else |
| 322 # error "CLD_VERSION must be 2 or 3" | 339 # error "CLD_VERSION must be 2 or 3" |
| 323 #endif | 340 #endif |
| 324 } | 341 } |
| 325 | 342 |
| 326 } // namespace extensions | 343 } // namespace extensions |
| OLD | NEW |