-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathpinyin.go
55 lines (44 loc) · 947 Bytes
/
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
package pinyin
import (
"regexp"
)
var pyValue_length = len(pyValue)
var hzRegexp = regexp.MustCompile("^[\u4e00-\u9fa5]$")
//get chinese pinyin number code
//param s must be chinese character with utf8 encoding
func Code(s string) int {
gbkString := UTF8ToGBK(s)
var i1, i2 int
i1 = int(gbkString[0])
i2 = int(gbkString[1])
return i1*256 + i2 - 65536
}
// convert chinese to pinyin
func Convert(s string) string {
pyString := ""
var str string
var code int
for _, rune := range s {
str = string(rune)
if hzRegexp.MatchString(str) { //chinese
code = Code(str)
if code > 0 && code < 160 {
pyString += str
} else {
if v, ok := tableMap[code]; ok { //map by table
pyString += v
} else {
for i := (pyValue_length - 1); i >= 0; i-- {
if pyValue[i] <= code {
pyString += pyName[i]
break
}
}
}
}
} else { //other
pyString += str
}
}
return pyString
}