forked from google/mozc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber_rewriter_test.cc
1253 lines (1077 loc) · 47.3 KB
/
number_rewriter_test.cc
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
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2010-2021, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "rewriter/number_rewriter.h"
#include <cstddef>
#include <iterator>
#include <memory>
#include <string>
#include "absl/log/check.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "base/number_util.h"
#include "base/strings/assign.h"
#include "config/character_form_manager.h"
#include "config/config_handler.h"
#include "converter/segments.h"
#include "converter/segments_matchers.h"
#include "data_manager/testing/mock_data_manager.h"
#include "dictionary/pos_matcher.h"
#include "protocol/commands.pb.h"
#include "request/conversion_request.h"
#include "request/request_test_util.h"
#include "rewriter/rewriter_interface.h"
#include "testing/gmock.h"
#include "testing/gunit.h"
#include "testing/mozctest.h"
namespace mozc {
namespace {
using ::mozc::dictionary::PosMatcher;
using ::testing::Field;
using ::testing::Matcher;
using ::testing::WithParamInterface;
constexpr absl::string_view kKanjiDescription = "漢数字";
constexpr absl::string_view kArabicDescription = "数字";
constexpr absl::string_view kOldKanjiDescription = "大字";
constexpr absl::string_view kMaruNumberDescription = "丸数字";
constexpr absl::string_view kRomanCapitalDescription = "ローマ数字(大文字)";
constexpr absl::string_view kRomanNoCapitalDescription = "ローマ数字(小文字)";
constexpr absl::string_view kSuperscriptDescription = "上付き文字";
constexpr absl::string_view kSubscriptDescription = "下付き文字";
Matcher<const Segment::Candidate *> ValueIs(absl::string_view value) {
return Field(&Segment::Candidate::value, value);
}
bool FindValue(const Segment &segment, const absl::string_view value) {
for (size_t i = 0; i < segment.candidates_size(); ++i) {
if (segment.candidate(i).value == value) {
return true;
}
}
return false;
}
Segment *SetupSegments(const PosMatcher &pos_matcher,
const absl::string_view candidate_value,
Segments *segments) {
segments->Clear();
Segment *segment = segments->push_back_segment();
Segment::Candidate *candidate = segment->add_candidate();
candidate->lid = pos_matcher.GetNumberId();
candidate->rid = pos_matcher.GetNumberId();
strings::Assign(candidate->value, candidate_value);
strings::Assign(candidate->content_value, candidate_value);
return segment;
}
bool HasDescription(const Segment &segment,
const absl::string_view description) {
for (size_t i = 0; i < segment.candidates_size(); ++i) {
if (segment.candidate(i).description == description) {
return true;
}
}
return false;
}
// Find candidate id
bool FindCandidateId(const Segment &segment, const absl::string_view value,
int *id) {
for (size_t i = 0; i < segment.candidates_size(); ++i) {
if (segment.candidate(i).value == value) {
*id = i;
return true;
}
}
return false;
}
} // namespace
class NumberRewriterTest : public testing::TestWithTempUserProfile {
protected:
void SetUp() override {
config::CharacterFormManager::GetCharacterFormManager()->ClearHistory();
pos_matcher_.Set(mock_data_manager_.GetPosMatcherData());
}
void TearDown() override {
config::CharacterFormManager::GetCharacterFormManager()->ClearHistory();
}
std::unique_ptr<NumberRewriter> CreateNumberRewriter() {
return std::make_unique<NumberRewriter>(&mock_data_manager_);
}
const testing::MockDataManager mock_data_manager_;
PosMatcher pos_matcher_;
const ConversionRequest default_request_;
};
namespace {
struct ExpectResult {
absl::string_view value;
absl::string_view content_value;
absl::string_view description;
};
} // namespace
TEST_F(NumberRewriterTest, BasicTest) {
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
Segments segments;
Segment *seg = segments.push_back_segment();
Segment::Candidate *candidate = seg->add_candidate();
candidate->lid = pos_matcher_.GetNumberId();
candidate->rid = pos_matcher_.GetNumberId();
candidate->value = "012";
candidate->content_value = "012";
candidate->key = "012";
candidate->content_key = "012";
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
constexpr ExpectResult kExpectResults[] = {
{"012", "012", ""},
{"〇一二", "〇一二", kKanjiDescription},
{"012", "012", kArabicDescription},
{"十二", "十二", kKanjiDescription},
{"壱拾弐", "壱拾弐", kOldKanjiDescription},
{"Ⅻ", "Ⅻ", kRomanCapitalDescription},
{"ⅻ", "ⅻ", kRomanNoCapitalDescription},
{"⑫", "⑫", kMaruNumberDescription},
{"0xc", "0xc", "16進数"},
{"014", "014", "8進数"},
{"0b1100", "0b1100", "2進数"},
};
constexpr size_t kExpectResultSize = std::size(kExpectResults);
EXPECT_EQ(seg->candidates_size(), kExpectResultSize);
for (size_t i = 0; i < kExpectResultSize; ++i) {
SCOPED_TRACE(absl::StrFormat("i = %d", i));
EXPECT_EQ(seg->candidate(i).value, kExpectResults[i].value);
EXPECT_EQ(seg->candidate(i).content_value, kExpectResults[i].content_value);
EXPECT_EQ(seg->candidate(i).description, kExpectResults[i].description);
}
seg->clear_candidates();
}
TEST_F(NumberRewriterTest, RequestType) {
class TestData {
public:
ConversionRequest::RequestType request_type_;
int expected_candidate_number_;
TestData(ConversionRequest::RequestType request_type, int expected_number)
: request_type_(request_type),
expected_candidate_number_(expected_number) {}
};
TestData test_data_list[] = {
TestData(ConversionRequest::CONVERSION, 11), // 11 comes from BasicTest
TestData(ConversionRequest::REVERSE_CONVERSION, 8),
TestData(ConversionRequest::PREDICTION, 8),
TestData(ConversionRequest::SUGGESTION, 8),
};
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
for (size_t i = 0; i < std::size(test_data_list); ++i) {
TestData &test_data = test_data_list[i];
Segments segments;
Segment *seg = segments.push_back_segment();
Segment::Candidate *candidate = seg->add_candidate();
candidate->lid = pos_matcher_.GetNumberId();
candidate->rid = pos_matcher_.GetNumberId();
candidate->value = "012";
candidate->content_value = "012";
ConversionRequest request;
request.set_request_type(test_data.request_type_);
EXPECT_TRUE(number_rewriter->Rewrite(request, &segments));
EXPECT_EQ(seg->candidates_size(), test_data.expected_candidate_number_);
}
}
TEST_F(NumberRewriterTest, BasicTestWithSuffix) {
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
Segments segments;
Segment *seg = segments.push_back_segment();
Segment::Candidate *candidate = seg->add_candidate();
candidate->lid = pos_matcher_.GetNumberId();
candidate->rid = pos_matcher_.GetNumberId();
candidate->value = "012が";
candidate->content_value = "012";
candidate->key = "012が";
candidate->content_key = "012";
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
constexpr ExpectResult kExpectResults[] = {
{"012が", "012", ""},
{"〇一二が", "〇一二", kKanjiDescription},
{"012が", "012", kArabicDescription},
{"十二が", "十二", kKanjiDescription},
{"壱拾弐が", "壱拾弐", kOldKanjiDescription},
{"Ⅻが", "Ⅻ", kRomanCapitalDescription},
{"ⅻが", "ⅻ", kRomanNoCapitalDescription},
{"⑫が", "⑫", kMaruNumberDescription},
{"0xcが", "0xc", "16進数"},
{"014が", "014", "8進数"},
{"0b1100が", "0b1100", "2進数"},
};
constexpr size_t kExpectResultSize = std::size(kExpectResults);
EXPECT_EQ(seg->candidates_size(), kExpectResultSize);
for (size_t i = 0; i < kExpectResultSize; ++i) {
SCOPED_TRACE(absl::StrFormat("i = %d", i));
EXPECT_EQ(seg->candidate(i).value, kExpectResults[i].value);
EXPECT_EQ(seg->candidate(i).content_value, kExpectResults[i].content_value);
EXPECT_EQ(seg->candidate(i).description, kExpectResults[i].description);
}
seg->clear_candidates();
}
TEST_F(NumberRewriterTest, BasicTestWithNumberSuffix) {
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
Segments segments;
Segment *seg = segments.push_back_segment();
Segment::Candidate *candidate = seg->add_candidate();
candidate->lid = pos_matcher_.GetNumberId();
candidate->rid = pos_matcher_.GetCounterSuffixWordId();
candidate->value = "十五個";
candidate->content_value = "十五個";
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
EXPECT_EQ(seg->candidates_size(), 2);
EXPECT_EQ(seg->candidate(0).value, "十五個");
EXPECT_EQ(seg->candidate(0).content_value, "十五個");
EXPECT_EQ(seg->candidate(0).description, "");
EXPECT_EQ(seg->candidate(1).value, "15個");
EXPECT_EQ(seg->candidate(1).content_value, "15個");
EXPECT_EQ(seg->candidate(1).description, "");
seg->clear_candidates();
}
TEST_F(NumberRewriterTest, TestWithMultipleNumberSuffix) {
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
Segments segments;
Segment *seg = segments.push_back_segment();
Segment::Candidate *candidate = seg->add_candidate();
candidate->lid = pos_matcher_.GetNumberId();
candidate->rid = pos_matcher_.GetCounterSuffixWordId();
candidate->value = "十五回";
candidate->content_value = "十五回";
candidate = seg->add_candidate();
candidate->lid = pos_matcher_.GetNumberId();
candidate->rid = pos_matcher_.GetCounterSuffixWordId();
candidate->value = "十五階";
candidate->content_value = "十五階";
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
EXPECT_EQ(seg->candidates_size(), 4);
EXPECT_EQ(seg->candidate(0).value, "十五回");
EXPECT_EQ(seg->candidate(0).content_value, "十五回");
EXPECT_EQ(seg->candidate(0).description, "");
EXPECT_EQ(seg->candidate(1).value, "15回");
EXPECT_EQ(seg->candidate(1).content_value, "15回");
EXPECT_EQ(seg->candidate(1).description, "");
EXPECT_EQ(seg->candidate(2).value, "十五階");
EXPECT_EQ(seg->candidate(2).content_value, "十五階");
EXPECT_EQ(seg->candidate(2).description, "");
EXPECT_EQ(seg->candidate(3).value, "15階");
EXPECT_EQ(seg->candidate(3).content_value, "15階");
EXPECT_EQ(seg->candidate(3).description, "");
seg->clear_candidates();
}
TEST_F(NumberRewriterTest, SpecialFormBoundaries) {
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
Segments segments;
// These special forms doesn't have zeros.
Segment *seg = SetupSegments(pos_matcher_, "0", &segments);
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
EXPECT_FALSE(HasDescription(*seg, kMaruNumberDescription));
EXPECT_FALSE(HasDescription(*seg, kRomanCapitalDescription));
EXPECT_FALSE(HasDescription(*seg, kRomanNoCapitalDescription));
// "0" has superscripts and subscripts
EXPECT_TRUE(HasDescription(*seg, kSuperscriptDescription));
EXPECT_TRUE(HasDescription(*seg, kSubscriptDescription));
// "1" has special forms.
seg = SetupSegments(pos_matcher_, "1", &segments);
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
EXPECT_TRUE(HasDescription(*seg, kMaruNumberDescription));
EXPECT_TRUE(HasDescription(*seg, kRomanCapitalDescription));
EXPECT_TRUE(HasDescription(*seg, kRomanNoCapitalDescription));
EXPECT_TRUE(HasDescription(*seg, kSuperscriptDescription));
EXPECT_TRUE(HasDescription(*seg, kSubscriptDescription));
// "12" has every special forms except superscript and subscript.
seg = SetupSegments(pos_matcher_, "12", &segments);
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
EXPECT_TRUE(HasDescription(*seg, kMaruNumberDescription));
EXPECT_TRUE(HasDescription(*seg, kRomanCapitalDescription));
EXPECT_TRUE(HasDescription(*seg, kRomanNoCapitalDescription));
EXPECT_FALSE(HasDescription(*seg, kSuperscriptDescription));
EXPECT_FALSE(HasDescription(*seg, kSubscriptDescription));
// "13" doesn't have roman forms.
seg = SetupSegments(pos_matcher_, "13", &segments);
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
EXPECT_TRUE(HasDescription(*seg, kMaruNumberDescription));
EXPECT_FALSE(HasDescription(*seg, kRomanCapitalDescription));
EXPECT_FALSE(HasDescription(*seg, kRomanNoCapitalDescription));
// "50" has circled numerics.
seg = SetupSegments(pos_matcher_, "50", &segments);
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
EXPECT_TRUE(HasDescription(*seg, kMaruNumberDescription));
EXPECT_FALSE(HasDescription(*seg, kRomanCapitalDescription));
EXPECT_FALSE(HasDescription(*seg, kRomanNoCapitalDescription));
// "51" doesn't have special forms.
seg = SetupSegments(pos_matcher_, "51", &segments);
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
EXPECT_FALSE(HasDescription(*seg, kMaruNumberDescription));
EXPECT_FALSE(HasDescription(*seg, kRomanCapitalDescription));
EXPECT_FALSE(HasDescription(*seg, kRomanNoCapitalDescription));
EXPECT_FALSE(HasDescription(*seg, kSuperscriptDescription));
EXPECT_FALSE(HasDescription(*seg, kSubscriptDescription));
}
TEST_F(NumberRewriterTest, OneOfCandidatesIsEmpty) {
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
Segments segments;
Segment *seg = segments.push_back_segment();
Segment::Candidate *first_candidate = seg->add_candidate();
// this candidate should be skipped
first_candidate->value = "";
first_candidate->content_value = first_candidate->value;
Segment::Candidate *second_candidate = seg->add_candidate();
second_candidate->value = "0";
second_candidate->lid = pos_matcher_.GetNumberId();
second_candidate->rid = pos_matcher_.GetNumberId();
second_candidate->content_value = second_candidate->value;
second_candidate->key = "0";
second_candidate->content_key = "0";
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
EXPECT_EQ(seg->candidate(0).value, "");
EXPECT_EQ(seg->candidate(0).content_value, "");
EXPECT_EQ(seg->candidate(0).description, "");
EXPECT_EQ(seg->candidate(1).value, "0");
EXPECT_EQ(seg->candidate(1).content_value, "0");
EXPECT_EQ(seg->candidate(1).description, "");
EXPECT_EQ(seg->candidate(2).value, "〇");
EXPECT_EQ(seg->candidate(2).content_value, "〇");
EXPECT_EQ(seg->candidate(2).description, kKanjiDescription);
EXPECT_EQ(seg->candidate(3).value, "0");
EXPECT_EQ(seg->candidate(3).content_value, "0");
EXPECT_EQ(seg->candidate(3).description, kArabicDescription);
EXPECT_EQ(seg->candidate(4).value, "零");
EXPECT_EQ(seg->candidate(4).content_value, "零");
EXPECT_EQ(seg->candidate(4).description, kOldKanjiDescription);
seg->clear_candidates();
}
TEST_F(NumberRewriterTest, RewriteDoesNotHappen) {
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
Segments segments;
Segment *seg = segments.push_back_segment();
Segment::Candidate *candidate = seg->add_candidate();
candidate->value = "タンポポ";
candidate->content_value = candidate->value;
// Number rewrite should not occur
EXPECT_FALSE(number_rewriter->Rewrite(default_request_, &segments));
// Number of candidates should be maintained
EXPECT_EQ(seg->candidates_size(), 1);
seg->clear_candidates();
}
TEST_F(NumberRewriterTest, NumberIsZero) {
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
Segments segments;
Segment *seg = segments.push_back_segment();
Segment::Candidate *candidate = seg->add_candidate();
candidate->lid = pos_matcher_.GetNumberId();
candidate->rid = pos_matcher_.GetNumberId();
candidate->value = "0";
candidate->content_value = "0";
candidate->key = "0";
candidate->content_key = "0";
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
EXPECT_EQ(seg->candidates_size(), 6);
EXPECT_EQ(seg->candidate(0).value, "0");
EXPECT_EQ(seg->candidate(0).content_value, "0");
EXPECT_EQ(seg->candidate(0).description, "");
EXPECT_EQ(seg->candidate(1).value, "〇");
EXPECT_EQ(seg->candidate(1).content_value, "〇");
EXPECT_EQ(seg->candidate(1).description, kKanjiDescription);
EXPECT_EQ(seg->candidate(2).value, "0");
EXPECT_EQ(seg->candidate(2).content_value, "0");
EXPECT_EQ(seg->candidate(2).description, kArabicDescription);
EXPECT_EQ(seg->candidate(3).value, "零");
EXPECT_EQ(seg->candidate(3).content_value, "零");
EXPECT_EQ(seg->candidate(3).description, kOldKanjiDescription);
EXPECT_EQ(seg->candidate(4).value, "⁰");
EXPECT_EQ(seg->candidate(4).content_value, "⁰");
EXPECT_EQ(seg->candidate(4).description, kSuperscriptDescription);
EXPECT_EQ(seg->candidate(5).value, "₀");
EXPECT_EQ(seg->candidate(5).content_value, "₀");
EXPECT_EQ(seg->candidate(5).description, kSubscriptDescription);
seg->clear_candidates();
}
TEST_F(NumberRewriterTest, NumberIsZeroZero) {
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
Segments segments;
Segment *seg = segments.push_back_segment();
Segment::Candidate *candidate = seg->add_candidate();
candidate->lid = pos_matcher_.GetNumberId();
candidate->rid = pos_matcher_.GetNumberId();
candidate->value = "00";
candidate->content_value = "00";
candidate->key = "00";
candidate->content_key = "00";
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
EXPECT_EQ(seg->candidates_size(), 6);
EXPECT_EQ(seg->candidate(0).value, "00");
EXPECT_EQ(seg->candidate(0).content_value, "00");
EXPECT_EQ(seg->candidate(0).description, "");
EXPECT_EQ(seg->candidate(1).value, "〇〇");
EXPECT_EQ(seg->candidate(1).content_value, "〇〇");
EXPECT_EQ(seg->candidate(1).description, kKanjiDescription);
EXPECT_EQ(seg->candidate(2).value, "00");
EXPECT_EQ(seg->candidate(2).content_value, "00");
EXPECT_EQ(seg->candidate(2).description, kArabicDescription);
EXPECT_EQ(seg->candidate(3).value, "零");
EXPECT_EQ(seg->candidate(3).content_value, "零");
EXPECT_EQ(seg->candidate(3).description, kOldKanjiDescription);
EXPECT_EQ(seg->candidate(4).value, "⁰");
EXPECT_EQ(seg->candidate(4).content_value, "⁰");
EXPECT_EQ(seg->candidate(4).description, kSuperscriptDescription);
EXPECT_EQ(seg->candidate(5).value, "₀");
EXPECT_EQ(seg->candidate(5).content_value, "₀");
EXPECT_EQ(seg->candidate(5).description, kSubscriptDescription);
seg->clear_candidates();
}
TEST_F(NumberRewriterTest, NumberIs19Digit) {
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
Segments segments;
Segment *seg = segments.push_back_segment();
Segment::Candidate *candidate = seg->add_candidate();
candidate->lid = pos_matcher_.GetNumberId();
candidate->rid = pos_matcher_.GetNumberId();
candidate->value = "1000000000000000000";
candidate->content_value = "1000000000000000000";
candidate->key = "1000000000000000000";
candidate->content_key = "1000000000000000000";
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
constexpr ExpectResult kExpectResults[] = {
{"1000000000000000000", "1000000000000000000", ""},
{"一〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇",
"一〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇", kKanjiDescription},
{"1000000000000000000",
"1000000000000000000", kArabicDescription},
{"1,000,000,000,000,000,000", "1,000,000,000,000,000,000",
kArabicDescription},
{"1,000,000,000,000,000,000",
"1,000,000,000,000,000,000",
kArabicDescription},
{"100京", "100京", kArabicDescription},
{"100京", "100京", kArabicDescription},
{"百京", "百京", kKanjiDescription},
{"壱百京", "壱百京", kOldKanjiDescription},
{"0xde0b6b3a7640000", "0xde0b6b3a7640000", "16進数"},
{"067405553164731000000", "067405553164731000000", "8進数"},
{"0b110111100000101101101011001110100111011001000000000000000000",
"0b110111100000101101101011001110100111011001000000000000000000",
"2進数"},
};
constexpr size_t kExpectResultSize = std::size(kExpectResults);
EXPECT_EQ(seg->candidates_size(), kExpectResultSize);
for (size_t i = 0; i < kExpectResultSize; ++i) {
SCOPED_TRACE(absl::StrFormat("i = %d", i));
EXPECT_EQ(seg->candidate(i).value, kExpectResults[i].value);
EXPECT_EQ(seg->candidate(i).content_value, kExpectResults[i].content_value);
EXPECT_EQ(seg->candidate(i).description, kExpectResults[i].description);
}
seg->clear_candidates();
}
TEST_F(NumberRewriterTest, NumberIsGreaterThanUInt64Max) {
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
Segments segments;
Segment *seg = segments.push_back_segment();
Segment::Candidate *candidate = seg->add_candidate();
candidate->lid = pos_matcher_.GetNumberId();
candidate->rid = pos_matcher_.GetNumberId();
candidate->value = "18446744073709551616"; // 2^64
candidate->content_value = "18446744073709551616";
candidate->key = "18446744073709551616";
candidate->content_key = "18446744073709551616";
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
constexpr ExpectResult kExpectResults[] = {
{"18446744073709551616", "18446744073709551616", ""},
{"一八四四六七四四〇七三七〇九五五一六一六",
"一八四四六七四四〇七三七〇九五五一六一六", kKanjiDescription},
{"18446744073709551616",
"18446744073709551616", kArabicDescription},
{"18,446,744,073,709,551,616", "18,446,744,073,709,551,616",
kArabicDescription},
{"18,446,744,073,709,551,616",
"18,446,744,073,709,551,616",
kArabicDescription},
{"1844京6744兆737億955万1616", "1844京6744兆737億955万1616",
kArabicDescription},
{"1844京6744兆737億955万1616",
"1844京6744兆737億955万1616", kArabicDescription},
{"千八百四十四京六千七百四十四兆七百三十七億九百五十五万千六百十六",
"千八百四十四京六千七百四十四兆七百三十七億九百五十五万千六百十六",
kKanjiDescription},
{"壱阡八百四拾四京六阡七百四拾四兆七百参拾七億九百五拾五萬壱阡六百壱拾六",
"壱阡八百四拾四京六阡七百四拾四兆七百参拾七億九百五拾五萬壱阡六百壱拾六",
kOldKanjiDescription},
};
constexpr size_t kExpectResultSize = std::size(kExpectResults);
EXPECT_EQ(seg->candidates_size(), kExpectResultSize);
for (size_t i = 0; i < kExpectResultSize; ++i) {
SCOPED_TRACE(absl::StrFormat("i = %d", i));
EXPECT_EQ(seg->candidate(i).value, kExpectResults[i].value);
EXPECT_EQ(seg->candidate(i).content_value, kExpectResults[i].content_value);
EXPECT_EQ(seg->candidate(i).description, kExpectResults[i].description);
}
seg->clear_candidates();
}
TEST_F(NumberRewriterTest, NumberIsGoogol) {
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
Segments segments;
Segment *seg = segments.push_back_segment();
Segment::Candidate *candidate = seg->add_candidate();
candidate->lid = pos_matcher_.GetNumberId();
candidate->rid = pos_matcher_.GetNumberId();
// 10^100 as "100000 ... 0"
std::string input = "1";
for (size_t i = 0; i < 100; ++i) {
input += "0";
}
candidate->key = input;
candidate->content_key = input;
candidate->value = input;
candidate->content_value = input;
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
EXPECT_EQ(seg->candidates_size(), 6);
EXPECT_EQ(seg->candidate(0).value, input);
EXPECT_EQ(seg->candidate(0).content_value, input);
EXPECT_EQ(seg->candidate(0).description, "");
// 10^100 as "一〇〇〇〇〇 ... 〇"
std::string expected2 = "一";
for (size_t i = 0; i < 100; ++i) {
expected2 += "〇";
}
EXPECT_EQ(seg->candidate(1).value, expected2);
EXPECT_EQ(seg->candidate(1).content_value, expected2);
EXPECT_EQ(seg->candidate(1).description, kKanjiDescription);
// 10^100 as "100000 ... 0"
std::string expected3 = "1";
for (size_t i = 0; i < 100; ++i) {
expected3 += "0";
}
EXPECT_EQ(seg->candidate(2).value, expected3);
EXPECT_EQ(seg->candidate(2).content_value, expected3);
EXPECT_EQ(seg->candidate(2).description, kArabicDescription);
// 10,000, ... ,000
std::string expected1 = "10";
for (size_t i = 0; i < 100 / 3; ++i) {
expected1 += ",000";
}
EXPECT_EQ(seg->candidate(3).value, expected1);
EXPECT_EQ(seg->candidate(3).content_value, expected1);
EXPECT_EQ(seg->candidate(3).description, kArabicDescription);
// "10,000, ... ,000"
std::string expected4 = "10"; // "10"
for (size_t i = 0; i < 100 / 3; ++i) {
expected4 += ",000";
}
EXPECT_EQ(seg->candidate(4).value, expected4);
EXPECT_EQ(seg->candidate(4).content_value, expected4);
EXPECT_EQ(seg->candidate(4).description, kArabicDescription);
EXPECT_EQ(seg->candidate(5).value, "Googol");
EXPECT_EQ(seg->candidate(5).content_value, "Googol");
EXPECT_EQ(seg->candidate(5).description, "");
seg->clear_candidates();
}
TEST_F(NumberRewriterTest, RankingForKanjiCandidate) {
// If kanji candidate is higher before we rewrite segments,
// kanji should have higher raking.
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
Segments segments;
{
Segment *segment = segments.add_segment();
DCHECK(segment);
segment->set_key("さんびゃく");
Segment::Candidate *candidate = segment->add_candidate();
candidate = segment->add_candidate();
candidate->lid = pos_matcher_.GetNumberId();
candidate->rid = pos_matcher_.GetNumberId();
candidate->key = "さんびゃく";
candidate->value = "三百";
candidate->content_value = "三百";
}
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
EXPECT_NE(segments.segments_size(), 0);
int kanji_pos = 0, arabic_pos = 0;
EXPECT_TRUE(FindCandidateId(segments.segment(0), "三百", &kanji_pos));
EXPECT_TRUE(FindCandidateId(segments.segment(0), "300", &arabic_pos));
EXPECT_LT(kanji_pos, arabic_pos);
}
TEST_F(NumberRewriterTest, ModifyExsistingRanking) {
// Modify existing ranking even if the converter returns unusual results
// due to dictionary noise, etc.
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
Segments segments;
{
Segment *segment = segments.add_segment();
DCHECK(segment);
segment->set_key("さんびゃく");
Segment::Candidate *candidate = segment->add_candidate();
candidate->lid = pos_matcher_.GetNumberId();
candidate->rid = pos_matcher_.GetNumberId();
candidate->key = "さんびゃく";
candidate->value = "参百";
candidate->content_value = "参百";
candidate = segment->add_candidate();
candidate->lid = pos_matcher_.GetNumberId();
candidate->rid = pos_matcher_.GetNumberId();
candidate->key = "さんびゃく";
candidate->value = "三百";
candidate->content_value = "三百";
}
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
int kanji_pos = 0, old_kanji_pos = 0;
EXPECT_NE(segments.segments_size(), 0);
EXPECT_TRUE(FindCandidateId(segments.segment(0), "三百", &kanji_pos));
EXPECT_TRUE(FindCandidateId(segments.segment(0), "参百", &old_kanji_pos));
EXPECT_LT(kanji_pos, old_kanji_pos);
}
TEST_F(NumberRewriterTest, EraseExistingCandidates) {
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
Segments segments;
{
Segment *segment = segments.add_segment();
DCHECK(segment);
segment->set_key("いち");
Segment::Candidate *candidate = segment->add_candidate();
candidate->lid = pos_matcher_.GetUnknownId(); // Not number POS
candidate->rid = pos_matcher_.GetUnknownId();
candidate->key = "いち";
candidate->content_key = "いち";
candidate->value = "壱";
candidate->content_value = "壱";
candidate = segment->add_candidate();
candidate->lid = pos_matcher_.GetNumberId(); // Number POS
candidate->rid = pos_matcher_.GetNumberId();
candidate->key = "いち";
candidate->content_key = "いち";
candidate->value = "一";
candidate->content_value = "一";
}
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
// "一" becomes the base candidate, instead of "壱"
int base_pos = 0;
EXPECT_TRUE(FindCandidateId(segments.segment(0), "一", &base_pos));
EXPECT_EQ(base_pos, 0);
// Daiji will be inserted with new correct POS ids.
int daiji_pos = 0;
EXPECT_TRUE(FindCandidateId(segments.segment(0), "壱", &daiji_pos));
EXPECT_GT(daiji_pos, 0);
EXPECT_EQ(pos_matcher_.GetNumberId(),
segments.segment(0).candidate(daiji_pos).lid);
EXPECT_EQ(pos_matcher_.GetNumberId(),
segments.segment(0).candidate(daiji_pos).rid);
}
TEST_F(NumberRewriterTest, SeparatedArabicsTest) {
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
// Expected data to succeed tests.
constexpr const char *kSuccess[][3] = {
{"1000", "1,000", "1,000"},
{"12345678", "12,345,678", "12,345,678"},
{"1234.5", "1,234.5", "1,234.5"},
};
for (size_t i = 0; i < std::size(kSuccess); ++i) {
Segments segments;
Segment *seg = segments.push_back_segment();
Segment::Candidate *candidate = seg->add_candidate();
candidate->lid = pos_matcher_.GetNumberId();
candidate->rid = pos_matcher_.GetNumberId();
candidate->key = kSuccess[i][0];
candidate->content_key = kSuccess[i][0];
candidate->value = kSuccess[i][0];
candidate->content_value = kSuccess[i][0];
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
EXPECT_TRUE(FindValue(segments.segment(0), kSuccess[i][1]))
<< "Input : " << kSuccess[i][0];
EXPECT_TRUE(FindValue(segments.segment(0), kSuccess[i][2]))
<< "Input : " << kSuccess[i][0];
}
// Expected data to fail tests.
constexpr const char *kFail[][3] = {
{"123", ",123", ",123"},
{"999", ",999", ",999"},
{"0000", "0,000", "0,000"},
};
for (size_t i = 0; i < std::size(kFail); ++i) {
Segments segments;
Segment *seg = segments.push_back_segment();
Segment::Candidate *candidate = seg->add_candidate();
candidate->lid = pos_matcher_.GetNumberId();
candidate->rid = pos_matcher_.GetNumberId();
candidate->key = kFail[i][0];
candidate->content_key = kFail[i][0];
candidate->value = kFail[i][0];
candidate->content_value = kFail[i][0];
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
EXPECT_FALSE(FindValue(segments.segment(0), kFail[i][1]))
<< "Input : " << kFail[i][0];
EXPECT_FALSE(FindValue(segments.segment(0), kFail[i][2]))
<< "Input : " << kFail[i][0];
}
}
// Consider the case where user dictionaries contain following entry.
// - Reading: "はやぶさ"
// - Value: "8823"
// - POS: GeneralNoun (not *Number*)
// In this case, NumberRewriter should not clear
// Segment::Candidate::USER_DICTIONARY bit in the base candidate.
TEST_F(NumberRewriterTest, PreserveUserDictionaryAttribute) {
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
{
Segments segments;
{
Segment *seg = segments.push_back_segment();
Segment::Candidate *candidate = seg->add_candidate();
candidate->lid = pos_matcher_.GetGeneralNounId();
candidate->rid = pos_matcher_.GetGeneralNounId();
candidate->key = "はやぶさ";
candidate->content_key = candidate->key;
candidate->value = "8823";
candidate->content_value = candidate->value;
candidate->cost = 5925;
candidate->wcost = 5000;
candidate->attributes = Segment::Candidate::USER_DICTIONARY |
Segment::Candidate::NO_VARIANTS_EXPANSION;
}
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
bool base_candidate_found = false;
{
const Segment &segment = segments.segment(0);
for (size_t i = 0; i < segment.candidates_size(); ++i) {
const Segment::Candidate &candidate = segment.candidate(i);
if (candidate.value == "8823" &&
(candidate.attributes & Segment::Candidate::USER_DICTIONARY)) {
base_candidate_found = true;
break;
}
}
}
EXPECT_TRUE(base_candidate_found);
}
}
TEST_F(NumberRewriterTest, DuplicateCandidateTest) {
// To reproduce issue b/6714268.
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
ConversionRequest convreq;
commands::Request request;
convreq.set_request(&request);
std::unique_ptr<NumberRewriter> rewriter(CreateNumberRewriter());
{
request.set_mixed_conversion(true);
EXPECT_EQ(rewriter->capability(convreq), RewriterInterface::ALL);
}
{
request.set_mixed_conversion(false);
EXPECT_EQ(rewriter->capability(convreq), RewriterInterface::CONVERSION);
}
}
TEST_F(NumberRewriterTest, NonNumberNounTest) {
// Test if "百舌鳥" is not rewritten to "100舌鳥", etc.
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
Segments segments;
Segment *segment = segments.push_back_segment();
segment->set_key("");
Segment::Candidate *cand = segment->add_candidate();
cand->key = "もず";
cand->content_key = cand->key;
cand->value = "百舌鳥";
cand->content_value = cand->value;
cand->lid = pos_matcher_.GetGeneralNounId();
cand->rid = pos_matcher_.GetGeneralNounId();
EXPECT_FALSE(number_rewriter->Rewrite(default_request_, &segments));
}
TEST_F(NumberRewriterTest, RewriteArabicNumberTest) {
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
Segments segments;
Segment *segment = segments.push_back_segment();
segment->set_key("いち");
struct CandData {
const char *value;
int pos_id;
};
const CandData kCandList[] = {
{"1", pos_matcher_.GetNumberId()},
{"一", pos_matcher_.GetNumberId()},
{"位置", pos_matcher_.GetGeneralNounId()},
{"イチ", pos_matcher_.GetGeneralNounId()},
{"壱", pos_matcher_.GetGeneralNounId()},
};
for (const auto &cand_data : kCandList) {
Segment::Candidate *cand = segment->add_candidate();
cand->key = "いち";
cand->content_key = cand->key;
cand->value = cand_data.value;
cand->content_value = cand->value;
cand->lid = cand_data.pos_id;
cand->rid = cand_data.pos_id;
}
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));
EXPECT_GT(segment->candidates_size(), 1);
EXPECT_EQ(segment->candidate(0).value, "一");
EXPECT_EQ(segment->candidate(1).value, "位置");
}
TEST_F(NumberRewriterTest, RewriteForPartialSuggestion_b16765535) {
std::unique_ptr<NumberRewriter> number_rewriter(CreateNumberRewriter());
Segments segments;
{
Segment *seg = segments.push_back_segment();
Segment::Candidate *candidate = seg->add_candidate();
candidate->lid = pos_matcher_.GetNumberId();
candidate->rid = pos_matcher_.GetNumberId();
candidate->key = "090";
candidate->value = "090";
candidate->content_key = "090";
candidate->content_value = "090";
candidate->attributes = (Segment::Candidate::PARTIALLY_KEY_CONSUMED |
Segment::Candidate::NO_SUGGEST_LEARNING);
candidate->consumed_key_size = 3;
}
{
Segment *seg = segments.push_back_segment();
Segment::Candidate *candidate = seg->add_candidate();
candidate->key = "-";
candidate->value = "-";
candidate->content_key = "-";
candidate->content_value = "-";
}
EXPECT_TRUE(number_rewriter->Rewrite(default_request_, &segments));