language_cld3.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #include "spellcheck/third_party/language_cld3.h"
  8. #include "nnet_language_identifier.h"
  9. namespace Platform::Language {
  10. LanguageId Recognize(QStringView text) {
  11. using chrome_lang_id::NNetLanguageIdentifier;
  12. constexpr auto kMinNumBytes = 0;
  13. constexpr auto kMaxNumBytes = 1000;
  14. constexpr auto kMaxLangs = 3;
  15. auto lang_id = NNetLanguageIdentifier(kMinNumBytes, kMaxNumBytes);
  16. const auto string = text.toUtf8().toStdString();
  17. const auto results = lang_id.FindTopNMostFreqLangs(string, kMaxLangs);
  18. auto maxRatio = 0.;
  19. auto final = NNetLanguageIdentifier::Result();
  20. for (const auto &result : results) {
  21. const auto ratio = result.probability * result.proportion;
  22. if (ratio > maxRatio) {
  23. maxRatio = ratio;
  24. final = result;
  25. }
  26. }
  27. if (final.language == NNetLanguageIdentifier::kUnknown) {
  28. return {};
  29. }
  30. return { QLocale(QString::fromStdString(final.language)).language() };
  31. }
  32. } // namespace Platform::Language