1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
|
// Copyright 2020 The Chromium Authors
// 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/understanding/intent_generator.h"
#include <memory>
#include <string>
#include "base/functional/bind.h"
#include "base/test/task_environment.h"
#include "chromeos/components/quick_answers/public/cpp/quick_answers_state.h"
#include "chromeos/components/quick_answers/quick_answers_model.h"
#include "chromeos/components/quick_answers/test/quick_answers_test_base.h"
#include "chromeos/components/quick_answers/utils/quick_answers_utils.h"
#include "chromeos/components/quick_answers/utils/spell_checker.h"
#include "chromeos/services/machine_learning/public/cpp/fake_service_connection.h"
#include "chromeos/services/machine_learning/public/mojom/machine_learning_service.mojom.h"
#include "chromeos/services/machine_learning/public/mojom/text_classifier.mojom.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace quick_answers {
namespace {
using ::chromeos::machine_learning::FakeServiceConnectionImpl;
using ::chromeos::machine_learning::mojom::TextAnnotation;
using ::chromeos::machine_learning::mojom::TextAnnotationPtr;
using ::chromeos::machine_learning::mojom::TextEntity;
using ::chromeos::machine_learning::mojom::TextEntityData;
using ::chromeos::machine_learning::mojom::TextEntityPtr;
using ::chromeos::machine_learning::mojom::TextLanguage;
using ::chromeos::machine_learning::mojom::TextLanguagePtr;
TextLanguagePtr DefaultLanguage() {
return TextLanguage::New("en", /* confidence */ 1);
}
std::vector<TextLanguagePtr> DefaultLanguages() {
std::vector<TextLanguagePtr> languages;
languages.push_back(DefaultLanguage());
return languages;
}
class FakeSpellChecker : public SpellChecker {
public:
FakeSpellChecker(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
: SpellChecker(url_loader_factory) {}
~FakeSpellChecker() override = default;
// SpellChecker:
void CheckSpelling(const std::string& word,
CheckSpellingCallback callback) override {
std::move(callback).Run(dictionary_.find(word) != dictionary_.end(),
dictionary_[word]);
}
void AddWordToDictionary(const std::string& word,
const std::string& language = "en") {
dictionary_.insert({word, language});
}
private:
std::map<std::string, std::string> dictionary_;
};
} // namespace
class IntentGeneratorTest : public QuickAnswersTestBase {
public:
IntentGeneratorTest() = default;
IntentGeneratorTest(const IntentGeneratorTest&) = delete;
IntentGeneratorTest& operator=(const IntentGeneratorTest&) = delete;
void SetUp() override {
QuickAnswersTestBase::SetUp();
test_shared_loader_factory_ =
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&test_url_loader_factory_);
spell_checker_ =
std::make_unique<FakeSpellChecker>(test_shared_loader_factory_);
intent_generator_ = std::make_unique<IntentGenerator>(
spell_checker_->GetWeakPtr(),
base::BindOnce(&IntentGeneratorTest::IntentGeneratorTestCallback,
base::Unretained(this)));
fake_quick_answers_state()->set_use_text_annotator_for_testing();
fake_quick_answers_state()->SetApplicationLocale("en");
fake_quick_answers_state()->SetPreferredLanguages("en");
}
void TearDown() override {
intent_generator_.reset();
spell_checker_.reset();
QuickAnswersTestBase::TearDown();
}
void IntentGeneratorTestCallback(const IntentInfo& intent_info) {
intent_info_ = intent_info;
}
// Flush all relevant Mojo pipes.
void FlushForTesting() {
intent_generator_->FlushForTesting();
fake_service_connection_.FlushForTesting();
}
protected:
void UseFakeServiceConnection(
const std::vector<TextAnnotationPtr>& annotations =
std::vector<TextAnnotationPtr>(),
const std::vector<TextLanguagePtr>& languages = DefaultLanguages()) {
chromeos::machine_learning::ServiceConnection::
UseFakeServiceConnectionForTesting(&fake_service_connection_);
chromeos::machine_learning::ServiceConnection::GetInstance()->Initialize();
fake_service_connection_.SetOutputAnnotation(annotations);
fake_service_connection_.SetOutputLanguages(languages);
}
FakeSpellChecker* spell_checker() { return spell_checker_.get(); }
base::test::TaskEnvironment task_environment_;
network::TestURLLoaderFactory test_url_loader_factory_;
scoped_refptr<network::SharedURLLoaderFactory> test_shared_loader_factory_;
std::unique_ptr<FakeSpellChecker> spell_checker_;
std::unique_ptr<IntentGenerator> intent_generator_;
IntentInfo intent_info_;
FakeServiceConnectionImpl fake_service_connection_;
};
TEST_F(IntentGeneratorTest, TranslationIntent) {
UseFakeServiceConnection();
QuickAnswersRequest request;
request.selected_text = "quick answers";
fake_quick_answers_state()->SetApplicationLocale("es");
fake_quick_answers_state()->SetPreferredLanguages("es");
intent_generator_->GenerateIntent(request);
FlushForTesting();
// Should generate translation intent.
EXPECT_EQ(IntentType::kTranslation, intent_info_.intent_type);
EXPECT_EQ("quick answers", intent_info_.intent_text);
EXPECT_EQ("es", intent_info_.device_language);
EXPECT_EQ("en", intent_info_.source_language);
}
TEST_F(IntentGeneratorTest, TranslationIntentWithSubtag) {
std::vector<TextLanguagePtr> languages;
languages.push_back(TextLanguage::New("en-US", /* confidence */ 1));
UseFakeServiceConnection({}, languages);
QuickAnswersRequest request;
request.selected_text = "quick answers";
fake_quick_answers_state()->SetApplicationLocale("es");
fake_quick_answers_state()->SetPreferredLanguages("es");
intent_generator_->GenerateIntent(request);
FlushForTesting();
// Should generate translation intent.
EXPECT_EQ(IntentType::kTranslation, intent_info_.intent_type);
EXPECT_EQ("quick answers", intent_info_.intent_text);
EXPECT_EQ("es", intent_info_.device_language);
// Should drop substag for source language.
EXPECT_EQ("en", intent_info_.source_language);
}
TEST_F(IntentGeneratorTest, TranslationIntentSameLanguage) {
UseFakeServiceConnection();
QuickAnswersRequest request;
request.selected_text = "quick answers";
fake_quick_answers_state()->SetApplicationLocale("en");
fake_quick_answers_state()->SetPreferredLanguages("en");
intent_generator_->GenerateIntent(request);
FlushForTesting();
// Should not generate translation intent since the detected language is the
// same as system language.
EXPECT_EQ(IntentType::kUnknown, intent_info_.intent_type);
EXPECT_EQ("quick answers", intent_info_.intent_text);
}
TEST_F(IntentGeneratorTest, TranslationIntentPreferredLocale) {
UseFakeServiceConnection();
QuickAnswersRequest request;
request.selected_text = "quick answers";
fake_quick_answers_state()->SetApplicationLocale("es");
fake_quick_answers_state()->SetPreferredLanguages("es,en,zh");
intent_generator_->GenerateIntent(request);
FlushForTesting();
// Should not generate translation intent since the detected language is in
// the preferred languages list.
EXPECT_EQ(IntentType::kUnknown, intent_info_.intent_type);
EXPECT_EQ("quick answers", intent_info_.intent_text);
}
TEST_F(IntentGeneratorTest, TranslationIntentPreferredLanguage) {
UseFakeServiceConnection();
QuickAnswersRequest request;
request.selected_text = "quick answers";
fake_quick_answers_state()->SetApplicationLocale("es");
fake_quick_answers_state()->SetPreferredLanguages("es-MX,en-US,zh-CN");
intent_generator_->GenerateIntent(request);
FlushForTesting();
// Should not generate translation intent since the detected language is in
// the preferred languages list.
EXPECT_EQ(IntentType::kUnknown, intent_info_.intent_type);
EXPECT_EQ("quick answers", intent_info_.intent_text);
}
TEST_F(IntentGeneratorTest, TranslationIntentTextLengthAboveThreshold) {
UseFakeServiceConnection();
QuickAnswersRequest request;
request.selected_text =
"Search the world's information, including webpages, images, videos and "
"more. Google has many special features to help you find exactly what "
"you're looking ...";
fake_quick_answers_state()->SetApplicationLocale("es");
fake_quick_answers_state()->SetPreferredLanguages("es");
intent_generator_->GenerateIntent(request);
FlushForTesting();
// Should not generate translation intent since the length of the selected
// text is above the threshold.
EXPECT_EQ(IntentType::kUnknown, intent_info_.intent_type);
EXPECT_EQ(
"Search the world's information, including webpages, images, videos and "
"more. Google has many special features to help you find exactly what "
"you're looking ...",
intent_info_.intent_text);
}
TEST_F(IntentGeneratorTest, TranslationIntentWithAnnotation) {
QuickAnswersRequest request;
request.selected_text = "prueba";
// Create the test annotations.
std::vector<TextEntityPtr> entities;
entities.emplace_back(TextEntity::New(
"dictionary", // Entity name.
1.0, // Confidence score.
TextEntityData::NewNumericValue(0.0))); // Data extracted.
auto dictionary_annotation = TextAnnotation::New(0, // Start offset.
6, // End offset.
std::move(entities));
std::vector<TextAnnotationPtr> annotations;
annotations.push_back(dictionary_annotation->Clone());
std::vector<TextLanguagePtr> languages;
languages.push_back(TextLanguage::New("es", /* confidence */ 1));
UseFakeServiceConnection(annotations, languages);
intent_generator_->GenerateIntent(request);
FlushForTesting();
// Should generate dictionary intent which is prioritized against
// translation.
EXPECT_EQ(IntentType::kDictionary, intent_info_.intent_type);
EXPECT_EQ("prueba", intent_info_.intent_text);
}
TEST_F(IntentGeneratorTest, TranslationIntentDeviceLanguageNotSet) {
UseFakeServiceConnection();
QuickAnswersRequest request;
request.selected_text = "quick answers";
intent_generator_->GenerateIntent(request);
FlushForTesting();
// Should not generate translation intent since the device language is not
// set.
EXPECT_EQ(IntentType::kUnknown, intent_info_.intent_type);
EXPECT_EQ("quick answers", intent_info_.intent_text);
}
TEST_F(IntentGeneratorTest, TranslationIntentUnsupportedDeviceLanguage) {
UseFakeServiceConnection();
QuickAnswersRequest request;
request.selected_text = "quick answers";
fake_quick_answers_state()->SetApplicationLocale("unk");
fake_quick_answers_state()->SetPreferredLanguages("unk");
intent_generator_->GenerateIntent(request);
FlushForTesting();
// Should not generate translation intent since the device language is
// not in the supported languages list.
EXPECT_EQ(IntentType::kUnknown, intent_info_.intent_type);
EXPECT_EQ("quick answers", intent_info_.intent_text);
}
TEST_F(IntentGeneratorTest, TranslationIntentUnsupportedSourceLanguage) {
std::vector<TextLanguagePtr> languages;
languages.push_back(TextLanguage::New("unk", /* confidence */ 1));
UseFakeServiceConnection({}, languages);
QuickAnswersRequest request;
request.selected_text = "quick answers";
fake_quick_answers_state()->SetApplicationLocale("en");
fake_quick_answers_state()->SetPreferredLanguages("en");
intent_generator_->GenerateIntent(request);
FlushForTesting();
// Should not generate translation intent since the detected source
// language is not in the supported languages list.
EXPECT_EQ(IntentType::kUnknown, intent_info_.intent_type);
EXPECT_EQ("quick answers", intent_info_.intent_text);
}
TEST_F(IntentGeneratorTest, TextAnnotationDefinitionIntent) {
std::unique_ptr<QuickAnswersRequest> quick_answers_request =
std::make_unique<QuickAnswersRequest>();
quick_answers_request->selected_text = "unfathomable";
// Create the test annotations.
std::vector<TextEntityPtr> entities;
entities.emplace_back(TextEntity::New(
"dictionary", // Entity name.
1.0, // Confidence score.
TextEntityData::NewNumericValue(0.0))); // 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());
UseFakeServiceConnection(annotations);
intent_generator_->GenerateIntent(*quick_answers_request);
FlushForTesting();
// Should generate dictionary intent.
EXPECT_EQ(IntentType::kDictionary, intent_info_.intent_type);
EXPECT_EQ("unfathomable", intent_info_.intent_text);
}
TEST_F(IntentGeneratorTest,
TextAnnotationDefinitionIntentExtraCharsBelowThreshold) {
std::unique_ptr<QuickAnswersRequest> quick_answers_request =
std::make_unique<QuickAnswersRequest>();
quick_answers_request->selected_text = "“unfathomable”";
// Create the test annotations.
std::vector<TextEntityPtr> entities;
entities.emplace_back(TextEntity::New(
"dictionary", // Entity name.
1.0, // Confidence score.
TextEntityData::NewNumericValue(0.0))); // Data extracted.
auto dictionary_annotation = TextAnnotation::New(1, // Start offset.
13, // End offset.
std::move(entities));
std::vector<TextAnnotationPtr> annotations;
annotations.push_back(dictionary_annotation->Clone());
UseFakeServiceConnection(annotations);
intent_generator_->GenerateIntent(*quick_answers_request);
FlushForTesting();
// Should generate dictionary intent since the extra characters is below the
// threshold.
EXPECT_EQ(IntentType::kDictionary, intent_info_.intent_type);
EXPECT_EQ("unfathomable", intent_info_.intent_text);
}
TEST_F(IntentGeneratorTest,
TextAnnotationDefinitionIntentExtraCharsAboveThreshold) {
std::unique_ptr<QuickAnswersRequest> quick_answers_request =
std::make_unique<QuickAnswersRequest>();
quick_answers_request->selected_text = "the unfathomable";
// Create the test annotations.
std::vector<TextEntityPtr> entities;
entities.emplace_back(TextEntity::New(
"dictionary", // Entity name.
1.0, // Confidence score.
TextEntityData::NewNumericValue(0.0))); // Data extracted.
auto dictionary_annotation = TextAnnotation::New(4, // Start offset.
16, // End offset.
std::move(entities));
std::vector<TextAnnotationPtr> annotations;
annotations.push_back(dictionary_annotation->Clone());
UseFakeServiceConnection(annotations);
intent_generator_->GenerateIntent(*quick_answers_request);
FlushForTesting();
// Should not generate dictionary intent since the extra characters is above
// the threshold.
EXPECT_EQ(IntentType::kUnknown, intent_info_.intent_type);
EXPECT_EQ("unfathomable", intent_info_.intent_text);
}
TEST_F(IntentGeneratorTest, TextAnnotationUnitIntentExtraChars) {
std::unique_ptr<QuickAnswersRequest> quick_answers_request =
std::make_unique<QuickAnswersRequest>();
quick_answers_request->selected_text = "23 cm to";
// Create the test annotations.
std::vector<TextEntityPtr> entities;
entities.emplace_back(TextEntity::New(
"unit", // Entity name.
1.0, // Confidence score.
TextEntityData::NewNumericValue(0.0))); // 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);
FlushForTesting();
// Should generate unit conversion intent.
EXPECT_EQ(IntentType::kUnit, intent_info_.intent_type);
EXPECT_EQ("23 cm", intent_info_.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::NewNumericValue(0.0))); // 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);
FlushForTesting();
// Should generate unit conversion intent.
EXPECT_EQ(IntentType::kUnit, intent_info_.intent_type);
EXPECT_EQ("350°F", intent_info_.intent_text);
}
TEST_F(IntentGeneratorTest, TextAnnotationUnitIntentExtraCharsAboveThreshold) {
std::unique_ptr<QuickAnswersRequest> quick_answers_request =
std::make_unique<QuickAnswersRequest>();
quick_answers_request->selected_text = "23 cm is equal to 9.06 inches";
// Create the test annotations.
std::vector<TextEntityPtr> entities;
entities.emplace_back(TextEntity::New(
"unit", // Entity name.
1.0, // Confidence score.
TextEntityData::NewNumericValue(0.0))); // 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);
FlushForTesting();
// Should not generate unit conversion intent since the extra characters is
// above the threshold.
EXPECT_EQ(IntentType::kUnknown, intent_info_.intent_type);
EXPECT_EQ("23 cm", intent_info_.intent_text);
}
TEST_F(IntentGeneratorTest, TextAnnotationNonEnglishLanguage) {
fake_quick_answers_state()->SetApplicationLocale("es");
fake_quick_answers_state()->SetPreferredLanguages("es");
std::unique_ptr<QuickAnswersRequest> quick_answers_request =
std::make_unique<QuickAnswersRequest>();
quick_answers_request->selected_text = "unfathomable";
// Create the test annotations.
std::vector<TextEntityPtr> entities;
entities.emplace_back(TextEntity::New(
"dictionary", // Entity name.
1.0, // Confidence score.
TextEntityData::NewNumericValue(0.0))); // 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(TextLanguage::New("en", /* confidence */ 1));
UseFakeServiceConnection(annotations, languages);
intent_generator_->GenerateIntent(*quick_answers_request);
FlushForTesting();
// Should not generate dictionary intent since English is not device language
// or preferred language. Should fallback to translation intent.
EXPECT_EQ(IntentType::kTranslation, intent_info_.intent_type);
EXPECT_EQ("unfathomable", intent_info_.intent_text);
}
TEST_F(IntentGeneratorTest, TextAnnotationIntentNoAnnotation) {
std::unique_ptr<QuickAnswersRequest> quick_answers_request =
std::make_unique<QuickAnswersRequest>();
quick_answers_request->selected_text = "the unfathomable reaches of space";
std::vector<TextAnnotationPtr> annotations;
UseFakeServiceConnection(annotations);
intent_generator_->GenerateIntent(*quick_answers_request);
FlushForTesting();
// Should generate unknown intent since no annotation found.
EXPECT_EQ(IntentType::kUnknown, intent_info_.intent_type);
EXPECT_EQ("the unfathomable reaches of space", intent_info_.intent_text);
}
TEST_F(IntentGeneratorTest, TextAnnotationIntentNoEntity) {
std::unique_ptr<QuickAnswersRequest> quick_answers_request =
std::make_unique<QuickAnswersRequest>();
quick_answers_request->selected_text = "the unfathomable reaches of space";
std::vector<TextEntityPtr> entities;
auto dictionary_annotation = TextAnnotation::New(4, // Start offset.
16, // End offset.
std::move(entities));
std::vector<TextAnnotationPtr> annotations;
annotations.push_back(dictionary_annotation->Clone());
UseFakeServiceConnection(annotations);
intent_generator_->GenerateIntent(*quick_answers_request);
FlushForTesting();
// Should generate unknown intent since no entity found.
EXPECT_EQ(IntentType::kUnknown, intent_info_.intent_type);
EXPECT_EQ("the unfathomable reaches of space", intent_info_.intent_text);
}
TEST_F(IntentGeneratorTest, TextAnnotationIntentUnSupportedEntity) {
std::unique_ptr<QuickAnswersRequest> quick_answers_request =
std::make_unique<QuickAnswersRequest>();
quick_answers_request->selected_text = "the unfathomable reaches of space";
// Create the test annotations.
std::vector<TextEntityPtr> entities;
entities.emplace_back(TextEntity::New(
"something_else", // Entity name.
1.0, // Confidence score.
TextEntityData::NewNumericValue(0.0))); // Data extracted.
auto some_annotation = TextAnnotation::New(4, // Start offset.
16, // End offset.
std::move(entities));
std::vector<TextAnnotationPtr> annotations;
annotations.push_back(some_annotation->Clone());
UseFakeServiceConnection(annotations);
intent_generator_->GenerateIntent(*quick_answers_request);
FlushForTesting();
// Should generate unknown intent unsupported entity is provided.
EXPECT_EQ(IntentType::kUnknown, intent_info_.intent_type);
EXPECT_EQ("the unfathomable reaches of space", intent_info_.intent_text);
}
TEST_F(IntentGeneratorTest, ShouldTriggerForSingleWordInDictionary) {
const std::string kWord = "single";
// No Annotation provided.
std::vector<TextAnnotationPtr> annotations;
UseFakeServiceConnection(annotations);
// Add word to the dictionary.
spell_checker()->AddWordToDictionary(kWord);
// Word selected.
std::unique_ptr<QuickAnswersRequest> quick_answers_request =
std::make_unique<QuickAnswersRequest>();
quick_answers_request->selected_text = kWord;
intent_generator_->GenerateIntent(*quick_answers_request);
task_environment_.RunUntilIdle();
// Should generate dictionary intent.
EXPECT_EQ(IntentType::kDictionary, intent_info_.intent_type);
EXPECT_EQ(kWord, intent_info_.intent_text);
}
TEST_F(IntentGeneratorTest, ShouldTriggerForNonEnglishWordInDictionary) {
const std::string kWord = "palabra";
const std::string kLanguage = "es";
// No Annotation provided.
std::vector<TextAnnotationPtr> annotations;
UseFakeServiceConnection(annotations);
// Add word to the dictionary.
spell_checker()->AddWordToDictionary(kWord, kLanguage);
// Word selected.
std::unique_ptr<QuickAnswersRequest> quick_answers_request =
std::make_unique<QuickAnswersRequest>();
quick_answers_request->selected_text = kWord;
intent_generator_->GenerateIntent(*quick_answers_request);
task_environment_.RunUntilIdle();
// Should generate dictionary intent.
EXPECT_EQ(IntentType::kDictionary, intent_info_.intent_type);
EXPECT_EQ(kWord, intent_info_.intent_text);
EXPECT_EQ(kLanguage, intent_info_.source_language);
}
TEST_F(IntentGeneratorTest,
ShouldNotTriggerForSingleWordInDictionaryWithDigits) {
const std::string kWord = "1st";
// No Annotation provided.
std::vector<TextAnnotationPtr> annotations;
UseFakeServiceConnection(annotations);
// Add word to the dictionary.
spell_checker()->AddWordToDictionary(kWord);
// Word selected.
std::unique_ptr<QuickAnswersRequest> quick_answers_request =
std::make_unique<QuickAnswersRequest>();
quick_answers_request->selected_text = kWord;
intent_generator_->GenerateIntent(*quick_answers_request);
task_environment_.RunUntilIdle();
// Should not generate dictionary intent if the word contains digits even if
// it is in the dictionary.
EXPECT_EQ(IntentType::kUnknown, intent_info_.intent_type);
EXPECT_EQ(kWord, intent_info_.intent_text);
}
TEST_F(IntentGeneratorTest, ShouldNotTriggerForProperNounInDictionary) {
const std::string kWord = "Amy";
// No Annotation provided.
std::vector<TextAnnotationPtr> annotations;
UseFakeServiceConnection(annotations);
// Add word to the dictionary.
spell_checker()->AddWordToDictionary(kWord);
// Word selected.
std::unique_ptr<QuickAnswersRequest> quick_answers_request =
std::make_unique<QuickAnswersRequest>();
quick_answers_request->selected_text = kWord;
intent_generator_->GenerateIntent(*quick_answers_request);
task_environment_.RunUntilIdle();
// Should not generate dictionary intent if the word contains digits even if
// it is in the dictionary.
EXPECT_EQ(IntentType::kUnknown, intent_info_.intent_type);
EXPECT_EQ(kWord, intent_info_.intent_text);
}
TEST_F(IntentGeneratorTest,
ShouldFallbackToAnnotationsForWordNotInDictionaryNoAnnotation) {
const std::string kWord = "single";
// No Annotation provided, and not add the word to the dictionary.
std::vector<TextAnnotationPtr> annotations;
UseFakeServiceConnection(annotations);
// Word selected.
std::unique_ptr<QuickAnswersRequest> quick_answers_request =
std::make_unique<QuickAnswersRequest>();
quick_answers_request->selected_text = kWord;
intent_generator_->GenerateIntent(*quick_answers_request);
task_environment_.RunUntilIdle();
// Should not generate dictionary intent if the word is not in the dictionary
// and no annotation provided.
EXPECT_EQ(IntentType::kUnknown, intent_info_.intent_type);
EXPECT_EQ(kWord, intent_info_.intent_text);
}
TEST_F(
IntentGeneratorTest,
ShouldFallbackToAnnotationsForWordNotInDictionaryWithDictionaryAnnotation) {
const std::string kWord = "unfathomable";
// Annotation provided, and not add the word to the dictionary.
std::vector<TextEntityPtr> entities;
entities.emplace_back(TextEntity::New(
"dictionary", // Entity name.
1.0, // Confidence score.
TextEntityData::NewNumericValue(0.0))); // 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());
UseFakeServiceConnection(annotations);
// Word selected.
std::unique_ptr<QuickAnswersRequest> quick_answers_request =
std::make_unique<QuickAnswersRequest>();
quick_answers_request->selected_text = kWord;
intent_generator_->GenerateIntent(*quick_answers_request);
task_environment_.RunUntilIdle();
// Should generate dictionary intent for the word.
EXPECT_EQ(IntentType::kDictionary, intent_info_.intent_type);
EXPECT_EQ(kWord, intent_info_.intent_text);
}
TEST_F(
IntentGeneratorTest,
ShouldFallbackToAnnotationsForWordNotInDictionaryWithUnitConversionAnnotation) {
const std::string kText = "50kg";
// Annotation provided, and not add the text to the dictionary.
std::vector<TextEntityPtr> entities;
entities.emplace_back(TextEntity::New(
"unit", // Entity name.
1.0, // Confidence score.
TextEntityData::NewNumericValue(0.0))); // Data extracted.
auto dictionary_annotation = TextAnnotation::New(0, // Start offset.
4, // End offset.
std::move(entities));
std::vector<TextAnnotationPtr> annotations;
annotations.push_back(dictionary_annotation->Clone());
UseFakeServiceConnection(annotations);
// Text selected.
std::unique_ptr<QuickAnswersRequest> quick_answers_request =
std::make_unique<QuickAnswersRequest>();
quick_answers_request->selected_text = kText;
intent_generator_->GenerateIntent(*quick_answers_request);
task_environment_.RunUntilIdle();
// Should generate unit conversion intent.
EXPECT_EQ(IntentType::kUnit, intent_info_.intent_type);
EXPECT_EQ(kText, intent_info_.intent_text);
}
TEST_F(IntentGeneratorTest, ShouldNotTriggerForMultipleWords) {
// No Annotation provided.
std::vector<TextAnnotationPtr> annotations;
UseFakeServiceConnection(annotations);
// Multiple words selected.
std::unique_ptr<QuickAnswersRequest> quick_answers_request =
std::make_unique<QuickAnswersRequest>();
quick_answers_request->selected_text = "multiple words";
intent_generator_->GenerateIntent(*quick_answers_request);
task_environment_.RunUntilIdle();
// Should fallback to unknown intent.
EXPECT_EQ(IntentType::kUnknown, intent_info_.intent_type);
EXPECT_EQ("multiple words", intent_info_.intent_text);
}
} // namespace quick_answers
|