jieba-lexicon.cc
10.6 KB
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
// sherpa-onnx/csrc/jieba-lexicon.cc
//
// Copyright (c) 2022-2024 Xiaomi Corporation
#include "sherpa-onnx/csrc/jieba-lexicon.h"
#include <algorithm>
#include <fstream>
#include <regex> // NOLINT
#include <strstream>
#include <unordered_set>
#include <utility>
#if __ANDROID_API__ >= 9
#include "android/asset_manager.h"
#include "android/asset_manager_jni.h"
#endif
#if __OHOS__
#include "rawfile/raw_file_manager.h"
#endif
#include "sherpa-onnx/csrc/file-utils.h"
#include "sherpa-onnx/csrc/jieba.h"
#include "sherpa-onnx/csrc/macros.h"
#include "sherpa-onnx/csrc/onnx-utils.h"
#include "sherpa-onnx/csrc/symbol-table.h"
#include "sherpa-onnx/csrc/text-utils.h"
namespace sherpa_onnx {
static bool IsPunct(const std::string &s) {
static const std::unordered_set<std::string> puncts = {
",", ".", "!", "?", ":", "\"", "'", ",",
"。", "!", "?", "“", "”", "‘", "’",
};
return puncts.count(s);
}
// end is inclusive
static std::string GetWord(const std::vector<std::string> &words, int32_t start,
int32_t end) {
std::string ans;
if (start >= words.size() || end >= words.size()) {
return ans;
}
for (int32_t i = start; i <= end; ++i) {
ans += words[i];
}
return ans;
}
class JiebaLexicon::Impl {
public:
Impl(const std::string &lexicon, const std::string &tokens,
const std::string &dict_dir, bool debug)
: debug_(debug) {
jieba_ = InitJieba(dict_dir);
{
std::ifstream is(tokens);
InitTokens(is);
}
{
std::ifstream is(lexicon);
InitLexicon(is);
}
}
template <typename Manager>
Impl(Manager *mgr, const std::string &lexicon, const std::string &tokens,
const std::string &dict_dir, bool debug)
: debug_(debug) {
jieba_ = InitJieba(dict_dir);
{
auto buf = ReadFile(mgr, tokens);
std::istrstream is(buf.data(), buf.size());
InitTokens(is);
}
{
auto buf = ReadFile(mgr, lexicon);
std::istrstream is(buf.data(), buf.size());
InitLexicon(is);
}
}
std::vector<TokenIDs> ConvertTextToTokenIds(const std::string &text) const {
// see
// https://github.com/Plachtaa/VITS-fast-fine-tuning/blob/main/text/mandarin.py#L244
std::regex punct_re{":|、|;"};
std::string s = std::regex_replace(text, punct_re, ",");
std::regex punct_re2("[.]");
s = std::regex_replace(s, punct_re2, "。");
std::regex punct_re3("[?]");
s = std::regex_replace(s, punct_re3, "?");
std::regex punct_re4("[!]");
s = std::regex_replace(s, punct_re4, "!");
std::vector<std::string> words;
bool is_hmm = true;
jieba_->Cut(text, words, is_hmm);
if (debug_) {
#if __OHOS__
SHERPA_ONNX_LOGE("input text:\n%{public}s", text.c_str());
SHERPA_ONNX_LOGE("after replacing punctuations:\n%{public}s", s.c_str());
#else
SHERPA_ONNX_LOGE("input text:\n%s", text.c_str());
SHERPA_ONNX_LOGE("after replacing punctuations:\n%s", s.c_str());
#endif
std::ostringstream os;
std::string sep = "";
for (const auto &w : words) {
os << sep << w;
sep = "_";
}
#if __OHOS__
SHERPA_ONNX_LOGE("after jieba processing:\n%{public}s", os.str().c_str());
#else
SHERPA_ONNX_LOGE("after jieba processing:\n%s", os.str().c_str());
#endif
}
// remove spaces after punctuations
std::vector<std::string> words2 = std::move(words);
words.reserve(words2.size());
for (int32_t i = 0; i < words2.size(); ++i) {
if (i == 0) {
words.push_back(std::move(words2[i]));
} else if (words2[i] == " ") {
if (words.back() == " " || IsPunct(words.back())) {
continue;
} else {
words.push_back(std::move(words2[i]));
}
} else if (IsPunct(words2[i])) {
if (words.back() == " " || IsPunct(words.back())) {
continue;
} else {
words.push_back(std::move(words2[i]));
}
} else {
words.push_back(std::move(words2[i]));
}
}
if (debug_) {
std::ostringstream os;
std::string sep = "";
for (const auto &w : words) {
os << sep << w;
sep = "_";
}
#if __OHOS__
SHERPA_ONNX_LOGE("after removing spaces after punctuations:\n%{public}s",
os.str().c_str());
#else
SHERPA_ONNX_LOGE("after removing spaces after punctuations:\n%s",
os.str().c_str());
#endif
}
std::vector<TokenIDs> ans;
std::vector<int64_t> this_sentence;
int32_t num_words = static_cast<int32_t>(words.size());
int32_t max_len = 10;
for (int32_t i = 0; i < num_words;) {
int32_t start = i;
int32_t end = std::min(i + max_len, num_words - 1);
std::string w;
while (end > start) {
auto this_word = GetWord(words, start, end);
if (debug_) {
#if __OHOS__
SHERPA_ONNX_LOGE("%{public}d-%{public}d: %{public}s", start, end,
this_word.c_str());
#else
SHERPA_ONNX_LOGE("%d-%d: %s", start, end, this_word.c_str());
#endif
}
if (word2ids_.count(this_word)) {
i = end + 1;
w = std::move(this_word);
if (debug_) {
#if __OHOS__
SHERPA_ONNX_LOGE("matched %{public}d-%{public}d: %{public}s", start,
end, w.c_str());
#else
SHERPA_ONNX_LOGE("matched %d-%d: %s", start, end, w.c_str());
#endif
}
break;
}
end -= 1;
}
if (w.empty()) {
w = words[i];
i += 1;
}
auto ids = ConvertWordToIds(w);
if (ids.empty()) {
#if __OHOS__
SHERPA_ONNX_LOGE("Ignore OOV '%{public}s'", w.c_str());
#else
SHERPA_ONNX_LOGE("Ignore OOV '%s'", w.c_str());
#endif
continue;
}
this_sentence.insert(this_sentence.end(), ids.begin(), ids.end());
if (IsPunct(w)) {
ans.emplace_back(std::move(this_sentence));
this_sentence = {};
}
} // for (const auto &w : words)
if (!this_sentence.empty()) {
ans.emplace_back(std::move(this_sentence));
}
return ans;
}
private:
std::vector<int32_t> ConvertWordToIds(const std::string &w) const {
std::vector<int32_t> ans;
if (word2ids_.count(w)) {
ans = word2ids_.at(w);
} else if (token2id_.count(w)) {
ans = {token2id_.at(w)};
} else {
std::vector<std::string> words = SplitUtf8(w);
for (const auto &word : words) {
if (word2ids_.count(word)) {
auto ids = ConvertWordToIds(word);
ans.insert(ans.end(), ids.begin(), ids.end());
}
}
}
if (debug_) {
std::ostringstream os;
os << w << ": ";
for (auto i : ans) {
os << id2token_.at(i) << " ";
}
os << "\n";
#if __OHOS__
SHERPA_ONNX_LOGE("%{public}s", os.str().c_str());
#else
SHERPA_ONNX_LOGE("%s", os.str().c_str());
#endif
}
return ans;
}
void InitTokens(std::istream &is) {
token2id_ = ReadTokens(is);
std::vector<std::pair<std::string, std::string>> puncts = {
{",", ","}, {".", "。"}, {"!", "!"}, {"?", "?"}, {":", ":"},
{"\"", "“"}, {"\"", "”"}, {"'", "‘"}, {"'", "’"}, {";", ";"},
};
for (const auto &p : puncts) {
if (token2id_.count(p.first) && !token2id_.count(p.second)) {
token2id_[p.second] = token2id_[p.first];
}
if (!token2id_.count(p.first) && token2id_.count(p.second)) {
token2id_[p.first] = token2id_[p.second];
}
}
if (!token2id_.count("、") && token2id_.count(",")) {
token2id_["、"] = token2id_[","];
}
if (!token2id_.count(";") && token2id_.count(",")) {
token2id_[";"] = token2id_[","];
}
if (debug_) {
for (const auto &p : token2id_) {
id2token_[p.second] = p.first;
}
}
}
void InitLexicon(std::istream &is) {
std::string word;
std::vector<std::string> token_list;
std::string line;
std::string phone;
int32_t line_num = 0;
while (std::getline(is, line)) {
++line_num;
std::istringstream iss(line);
token_list.clear();
iss >> word;
ToLowerCase(&word);
if (word2ids_.count(word)) {
#if __OHOS__
SHERPA_ONNX_LOGE(
"Duplicated word: %{public}s at line %{public}d:%{public}s. Ignore "
"it.",
word.c_str(), line_num, line.c_str());
#else
SHERPA_ONNX_LOGE("Duplicated word: %s at line %d:%s. Ignore it.",
word.c_str(), line_num, line.c_str());
#endif
continue;
}
while (iss >> phone) {
token_list.push_back(std::move(phone));
}
std::vector<int32_t> ids = ConvertTokensToIds(token2id_, token_list);
if (ids.empty()) {
#if __OHOS__
SHERPA_ONNX_LOGE("Empty token ids for %{public}s", line.c_str());
#else
SHERPA_ONNX_LOGE("Empty token ids for %s", line.c_str());
#endif
continue;
}
word2ids_.insert({std::move(word), std::move(ids)});
}
}
private:
// lexicon.txt is saved in word2ids_
std::unordered_map<std::string, std::vector<int32_t>> word2ids_;
// tokens.txt is saved in token2id_
std::unordered_map<std::string, int32_t> token2id_;
std::unordered_map<int32_t, std::string> id2token_;
std::unique_ptr<cppjieba::Jieba> jieba_;
bool debug_ = false;
};
JiebaLexicon::~JiebaLexicon() = default;
JiebaLexicon::JiebaLexicon(const std::string &lexicon,
const std::string &tokens,
const std::string &dict_dir, bool debug)
: impl_(std::make_unique<Impl>(lexicon, tokens, dict_dir, debug)) {}
template <typename Manager>
JiebaLexicon::JiebaLexicon(Manager *mgr, const std::string &lexicon,
const std::string &tokens,
const std::string &dict_dir, bool debug)
: impl_(std::make_unique<Impl>(mgr, lexicon, tokens, dict_dir, debug)) {}
std::vector<TokenIDs> JiebaLexicon::ConvertTextToTokenIds(
const std::string &text, const std::string & /*unused_voice = ""*/) const {
return impl_->ConvertTextToTokenIds(text);
}
#if __ANDROID_API__ >= 9
template JiebaLexicon::JiebaLexicon(AAssetManager *mgr,
const std::string &lexicon,
const std::string &tokens,
const std::string &dict_dir, bool debug);
#endif
#if __OHOS__
template JiebaLexicon::JiebaLexicon(NativeResourceManager *mgr,
const std::string &lexicon,
const std::string &tokens,
const std::string &dict_dir, bool debug);
#endif
} // namespace sherpa_onnx