Commit 3b94b2d0 authored by Li Lin's avatar Li Lin Committed by Commit Bot

Support extracting entity string from string with UTF-16 characters.

Bug: b/157779488
Test: unit test
Change-Id: Ib7626b9c78c197f2ec2b7e7b7eac489994262893
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2223374
Commit-Queue: Li Lin <llin@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#773754}
parent b41c92b1
......@@ -7,6 +7,7 @@
#include <map>
#include "base/no_destructor.h"
#include "base/strings/utf_string_conversions.h"
#include "chromeos/components/quick_answers/quick_answers_model.h"
#include "chromeos/components/quick_answers/utils/language_detector.h"
#include "chromeos/constants/chromeos_features.h"
......@@ -38,9 +39,13 @@ bool ExtractEntity(const std::string& selected_text,
std::string* entity_str,
std::string* type) {
for (auto& annotation : annotations) {
*entity_str =
selected_text.substr(annotation->start_offset,
annotation->end_offset - annotation->start_offset);
// The offset in annotation result is by chars instead of by bytes. Converts
// to string16 to support extracting substring from string with UTF-16
// characters.
*entity_str = base::UTF16ToUTF8(
base::UTF8ToUTF16(selected_text)
.substr(annotation->start_offset,
annotation->end_offset - annotation->start_offset));
// Use the first entity type.
auto intent_type_map = GetIntentTypeMap();
......
......@@ -241,6 +241,35 @@ TEST_F(IntentGeneratorTest, TextAnnotationUnitIntentExtraChars) {
EXPECT_EQ("23 cm", intent_text_);
}
TEST_F(IntentGeneratorTest, TextAnnotationUnitIntentUtf16Char) {
std::unique_ptr<QuickAnswersRequest> quick_answers_request =
std::make_unique<QuickAnswersRequest>();
quick_answers_request->selected_text = "350°F";
// Create the test annotations.
std::vector<TextEntityPtr> entities;
entities.emplace_back(
TextEntity::New("unit", // Entity name.
1.0, // Confidence score.
TextEntityData::New())); // Data extracted.
auto dictionary_annotation = TextAnnotation::New(0, // Start offset.
5, // End offset.
std::move(entities));
std::vector<TextAnnotationPtr> annotations;
annotations.push_back(dictionary_annotation->Clone());
UseFakeServiceConnection(annotations);
intent_generator_->GenerateIntent(*quick_answers_request);
task_environment_.RunUntilIdle();
EXPECT_EQ(IntentType::kUnit, intent_type_);
EXPECT_EQ("350°F", intent_text_);
}
TEST_F(IntentGeneratorTest, TextAnnotationUnitIntentExtraCharsAboveThreshold) {
std::unique_ptr<QuickAnswersRequest> quick_answers_request =
std::make_unique<QuickAnswersRequest>();
......
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