-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathconvert.go
92 lines (86 loc) · 1.91 KB
/
convert.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
package pinyingo
import (
"strings"
"unicode/utf8"
)
type options struct {
style int
segment bool
heteronym bool
}
func (this *options) perStr(pinyinStrs string) string {
switch this.style {
case STYLE_INITIALS:
for i := 0; i < len(INITIALS); i++ {
if strings.Index(pinyinStrs, INITIALS[i]) == 0 {
return INITIALS[i]
}
}
return ""
case STYLE_TONE:
ret := strings.Split(pinyinStrs, ",")
return ret[0]
case STYLE_NORMAL:
ret := strings.Split(pinyinStrs, ",")
return normalStr(ret[0])
case STYLE_FIRST_LETTER:
ret := strings.Split(pinyinStrs, ",")
return firstLetter(ret[0])
}
return ""
}
func (this *options) doConvert(strs string) []string {
//获取字符串的长度
bytes := []byte(strs)
pinyinArr := make([]string, 0)
nohans := ""
var tempStr string
var single string
for len(bytes) > 0 {
r, w := utf8.DecodeRune(bytes)
bytes = bytes[w:]
single = get(int(r))
// 中文字符判断
tempStr = string(r)
if len(single) == 0 {
nohans += tempStr
} else {
if len(nohans) > 0 {
pinyinArr = append(pinyinArr, nohans)
nohans = ""
}
pinyinArr = append(pinyinArr, this.perStr(single))
}
}
//处理末尾非中文的字符串
if len(nohans) > 0 {
pinyinArr = append(pinyinArr, nohans)
}
return pinyinArr
}
func (this *options) Convert(strs string) []string {
retArr := make([]string, 0)
if this.segment {
jiebaed := jieba.Cut(strs, use_hmm)
for _, item := range jiebaed {
mapValuesStr, exist := phrasesDict[item]
mapValuesArr := strings.Split(mapValuesStr, ",")
if exist {
for _, v := range mapValuesArr {
retArr = append(retArr, this.perStr(v))
}
} else {
converted := this.doConvert(item)
for _, v := range converted {
retArr = append(retArr, v)
}
}
}
} else {
retArr = this.doConvert(strs)
}
return retArr
}
func NewPy(style int, segment bool) *options {
return &options{style, segment, false}
}