-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathZhConverter.cs
377 lines (326 loc) · 12.7 KB
/
ZhConverter.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
namespace OpenCCNET
{
public static partial class ZhConverter
{
/// <summary>
/// 是否启用并行处理
/// </summary>
public static bool IsParallelEnabled { get; set; } = false;
/// <summary>
/// 初始化
/// </summary>
/// <param name="dictionaryDirectory">字典文件夹路径</param>
/// <param name="jiebaResourceDirectory">Jieba.NET资源路径</param>
/// <param name="isParallelEnabled">是否启用并行处理。默认为 false</param>
public static void Initialize(string dictionaryDirectory = "Dictionary",
string jiebaResourceDirectory = "JiebaResource",
bool isParallelEnabled = false)
{
ZhSegment.Initialize(jiebaResourceDirectory);
ZhDictionary.Initialize(dictionaryDirectory);
IsParallelEnabled = isParallelEnabled;
}
#region 简体中文
/// <summary>
/// 简体中文=>繁体中文(OpenCC标准)
/// </summary>
public static string HansToHant(string text)
{
var phrases = ZhSegment.Segment(text);
return phrases.ConvertBy(ZhDictionary.STPhrases, ZhDictionary.STCharacters)
.Join();
}
/// <summary>
/// 简体中文=>繁体中文(OpenCC标准)
/// </summary>
public static string ToHantFromHans(this string text)
{
return HansToHant(text);
}
/// <summary>
/// 简体中文=>繁体中文(台湾)
/// </summary>
/// <param name="isIdiomConvert">是否转换地区词汇</param>
public static string HansToTW(string text, bool isIdiomConvert = false)
{
var phrases = ZhSegment.Segment(text);
return phrases.ConvertBy(ZhDictionary.STPhrases, ZhDictionary.STCharacters)
.ConvertBy(isIdiomConvert ? ZhDictionary.TWPhrases : null)
.ConvertBy(ZhDictionary.TWVariants)
.Join();
}
/// <summary>
/// 简体中文=>繁体中文(台湾)
/// </summary>
/// <param name="isIdiomConvert">是否转换地区词汇</param>
public static string ToTWFromHans(this string text, bool isIdiomConvert = false)
{
return HansToTW(text, isIdiomConvert);
}
/// <summary>
/// 简体中文=>繁体中文(香港)
/// </summary>
public static string HansToHK(string text)
{
var phrases = ZhSegment.Segment(text);
return phrases.ConvertBy(ZhDictionary.STPhrases, ZhDictionary.STCharacters)
.ConvertBy(ZhDictionary.HKVariants)
.Join();
}
/// <summary>
/// 简体中文=>繁体中文(香港)
/// </summary>
public static string ToHKFromHans(this string text)
{
return HansToHK(text);
}
#endregion
#region 繁体中文(OpenCC标准)
/// <summary>
/// 繁体中文(OpenCC标准)=>简体中文
/// </summary>
public static string HantToHans(string text)
{
var phrases = ZhSegment.Segment(text);
return phrases.ConvertBy(ZhDictionary.TSPhrases, ZhDictionary.TSCharacters)
.Join();
}
/// <summary>
/// 繁体中文(OpenCC标准)=>简体中文
/// </summary>
public static string ToHansFromHant(this string text)
{
return HantToHans(text);
}
/// <summary>
/// 繁体中文(OpenCC标准)=>繁体中文(台湾)
/// </summary>
/// <param name="isIdiomConvert">是否转换地区词汇</param>
public static string HantToTW(string text, bool isIdiomConvert = false)
{
var phrases = ZhSegment.Segment(text);
return phrases.ConvertBy(isIdiomConvert ? ZhDictionary.TWPhrases : null)
.ConvertBy(ZhDictionary.TWVariants)
.Join();
}
/// <summary>
/// 繁体中文(OpenCC标准)=>繁体中文(台湾)
/// </summary>
/// <param name="isIdiomConvert">是否转换地区词汇</param>
public static string ToTWFromHant(this string text, bool isIdiomConvert = false)
{
return HantToTW(text, isIdiomConvert);
}
/// <summary>
/// 繁体中文(OpenCC标准)=>繁体中文(香港)
/// </summary>
public static string HantToHK(string text)
{
var phrases = ZhSegment.Segment(text);
return phrases.ConvertBy(ZhDictionary.HKVariants)
.Join();
}
/// <summary>
/// 繁体中文(OpenCC标准)=>繁体中文(香港)
/// </summary>
public static string ToHKFromHant(this string text)
{
return HantToHK(text);
}
#endregion
#region 繁体中文(台湾)
/// <summary>
/// 繁体中文(台湾)=>繁体中文(OpenCC标准)
/// </summary>
/// <param name="isIdiomConvert">是否转换地区词汇</param>
public static string TWToHant(string text, bool isIdiomConvert = false)
{
var phrases = ZhSegment.Segment(text);
return phrases.ConvertBy(ZhDictionary.TWVariantsRev, ZhDictionary.TWVariantsRevPhrases)
.ConvertBy(isIdiomConvert ? ZhDictionary.TWPhrasesRev : null)
.Join();
}
/// <summary>
/// 繁体中文(台湾)=>繁体中文(OpenCC标准)
/// </summary>
/// <param name="isIdiomConvert">是否转换地区词汇</param>
public static string ToHantFromTW(this string text, bool isIdiomConvert = false)
{
return TWToHant(text, isIdiomConvert);
}
/// <summary>
/// 繁体中文(台湾)=>简体中文
/// </summary>
/// <param name="isIdiomConvert">是否转换地区词汇</param>
public static string TWToHans(string text, bool isIdiomConvert = false)
{
var phrases = ZhSegment.Segment(text);
return phrases.ConvertBy(ZhDictionary.TWVariantsRev, ZhDictionary.TWVariantsRevPhrases)
.ConvertBy(isIdiomConvert ? ZhDictionary.TWPhrasesRev : null)
.ConvertBy(ZhDictionary.TSPhrases, ZhDictionary.TSCharacters)
.Join();
}
/// <summary>
/// 繁体中文(台湾)=>简体中文
/// </summary>
/// <param name="isIdiomConvert">是否转换地区词汇</param>
public static string ToHansFromTW(this string text, bool isIdiomConvert = false)
{
return TWToHans(text, isIdiomConvert);
}
#endregion
#region 繁体中文(香港)
/// <summary>
/// 繁体中文(香港)=>繁体中文(OpenCC标准)
/// </summary>
public static string HKToHant(string text)
{
var phrases = ZhSegment.Segment(text);
return phrases.ConvertBy(ZhDictionary.HKVariantsRevPhrases, ZhDictionary.HKVariantsRev)
.Join();
}
/// <summary>
/// 繁体中文(香港)=>繁体中文(OpenCC标准)
/// </summary>
public static string ToHantFromHK(this string text)
{
return HKToHant(text);
}
/// <summary>
/// 繁体中文(香港)=>简体中文
/// </summary>
public static string HKToHans(string text)
{
var phrases = ZhSegment.Segment(text);
return phrases.ConvertBy(ZhDictionary.HKVariantsRevPhrases, ZhDictionary.HKVariantsRev)
.ConvertBy(ZhDictionary.TSPhrases, ZhDictionary.TSCharacters)
.Join();
}
/// <summary>
/// 繁体中文(香港)=>简体中文
/// </summary>
public static string ToHansFromHK(this string text)
{
return HKToHans(text);
}
#endregion
#region 日语汉字
/// <summary>
/// 日语(旧字体)=>日语(新字体)
/// </summary>
public static string KyuuToShin(string text)
{
var phrases = ZhSegment.Segment(text);
return phrases.ConvertBy(ZhDictionary.JPVariants)
.Join();
}
/// <summary>
/// 日语(旧字体)=>日语(新字体)
/// </summary>
public static string ToShinFromKyuu(this string text)
{
return KyuuToShin(text);
}
/// <summary>
/// 日语(新字体)=>日语(旧字体)
/// </summary>
public static string ShinToKyuu(string text)
{
var phrases = ZhSegment.Segment(text);
return phrases.ConvertBy(ZhDictionary.JPShinjitaiPhrases, ZhDictionary.JPShinjitaiCharacters,
ZhDictionary.JPVariantsRev)
.Join();
}
/// <summary>
/// 日语(新字体)=>日语(旧字体)
/// </summary>
public static string ToKyuuFromShin(this string text)
{
return ShinToKyuu(text);
}
#endregion
#region 扩展方法
/// <summary>
/// 利用字典进行转换
/// </summary>
/// <param name="phrases">词组</param>
/// <param name="dictionaries">字典</param>
/// <returns></returns>
private static IEnumerable<string> ConvertBy(this IEnumerable<string> phrases,
params IDictionary<string, string>[] dictionaries)
{
if (phrases == null)
{
throw new ArgumentNullException(nameof(phrases));
}
if (dictionaries is not { Length: > 0 })
{
return phrases;
}
var phrasesList = phrases is IList<string> tempList ? tempList : phrases.ToList();
// 是否使用并行处理
if (IsParallelEnabled)
{
return phrasesList.AsParallel()
.AsOrdered()
.Select(phrase => ConvertPhrase(phrase, dictionaries))
.ToList();
}
else
{
return phrasesList.Select(phrase => ConvertPhrase(phrase, dictionaries)).ToList();
}
}
private static string ConvertPhrase(string phrase, params IDictionary<string, string>[] dictionaries)
{
if (string.IsNullOrEmpty(phrase))
{
return phrase;
}
// 整词转换
foreach (var dictionary in dictionaries)
{
if (dictionary != null && dictionary.TryGetValue(phrase, out var converted))
{
return converted;
}
}
// 逐字转换(考虑多字节字符)
var textElements = StringInfo.GetTextElementEnumerator(phrase);
var resultBuilder = new StringBuilder(phrase.Length * 2);
var hasConversion = false;
while (textElements.MoveNext())
{
var textElement = textElements.GetTextElement();
var convertedElement = textElement;
foreach (var dictionary in dictionaries)
{
if (dictionary != null && dictionary.TryGetValue(textElement, out var converted))
{
convertedElement = converted;
hasConversion = true;
break;
}
}
resultBuilder.Append(convertedElement);
}
return hasConversion ? resultBuilder.ToString() : phrase;
}
/// <summary>
/// 合并字符串组
/// </summary>
/// <param name="values">字符串组</param>
/// <param name="separator">间隔符号,默认为无间隔</param>
/// <returns></returns>
private static string Join(this IEnumerable<string> values, string separator = "")
{
return string.Join(separator, values);
}
#endregion
}
}