summaryrefslogtreecommitdiff
path: root/native/annotator/pod_ner/pod-ner-impl_test.cc
blob: c7d0bee8904df38c65241d8f887f068182a8c9be (plain)
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
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "annotator/pod_ner/pod-ner-impl.h"

#include <iostream>
#include <memory>
#include <thread>  // NOLINT(build/c++11)

#include "annotator/model_generated.h"
#include "annotator/types.h"
#include "utils/jvm-test-utils.h"
#include "utils/test-data-test-utils.h"
#include "utils/tokenizer-utils.h"
#include "utils/utf8/unicodetext.h"
#include "utils/utf8/unilib.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace libtextclassifier3 {
namespace {

using ::testing::IsEmpty;
using ::testing::Not;

using PodNerModel_::Label_::BoiseType;
using PodNerModel_::Label_::BoiseType_BEGIN;
using PodNerModel_::Label_::BoiseType_END;
using PodNerModel_::Label_::BoiseType_INTERMEDIATE;
using PodNerModel_::Label_::BoiseType_O;
using PodNerModel_::Label_::BoiseType_SINGLE;
using PodNerModel_::Label_::MentionType;
using PodNerModel_::Label_::MentionType_NAM;
using PodNerModel_::Label_::MentionType_NOM;
using PodNerModel_::Label_::MentionType_UNDEFINED;

constexpr int kMinNumberOfTokens = 1;
constexpr int kMinNumberOfWordpieces = 1;
constexpr float kDefaultPriorityScore = 0.5;

class PodNerTest : public testing::Test {
 protected:
  PodNerTest() {
    PodNerModelT model;

    model.min_number_of_tokens = kMinNumberOfTokens;
    model.min_number_of_wordpieces = kMinNumberOfWordpieces;
    model.priority_score = kDefaultPriorityScore;

    const std::string tflite_model_buffer =
        GetTestFileContent("annotator/pod_ner/test_data/tflite_model.tflite");
    model.tflite_model = std::vector<uint8_t>(tflite_model_buffer.begin(),
                                              tflite_model_buffer.end());
    const std::string word_piece_vocab_buffer =
        GetTestFileContent("annotator/pod_ner/test_data/vocab.txt");
    model.word_piece_vocab = std::vector<uint8_t>(
        word_piece_vocab_buffer.begin(), word_piece_vocab_buffer.end());

    flatbuffers::FlatBufferBuilder builder;
    builder.Finish(PodNerModel::Pack(builder, &model));

    model_buffer_ =
        std::string(reinterpret_cast<const char*>(builder.GetBufferPointer()),
                    builder.GetSize());
    model_ = static_cast<const PodNerModel*>(
        flatbuffers::GetRoot<PodNerModel>(model_buffer_.data()));

    model.append_final_period = true;
    flatbuffers::FlatBufferBuilder builder_append_final_period;
    builder_append_final_period.Finish(
        PodNerModel::Pack(builder_append_final_period, &model));

    model_buffer_append_final_period_ =
        std::string(reinterpret_cast<const char*>(
                        builder_append_final_period.GetBufferPointer()),
                    builder_append_final_period.GetSize());
    model_append_final_period_ =
        static_cast<const PodNerModel*>(flatbuffers::GetRoot<PodNerModel>(
            model_buffer_append_final_period_.data()));

    unilib_ = CreateUniLibForTesting();
  }

  std::string model_buffer_;
  const PodNerModel* model_;
  std::string model_buffer_append_final_period_;
  const PodNerModel* model_append_final_period_;
  std::unique_ptr<UniLib> unilib_;
};

TEST_F(PodNerTest, AnnotateSmokeTest) {
  std::unique_ptr<PodNerAnnotator> annotator =
      PodNerAnnotator::Create(model_, *unilib_);
  ASSERT_TRUE(annotator != nullptr);

  {
    std::vector<AnnotatedSpan> annotations;
    ASSERT_TRUE(annotator->Annotate(
        UTF8ToUnicodeText("Google New York , in New York"), &annotations));
    EXPECT_THAT(annotations, Not(IsEmpty()));
  }

  {
    std::vector<AnnotatedSpan> annotations;
    ASSERT_TRUE(annotator->Annotate(
        UTF8ToUnicodeText("Jamie I'm in the first picture and Cameron and Zach "
                          "are in the second "
                          "picture."),
        &annotations));
    EXPECT_THAT(annotations, Not(IsEmpty()));
  }
}

TEST_F(PodNerTest, AnnotateEmptyInput) {
  std::unique_ptr<PodNerAnnotator> annotator =
      PodNerAnnotator::Create(model_, *unilib_);
  ASSERT_TRUE(annotator != nullptr);

  {
    std::vector<AnnotatedSpan> annotations;
    ASSERT_TRUE(annotator->Annotate(UTF8ToUnicodeText(""), &annotations));
    EXPECT_THAT(annotations, IsEmpty());
  }
}

void FillCollections(
    const std::vector<std::string>& collection_names,
    const std::vector<float>& single_token_priority_scores,
    const std::vector<float>& multi_token_priority_scores,
    std::vector<std::unique_ptr<PodNerModel_::CollectionT>>* collections) {
  ASSERT_TRUE(collection_names.size() == single_token_priority_scores.size() &&
              collection_names.size() == multi_token_priority_scores.size());
  collections->clear();
  for (int i = 0; i < collection_names.size(); ++i) {
    collections->push_back(std::make_unique<PodNerModel_::CollectionT>());
    collections->back()->name = collection_names[i];
    collections->back()->single_token_priority_score =
        single_token_priority_scores[i];
    collections->back()->multi_token_priority_score =
        multi_token_priority_scores[i];
  }
}

void EmplaceToLabelVector(
    BoiseType boise_type, MentionType mention_type, int collection_id,
    std::vector<std::unique_ptr<PodNerModel_::LabelT>>* labels) {
  labels->push_back(std::make_unique<PodNerModel_::LabelT>());
  labels->back()->boise_type = boise_type;
  labels->back()->mention_type = mention_type;
  labels->back()->collection_id = collection_id;
}

void FillLabels(int num_collections,
                std::vector<std::unique_ptr<PodNerModel_::LabelT>>* labels) {
  labels->clear();
  for (auto boise_type :
       {BoiseType_BEGIN, BoiseType_END, BoiseType_INTERMEDIATE}) {
    for (auto mention_type : {MentionType_NAM, MentionType_NOM}) {
      for (int i = 0; i < num_collections - 1; ++i) {  // skip undefined
        EmplaceToLabelVector(boise_type, mention_type, i, labels);
      }
    }
  }
  EmplaceToLabelVector(BoiseType_O, MentionType_UNDEFINED, num_collections - 1,
                       labels);
  for (auto mention_type : {MentionType_NAM, MentionType_NOM}) {
    for (int i = 0; i < num_collections - 1; ++i) {  // skip undefined
      EmplaceToLabelVector(BoiseType_SINGLE, mention_type, i, labels);
    }
  }
}

TEST_F(PodNerTest, AnnotateDefaultCollections) {
  std::unique_ptr<PodNerAnnotator> annotator =
      PodNerAnnotator::Create(model_, *unilib_);
  ASSERT_TRUE(annotator != nullptr);

  std::string multi_word_location = "I live in New York";
  std::string single_word_location = "I live in Zurich";
  {
    std::vector<AnnotatedSpan> annotations;
    ASSERT_TRUE(annotator->Annotate(UTF8ToUnicodeText(multi_word_location),
                                    &annotations));
    EXPECT_THAT(annotations, Not(IsEmpty()));
    EXPECT_EQ(annotations[0].classification[0].collection, "location");
    EXPECT_EQ(annotations[0].classification[0].priority_score,
              kDefaultPriorityScore);

    annotations.clear();
    ASSERT_TRUE(annotator->Annotate(UTF8ToUnicodeText(single_word_location),
                                    &annotations));
    EXPECT_THAT(annotations, Not(IsEmpty()));
    EXPECT_EQ(annotations[0].classification[0].collection, "location");
    EXPECT_EQ(annotations[0].classification[0].priority_score,
              kDefaultPriorityScore);
  }
}

TEST_F(PodNerTest, AnnotateConfigurableCollections) {
  std::unique_ptr<PodNerModelT> unpacked_model(model_->UnPack());
  ASSERT_TRUE(unpacked_model != nullptr);

  float xxx_single_token_priority = 0.9;
  float xxx_multi_token_priority = 1.7;
  const std::vector<std::string> collection_names = {
      "art",          "consumer_good", "event",  "xxx",
      "organization", "ner_entity",    "person", "undefined"};
  FillCollections(collection_names,
                  /*single_token_priority_scores=*/
                  {0., 0., 0., xxx_single_token_priority, 0., 0., 0., 0.},
                  /*multi_token_priority_scores=*/
                  {0., 0., 0., xxx_multi_token_priority, 0., 0., 0., 0.},
                  &(unpacked_model->collections));
  FillLabels(collection_names.size(), &(unpacked_model->labels));
  flatbuffers::FlatBufferBuilder builder;
  builder.Finish(PodNerModel::Pack(builder, unpacked_model.get()));
  std::string model_buffer =
      std::string(reinterpret_cast<const char*>(builder.GetBufferPointer()),
                  builder.GetSize());
  std::unique_ptr<PodNerAnnotator> annotator = PodNerAnnotator::Create(
      static_cast<const PodNerModel*>(
          flatbuffers::GetRoot<PodNerModel>(model_buffer.data())),
      *unilib_);
  ASSERT_TRUE(annotator != nullptr);

  std::string multi_word_location = "I live in New York";
  std::string single_word_location = "I live in Zurich";
  {
    std::vector<AnnotatedSpan> annotations;
    ASSERT_TRUE(annotator->Annotate(UTF8ToUnicodeText(multi_word_location),
                                    &annotations));
    EXPECT_THAT(annotations, Not(IsEmpty()));
    EXPECT_EQ(annotations[0].classification[0].collection, "xxx");
    EXPECT_EQ(annotations[0].classification[0].priority_score,
              xxx_multi_token_priority);

    annotations.clear();
    ASSERT_TRUE(annotator->Annotate(UTF8ToUnicodeText(single_word_location),
                                    &annotations));
    EXPECT_THAT(annotations, Not(IsEmpty()));
    EXPECT_EQ(annotations[0].classification[0].collection, "xxx");
    EXPECT_EQ(annotations[0].classification[0].priority_score,
              xxx_single_token_priority);
  }
}

TEST_F(PodNerTest, AnnotateMinNumTokens) {
  std::unique_ptr<PodNerAnnotator> annotator =
      PodNerAnnotator::Create(model_, *unilib_);
  ASSERT_TRUE(annotator != nullptr);

  std::string text = "in New York";
  {
    std::vector<AnnotatedSpan> annotations;
    ASSERT_TRUE(annotator->Annotate(UTF8ToUnicodeText(text), &annotations));
    EXPECT_THAT(annotations, Not(IsEmpty()));
  }

  std::unique_ptr<PodNerModelT> unpacked_model(model_->UnPack());
  ASSERT_TRUE(unpacked_model != nullptr);

  unpacked_model->min_number_of_tokens = 4;
  flatbuffers::FlatBufferBuilder builder;
  builder.Finish(PodNerModel::Pack(builder, unpacked_model.get()));

  std::string model_buffer =
      std::string(reinterpret_cast<const char*>(builder.GetBufferPointer()),
                  builder.GetSize());
  annotator = PodNerAnnotator::Create(
      static_cast<const PodNerModel*>(
          flatbuffers::GetRoot<PodNerModel>(model_buffer.data())),
      *unilib_);
  ASSERT_TRUE(annotator != nullptr);
  {
    std::vector<AnnotatedSpan> annotations;
    ASSERT_TRUE(annotator->Annotate(UTF8ToUnicodeText(text), &annotations));
    EXPECT_THAT(annotations, IsEmpty());
  }
}

TEST_F(PodNerTest, AnnotateMinNumWordpieces) {
  std::unique_ptr<PodNerAnnotator> annotator =
      PodNerAnnotator::Create(model_, *unilib_);
  ASSERT_TRUE(annotator != nullptr);

  std::string text = "in New York";
  {
    std::vector<AnnotatedSpan> annotations;
    ASSERT_TRUE(annotator->Annotate(UTF8ToUnicodeText(text), &annotations));
    EXPECT_THAT(annotations, Not(IsEmpty()));
  }

  std::unique_ptr<PodNerModelT> unpacked_model(model_->UnPack());
  ASSERT_TRUE(unpacked_model != nullptr);

  unpacked_model->min_number_of_wordpieces = 10;
  flatbuffers::FlatBufferBuilder builder;
  builder.Finish(PodNerModel::Pack(builder, unpacked_model.get()));

  std::string model_buffer =
      std::string(reinterpret_cast<const char*>(builder.GetBufferPointer()),
                  builder.GetSize());
  annotator = PodNerAnnotator::Create(
      static_cast<const PodNerModel*>(
          flatbuffers::GetRoot<PodNerModel>(model_buffer.data())),
      *unilib_);
  ASSERT_TRUE(annotator != nullptr);
  {
    std::vector<AnnotatedSpan> annotations;
    ASSERT_TRUE(annotator->Annotate(UTF8ToUnicodeText(text), &annotations));
    EXPECT_THAT(annotations, IsEmpty());
  }
}

TEST_F(PodNerTest, AnnotateNonstandardText) {
  std::unique_ptr<PodNerAnnotator> annotator =
      PodNerAnnotator::Create(model_, *unilib_);
  ASSERT_TRUE(annotator != nullptr);

  const std::string nonstandard_text =
      "abcNxCDU1RWNvbXByLXI4NS8xNzcwLzE3NzA4NDY2L3J1Ymluby1raWRzLXJlY2xpbmVyLXd"
      "pdGgtY3VwLWhvbGRlci5qcGc=/"
      "UnViaW5vIEtpZHMgUmVjbGluZXIgd2l0aCBDdXAgSG9sZGVyIGJ5IEhhcnJpZXQgQmVl."
      "html>";
  std::vector<AnnotatedSpan> annotations;
  ASSERT_TRUE(
      annotator->Annotate(UTF8ToUnicodeText(nonstandard_text), &annotations));
  EXPECT_THAT(annotations, IsEmpty());
}

TEST_F(PodNerTest, AnnotateTextWithLinefeed) {
  std::unique_ptr<PodNerAnnotator> annotator =
      PodNerAnnotator::Create(model_, *unilib_);
  ASSERT_TRUE(annotator != nullptr);

  std::string nonstandard_text = "My name is Kuba\x09";
  nonstandard_text += "and this is a test.";
  std::vector<AnnotatedSpan> annotations;
  ASSERT_TRUE(
      annotator->Annotate(UTF8ToUnicodeText(nonstandard_text), &annotations));
  EXPECT_THAT(annotations, Not(IsEmpty()));
  EXPECT_EQ(annotations[0].span, CodepointSpan(11, 15));

  nonstandard_text = "My name is Kuba\x09 and this is a test.";
  ASSERT_TRUE(
      annotator->Annotate(UTF8ToUnicodeText(nonstandard_text), &annotations));
  EXPECT_THAT(annotations, Not(IsEmpty()));
  EXPECT_EQ(annotations[0].span, CodepointSpan(11, 15));
}

TEST_F(PodNerTest, AnnotateWithUnknownWordpieces) {
  std::unique_ptr<PodNerAnnotator> annotator =
      PodNerAnnotator::Create(model_, *unilib_);
  ASSERT_TRUE(annotator != nullptr);

  const std::string long_text =
      "It is easy to spend a fun and exciting day in Seattle without a car.  "
      "There are lots of ways to modify this itinerary. Add a ferry ride "
      "from the waterfront. Spending the day at the Seattle Center or at the "
      "aquarium could easily extend this from one to several days. Take the "
      "Underground Tour in Pioneer Square. Visit the Klondike Gold Rush "
      "Museum which is fun and free.  In the summer months you can ride the "
      "passenger-only Water Taxi from the waterfront to West Seattle and "
      "Alki Beach. Here's a sample one day itinerary: Start at the Space "
      "Needle by taking the Seattle Monorail from downtown. Look around the "
      "Seattle Center or go to the Space Needle.";
  const std::string text_with_unknown_wordpieces = "před chvílí";

  std::vector<AnnotatedSpan> annotations;
  ASSERT_TRUE(
      annotator->Annotate(UTF8ToUnicodeText("Google New York , in New York. " +
                                            text_with_unknown_wordpieces),
                          &annotations));
  EXPECT_THAT(annotations, IsEmpty());
  ASSERT_TRUE(annotator->Annotate(
      UTF8ToUnicodeText(long_text + " " + text_with_unknown_wordpieces),
      &annotations));
  EXPECT_THAT(annotations, Not(IsEmpty()));
}

class PodNerTestWithOrWithoutFinalPeriod
    : public PodNerTest,
      public testing::WithParamInterface<bool> {};

INSTANTIATE_TEST_SUITE_P(TestAnnotateLongText,
                         PodNerTestWithOrWithoutFinalPeriod,
                         testing::Values(true, false));

TEST_P(PodNerTestWithOrWithoutFinalPeriod, AnnotateLongText) {
  std::unique_ptr<PodNerAnnotator> annotator = PodNerAnnotator::Create(
      GetParam() ? model_append_final_period_ : model_, *unilib_);
  ASSERT_TRUE(annotator != nullptr);

  const std::string long_text =
      "It is easy to spend a fun and exciting day in Seattle without a car.  "
      "There are lots of ways to modify this itinerary. Add a ferry ride "
      "from the waterfront. Spending the day at the Seattle Center or at the "
      "aquarium could easily extend this from one to several days. Take the "
      "Underground Tour in Pioneer Square. Visit the Klondike Gold Rush "
      "Museum which is fun and free.  In the summer months you can ride the "
      "passenger-only Water Taxi from the waterfront to West Seattle and "
      "Alki Beach. Here's a sample one day itinerary: Start at the Space "
      "Needle by taking the Seattle Monorail from downtown. Look around the "
      "Seattle Center or go to the Space Needle. If you're interested in "
      "music the EMP-SFM (Experience Music Project - Science Fiction Musuem) "
      "is located at the foot of the Space Needle.  It has a lot of rock'n "
      "roll memorabilia that you may find interesting.  The Chihuly Garden "
      "and Glass musuem is near the Space Needle and you can get a "
      "combination ticket for both.  It gets really good reviews.  If you're "
      "interested, then the Bill & Melinda Gates Foundation is across from "
      "the EMP and has a visitors center that is free.  Come see how Bill "
      "Gates is giving away his millions. Take the Monorail back downtown.  "
      "You will be at 5th and Pine (Westlake Center). Head west to the Pike "
      "Place Market. Look around then head for the Pike Place hill climb "
      "which is a series of steps that walk down to the waterfront. You will "
      "end up across the street from the Seattle Aquarium. Plenty of things "
      "to do on the waterfront, boat cruises, seafood restaurants, the "
      "Aquarium, or your typical tourist activities. You can walk or take "
      "the waterfront trolley bus.  Note that waterfront construction has "
      "relocated the  trolley Metro bus route 99 that will take you from "
      "Pioneer Square all the way to the end of the waterfront where you can "
      "visit the Seattle Art Musuem's XXX Sculpture Garden just north of "
      "Pier 70. The route goes thru Chinatown/International District, "
      "through Pioneer Square, up 1st ave past the Pike Place Market and to "
      "1st and Cedar which is walking distance to the Space Needle.  It then "
      "goes down Broad Street toward the Olympic Sculpture Garden.   It runs "
      "approximately every 30 minutes during the day and early evening.";
  std::vector<AnnotatedSpan> annotations;
  ASSERT_TRUE(annotator->Annotate(UTF8ToUnicodeText(long_text), &annotations));
  EXPECT_THAT(annotations, Not(IsEmpty()));

  const std::string location_from_beginning = "Seattle";
  int start_span_location_from_beginning =
      long_text.find(location_from_beginning);
  EXPECT_EQ(annotations[0].span,
            CodepointSpan(start_span_location_from_beginning,
                          start_span_location_from_beginning +
                              location_from_beginning.length()));

  const std::string location_from_end = "Olympic Sculpture Garden";
  int start_span_location_from_end = long_text.find(location_from_end);
  const AnnotatedSpan& last_annotation = *annotations.rbegin();
  EXPECT_EQ(
      last_annotation.span,
      CodepointSpan(start_span_location_from_end,
                    start_span_location_from_end + location_from_end.length()));
}

TEST_F(PodNerTest, SuggestSelectionLongText) {
  std::unique_ptr<PodNerAnnotator> annotator =
      PodNerAnnotator::Create(model_, *unilib_);
  ASSERT_TRUE(annotator != nullptr);

  const std::string long_text =
      "It is easy to spend a fun and exciting day in Seattle without a car.  "
      "There are lots of ways to modify this itinerary. Add a ferry ride "
      "from the waterfront. Spending the day at the Seattle Center or at the "
      "aquarium could easily extend this from one to several days. Take the "
      "Underground Tour in Pioneer Square. Visit the Klondike Gold Rush "
      "Museum which is fun and free.  In the summer months you can ride the "
      "passenger-only Water Taxi from the waterfront to West Seattle and "
      "Alki Beach. Here's a sample one day itinerary: Start at the Space "
      "Needle by taking the Seattle Monorail from downtown. Look around the "
      "Seattle Center or go to the Space Needle. If you're interested in "
      "music the EMP-SFM (Experience Music Project - Science Fiction Musuem) "
      "is located at the foot of the Space Needle.  It has a lot of rock'n "
      "roll memorabilia that you may find interesting.  The Chihuly Garden "
      "and Glass musuem is near the Space Needle and you can get a "
      "combination ticket for both.  It gets really good reviews.  If you're "
      "interested, then the Bill & Melinda Gates Foundation is across from "
      "the EMP and has a visitors center that is free.  Come see how Bill "
      "Gates is giving away his millions. Take the Monorail back downtown.  "
      "You will be at 5th and Pine (Westlake Center). Head west to the Pike "
      "Place Market. Look around then head for the Pike Place hill climb "
      "which is a series of steps that walk down to the waterfront. You will "
      "end up across the street from the Seattle Aquarium. Plenty of things "
      "to do on the waterfront, boat cruises, seafood restaurants, the "
      "Aquarium, or your typical tourist activities. You can walk or take "
      "the waterfront trolley bus.  Note that waterfront construction has "
      "relocated the  trolley Metro bus route 99 that will take you from "
      "Pioneer Square all the way to the end of the waterfront where you can "
      "visit the Seattle Art Musuem's XXX Sculpture Garden just north of "
      "Pier 70. The route goes thru Chinatown/International District, "
      "through Pioneer Square, up 1st ave past the Pike Place Market and to "
      "1st and Cedar which is walking distance to the Space Needle.  It then "
      "goes down Broad Street toward the Olympic Sculpture Garden.   It runs "
      "approximately every 30 minutes during the day and early evening.";
  const std::string klondike = "Klondike Gold Rush Museum";
  int klondike_start = long_text.find(klondike);

  AnnotatedSpan suggested_span;
  EXPECT_TRUE(annotator->SuggestSelection(UTF8ToUnicodeText(long_text),
                                          {klondike_start, klondike_start + 8},
                                          &suggested_span));
  EXPECT_EQ(suggested_span.span,
            CodepointSpan(klondike_start, klondike_start + klondike.length()));
}

TEST_F(PodNerTest, SuggestSelectionTest) {
  std::unique_ptr<PodNerAnnotator> annotator =
      PodNerAnnotator::Create(model_, *unilib_);
  ASSERT_TRUE(annotator != nullptr);

  AnnotatedSpan suggested_span;
  EXPECT_TRUE(annotator->SuggestSelection(
      UTF8ToUnicodeText("Google New York, in New York"), {7, 10},
      &suggested_span));
  EXPECT_EQ(suggested_span.span, CodepointSpan(7, 15));
  EXPECT_FALSE(annotator->SuggestSelection(
      UTF8ToUnicodeText("Google New York, in New York"), {17, 19},
      &suggested_span));
  EXPECT_EQ(suggested_span.span, CodepointSpan(kInvalidIndex, kInvalidIndex));
}

TEST_F(PodNerTest, ClassifyTextTest) {
  std::unique_ptr<PodNerAnnotator> annotator =
      PodNerAnnotator::Create(model_, *unilib_);
  ASSERT_TRUE(annotator != nullptr);

  ClassificationResult result;
  ASSERT_TRUE(annotator->ClassifyText(UTF8ToUnicodeText("We met in New York"),
                                      {10, 18}, &result));
  EXPECT_EQ(result.collection, "location");
}

TEST_F(PodNerTest, ThreadSafety) {
  std::unique_ptr<PodNerAnnotator> annotator =
      PodNerAnnotator::Create(model_, *unilib_);
  ASSERT_TRUE(annotator != nullptr);

  // Do inference in 20 threads. When run with --config=tsan, this should fire
  // if there's a problem.
  std::vector<std::thread> thread_pool(20);
  for (std::thread& thread : thread_pool) {
    thread = std::thread([&annotator]() {
      AnnotatedSpan suggested_span;
      EXPECT_TRUE(annotator->SuggestSelection(
          UTF8ToUnicodeText("Google New York, in New York"), {7, 10},
          &suggested_span));
      EXPECT_EQ(suggested_span.span, CodepointSpan(7, 15));
    });
  }
  for (std::thread& thread : thread_pool) {
    thread.join();
  }
}

}  // namespace
}  // namespace libtextclassifier3