This repository has been archived by the owner on Dec 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate-unicode-han-mappings.cs
405 lines (387 loc) · 10.6 KB
/
generate-unicode-han-mappings.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
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
namespace Mugene
{
public class ChineseUtility
{
public static void Main (string [] args)
{
switch (args [0]) {
case "bopomofo":
PrintBopomofo (args);
break;
case "pinyin":
PrintPinyin (args);
break;
case "readings-bopomofo":
DumpBopomofoReadings (args);
break;
case "traditional":
PrintTraditional (args);
break;
case "readings":
GenerateReadingMap (args);
break;
case "help":
default:
Console.Error.WriteLine (@"
Usage: generate-unicode-han-mappings.exe [command] [args]
Commands and args:
traditional Unicode_Variants.txt - generate Simplified-Traditional mappings
readings Unicode_Readings.txt - generate Hanzi-to-reading mappings
readings-bopomofo Unicode_Readings.txt - generate Hanzi-to-reading mappings in Zhuyin
pinyin Unicode_Readings.txt textfile - translate Chinese to Pinyin
bopomofo Unicode_Readings.txt textfile - translate Chinese to Bopomofo
");
break;
}
}
static void PrintTraditional (string [] args)
{
Console.WriteLine (ConvertToTraditional (args [1], args [2]));
}
static void HanziToPinyin (string [] args)
{
var s = GetPinyin (args);
Console.WriteLine (s);
}
static void PrintBopomofo (string [] args)
{
var bpmf = GetBopomofo (args);
Console.WriteLine (bpmf);
}
static void PrintPinyin (string [] args)
{
var bpmf = GetPinyin (args);
Console.WriteLine (bpmf);
}
static void DumpBopomofoReadings (string [] args)
{
var s = GetReadingMap (args);
var bpmf = PinyinToBopomofo (s);
Console.WriteLine (bpmf);
}
static string GetPinyin (string [] args)
{
var map = GetReadingMap (args).Replace ('\n', ' ').Split (' ');
var ret = File.ReadAllText (args [2]);
return GetPinyin (map, ret);
}
static string GetPinyin (string [] map, string text)
{
var sb = new StringBuilder ();
return string.Join ("", GetPinyinTokenized (map, text).Select (p => p.Key).ToArray ());
}
static IEnumerable<KeyValuePair<string,bool>> GetPinyinTokenized (string [] map, string text)
{
foreach (var c in text) {
bool done = false;
for (int i = 0; i < map.Length; i++) {
if (map [i].Length == 0)
continue; // how come does this happen?
if (map [i] [0] == c) {
yield return new KeyValuePair<string,bool> (map [i].Substring (1), true);
done = true;
break;
}
else if (map [i] [0] > c)
break;
}
if (!done)
// FIXME: super duper inefficient!
yield return new KeyValuePair<string,bool> (c.ToString (), false);
}
}
static string GetBopomofo (string [] args)
{
var map = GetReadingMap (args).Replace ('\n', ' ').Split (' ');
var text = File.ReadAllText (args [2]);
var sb = new StringBuilder ();
var ret = GetPinyinTokenized (map, text);
foreach (var i in ret) {
if (i.Value)
sb.Append (PinyinToBopomofo (i.Key));
else
sb.Append (i.Key);
}
return sb.ToString ();
}
static string PinyinToBopomofo (string pinyin)
{
var sb = new StringBuilder ();
var item = pinyin;
// foreach (var item in ret.Replace ("\n", "").Split (' ')) {
// for (int i = 1; i < item.Length; i++) {
for (int i = 0; i < item.Length; i++) {
char c = item [i];
{
switch (c) {
// consonants
case 'b': sb.Append ('\u3105'); break;
case 'p': sb.Append ('\u3106'); break;
case 'm': sb.Append ('\u3107'); break;
case 'f': sb.Append ('\u3108'); break;
case 'd': sb.Append ('\u3109'); break;
case 't': sb.Append ('\u310A'); break;
case 'n': sb.Append ('\u310B'); break;
case 'l': sb.Append ('\u310C'); break;
case 'g': sb.Append ('\u310D'); break;
case 'k': sb.Append ('\u310E'); break;
case 'h': sb.Append ('\u310F'); break;
case 'j': sb.Append ('\u3110'); break;
case 'q': sb.Append ('\u3111'); break;
case 'x': sb.Append ('\u3112'); break;
case 'r':
if (i < item.Length - 1 && item [i + 1] == 'i')
i++;
sb.Append ('\u3116');
break;
case 'z':
case 'c':
case 's':
if (i < item.Length - 1) {
switch (item [i + 1]) {
case 'h':
switch (item [i]) {
case 'z': sb.Append ('\u3113'); break;
case 'c': sb.Append ('\u3114'); break;
case 's': sb.Append ('\u3115'); break;
}
break;
case 'i':
switch (item [i]) {
case 'z': sb.Append ('\u3117'); break;
case 'c': sb.Append ('\u3118'); break;
case 's': sb.Append ('\u3119'); break;
}
break;
}
i++;
} else {
switch (item [i]) {
case 'z': sb.Append ('\u3117'); break;
case 'c': sb.Append ('\u3118'); break;
case 's': sb.Append ('\u3119'); break;
}
}
break;
// vowels
case 'a':
if (i < item.Length - 1) {
switch (item [i + 1]) {
case 'i': sb.Append ('\u311E'); break;
case 'o': sb.Append ('\u3120'); break;
case 'n':
if (i < item.Length - 2 && item [i + 2] == 'g') {
sb.Append ('\u3124');
i++;
}
else
sb.Append ('\u3122');
break;
default:
sb.Append ('\u311A');
i--;
break;
}
i++;
}
else
sb.Append ('\u311A');
break;
case 'i':
sb.Append ('\u3127');
if (i < item.Length - 1 && item [i + 1] == 'e') {
sb.Append ('\u311D');
i++;
}
if (i < item.Length - 2 && item [i + 1] == 'n' && item [i + 2] == 'g') {
sb.Append ('\u3125');
i++;
i++;
}
break;
// FIXME: it needs to handle 'u' + U+0308 (to represent \u3129)
case 'u': sb.Append ('\u3128'); break;
case 'e':
if (i < item.Length - 1) {
switch (item [i + 1]) {
case 'i': sb.Append ('\u311F'); break;
case 'n':
if (i < item.Length - 2 && item [i + 2] == 'g') {
sb.Append ('\u3125');
i++;
}
else
sb.Append ('\u3123');
break;
case 'r': sb.Append ('\u3126'); break;
case 'h': sb.Append ('\u311D'); break;
default:
sb.Append ('\u311C');
i--;
break;
}
i++;
}
else
sb.Append ('\u311C');
break;
case 'o':
if (i < item.Length - 2 && item [i + 1] == 'n' && item [i + 2] == 'g') {
sb.Append ('\u3128');
sb.Append ('\u3125');
i++;
i++;
} else if (i < item.Length - 1 && item [i + 1] == 'u') {
sb.Append ('\u3121');
i++;
}
else
sb.Append ('\u311B');
break;
case 'w':
if (i < item.Length - 1 && item [i + 1] == 'u')
i++;
sb.Append ('\u3128');
break;
case 'y':
if (i < item.Length - 1) {
switch (item [i + 1]) {
case 'u': sb.Append ('\u3129'); i++; break;
case 'i':
if (i < item.Length - 3 && item [i + 2] == 'n' && item [i + 3] == 'g')
break; // deal with them at 'i'.
sb.Append ('\u3127'); i++; break;
case 'e':
sb.Append ('\u3127');
sb.Append ('\u311D');
i++;
break;
case 'a':
case 'o':
// does not consume those, but valid sequence.
sb.Append ('\u3127');
break;
default: // anything else is unexpected.
throw new InvalidOperationException (item [i + 1].ToString ());
}
}
else
throw new InvalidOperationException ();
break;
default:
sb.Append (c);
break;
}
}
}
return sb.ToString ();
}
static void GenerateReadingMap (string [] args)
{
var ret = GetReadingMap (args);
Console.WriteLine (ret);
Console.WriteLine ("Database size: " + ret.Length);
}
static string GetReadingMap (string [] args)
{
StringBuilder sb = new StringBuilder ();
int n = 0;
foreach (var line in File.ReadAllLines (args [1])) {
var ents = line.Split (null);
if (ents.Length < 3 || ents [1] != "kMandarin")
continue;
AppendHexAsChar (sb, ents [0]);
var nfd = ents [2].Normalize (NormalizationForm.FormD);
int tone = 0, existing = 0;
foreach (var c in nfd) {
switch (c) {
case '\u0300': tone += 4; goto case '\0';
case '\u0301': tone += 2; goto case '\0';
case '\u0304': tone += 1; goto case '\0';
case '\u030C': tone += 3; goto case '\0';
case '\u0308': tone = 8; goto case '\0';
default:
if (c < 'a' || 'z' < c)
Console.Error.WriteLine ("{0:X04} [{1}] [{2}] [{3}]", (int) c, c, ents [0], nfd);
else
sb.Append (c);
break;
tone = -1;
goto case '\0';
case '\0':
if (existing != 0 && existing != 8) // 8 is the only valid combination
Console.Error.WriteLine ("More than one tone: {0}: [{1}]", ents [0], string.Join (" ", nfd.Select (i => ((int) i).ToString ("X04")).ToArray ()));
else if (tone != 0)
existing = tone;
break;
}
}
sb.Append (tone.ToString ());
sb.Append (' ');
if (++n % 16 == 0)
sb.Append ('\n');
}
return sb.ToString ();
}
static string GenerateTraditionalVariantsMap (string variantsFile)
{
StringBuilder sb = new StringBuilder ();
int n = 0;
foreach (var line in File.ReadAllLines (variantsFile)) {
var ents = line.Split (null);
if (ents.Length < 3 || ents [1] != "kTraditionalVariant")
continue;
AppendHexAsChar (sb, ents [0]);
AppendHexAsChar (sb, ents [2]);
if (++n % 16 == 0)
sb.Append ('\n');
}
return sb.ToString ();
}
static void AppendHexAsChar (StringBuilder sb, string s)
{
int x = int.Parse (s.Substring (2), NumberStyles.HexNumber);
if (x < 0x10000)
sb.Append ((char) x);
else
sb.Append ((char) ((x - 0x10000) / 0x400 + 0xD800)).Append ((char) ((x - 0x10000) % 0x400 + 0xDC00));
}
static string ConvertToTraditional (string variantsFile, string textFile)
{
var sb = new StringBuilder ();
var db = GenerateTraditionalVariantsMap (variantsFile);
Console.WriteLine ("Database size: " + db.Length);
char hs = '\0';
int c = 0;
foreach (var ch in File.ReadAllText (textFile)) {
if (char.IsSurrogate (ch)) {
if (hs == '\0')
hs = ch;
else {
int l = 0;
do {
l = db.IndexOf (hs, l, db.Length - l);
if (l < db.Length - 3 && db [l + 1] == ch) {
sb.Append (db [l + 2]).Append (db [l + 3]);
break;
}
} while (true);
if (l < 0)
sb.Append (hs).Append (ch);
}
}
else {
c = db.IndexOf (ch);
sb.Append (c < 0 || c % 2 != 0 ? (char) ch : db [c + 1]);
}
}
return sb.ToString ();
}
}
}