Commit 4fe1d718 authored by Yue Li's avatar Yue Li Committed by Chromium LUCI CQ

Quick Answers: Prioritize annotations against translation

We used to prioritize translation intent since the annonation model has
performance issues. Since we switched to the more stable dictionary
based model, reset the priority.

Bug: None
Test: Run existing tests
Change-Id: I939d0f237c6adaf0e12361177efb60ed17990a9d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2641993Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Reviewed-by: default avatarJeroen Dhollander <jeroendh@chromium.org>
Commit-Queue: Yue Li <updowndota@chromium.org>
Cr-Commit-Position: refs/heads/master@{#845845}
parent 7e4dc059
......@@ -159,11 +159,20 @@ void IntentGenerator::LoadModelCallback(const QuickAnswersRequest& request,
}
if (text_classifier_) {
language_detector_ =
std::make_unique<LanguageDetector>(text_classifier_.get());
language_detector_->DetectLanguage(
request.context.surrounding_text, request.selected_text,
base::BindOnce(&IntentGenerator::LanguageDetectorCallback,
TextAnnotationRequestPtr text_annotation_request =
machine_learning::mojom::TextAnnotationRequest::New();
// TODO(b/159664194): There is a issue with text classifier that some
// capitalized words are not annotated properly. Convert the text to lower
// case for now. Clean up after the issue is fixed.
text_annotation_request->text = base::UTF16ToUTF8(
base::i18n::ToLower(base::UTF8ToUTF16(request.selected_text)));
text_annotation_request->default_locales =
request.context.device_properties.language;
text_classifier_->Annotate(
std::move(text_annotation_request),
base::BindOnce(&IntentGenerator::AnnotationCallback,
weak_factory_.GetWeakPtr(), request));
}
}
......@@ -181,8 +190,8 @@ void IntentGenerator::AnnotationCallback(
// Skip the entity for definition annonation.
if (it->second == IntentType::kDictionary &&
ShouldSkipDefinition(request.selected_text)) {
std::move(complete_callback_)
.Run(IntentInfo(request.selected_text, IntentType::kUnknown));
// Fallback to language detection for generating translation intent.
MaybeGenerateTranslationIntent(request);
return;
}
std::move(complete_callback_)
......@@ -191,46 +200,12 @@ void IntentGenerator::AnnotationCallback(
return;
}
}
std::move(complete_callback_)
.Run(IntentInfo(request.selected_text, IntentType::kUnknown));
}
void IntentGenerator::LanguageDetectorCallback(
const QuickAnswersRequest& request,
base::Optional<std::string> detected_language) {
language_detector_.reset();
// Generate translation intent if the detected language is different to the
// system language and is not one of the preferred languages.
if (detected_language.has_value() &&
detected_language.value() != request.context.device_properties.language &&
!IsPreferredLanguage(
detected_language.value(),
request.context.device_properties.preferred_languages)) {
MaybeGenerateTranslationIntent(request, detected_language.value());
return;
}
TextAnnotationRequestPtr text_annotation_request =
machine_learning::mojom::TextAnnotationRequest::New();
// TODO(b/159664194): There is a issue with text classifier that some
// capitalized words are not annotated properly. Convert the text to lower
// case for now. Clean up after the issue is fixed.
text_annotation_request->text = base::UTF16ToUTF8(
base::i18n::ToLower(base::UTF8ToUTF16(request.selected_text)));
text_annotation_request->default_locales =
request.context.device_properties.language;
text_classifier_->Annotate(
std::move(text_annotation_request),
base::BindOnce(&IntentGenerator::AnnotationCallback,
weak_factory_.GetWeakPtr(), request));
// Fallback to language detection for generating translation intent.
MaybeGenerateTranslationIntent(request);
}
void IntentGenerator::MaybeGenerateTranslationIntent(
const QuickAnswersRequest& request,
const std::string& detected_language) {
const QuickAnswersRequest& request) {
DCHECK(complete_callback_);
if (!features::IsQuickAnswersTranslationEnabled()) {
......@@ -249,10 +224,35 @@ void IntentGenerator::MaybeGenerateTranslationIntent(
return;
}
language_detector_ =
std::make_unique<LanguageDetector>(text_classifier_.get());
language_detector_->DetectLanguage(
request.context.surrounding_text, request.selected_text,
base::BindOnce(&IntentGenerator::LanguageDetectorCallback,
weak_factory_.GetWeakPtr(), request));
}
void IntentGenerator::LanguageDetectorCallback(
const QuickAnswersRequest& request,
base::Optional<std::string> detected_language) {
language_detector_.reset();
// Generate translation intent if the detected language is different to the
// system language and is not one of the preferred languages.
if (detected_language.has_value() &&
detected_language.value() != request.context.device_properties.language &&
!IsPreferredLanguage(
detected_language.value(),
request.context.device_properties.preferred_languages)) {
std::move(complete_callback_)
.Run(IntentInfo(request.selected_text, IntentType::kTranslation,
detected_language.value(),
request.context.device_properties.language));
return;
}
std::move(complete_callback_)
.Run(IntentInfo(request.selected_text, IntentType::kTranslation,
detected_language,
request.context.device_properties.language));
.Run(IntentInfo(request.selected_text, IntentType::kUnknown));
}
} // namespace quick_answers
......
......@@ -51,11 +51,10 @@ class IntentGenerator {
void AnnotationCallback(
const QuickAnswersRequest& request,
std::vector<machine_learning::mojom::TextAnnotationPtr> annotations);
void LanguageDetectorCallback(const QuickAnswersRequest& request,
base::Optional<std::string> detected_locale);
void MaybeGenerateTranslationIntent(const QuickAnswersRequest& request,
const std::string& detected_locale);
void MaybeGenerateTranslationIntent(const QuickAnswersRequest& request);
void LanguageDetectorCallback(const QuickAnswersRequest& request,
base::Optional<std::string> detected_language);
IntentGeneratorCallback complete_callback_;
mojo::Remote<::chromeos::machine_learning::mojom::TextClassifier>
......
......@@ -212,12 +212,10 @@ TEST_F(IntentGeneratorTest, TranslationIntentWithAnnotation) {
task_environment_.RunUntilIdle();
// Should generate translation intent which is prioritized against
// annotations.
EXPECT_EQ(IntentType::kTranslation, intent_info_.intent_type);
// Should generate dictionary intent which is prioritized against
// translation.
EXPECT_EQ(IntentType::kDictionary, intent_info_.intent_type);
EXPECT_EQ("unfathomable", intent_info_.intent_text);
EXPECT_EQ("en", intent_info_.source_language);
EXPECT_EQ("es", intent_info_.target_language);
}
TEST_F(IntentGeneratorTest, TranslationIntentNotEnabled) {
......
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