-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathChineseG2p.cpp
More file actions
323 lines (287 loc) · 14.3 KB
/
ChineseG2p.cpp
File metadata and controls
323 lines (287 loc) · 14.3 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
#include <cpp-pinyin/ChineseG2p.h>
#include <cpp-pinyin/G2pglobal.h>
#include "ChineseG2p_p.h"
#include "DictUtil.h"
#include <algorithm>
#include <filesystem>
#include "cpp-pinyin/U16Str.h"
namespace Pinyin
{
static std::vector<std::u16string> splitString(const std::u16string &input) {
std::vector<std::u16string> res;
res.reserve(input.size());
auto start = input.begin();
const auto end = input.end();
while (start != end) {
const auto ¤tChar = *start;
if (Pinyin::isLetter(currentChar)) {
auto letterStart = start;
while (start != end && Pinyin::isLetter(*start)) {
++start;
}
res.emplace_back(letterStart, start);
} else if (Pinyin::isHanzi(currentChar) || Pinyin::isDigit(currentChar) || !Pinyin::isSpace(currentChar)) {
res.emplace_back(1, currentChar);
++start;
} else if (Pinyin::isKana(currentChar)) {
const int length = (start + 1 != end && Pinyin::isSpecialKana(*(start + 1))) ? 2 : 1;
res.emplace_back(start, start + length);
std::advance(start, length);
} else {
++start;
}
}
return res;
}
static std::u16string mid(const std::vector<char16_t> &inputList, const size_t cursor, const size_t length) {
const size_t end = std::min(cursor + length, inputList.size());
return {inputList.begin() + cursor, inputList.begin() + end};
}
// reset pinyin to raw string
static PinyinResVector resetZH(const std::vector<std::u16string> &input, const PinyinResVector &res,
const std::vector<bool> &positions) {
PinyinResVector result;
result.reserve(input.size());
int offset = 0;
for (int i = 0; i < input.size(); ++i) {
const auto &encodeStr = u16strToUtf8str(input[i]);
if (positions[i])
result.emplace_back(PinyinRes{encodeStr, res[i - offset].pinyin,
res[i - offset].candidates, false});
else {
result.emplace_back(PinyinRes{encodeStr, encodeStr, {encodeStr},
true});
offset++;
}
}
return result;
}
// delete elements from the list
template <class T>
static inline void removeElements(std::vector<T> &vector, int start, int n) {
vector.erase(vector.begin() + start, vector.begin() + start + n);
}
ChineseG2pPrivate::ChineseG2pPrivate(std::string language) :
m_language(std::move(language)) {}
ChineseG2pPrivate::~ChineseG2pPrivate() = default;
// load zh convert dict
void ChineseG2pPrivate::init() {
const std::filesystem::path dict_dir = dictionaryPath() / m_language;
initialized = loadDict(dict_dir / "phrases_map.txt", phrases_map) &&
loadDict(dict_dir / "phrases_dict.txt", phrases_dict) &&
loadAdditionalDict(dict_dir / "user_dict.txt", phrases_dict) &&
loadDict(dict_dir / "word.txt", word_dict) &&
loadDict(dict_dir / "trans_word.txt", trans_dict);
toneSeen.reserve(4);
toneCandidates.reserve(4);
}
// get all chinese characters and positions in the list
void ChineseG2pPrivate::zhPosition(const std::vector<std::u16string> &input, std::vector<char16_t> &res,
std::vector<bool> &positions) {
res.reserve(input.size());
for (int i = 0; i < input.size(); ++i) {
const auto &item = input[i][0];
if (!item)
continue;
const auto &simItem = trans_dict.find(item) != trans_dict.end() ? trans_dict[item] : item;
if (word_dict.find(simItem) != word_dict.end()) {
res.emplace_back(simItem);
positions[i] = true;
}
}
}
ChineseG2p::ChineseG2p(const std::string &language) {
d_ptr = std::make_unique<ChineseG2pPrivate>(language);
d_ptr->init();
}
ChineseG2p::~ChineseG2p() = default;
bool ChineseG2p::initialized() const {
return d_ptr->initialized;
}
/*
Style:
陟罚臧否:zhi4 fa2 zang2 pi3
汤汤:shang1 shang1
到了:dao4 le1
*/
bool ChineseG2p::loadUserDict(const std::filesystem::path &filePath) const {
return loadAdditionalDict(filePath / "user_dict.txt", d_ptr->phrases_dict);
}
void ChineseG2p::setToneConverter(const ToneConverter &toneConverter) const {
d_ptr->m_toneConverter = toneConverter;
}
PinyinResVector ChineseG2p::hanziToPinyin(const std::string &hans, int style, Error error, bool candidates,
bool v_to_u, bool neutral_tone_with_five) const {
return hanziToPinyin(splitString(utf8strToU16str(hans)), style, error, candidates, v_to_u,
neutral_tone_with_five);
}
PinyinResVector ChineseG2p::hanziToPinyin(const std::vector<std::string> &hans, int style, Error error,
bool candidates, bool v_to_u, bool neutral_tone_with_five) const {
std::vector<std::u16string> hansList;
hansList.reserve(hans.size());
for (const auto &item : hans) {
hansList.emplace_back(utf8strToU16str(item));
}
return hanziToPinyin(hansList, style, error, candidates, v_to_u, neutral_tone_with_five);
}
PinyinResVector ChineseG2p::hanziToPinyin(const std::vector<std::u16string> &hans, int style, Error error,
bool candidates, bool v_to_u, bool neutral_tone_with_five) const {
std::vector<char16_t> hansList;
std::vector<bool> inputPos(hans.size(), false);
// get char&pos in dict
d_ptr->zhPosition(hans, hansList, inputPos);
return resetZH(hans, hanziToPinyin(hansList, style, error, candidates, v_to_u, neutral_tone_with_five),
inputPos);
}
PinyinResVector ChineseG2p::hanziToPinyin(const std::vector<char16_t> &hansList, int style, Error error,
bool candidates,
bool v_to_u, bool neutral_tone_with_five) const {
PinyinResVector result;
result.reserve(hansList.size());
int cursor = 0;
while (cursor < hansList.size()) {
// get char
const char16_t ¤t_char = hansList[cursor];
// not in dict
if (d_ptr->word_dict.find(current_char) == d_ptr->word_dict.end()) {
result.emplace_back(PinyinRes{u16strToUtf8str(current_char)});
cursor++;
continue;
}
// is polypropylene
if (!d_ptr->isPolyphonic(current_char)) {
const auto &pinyin = d_ptr->getDefaultPinyin(current_char, style, v_to_u, neutral_tone_with_five);
result.emplace_back(PinyinRes{
u16strToUtf8str(current_char),
pinyin[0],
candidates ? pinyin : std::vector<std::string>{},
false
});
cursor++;
} else {
bool found = false;
for (int length = 4; length >= 2 && !found; length--) {
if (cursor + length <= hansList.size()) {
// cursor: 地, subPhrase: 地久天长
const std::u16string subPhrase = mid(hansList, cursor, length);
const auto &it = d_ptr->phrases_dict.find(subPhrase);
if (it != d_ptr->phrases_dict.end()) {
const auto &subRes = d_ptr->toneConvert(it->second, style, v_to_u, neutral_tone_with_five);
for (int i = 0; i < subRes.size(); i++) {
const auto &lyric = subPhrase[i];
result.emplace_back(PinyinRes{
u16strToUtf8str(lyric),
u16strToUtf8str(subRes[i]),
candidates
? d_ptr->getDefaultPinyin(lyric, style, v_to_u, neutral_tone_with_five)
: std::vector<std::string>{},
false
});
}
cursor += length;
found = true;
}
if (cursor >= 1) {
// cursor: 重, subPhrase_1: 语重心长
const std::u16string subPhrase1 = mid(hansList, cursor - 1, length);
const auto &it1 = d_ptr->phrases_dict.find(subPhrase1);
if (it1 != d_ptr->phrases_dict.end()) {
result.pop_back();
const auto &subRes1 = d_ptr->toneConvert(
it1->second, style, v_to_u, neutral_tone_with_five);
for (int i = 0; i < subRes1.size(); i++) {
const auto &lyric = subPhrase1[i];
result.emplace_back(PinyinRes{
u16strToUtf8str(lyric),
u16strToUtf8str(subRes1[i]),
candidates
? d_ptr->getDefaultPinyin(lyric, style, v_to_u, neutral_tone_with_five)
: std::vector<std::string>{},
false
});
}
cursor += length - 1;
found = true;
}
}
}
if (cursor + 1 >= length && cursor + 1 <= hansList.size()) {
// cursor: 好, xSubPhrase: 各有所好
const std::u16string subPhraseBack = mid(hansList, cursor + 1 - length, length);
const auto &it = d_ptr->phrases_dict.find(subPhraseBack);
if (it != d_ptr->phrases_dict.end()) {
// overwrite pinyin
removeElements(result, cursor + 1 - length, length - 1);
const auto &subResBack = d_ptr->toneConvert(it->second, style, v_to_u,
neutral_tone_with_five);
for (int i = 0; i < subResBack.size(); i++) {
const auto &lyric = subPhraseBack[i];
result.emplace_back(PinyinRes{
u16strToUtf8str(lyric),
u16strToUtf8str(subResBack[i]),
candidates
? d_ptr->getDefaultPinyin(lyric, style, v_to_u, neutral_tone_with_five)
: std::vector<std::string>{},
false
});
}
cursor += 1;
found = true;
}
}
if (cursor + 2 >= length && cursor + 2 <= hansList.size()) {
// cursor: 好, xSubPhrase: 叶公好龙
const std::u16string subPhraseBack1 = mid(hansList, cursor + 2 - length, length);
const auto &it = d_ptr->phrases_dict.find(subPhraseBack1);
if (it != d_ptr->phrases_dict.end()) {
// overwrite pinyin
removeElements(result, cursor + 2 - length, length - 2);
const auto &subResBack1 = d_ptr->toneConvert(
it->second, style, v_to_u, neutral_tone_with_five);
for (int i = 0; i < subResBack1.size(); i++) {
const auto &lyric = subPhraseBack1[i];
result.emplace_back(PinyinRes{
u16strToUtf8str(lyric),
u16strToUtf8str(subResBack1[i]),
candidates
? d_ptr->getDefaultPinyin(lyric, style, v_to_u, neutral_tone_with_five)
: std::vector<std::string>{},
false
});
}
cursor += 2;
found = true;
}
}
}
// not found, use default pinyin
if (!found) {
const auto &pinyin = d_ptr->getDefaultPinyin(current_char, style, v_to_u, neutral_tone_with_five);
result.emplace_back(PinyinRes{
u16strToUtf8str(current_char),
pinyin[0],
candidates ? pinyin : std::vector<std::string>{},
false
});
cursor++;
}
}
}
// Alloc 2
if (error == Error::Ignore) {
return result;
}
return result;
}
std::string ChineseG2p::tradToSim(const std::string &oneHanzi) const {
return u16strToUtf8str(d_ptr->tradToSim(utf8strToU16str(oneHanzi)[0]));
}
bool ChineseG2p::isPolyphonic(const std::string &oneHanzi) const {
return d_ptr->isPolyphonic(utf8strToU16str(oneHanzi)[0]);
}
std::vector<std::string> ChineseG2p::getDefaultPinyin(const std::string &oneHanzi, int style, bool v_to_u,
bool neutral_tone_with_five) const {
return d_ptr->getDefaultPinyin(d_ptr->tradToSim(utf8strToU16str(oneHanzi)[0]), style, v_to_u,
neutral_tone_with_five);
}
}