Commit f0d3a4e8 authored by Yue Li's avatar Yue Li Committed by Commit Bot

Quick Answers: Prioritize translation intent

The current annotation model does not work well with non-English words.
Prioritize translation to solve the issue.

Also increase the translation text length threshold.

Bug: b/158509369
Test: Run existing tests
Change-Id: I7316a55f934f570f5c035ced35b03369717a160f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2487540Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Commit-Queue: Yue Li <updowndota@chromium.org>
Cr-Commit-Position: refs/heads/master@{#819652}
parent 1858b3af
...@@ -26,7 +26,7 @@ using machine_learning::mojom::TextClassifier; ...@@ -26,7 +26,7 @@ using machine_learning::mojom::TextClassifier;
// TODO(llin): Finalize on the threshold based on user feedback. // TODO(llin): Finalize on the threshold based on user feedback.
constexpr int kUnitConversionIntentAndSelectionLengthDiffThreshold = 5; constexpr int kUnitConversionIntentAndSelectionLengthDiffThreshold = 5;
constexpr int kTranslationTextLengthThreshold = 50; constexpr int kTranslationTextLengthThreshold = 100;
constexpr int kDefinitionIntentAndSelectionLengthDiffThreshold = 2; constexpr int kDefinitionIntentAndSelectionLengthDiffThreshold = 2;
// TODO(b/169370175): Remove the temporary invalid set after we ramp up to v2 // TODO(b/169370175): Remove the temporary invalid set after we ramp up to v2
...@@ -144,20 +144,11 @@ void IntentGenerator::LoadModelCallback(const QuickAnswersRequest& request, ...@@ -144,20 +144,11 @@ void IntentGenerator::LoadModelCallback(const QuickAnswersRequest& request,
} }
if (text_classifier_) { if (text_classifier_) {
TextAnnotationRequestPtr text_annotation_request = text_classifier_->FindLanguages(
machine_learning::mojom::TextAnnotationRequest::New(); !request.context.surrounding_text.empty()
? request.context.surrounding_text
// TODO(b/159664194): There is a issue with text classifier that some : request.selected_text,
// capitalized words are not annotated properly. Convert the text to lower base::BindOnce(&IntentGenerator::FindLanguagesCallback,
// 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)); weak_factory_.GetWeakPtr(), request));
} }
} }
...@@ -175,8 +166,8 @@ void IntentGenerator::AnnotationCallback( ...@@ -175,8 +166,8 @@ void IntentGenerator::AnnotationCallback(
// Skip the entity for definition annonation. // Skip the entity for definition annonation.
if (it->second == IntentType::kDictionary && if (it->second == IntentType::kDictionary &&
ShouldSkipDefinition(request.selected_text)) { ShouldSkipDefinition(request.selected_text)) {
// Fallback to language detection for generating translation intent. std::move(complete_callback_)
MaybeGenerateTranslationIntent(request); .Run(IntentInfo(request.selected_text, IntentType::kUnknown));
return; return;
} }
std::move(complete_callback_) std::move(complete_callback_)
...@@ -185,30 +176,41 @@ void IntentGenerator::AnnotationCallback( ...@@ -185,30 +176,41 @@ void IntentGenerator::AnnotationCallback(
return; return;
} }
} }
// Fallback to language detection for generating translation intent. std::move(complete_callback_)
MaybeGenerateTranslationIntent(request); .Run(IntentInfo(request.selected_text, IntentType::kUnknown));
} }
void IntentGenerator::FindLanguagesCallback( void IntentGenerator::FindLanguagesCallback(
const QuickAnswersRequest& request, const QuickAnswersRequest& request,
std::vector<machine_learning::mojom::TextLanguagePtr> languages) { std::vector<machine_learning::mojom::TextLanguagePtr> languages) {
auto intent_type = IntentType::kUnknown;
// TODO(b/150034512): Take confidence level into consideration. // TODO(b/150034512): Take confidence level into consideration.
if (languages.empty() || if (!languages.empty() &&
languages.front()->locale == request.context.device_properties.language) { !request.context.device_properties.language.empty() &&
std::move(complete_callback_) languages.front()->locale != request.context.device_properties.language) {
.Run(IntentInfo(request.selected_text, IntentType::kUnknown)); MaybeGenerateTranslationIntent(request, languages.front()->locale);
return; return;
} }
intent_type = IntentType::kTranslation;
std::move(complete_callback_) TextAnnotationRequestPtr text_annotation_request =
.Run(IntentInfo(request.selected_text, intent_type, machine_learning::mojom::TextAnnotationRequest::New();
languages.front()->locale,
request.context.device_properties.language)); // 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));
} }
void IntentGenerator::MaybeGenerateTranslationIntent( void IntentGenerator::MaybeGenerateTranslationIntent(
const QuickAnswersRequest& request) { const QuickAnswersRequest& request,
const std::string& detected_locale) {
DCHECK(complete_callback_); DCHECK(complete_callback_);
if (!features::IsQuickAnswersTranslationEnabled()) { if (!features::IsQuickAnswersTranslationEnabled()) {
...@@ -226,14 +228,10 @@ void IntentGenerator::MaybeGenerateTranslationIntent( ...@@ -226,14 +228,10 @@ void IntentGenerator::MaybeGenerateTranslationIntent(
return; return;
} }
if (text_classifier_) { std::move(complete_callback_)
text_classifier_->FindLanguages( .Run(IntentInfo(request.selected_text, IntentType::kTranslation,
!request.context.surrounding_text.empty() detected_locale,
? request.context.surrounding_text request.context.device_properties.language));
: request.selected_text,
base::BindOnce(&IntentGenerator::FindLanguagesCallback,
weak_factory_.GetWeakPtr(), request));
}
} }
} // namespace quick_answers } // namespace quick_answers
......
...@@ -54,7 +54,8 @@ class IntentGenerator { ...@@ -54,7 +54,8 @@ class IntentGenerator {
const QuickAnswersRequest& request, const QuickAnswersRequest& request,
std::vector<machine_learning::mojom::TextLanguagePtr> languages); std::vector<machine_learning::mojom::TextLanguagePtr> languages);
void MaybeGenerateTranslationIntent(const QuickAnswersRequest& request); void MaybeGenerateTranslationIntent(const QuickAnswersRequest& request,
const std::string& detected_locale);
IntentGeneratorCallback complete_callback_; IntentGeneratorCallback complete_callback_;
mojo::Remote<::chromeos::machine_learning::mojom::TextClassifier> mojo::Remote<::chromeos::machine_learning::mojom::TextClassifier>
......
...@@ -124,7 +124,8 @@ TEST_F(IntentGeneratorTest, TranslationIntentTextLengthAboveThreshold) { ...@@ -124,7 +124,8 @@ TEST_F(IntentGeneratorTest, TranslationIntentTextLengthAboveThreshold) {
QuickAnswersRequest request; QuickAnswersRequest request;
request.selected_text = request.selected_text =
"Search the world's information, including webpages, images, videos and " "Search the world's information, including webpages, images, videos and "
"more."; "more. Google has many special features to help you find exactly what "
"you're looking ...";
request.context.device_properties.language = "es"; request.context.device_properties.language = "es";
intent_generator_->GenerateIntent(request); intent_generator_->GenerateIntent(request);
...@@ -133,10 +134,43 @@ TEST_F(IntentGeneratorTest, TranslationIntentTextLengthAboveThreshold) { ...@@ -133,10 +134,43 @@ TEST_F(IntentGeneratorTest, TranslationIntentTextLengthAboveThreshold) {
EXPECT_EQ(IntentType::kUnknown, intent_info_.intent_type); EXPECT_EQ(IntentType::kUnknown, intent_info_.intent_type);
EXPECT_EQ( EXPECT_EQ(
"Search the world's information, including webpages, images, videos and " "Search the world's information, including webpages, images, videos and "
"more.", "more. Google has many special features to help you find exactly what "
"you're looking ...",
intent_info_.intent_text); intent_info_.intent_text);
} }
TEST_F(IntentGeneratorTest, TranslationIntentWithAnnotation) {
QuickAnswersRequest request;
request.selected_text = "unfathomable";
request.context.device_properties.language = "es";
// Create the test annotations.
std::vector<TextEntityPtr> entities;
entities.emplace_back(
TextEntity::New("dictionary", // Entity name.
1.0, // Confidence score.
TextEntityData::New())); // Data extracted.
auto dictionary_annotation = TextAnnotation::New(0, // Start offset.
12, // End offset.
std::move(entities));
std::vector<TextAnnotationPtr> annotations;
annotations.push_back(dictionary_annotation->Clone());
std::vector<TextLanguagePtr> languages;
languages.push_back(DefaultLanguage());
UseFakeServiceConnection(annotations, languages);
intent_generator_->GenerateIntent(request);
task_environment_.RunUntilIdle();
EXPECT_EQ(IntentType::kTranslation, 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) { TEST_F(IntentGeneratorTest, TranslationIntentNotEnabled) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatures( scoped_feature_list.InitWithFeatures(
......
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