-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjp_normalizer.hh
923 lines (777 loc) · 25.7 KB
/
jp_normalizer.hh
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
// SPDX-License-Identifier: Apache 2.0
// Copyright 2023 - Present, Light Transport Entertainement Inc.
//
// Assume this file is encoded in UTF-8
#pragma once
#include <cstdint>
#include <string>
#include <unordered_map>
#include <unordered_set>
namespace jpnormalizer {
struct NormalizationOption {
enum class TildeMode {
Remove, // remove tilde
Ignore, // ignore(do nothing) for tilde char.
Normalize, // convert to ASCII '~'
Zenkaku, // convert to Zenkaku '〜'
};
uint32_t max_tokens{1024ull*1024ull*1024ull}; // default 1 GB tokens(~= 3GB in Japanese UTF-8 chars)
bool remove_space{true};
uint32_t repeat{0};
uint32_t max_repeat_substr_len{8};
TildeMode tilde{TildeMode::Remove};
// jpnormalizer specific feature.
bool parenthesized_ideographs{true};
};
struct DedupNormalizationOption {
// Placeholder token for digits
char digit_placeholder{'0'};
};
std::string normalize(const std::string& str,
const NormalizationOption option = NormalizationOption());
std::string normalize_for_dedup(const std::string& str,
const DedupNormalizationOption option = DedupNormalizationOption());
std::unordered_set<std::string> get_digits();
std::unordered_set<std::string> get_digits_and_parentized_ideographs();
const std::unordered_set<std::string> &get_unicode_puncts();
#if defined(WIN32)
// TODO: wstring version
#endif
} // namespace jpnormalizer
#if defined(JP_NORMALIZER_IMPLEMENTATION)
#include <cstring>
#include <memory>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace jpnormalizer {
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wexit-time-destructors"
#pragma clang diagnostic ignored "-Wglobal-constructors"
#endif
static std::unordered_map<std::string, char> sASCII{
{"a", 'a'}, {"b", 'b'}, {"c", 'c'}, {"d", 'd'}, {"e", 'e'},
{"f", 'f'}, {"g", 'g'}, {"h", 'h'}, {"i", 'i'}, {"j", 'j'},
{"k", 'k'}, {"l", 'l'}, {"m", 'm'}, {"n", 'n'}, {"o", 'o'},
{"p", 'p'}, {"q", 'q'}, {"r", 'r'}, {"s", 's'}, {"t", 't'},
{"u", 'u'}, {"v", 'v'}, {"w", 'w'}, {"x", 'x'}, {"y", 'y'},
{"z", 'z'}, {"A", 'A'}, {"B", 'B'}, {"C", 'C'}, {"D", 'D'},
{"E", 'E'}, {"F", 'F'}, {"G", 'G'}, {"H", 'H'}, {"I", 'I'},
{"J", 'J'}, {"K", 'K'}, {"L", 'L'}, {"M", 'M'}, {"N", 'N'},
{"O", 'O'}, {"P", 'P'}, {"Q", 'Q'}, {"R", 'R'}, {"S", 'S'},
{"T", 'T'}, {"U", 'U'}, {"V", 'V'}, {"W", 'W'}, {"X", 'X'},
{"Y", 'Y'}, {"Z", 'Z'}, {"!", '!'}, {"”", '"'}, {"#", '#'},
{"$", '$'}, {"%", '%'}, {"&", '&'}, {"’", '\''}, {"(", '{'},
{")", '}'}, {"*", '*'}, {"+", '+'}, {",", ','}, {"−", '-'},
{".", '.'}, {"/", '/'}, {":", ':'}, {";", ';'}, {"<", '<'},
{"=", '='}, {">", '>'}, {"?", '?'}, {"@", '@'}, {"[", '['},
{"¥", '\\'}, {"]", ']'}, {"^", '^'}, {"_", '_'}, {"‘", '`'},
{"{", '{'}, {"|", '|'}, {"}", '}'}};
static std::unordered_map<std::string, std::string> sKANA = {
{"ア", "ア"}, {"イ", "イ"}, {"ウ", "ウ"}, {"エ", "エ"}, {"オ", "オ"},
{"カ", "カ"}, {"キ", "キ"}, {"ク", "ク"}, {"ケ", "ケ"}, {"コ", "コ"},
{"サ", "サ"}, {"シ", "シ"}, {"ス", "ス"}, {"セ", "セ"}, {"ソ", "ソ"},
{"タ", "タ"}, {"チ", "チ"}, {"ツ", "ツ"}, {"テ", "テ"}, {"ト", "ト"},
{"ナ", "ナ"}, {"ニ", "ニ"}, {"ヌ", "ヌ"}, {"ネ", "ネ"}, {"ノ", "ノ"},
{"ハ", "ハ"}, {"ヒ", "ヒ"}, {"フ", "フ"}, {"ヘ", "ヘ"}, {"ホ", "ホ"},
{"マ", "マ"}, {"ミ", "ミ"}, {"ム", "ム"}, {"メ", "メ"}, {"モ", "モ"},
{"ヤ", "ヤ"}, {"ユ", "ユ"}, {"ヨ", "ヨ"}, {"ラ", "ラ"}, {"リ", "リ"},
{"ル", "ル"}, {"レ", "レ"}, {"ロ", "ロ"}, {"ワ", "ワ"}, {"ヲ", "ヲ"},
{"ン", "ン"}, {"ァ", "ァ"}, {"ィ", "ィ"}, {"ゥ", "ゥ"}, {"ェ", "ェ"},
{"ォ", "ォ"}, {"ッ", "ッ"}, {"ャ", "ャ"}, {"ュ", "ュ"}, {"ョ", "ョ"},
{"。", "。"}, {"、", "、"}, {"・", "・"}, {"゛", "゙"}, {"゜", "゚"},
{"「", "「"}, {"」", "」"}, {"ー", "ー"}};
static std::unordered_map<std::string, std::string> sDIGIT = {
{"0", "0"}, {"1", "1"}, {"2", "2"}, {"3", "3"}, {"4", "4"},
{"5", "5"}, {"6", "6"}, {"7", "7"}, {"8", "8"}, {"9", "9"}};
static std::unordered_map<std::string, std::string> sKANA_TEN = {
{"カ", "ガ"}, {"キ", "ギ"}, {"ク", "グ"}, {"ケ", "ゲ"}, {"コ", "ゴ"},
{"サ", "ザ"}, {"シ", "ジ"}, {"ス", "ズ"}, {"セ", "ゼ"}, {"ソ", "ゾ"},
{"タ", "ダ"}, {"チ", "ヂ"}, {"ツ", "ヅ"}, {"テ", "デ"}, {"ト", "ド"},
{"ハ", "バ"}, {"ヒ", "ビ"}, {"フ", "ブ"}, {"ヘ", "ベ"}, {"ホ", "ボ"},
{"ウ", "ヴ"}, {"う", "ゔ"}};
static std::unordered_map<std::string, std::string> sKANA_MARU = {
{"ハ", "パ"}, {"ヒ", "ピ"}, {"フ", "プ"}, {"ヘ", "ペ"}, {"ホ", "ポ"},
{"は", "ぱ"}, {"ひ", "ぴ"}, {"ふ", "ぷ"}, {"へ", "ぺ"}, {"ほ", "ぽ"}};
// not in neologdn
static std::unordered_map<std::string, std::string> sParenthesizedIdeographs = {
{"㈠", "(一)"}, {"㈡", "(二)"}, {"㈢", "(三)"}, {"㈣", "(四)"},
{"㈤", "(五)"}, {"㈥", "(六)"}, {"㈦", "(七)"}, {"㈧", "(八)"},
{"㈨", "(九)"}, {"㈩", "(十)"}, {"㈪", "(月)"}, {"㈫", "(火)"},
{"㈬", "(水)"}, {"㈭", "(木)"}, {"㈮", "(金)"}, {"㈯", "(土)"},
{"㈰", "(日)"}, {"㈱", "(株)"}, {"㈲", "(有)"}, {"㈳", "(社)"},
{"㈴", "(名)"}, {"㈵", "(特)"}, {"㈶", "(財)"}, {"㈷", "(祝)"},
{"㈸", "(労)"}, {"㈹", "(代)"}, {"㈺", "(呼)"}, {"㈻", "(学)"},
{"㈼", "(監)"}, {"㈽", "(企)"}, {"㈾", "(資)"}, {"㈿", "(協)"},
{"㉀", "(祭)"}, {"㉁", "(休)"}, {"㉂", "(自)"}, {"㉃", "(至)"}};
static std::unordered_set<std::string> sHYPHENS = {"˗", "֊", "‐", "‑", "‒",
"–", "⁃", "⁻", "₋", "−"};
static std::unordered_set<std::string> sCHOONPUS = {"﹣", "-", "ー", "—",
"―", "─", "━", "ー"};
static std::unordered_set<std::string> sTILDES = {"~", "∼", "∾",
"〜", "〰", "~"};
// TODO: We can simply use direct string comparision for space character.
static std::unordered_set<std::string> sSPACE = {" ", " "};
static std::unordered_set<std::string> sUNICODE_PUNCT = {
",",
"。",
"、",
"„",
"”",
"“",
"«",
"»",
"1",
"」",
"「",
"《",
"》",
"´",
"∶",
":",
"?",
"!",
"(",
")",
";",
"–",
"—",
".",
"~",
"’",
"…",
"━",
"〈",
"〉",
"【",
"】",
"%",
"►",
":",
";",
"-",
",",
".",
"?",
"!",
")",
"(",
"<",
">",
"[",
"]",
"\"",
"'"
};
#ifdef __clang__
#pragma clang diagnostic pop
#endif
namespace detail {
inline uint32_t utf8_len(const uint8_t c) {
if (c <= 127) {
// ascii
return 1;
} else if ((c & 0xE0) == 0xC0) {
return 2;
} else if ((c & 0xF0) == 0xE0) {
return 3;
} else if ((c & 0xF8) == 0xF0) {
return 4;
}
// invalid
return 0;
}
inline uint32_t to_codepoint(const char *s, uint32_t &len) {
if (!s) {
return ~0u;
}
uint32_t char_len = utf8_len(uint8_t(*s));
uint32_t code = 0;
if (char_len == 1) {
unsigned char s0 = static_cast<unsigned char>(s[0]);
if (s0 > 0x7f) {
len = 0;
return ~0u;
}
code = uint32_t(s0) & 0x7f;
} else if (char_len == 2) {
// 11bit: 110y-yyyx 10xx-xxxx
unsigned char s0 = static_cast<unsigned char>(s[0]);
unsigned char s1 = static_cast<unsigned char>(s[1]);
if (((s0 & 0xe0) == 0xc0) && ((s1 & 0xc0) == 0x80)) {
code = (uint32_t(s0 & 0x1f) << 6) | (s1 & 0x3f);
} else {
len = 0;
return ~0u;
}
} else if (char_len == 3) {
// 16bit: 1110-yyyy 10yx-xxxx 10xx-xxxx
unsigned char s0 = static_cast<unsigned char>(s[0]);
unsigned char s1 = static_cast<unsigned char>(s[1]);
unsigned char s2 = static_cast<unsigned char>(s[2]);
if (((s0 & 0xf0) == 0xe0) && ((s1 & 0xc0) == 0x80) &&
((s2 & 0xc0) == 0x80)) {
code =
(uint32_t(s0 & 0xf) << 12) | (uint32_t(s1 & 0x3f) << 6) | (s2 & 0x3f);
} else {
len = 0;
return ~0u;
}
} else if (char_len == 4) {
// 21bit: 1111-0yyy 10yy-xxxx 10xx-xxxx 10xx-xxxx
unsigned char s0 = static_cast<unsigned char>(s[0]);
unsigned char s1 = static_cast<unsigned char>(s[1]);
unsigned char s2 = static_cast<unsigned char>(s[2]);
unsigned char s3 = static_cast<unsigned char>(s[3]);
if (((s0 & 0xf8) == 0xf0) && ((s1 & 0xc0) == 0x80) &&
((s2 & 0xc0) == 0x80) && ((s2 & 0xc0) == 0x80)) {
code = (uint32_t(s0 & 0x7) << 18) | (uint32_t(s1 & 0x3f) << 12) |
(uint32_t(s2 & 0x3f) << 6) | uint32_t(s3 & 0x3f);
} else {
len = 0;
return ~0u;
}
} else {
len = 0;
return ~0u;
}
len = char_len;
return code;
}
inline std::string codepoint_to_utf8(uint32_t code) {
if (code <= 0x7f) {
return std::string(1, char(code));
} else if (code <= 0x7ff) {
// 11bit: 110y-yyyx 10xx-xxxx
uint8_t buf[2];
buf[0] = uint8_t(((code >> 6) & 0x1f) | 0xc0);
buf[1] = uint8_t(((code >> 0) & 0x3f) | 0x80);
return std::string(reinterpret_cast<const char *>(&buf[0]), 2);
} else if (code <= 0xffff) {
// 16bit: 1110-yyyy 10yx-xxxx 10xx-xxxx
uint8_t buf[3];
buf[0] = uint8_t(((code >> 12) & 0x0f) | 0xe0);
buf[1] = uint8_t(((code >> 6) & 0x3f) | 0x80);
buf[2] = uint8_t(((code >> 0) & 0x3f) | 0x80);
return std::string(reinterpret_cast<const char *>(&buf[0]), 3);
} else if (code <= 0x10ffff) {
// 21bit: 1111-0yyy 10yy-xxxx 10xx-xxxx 10xx-xxxx
uint8_t buf[4];
buf[0] = uint8_t(((code >> 18) & 0x07) | 0xF0);
buf[1] = uint8_t(((code >> 12) & 0x3F) | 0x80);
buf[2] = uint8_t(((code >> 6) & 0x3F) | 0x80);
buf[3] = uint8_t(((code >> 0) & 0x3F) | 0x80);
return std::string(reinterpret_cast<const char *>(&buf[0]), 4);
}
// invalid
return std::string();
}
inline std::vector<uint32_t> to_codepoints(const std::string &str)
{
size_t i = 0;
std::vector<uint32_t> codes;
while (i < str.size()) {
uint32_t len;
uint32_t code = to_codepoint(&str[i], len);
codes.push_back(code);
i += len;
}
return codes;
}
inline std::string codepoints_to_string(const std::vector<uint32_t> &codepoints)
{
std::string ret;
for (const auto &code : codepoints) {
ret += codepoint_to_utf8(code);
}
return ret;
}
inline std::string extract_utf8_char(const std::string& str, uint32_t start_i,
int& len) {
len = 0;
if ((start_i + 1) > str.size()) {
len = 0;
return std::string();
}
unsigned char c = static_cast<unsigned char>(str[start_i]);
if (c <= 127) {
// ascii
len = 1;
return str.substr(start_i, 1);
} else if ((c & 0xE0) == 0xC0) {
if ((start_i + 2) > str.size()) {
len = 0;
return std::string();
}
len = 2;
return str.substr(start_i, 2);
} else if ((c & 0xF0) == 0xE0) {
if ((start_i + 3) > str.size()) {
len = 0;
return std::string();
}
len = 3;
return str.substr(start_i, 3);
} else if ((c & 0xF8) == 0xF0) {
if ((start_i + 4) > str.size()) {
len = 0;
return std::string();
}
len = 4;
return str.substr(start_i, 4);
} else {
// invalid utf8
len = 0;
return std::string();
}
}
inline uint32_t utf8_code(const std::string &s) {
if (s.empty() || (s.size() > 4)) {
return ~0u; // invalid
}
// TODO: endianness
uint32_t code = 0;
if (s.size() == 1) {
unsigned char s0 = static_cast<unsigned char>(s[0]);
if (s0 > 0x7f) {
return ~0u;
}
code = uint32_t(s0) & 0x7f;
} else if (s.size() == 2) {
// 11bit: 110y-yyyx 10xx-xxxx
unsigned char s0 = static_cast<unsigned char>(s[0]);
unsigned char s1 = static_cast<unsigned char>(s[1]);
if (((s0 & 0xe0) == 0xc0) && ((s1 & 0xc0) == 0x80)) {
code = (uint32_t(s0 & 0x1f) << 6) | (s1 & 0x3f);
} else {
return ~0u;
}
} else if (s.size() == 3) {
// 16bit: 1110-yyyy 10yx-xxxx 10xx-xxxx
unsigned char s0 = static_cast<unsigned char>(s[0]);
unsigned char s1 = static_cast<unsigned char>(s[1]);
unsigned char s2 = static_cast<unsigned char>(s[2]);
if (((s0 & 0xf0) == 0xe0) && ((s1 & 0xc0) == 0x80) && ((s2 & 0xc0) == 0x80)) {
code = (uint32_t(s0 & 0xf) << 12) | (uint32_t(s1 & 0x3f) << 6) | (s2 & 0x3f);
} else {
return ~0u;
}
} else {
// 21bit: 1111-0yyy 10yy-xxxx 10xx-xxxx 10xx-xxxx
unsigned char s0 = static_cast<unsigned char>(s[0]);
unsigned char s1 = static_cast<unsigned char>(s[1]);
unsigned char s2 = static_cast<unsigned char>(s[2]);
unsigned char s3 = static_cast<unsigned char>(s[3]);
if (((s0 & 0xf8) == 0xf0) && ((s1 & 0xc0) == 0x80) && ((s2 & 0xc0) == 0x80) && ((s2 & 0xc0) == 0x80)) {
code = (uint32_t(s0 & 0x7) << 18) | (uint32_t(s1 & 0x3f) << 12) | (uint32_t(s2 & 0x3f) << 6) | uint32_t(s3 & 0x3f);
} else {
return ~0u;
}
}
return code;
}
inline bool is_cjk_char(const std::string &s) {
// range(19968, 40960), # CJK UNIFIED IDEOGRAPHS
// range(12352, 12448), # HIRAGANA
// range(12448, 12544), # KATAKANA
// range(12289, 12352), # CJK SYMBOLS AND PUNCTUATION
// range(65280, 65520) # HALFWIDTH AND FULLWIDTH FORMS
uint32_t code = utf8_code(s);
if ((code >= 19968) && (code < 40960)) {
return true;
} else if ((code >= 12352) && (code < 12448)) {
return true;
} else if ((code >= 12448) && (code < 12544)) {
return true;
} else if ((code >= 12289) && (code < 12352)) {
return true;
} else if ((code >= 65280) && (code < 65520)) {
return true;
}
return false;
}
inline bool is_equal(const std::vector<uint32_t> &in,
size_t s_pos0, size_t s_pos1, size_t len) {
// return false when pos is out-of-range.
if ((s_pos0 >= in.size()) ||
(s_pos1 >= in.size()) ||
((s_pos0 + len) > in.size()) ||
((s_pos1 + len) > in.size())) {
return false;
}
return std::equal(std::begin(in) + int64_t(s_pos0), std::begin(in) + int64_t(s_pos0 + len),
std::begin(in) + int64_t(s_pos1));
}
static std::vector<uint32_t> shorten_repeat_codepoints(const std::vector<uint32_t> &u8_codepoints, uint32_t repeat_threshold, uint32_t max_repeat_substr_len=8) {
std::vector<uint32_t> text = u8_codepoints;
size_t i = 0;
while (i < text.size()) {
size_t text_len = text.size();
// upper bound of repeat size = 1/2 of input text.
size_t ceil_repeat_len = (text_len - i) / 2;
if (max_repeat_substr_len < ceil_repeat_len) {
ceil_repeat_len = max_repeat_substr_len + 1;
}
for (size_t repeat_len = 1; repeat_len < ceil_repeat_len; repeat_len++) {
size_t right_start = i + repeat_len;
size_t right_end = right_start + repeat_len;
size_t num_repeat = 1;
while (right_end <= text_len) {
if (!is_equal(text, i, right_start, repeat_len)) {
break;
}
num_repeat++;
right_start += repeat_len;
right_end += repeat_len;
}
if (num_repeat > repeat_threshold) {
// cut out repeated substr
#if 0
std::vector<uint32_t> buf;
for (size_t k = 0; k < (std::min)(i + repeat_len * repeat_threshold, text.size()); k++) {
buf.push_back(text[k]);
}
for (size_t k = i + repeat_len * num_repeat; k < text.size(); k++) {
buf.push_back(text[k]);
}
text.swap(buf);
#else
text.erase(text.begin() + int64_t((std::min)(i + repeat_len * repeat_threshold, text.size())), text.begin() + int64_t(i + repeat_len * num_repeat));
#endif
}
}
i++;
}
return text;
}
static std::string shorten_repeat(const std::string &text, uint32_t repeat_threshold, uint32_t max_repeat_substr_len=8) {
std::vector<uint32_t> codepoints = to_codepoints(text);
std::vector<uint32_t> ret = shorten_repeat_codepoints(codepoints, repeat_threshold, max_repeat_substr_len);
return codepoints_to_string(ret);
}
} // namespace detail
std::string normalize(const std::string& str,
const NormalizationOption option) {
if (str.empty()) {
return std::string();
}
if (str.size() > option.max_tokens) {
return std::string();
}
///
/// Decompose input string into UTF8 char list.
///
uint64_t sz = str.size();
std::vector<std::string> utf8_chars;
for (size_t i = 0; i <= sz;) {
int len=0;
std::string s = detail::extract_utf8_char(str, uint32_t(i), len);
if (len == 0) {
// invalid char
break;
}
i += uint64_t(len);
utf8_chars.push_back(s);
}
std::vector<std::string> dst_buf;
// normalized text should not exceed input length.
dst_buf.resize(utf8_chars.size());
std::string prev_c = "\0";
bool latin_space = false;
uint64_t loc = 0;
for (size_t i = 0 ; i < utf8_chars.size(); i++) {
std::string c = utf8_chars[i];
if (sSPACE.count(c)) {
c = " ";
//std::cout << "c is space: prev_c = " << prev_c << ", utf8 code " << detail::utf8_code(prev_c) << "\n";
if (((prev_c == " ") || detail::is_cjk_char(prev_c)) && option.remove_space) {
continue;
} else if ((prev_c != "*") && (loc > 0) && (detail::utf8_code(prev_c) < 128)) {
//std::cout << "latin space\n";
latin_space = true;
if (loc >= dst_buf.size()) {
// ???
return std::string();
}
dst_buf[loc] = c;
} else if (option.remove_space) {
if (loc == 0) {
prev_c = c;
continue;
} else {
loc--;
}
} else {
if (loc >= dst_buf.size()) {
// ???
return std::string();
}
dst_buf[loc] = c;
}
} else {
if (sHYPHENS.count(c)) {
if (prev_c == "-") {
continue;
} else {
c = "-";
dst_buf[loc] = c;
}
} else if (sCHOONPUS.count(c)) {
if (prev_c == "ー") { // zenkaku
continue;
} else {
c = "ー";
dst_buf[loc] = c;
}
} else if (sTILDES.count(c)) {
if (option.tilde == NormalizationOption::TildeMode::Ignore) {
// pass
} else if (option.tilde == NormalizationOption::TildeMode::Normalize) {
c = "~";
} else if (option.tilde == NormalizationOption::TildeMode::Zenkaku) {
c = "〜";
} else {
continue;
}
dst_buf[loc] = c;
} else {
if (sASCII.count(c)) {
c = sASCII.at(c);
} else if (sDIGIT.count(c)) {
c = sDIGIT.at(c);
} else if (sKANA.count(c)) {
c = sKANA.at(c);
} else {
// Additional unicode normalizations
if (option.parenthesized_ideographs && sParenthesizedIdeographs.count(c)) {
c = sParenthesizedIdeographs.at(c);
}
}
if ((c == "゙") && (sKANA_TEN.count(prev_c))) {
if (loc == 0) {
//std::cerr << "b loc = 0\n";
return std::string();
}
loc--;
c = sKANA_TEN.at(prev_c);
} else if ((c == "゚") && (sKANA_MARU.count(prev_c))) {
if (loc == 0) {
//std::cerr << "c loc = 0\n";
return std::string();
}
loc--;
c = sKANA_MARU.at(prev_c);
}
//std::cout << "latin_space " << latin_space << "\n";
//std::cout << "is_cjk_char " << detail::is_cjk_char(c) << "\n";
// TODO: allow all non-latin char?
if (latin_space && detail::is_cjk_char(c) && option.remove_space) {
//std::cout << "latin space: c = " << c << "\n";
if (loc == 0) {
return std::string();
}
loc--;
}
latin_space = false;
dst_buf[loc] = c;
}
}
prev_c = c;
loc++;
}
if (loc == 0) {
// This should not happen though.
//std::cerr << "e loc = 0\n";
return std::string();
}
if (dst_buf[loc-1] == " ") {
loc--;
}
std::string dst_str;
// simply concat chars.
for (size_t i = 0; i < loc; i++) {
dst_str += dst_buf[i];
}
if (option.repeat > 0) {
return detail::shorten_repeat(dst_str, option.repeat, option.max_repeat_substr_len);
}
return dst_str;
}
#if 0 // TODO
// replace all digits to a placeholder character
std::string normalize_for_dedup(const std::string& str,
const DedupNormalizationOption dedup_option) {
NormalizationOption option;
option.tilde = NormalizationOption::TildeMode::Remove;
option.remove_space = true;
option.repeat = 8;
option.max_repeat = 8;
option.parenthesized_ideographs = true;
if (str.empty()) {
return std::string();
}
if (str.size() > option.max_tokens) {
return std::string();
}
///
/// Decompose input string into UTF8 char list.
///
uint64_t sz = str.size();
std::vector<std::string> utf8_chars;
for (size_t i = 0; i <= sz;) {
int len=0;
std::string s = detail::extract_utf8_char(str, uint32_t(i), len);
if (len == 0) {
// invalid char
break;
}
i += uint64_t(len);
utf8_chars.push_back(s);
}
std::vector<std::string> dst_buf;
// normalized text should not exceed input length.
dst_buf.resize(utf8_chars.size());
std::string prev_c = "\0";
bool latin_space = false;
uint64_t loc = 0;
for (size_t i = 0 ; i < utf8_chars.size(); i++) {
std::string c = utf8_chars[i];
if (sSPACE.count(c)) {
c = " ";
//std::cout << "c is space: prev_c = " << prev_c << ", utf8 code " << detail::utf8_code(prev_c) << "\n";
if (((prev_c == " ") || detail::is_cjk_char(prev_c)) && option.remove_space) {
continue;
} else if ((prev_c != "*") && (loc > 0) && (detail::utf8_code(prev_c) < 128)) {
//std::cout << "latin space\n";
latin_space = true;
if (loc >= dst_buf.size()) {
// ???
return std::string();
}
dst_buf[loc] = c;
} else if (option.remove_space) {
if (loc == 0) {
prev_c = c;
continue;
} else {
loc--;
}
} else {
if (loc >= dst_buf.size()) {
// ???
return std::string();
}
dst_buf[loc] = c;
}
} else {
if (sHYPHENS.count(c)) {
if (prev_c == "-") {
continue;
} else {
c = "-";
dst_buf[loc] = c;
}
} else if (sCHOONPUS.count(c)) {
if (prev_c == "ー") { // zenkaku
continue;
} else {
c = "ー";
dst_buf[loc] = c;
}
} else if (sTILDES.count(c)) {
if (option.tilde == NormalizationOption::TildeMode::Ignore) {
// pass
} else if (option.tilde == NormalizationOption::TildeMode::Normalize) {
c = "~";
} else if (option.tilde == NormalizationOption::TildeMode::Zenkaku) {
c = "〜";
} else {
continue;
}
dst_buf[loc] = c;
} else {
if (sASCII.count(c)) {
c = sASCII.at(c);
} else if (sDIGIT.count(c)) {
c = sDIGIT.at(c);
} else if (sKANA.count(c)) {
c = sKANA.at(c);
} else {
// Additional unicode normalizations
if (option.parenthesized_ideographs && sParenthesizedIdeographs.count(c)) {
c = sParenthesizedIdeographs.at(c);
}
}
if ((c == "゙") && (sKANA_TEN.count(prev_c))) {
if (loc == 0) {
//std::cerr << "b loc = 0\n";
return std::string();
}
loc--;
c = sKANA_TEN.at(prev_c);
} else if ((c == "゚") && (sKANA_MARU.count(prev_c))) {
if (loc == 0) {
//std::cerr << "c loc = 0\n";
return std::string();
}
loc--;
c = sKANA_MARU.at(prev_c);
}
//std::cout << "latin_space " << latin_space << "\n";
//std::cout << "is_cjk_char " << detail::is_cjk_char(c) << "\n";
// TODO: allow all non-latin char?
if (latin_space && detail::is_cjk_char(c) && option.remove_space) {
//std::cout << "latin space: c = " << c << "\n";
if (loc == 0) {
return std::string();
}
loc--;
}
latin_space = false;
dst_buf[loc] = c;
}
}
prev_c = c;
loc++;
}
if (loc == 0) {
// This should not happen though.
//std::cerr << "e loc = 0\n";
return std::string();
}
if (dst_buf[loc-1] == " ") {
loc--;
}
std::string dst_str;
// simply concat chars.
for (size_t i = 0; i < loc; i++) {
dst_str += dst_buf[i];
}
if (option.repeat > 0) {
return detail::shorten_repeat(dst_str, option.repeat, option.max_repeat);
}
return dst_str;
}
#endif
const std::unordered_set<std::string> &get_unicode_puncts() {
return sUNICODE_PUNCT;
}
std::unordered_set<std::string> get_digits() {
std::unordered_set<std::string> dst;
for (const auto &it : sDIGIT) {
dst.insert(it.first);
dst.insert(it.second);
}
return dst;
}
std::unordered_set<std::string> get_digits_and_parentized_ideographs() {
std::unordered_set<std::string> dst = get_digits();
for (const auto &it : sParenthesizedIdeographs) {
dst.insert(it.first);
dst.insert(it.second);
}
return dst;
}
} // namespace jpnormalizer
#endif // JP_NORMALIZER_IMPLEMENTATION