-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjianfan.go
More file actions
71 lines (63 loc) · 1.71 KB
/
jianfan.go
File metadata and controls
71 lines (63 loc) · 1.71 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
// Package ccst provides Conversion between Simplified and Traditional Chinese-Characters.
//
// This is a simple implementation of Chinese one-to-one conversion.
// If you need more advanced converter, please visit
// OpenCC https://github.com/BYVoid/OpenCC
package ccst
import "fmt"
var t2sMapping = make(map[rune]rune)
var s2tMapping = make(map[rune]rune)
func init() {
if len([]rune(chT)) != len([]rune(chS)) {
panic("cht and chs data length not equal")
}
for index := 0; index < len([]rune(chS)); index++ {
runeValueS := []rune(chS)[index]
runeValueT := []rune(chT)[index]
// if runeValueS == '𬸪' || runeValueS == '𬸪' {
// fmt.Fprintf(os.Stderr, "%c:%c\n", runeValueS, runeValueT)
// continue
// }
t2sMapping[runeValueT] = runeValueS
// Simplified => Traditional is one to many, pick first
if _, ok := s2tMapping[runeValueS]; !ok {
s2tMapping[runeValueS] = runeValueT
}
}
}
// T2S converts Traditional Chinese to Simplified Chinese
func T2S(s string) string {
var chs []rune
for _, runeValue := range s {
if v, ok := t2sMapping[runeValue]; ok {
chs = append(chs, v)
} else {
chs = append(chs, runeValue)
}
}
return string(chs)
}
// S2T converts Simplified Chinese to Traditional Chinese
func S2T(s string) string {
var cht []rune
for _, runeValue := range s {
if v, ok := s2tMapping[runeValue]; ok {
cht = append(cht, v)
} else {
cht = append(cht, runeValue)
}
}
return string(cht)
}
// STDump dumps Simplified to Traditional Chinese dict
func STDump() {
for kk, vv := range s2tMapping {
fmt.Printf("%c:%c\n", kk, vv)
}
}
// TSDump dumps Traditional to Simplified Chinese dict
func TSDump() {
for kk, vv := range t2sMapping {
fmt.Printf("%c:%c\n", vv, kk)
}
}