-
Notifications
You must be signed in to change notification settings - Fork 54
/
pinyin.go
173 lines (148 loc) · 3.22 KB
/
pinyin.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
package pinyin
import (
"strconv"
"strings"
"unicode/utf8"
)
var (
tones = [][]rune{
{'ā', 'ē', 'ī', 'ō', 'ū', 'ǖ', 'Ā', 'Ē', 'Ī', 'Ō', 'Ū', 'Ǖ'},
{'á', 'é', 'í', 'ó', 'ú', 'ǘ', 'Á', 'É', 'Í', 'Ó', 'Ú', 'Ǘ'},
{'ǎ', 'ě', 'ǐ', 'ǒ', 'ǔ', 'ǚ', 'Ǎ', 'Ě', 'Ǐ', 'Ǒ', 'Ǔ', 'Ǚ'},
{'à', 'è', 'ì', 'ò', 'ù', 'ǜ', 'À', 'È', 'Ì', 'Ò', 'Ù', 'Ǜ'},
}
neutrals = []rune{'a', 'e', 'i', 'o', 'u', 'v', 'A', 'E', 'I', 'O', 'U', 'V'}
)
var (
// 从带声调的声母到对应的英文字符的映射
tonesMap map[rune]rune
// 从汉字到声调的映射
numericTonesMap map[rune]int
// 从汉字到拼音的映射(带声调)
pinyinMap map[rune]string
initialized bool
)
type Mode int
const (
WithoutTone Mode = iota + 1 // 默认模式,例如:guo
Tone // 带声调的拼音 例如:guó
InitialsInCapitals // 首字母大写不带声调,例如:Guo
)
type pinyin struct {
origin string
split string
mode Mode
}
func init() {
tonesMap = make(map[rune]rune)
numericTonesMap = make(map[rune]int)
pinyinMap = make(map[rune]string)
for i, runes := range tones {
for j, tone := range runes {
tonesMap[tone] = neutrals[j]
numericTonesMap[tone] = i + 1
}
}
for k, v := range resource {
i, err := strconv.ParseInt(k, 16, 32)
if err != nil {
continue
}
pinyinMap[rune(i)] = v
}
initialized = true
}
func New(origin string) *pinyin {
return &pinyin{
origin: origin,
split: " ",
mode: WithoutTone,
}
}
func (py *pinyin) Split(split string) *pinyin {
py.split = split
return py
}
func (py *pinyin) Mode(mode Mode) *pinyin {
py.mode = mode
return py
}
func (py *pinyin) Convert() (string, error) {
if !initialized {
return "", ErrInitialize
}
sr := []rune(py.origin)
words := make([]string, 0)
var temp string
for i, s := range sr {
_, ok := pinyinMap[s]
if !ok {
// 非中文处理
temp += string(s)
if i == len(sr)-1 {
words = append(words, temp)
}
continue
}
word, err := getPinyin(s, py.mode)
if err != nil {
return "", err
}
if len(temp) > 0 {
words = append(words, temp)
temp = ""
}
if len(word) > 0 {
words = append(words, word)
}
}
result := strings.Join(words, py.split)
result = strings.Replace(result, " ", " ", -1)
result = strings.Replace(result, " ", " ", -1)
return result, nil
}
func getPinyin(hanzi rune, mode Mode) (string, error) {
if !initialized {
return "", ErrInitialize
}
switch mode {
case Tone:
return getTone(hanzi), nil
case InitialsInCapitals:
return getInitialsInCapitals(hanzi), nil
default:
return getDefault(hanzi), nil
}
}
func getTone(hanzi rune) string {
return pinyinMap[hanzi]
}
func getDefault(hanzi rune) string {
tone := getTone(hanzi)
if tone == "" {
return tone
}
output := make([]rune, utf8.RuneCountInString(tone))
count := 0
for _, t := range tone {
neutral, found := tonesMap[t]
if found {
output[count] = neutral
} else {
output[count] = t
}
count++
}
return string(output)
}
func getInitialsInCapitals(hanzi rune) string {
def := getDefault(hanzi)
if def == "" {
return def
}
sr := []rune(def)
if sr[0] > 32 {
sr[0] = sr[0] - 32
}
return string(sr)
}