/
TextTokenizer.cpp
258 lines (163 loc) · 5.19 KB
/
TextTokenizer.cpp
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
#include "TextTokenizer.h"
#include "ext/ZCharScanner.h"
#include <algorithm>
#include <cassert>
#include <cctype>
#include <iostream>
#include <algorithm>
// Punctuation, this gets auto-converted to SIL
const std::u32string punctuation_f = U",.-;";
// For Tacotron2, including question and other marks
const std::u32string punctuation_tac = U",.;¡!¿?:-";
const std::u32string digits = U"1234567890";
using namespace std;
void TextTokenizer::SetAllowedChars(const std::string &value)
{
AllowedChars = VoxUtil::StrToU32(value);
}
vector<string> TextTokenizer::ExpandNumbers(const std::vector<std::string>& SpaceTokens)
{
vector<string> RetVec;
RetVec.reserve(SpaceTokens.size());
for (auto& Token : SpaceTokens) {
char* p;
strtol(Token.c_str(), &p, 10);
if (*p) {
RetVec.push_back(Token);
}
else {
std::string ModTk = Token;
CuNumber->numbertext(ModTk,NumLang);
std::replace(ModTk.begin(),ModTk.end(),'-',' ');
// If the number has spaces we must sep again and add one by one otherwise all the words are merged together due to the
// nature of it
ZStringDelimiter DelSp(ModTk);
DelSp.AddDelimiter(" ");
if (DelSp.szTokens())
{
for (const auto& Ttk : DelSp.GetTokens())
RetVec.push_back(Ttk);
}else{
RetVec.push_back(ModTk);
}
}
}
return RetVec;
}
string TextTokenizer::SpaceChars(const string &InStr)
{
std::u32string AsmStr = U"";
std::u32string Stry = VoxUtil::StrToU32(InStr);
bool InNumChain = false;
bool InPhn = false;
for (size_t i = 0; i < Stry.size();i++)
{
auto uChar = Stry[i];
if (uChar == U'@')
InPhn = true;
if (uChar == U' ')
InPhn = false;
if (InPhn)
{
AsmStr += uChar;
continue;
}
if (digits.find(uChar) != std::u32string::npos && !InNumChain)
{
AsmStr += U" ";
AsmStr += uChar;
InNumChain = true;
continue;
}
if (digits.find(uChar) == std::u32string::npos && InNumChain )
{
AsmStr += U" ";
AsmStr += uChar;
InNumChain = false;
continue;
}
AsmStr += uChar;
}
return VoxUtil::U32ToStr(AsmStr);
}
TextTokenizer::TextTokenizer()
{
}
TextTokenizer::~TextTokenizer()
{
}
void TextTokenizer::SetNumberText(Numbertext &INum, const string &Lang)
{
CuNumber = &INum;
NumLang = Lang;
}
vector<string> TextTokenizer::Tokenize(const std::string & InTxt,bool IsTacotron, bool IsTorchMoji)
{
vector<string> ProcessedTokens;
std::string TxtPreProc = SpaceChars(InTxt);
ZStringDelimiter Delim(TxtPreProc);
Delim.AddDelimiter(" ");
vector<string> DelimitedTokens = Delim.GetTokens();
// Single word handler
if (!Delim.szTokens())
DelimitedTokens.push_back(TxtPreProc);
DelimitedTokens = ExpandNumbers(DelimitedTokens);
std::u32string punctuation = punctuation_f;
if (IsTacotron)
punctuation = punctuation_tac;
// We know that the new vector is going to be at least this size so we reserve
ProcessedTokens.reserve(DelimitedTokens.size());
/*
In this step we go through the string and only allow qualified character to pass through.
*/
for (size_t TokCtr = 0; TokCtr < DelimitedTokens.size();TokCtr++)
{
// We are now using U32string because it's guaranteed to be 1 character = 1 element
const auto& tok = VoxUtil::StrToU32(DelimitedTokens[TokCtr]);
std::u32string AppTok = U"";
if (tok.find(U"@") != string::npos)
{
ProcessedTokens.push_back(VoxUtil::U32ToStr(tok));
continue;
}
for (size_t s = 0;s < tok.size();s++)
{
if (AllowedChars.find(tok[s]) != std::u32string::npos)
AppTok += tok[s];
// Punctuation handler
// This time we explicitly add a token to the vector
if (punctuation.find(tok[s]) != std::u32string::npos) {
// First, if the assembled string isn't empty, we add it in its current state
// Otherwise, the SIL could end up appearing before the word.
if (!AppTok.empty()) {
ProcessedTokens.push_back(VoxUtil::U32ToStr(AppTok));
AppTok = U"";
}
if (IsTacotron){
// Double at-symbol is handled later
if (!IsTorchMoji)
AppTok += U"@@";
AppTok += tok[s];
}
else{
AppTok = U"@SIL";
}
ProcessedTokens.push_back(VoxUtil::U32ToStr(AppTok));
AppTok = U"";
continue;
}
}
if (!AppTok.empty())
{
ProcessedTokens.push_back(VoxUtil::U32ToStr(AppTok));
AppTok = U"";
}
}
// Prevent out of range error if the user inputs one word
if (ProcessedTokens.size() > 1)
{
if (ProcessedTokens[ProcessedTokens.size() - 1] == "SIL")
ProcessedTokens.pop_back();
}
return ProcessedTokens;
}