Commit 0efc948b authored by Yue Li's avatar Yue Li Committed by Commit Bot

Quick Answers: Clean up language detector

Bug: b/168541952
Test: Run existing tests
Change-Id: I3aa883b677516b646658c583165eff017e728117
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2518722
Auto-Submit: Yue Li <updowndota@chromium.org>
Commit-Queue: Xiyuan Xia <xiyuan@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#824029}
parent 56345663
......@@ -32,8 +32,6 @@ source_set("quick_answers") {
"translation_result_loader.h",
"understanding/intent_generator.cc",
"understanding/intent_generator.h",
"utils/language_detector.cc",
"utils/language_detector.h",
"utils/quick_answers_metrics.cc",
"utils/quick_answers_metrics.h",
"utils/quick_answers_utils.cc",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/components/quick_answers/utils/language_detector.h"
#include "third_party/cld_3/src/src/nnet_language_identifier.h"
namespace chromeos {
namespace quick_answers {
namespace {
using chrome_lang_id::NNetLanguageIdentifier;
const int kMinNumBytes = 0;
const int kMaxNumBytes = 1000;
} // namespace
LanguageDetector::LanguageDetector() {
lang_id_ =
std::make_unique<NNetLanguageIdentifier>(kMinNumBytes, kMaxNumBytes);
}
LanguageDetector::~LanguageDetector() = default;
std::string LanguageDetector::DetectLanguage(const std::string& text) {
const NNetLanguageIdentifier::Result result = lang_id_->FindLanguage(text);
std::string language;
if (result.is_reliable)
language = result.language;
return language;
}
} // namespace quick_answers
} // namespace chromeos
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_COMPONENTS_QUICK_ANSWERS_UTILS_LANGUAGE_DETECTOR_H_
#define CHROMEOS_COMPONENTS_QUICK_ANSWERS_UTILS_LANGUAGE_DETECTOR_H_
#include <memory>
#include <string>
namespace chrome_lang_id {
class NNetLanguageIdentifier;
} // namespace chrome_lang_id
namespace chromeos {
namespace quick_answers {
// Utility class for language detection.
// TODO(b/168541952): Cleanup this class after the new language detection API
// becomes stable.
class LanguageDetector {
public:
LanguageDetector();
LanguageDetector(const LanguageDetector&) = delete;
LanguageDetector& operator=(const LanguageDetector&) = delete;
virtual ~LanguageDetector();
// Returns the ISO 639 language code of the specified |text|, or empty string
// if it failed. Virtual for testing.
virtual std::string DetectLanguage(const std::string& text);
private:
std::unique_ptr<chrome_lang_id::NNetLanguageIdentifier> lang_id_;
};
} // namespace quick_answers
} // namespace chromeos
#endif // CHROMEOS_COMPONENTS_QUICK_ANSWERS_UTILS_LANGUAGE_DETECTOR_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment