-
Notifications
You must be signed in to change notification settings - Fork 4
/
zhuyin.go
512 lines (449 loc) · 12.2 KB
/
zhuyin.go
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// Copyright (c) 2013 Localvar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package zhuyin
var (
// map of Pinyin to Zhuyin
map_p2z = map[string]string{
// Consonant
"b": "ㄅ", "p": "ㄆ", "m": "ㄇ", "f": "ㄈ",
"d": "ㄉ", "t": "ㄊ", "n": "ㄋ", "l": "ㄌ",
"g": "ㄍ", "k": "ㄎ", "h": "ㄏ",
"j": "ㄐ", "q": "ㄑ", "x": "ㄒ",
"zh": "ㄓ", "ch": "ㄔ", "sh": "ㄕ", "r": "ㄖ",
"z": "ㄗ", "c": "ㄘ", "s": "ㄙ",
"y": "ㄧ", "w": "ㄨ",
// Rhymes
"a": "ㄚ", "o": "ㄛ", "e": "ㄜ",
"i": "ㄧ", "u": "ㄨ", "v": "ㄩ",
"ai": "ㄞ", "ei": "ㄟ", "ui": "ㄨㄟ",
"ao": "ㄠ", "ou": "ㄡ", "iu": "ㄧㄡ",
"an": "ㄢ", "en": "ㄣ", "in": "ㄧㄣ",
"ang": "ㄤ", "eng": "ㄥ", "ing": "ㄧㄥ",
"ong": "ㄨㄥ", "ie": "ㄧㄝ", "er": "ㄦ",
"ue": "ㄩㄝ", "ve": "ㄩㄝ", // 'ue' is same as 've', for typo
"un": "ㄨㄣ", "vn": "ㄩㄣ", "ia": "ㄧㄚ",
"ua": "ㄨㄚ", "uan": "ㄨㄢ", "van": "ㄩㄢ",
"uai": "ㄨㄞ", "uo": "ㄨㄛ", "iong": "ㄩㄥ",
"iang": "ㄧㄤ", "uang": "ㄨㄤ", "ian": "ㄧㄢ",
"iao": "ㄧㄠ",
}
// map of Zhuyin to Pinyin
map_z2p = map[string]string{
// Consonant
"ㄅ": "b", "ㄆ": "p", "ㄇ": "m", "ㄈ": "f",
"ㄉ": "d", "ㄊ": "t", "ㄋ": "n", "ㄌ": "l",
"ㄍ": "g", "ㄎ": "k", "ㄏ": "h",
"ㄐ": "j", "ㄑ": "q", "ㄒ": "x",
"ㄓ": "zh", "ㄔ": "ch", "ㄕ": "sh", "ㄖ": "r",
"ㄗ": "z", "ㄘ": "c", "ㄙ": "s",
// Rhymes
"ㄚ": "a", "ㄛ": "o", "ㄜ": "e", "ㄝ": "e",
"ㄧ": "i", "ㄨ": "u", "ㄩ": "v",
"ㄞ": "ai", "ㄟ": "ei", "ㄦ": "er",
"ㄠ": "ao", "ㄡ": "ou",
"ㄢ": "an", "ㄣ": "en",
"ㄤ": "ang", "ㄥ": "eng",
"ㄨㄥ": "ong", "ㄧㄝ": "ie",
"ㄧㄡ": "iu", "ㄧㄣ": "in", "ㄧㄥ": "ing",
"ㄩㄝ": "ve",
"ㄨㄣ": "un", "ㄩㄣ": "vn", "ㄧㄚ": "ia",
"ㄨㄚ": "ua", "ㄨㄢ": "uan", "ㄩㄢ": "van",
"ㄨㄞ": "uai", "ㄨㄛ": "uo", "ㄩㄥ": "iong",
"ㄧㄤ": "iang", "ㄨㄤ": "uang", "ㄧㄢ": "ian",
"ㄧㄠ": "iao", "ㄨㄟ": "ui",
// 'y' and 'w' is not included because 'ㄧ' and 'ㄨ' are already
// mapped to 'i' and 'u'
// "ㄧ": "y", "ㄨ": "w",
}
// tonal marks for Pinyin
pinyinTones = [6][5]rune{
{'a', 'ā', 'á', 'ǎ', 'à'},
{'o', 'ō', 'ó', 'ǒ', 'ò'},
{'e', 'ē', 'é', 'ě', 'è'},
{'i', 'ī', 'í', 'ǐ', 'ì'},
{'u', 'ū', 'ú', 'ǔ', 'ù'},
{'ü', 'ǖ', 'ǘ', 'ǚ', 'ǜ'},
}
// tonal marks for Zhuyin
zhuyinTones = [5]string{"˙", "", "ˊ", "ˇ", "ˋ"}
)
// getTonalMark returns the tonal marks for char 'c' of 'tone' in Pinyin
func getTonalMark(c byte, tone byte) string {
r := rune(c)
if r == 'v' {
r = 'ü'
}
for _, t := range pinyinTones {
if r == t[0] {
return string(t[tone])
}
}
panic("IMPOSSIBLE: should not run to here.")
}
// toneRhymes returns the toned rhymes in Pinyin
func toneRhymes(s string, tone byte) string {
// if only one character, tone this character
if len(s) == 1 {
return getTonalMark(s[0], tone)
}
a, b := s[0], s[1]
// Tone the 1st character, if:
// * the first character is 'a'
// * there's no 'a' and the 1st character is 'o' or 'e'
// * the 2nd character is not rhymes
if a == 'a' || ((a == 'o' || a == 'e') && b != 'a') || !isRhymes(b) {
return getTonalMark(a, tone) + s[1:]
}
// tone the 2nd character otherwise
return string(a) + getTonalMark(b, tone) + s[2:]
}
// return true if the input character is rhymes, otherwise return false
func isRhymes(b byte) bool {
return b == 'a' || b == 'e' || b == 'i' || b == 'o' || b == 'u' || b == 'v'
}
// return true if the input character is consonant, otherwise return false
func isConsonant(b byte) bool {
return b >= 'a' && b <= 'z' && !isRhymes(b)
}
// split the input string into consonant, rhymes and tone
// for example: 'zhang1' will be split to consonant 'zh', rhymes 'ang' and
// tone '1'
// returns an empty rhymes in case an error
func split(s string) (string, string, byte) {
i := 0
for ; i < len(s); i++ {
c := s[i]
if !isConsonant(c) {
break
}
}
consonant := s[:i]
for ; i < len(s); i++ {
c := s[i]
if c < 'a' || c > 'z' {
break
}
}
rhymes := s[len(consonant):i]
// rhymes could not be empty, and the length of tone is at most 1
if len(rhymes) == 0 || len(s)-i > 2 {
return "", "", 0
}
var tone byte
if i < len(s) {
tone = s[i] - '0'
if tone < 0 || tone > 4 {
return "", "", 0
}
}
return consonant, rhymes, tone
}
// encodePinyin encode the input consonant, rhymes and tone into Pinyin
// for example: encodePinyin("zh", "ang", 1) outputs 'zhāng'
// return an empty string in case an error
func encodePinyin(consonant string, rhymes string, tone byte) string {
// rhymes could not be empty and the maximum value of tone is 4
if len(rhymes) == 0 || tone > 4 {
return ""
}
if len(consonant) > 0 {
// is it an valid consonant?
if !isConsonant(consonant[0]) {
return ""
}
if _, ok := map_p2z[consonant]; !ok {
return ""
}
// convert rhymes 'ü' to 'u' if consonant is 'j', 'q', 'x' or 'y'
if rhymes[0] == 'v' {
c := consonant[0]
if c == 'j' || c == 'q' || c == 'x' || c == 'y' {
rhymes = "u" + rhymes[1:]
}
}
}
// is it an valid rhymes?
if !isRhymes(rhymes[0]) {
return ""
}
if _, ok := map_p2z[rhymes]; !ok {
return ""
}
// tone the rhymes and convert 'v' to 'ü'
rhymes = toneRhymes(rhymes, tone)
if rhymes[0] == 'v' {
rhymes = "ü" + rhymes[1:]
}
return consonant + rhymes
}
// EncodePinyin encode the input string into Pinyin
// for example: EncodePinyin("zhang1") outputs 'zhāng'
// return an empty string in case an error
func EncodePinyin(s string) string {
// the special case
if s == "e5" {
return "ê"
}
return encodePinyin(split(s))
}
// decodeRhymes decode the input string into rhymes and tone
// for example: decodeRhymes("āng") outputs 'ang' and 1
// returns an empty rhymes in case an error
func decodeRhymes(s string) (string, byte) {
var tone byte
var rhymes string
for _, ch := range s {
for _, t := range pinyinTones {
for j := 1; j < len(t); j++ {
if ch == t[j] {
ch = t[0]
if tone > 0 {
return "", 0
}
tone = byte(j)
}
}
}
if ch == 'ü' {
ch = 'v'
}
rhymes = rhymes + string(ch)
}
return rhymes, tone
}
// decodePinyin decode the input string into consonant, rhymes and tone
// for example: decodePinyin("zhāng") outputs 'zh','ang' and 1
// return an empty rhymes in case an error
func decodePinyin(s string) (string, string, byte) {
var consonant, rhymes string
// split the input into consonant and rhymes(toned)
for i := 0; i < len(s); i++ {
c := s[i]
if !isConsonant(c) {
consonant, rhymes = s[:i], s[i:]
break
}
}
// is it an valid consonant?
if len(consonant) > 0 {
if _, ok := map_p2z[consonant]; !ok {
return "", "", 0
}
}
// decode the toned rhymes into rhymes and tone
rhymes, tone := decodeRhymes(rhymes)
if len(rhymes) == 0 {
return "", "", 0
}
// convert 'u' to 'v' if consonant is 'j', 'q', 'x' or 'y'
if len(consonant) > 0 && rhymes[0] == 'u' {
c := consonant[0]
if c == 'j' || c == 'q' || c == 'x' || c == 'y' {
rhymes = "v" + rhymes[1:]
}
}
// is it an valid rhymes?
if _, ok := map_p2z[rhymes]; !ok {
return "", "", 0
}
return consonant, rhymes, tone
}
// DecodePinyin decode the input Pinyin
// for example: DecodePinyin("zhāng") outputs 'zhang1'
// return an empty string in case an error
func DecodePinyin(s string) string {
if s == "ê" {
return "e5"
}
consonant, rhymes, tone := decodePinyin(s)
if len(rhymes) == 0 {
return ""
}
return consonant + rhymes + string(tone+'0')
}
// encodePinyin encode the input consonant, rhymes and tone into Zhuyin
// for example: encodePinyin("m", "in", 2) outputs 'ㄇㄧㄣˊ'
// return an empty string in case an error
func encodeZhuyin(consonant string, rhymes string, tone byte) string {
if len(rhymes) == 0 {
return ""
}
if rhymes[0] == 'u' && len(consonant) > 0 {
c := consonant[0]
if c == 'j' || c == 'q' || c == 'x' || c == 'y' {
rhymes = "v" + rhymes[1:]
}
}
// the special cases for '整体认读'
if rhymes == "i" {
switch consonant {
case "zh", "ch", "sh", "r", "z", "c", "s", "y":
rhymes = ""
}
} else if consonant == "w" {
if rhymes == "u" {
consonant = ""
}
} else if consonant == "y" {
switch rhymes {
case "e", "ong":
rhymes = "i" + rhymes
fallthrough
case "v", "ve", "in", "van", "ing", "vn":
consonant = ""
}
}
// consonant must be valid
if len(consonant) > 0 {
if l, ok := map_p2z[consonant]; ok {
consonant = l
} else {
return ""
}
}
// rhymes must be valid
if len(rhymes) > 0 {
if l, ok := map_p2z[rhymes]; ok {
rhymes = l
} else {
return ""
}
}
return consonant + rhymes + zhuyinTones[tone]
}
// encodeZhuyin encode the input string into Zhuyin
// for example: encodeZhuyin("min2") outputs 'ㄇㄧㄣˊ'
// return an empty string in case an error
func EncodeZhuyin(s string) string {
if s == "e5" {
return "ㄝ"
}
return encodeZhuyin(split(s))
}
// decodeZhuyin decode the input string into consonant, rhymes and tone
// for example: decodeZhuyin("ㄇㄧㄣˊ") outputs 'm','in' and 2
// return an empty rhymes in case an error
func decodeZhuyin(s string) (string, string, byte) {
var consonant, rhymes string
var tone byte = 1
// split input into consonant, rhymes and tone
split_input:
for i, ch := range s {
// if the character is consonant or rhymes
if v, ok := map_z2p[string(ch)]; ok {
// if it is the 1st character and it is consonant
if i == 0 && isConsonant(v[0]) {
consonant = v
} else {
// add it to rhymes, note, rhymes is still Zhuyin
rhymes = rhymes + string(ch)
}
continue
}
// both consonant and rhymes is empty?
if i == 0 {
return "", "", 0
}
// try to find the tone
st := s[i:]
for j, t := range zhuyinTones {
if st == t {
tone = byte(j)
break split_input
}
}
return "", "", 0
}
if len(rhymes) == 0 {
// if it is '整体认读', the rhymes should be 'i'
switch consonant {
case "zh", "ch", "sh", "r", "z", "c", "s":
rhymes = "i"
}
// rhymes will be empty if not '整体认读',
// this is an error case, will be handled outside
return consonant, rhymes, tone
}
// is it an valid rhymes?
rhymes, ok := map_z2p[rhymes]
if (!ok) || (!isRhymes(rhymes[0])) {
return "", "", 0
}
if len(consonant) == 0 {
// first: check if it is '整体认读',
// second: remove leading 'u' and set consonant to 'w', or
// remove leading 'i' and set consonant to 'y',
// last: check for the very special case,
// 'ong' need to be converted to 'weng'
if rhymes == "i" || rhymes == "v" || rhymes == "e" ||
rhymes == "ve" || rhymes == "in" || rhymes == "van" ||
rhymes == "ing" || rhymes == "vn" {
consonant = "y"
} else if rhymes == "u" {
consonant = "w"
} else if rhymes[0] == 'u' {
consonant = "w"
rhymes = rhymes[1:]
} else if rhymes[0] == 'i' {
consonant = "y"
rhymes = rhymes[1:]
} else if rhymes == "ong" {
consonant = "w"
rhymes = "eng"
}
}
return consonant, rhymes, tone
}
// DecodePinyin decode the input Zhuyin
// for example: DecodeZhuyin("ㄇㄧㄣˊ") outputs 'min2'
// return an empty string in case an error
func DecodeZhuyin(s string) string {
if s == "ㄝ" {
return "e5"
}
consonant, rhymes, tone := decodeZhuyin(s)
if len(rhymes) == 0 {
return ""
}
return consonant + rhymes + string(tone+'0')
}
// PinyinToZhuyin converts the input Pinyin to Zhuyin
// for example: zhāng --> ㄓㄤ
func PinyinToZhuyin(s string) string {
if s == "ê" {
return "ㄝ"
}
return encodeZhuyin(decodePinyin(s))
}
// ZhuyinToPinyin converts the input Zhuyin to Pinyin
// for example: ㄓㄤ --> zhāng
func ZhuyinToPinyin(s string) string {
if s == "ㄝ" {
return "ê"
}
return encodePinyin(decodeZhuyin(s))
}
/*
type EncodeFunc func(p string) string
func encode(s string, fn EncodeFunc) []string {
var result []string
start := 0
for i, c := range s {
if c == ' ' || c == '\t' {
if start < i {
result = append(result, fn(s[start:i]))
}
start = i + 1
} else if c >= '0' && c <= '5' {
result = append(result, fn(s[start:i+1]))
start = i + 1
} else if c < 'a' || c > 'z' {
}
}
if start < len(s) {
result = append(result, fn(s[start:]))
}
return result
}
*/