-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpy.go
61 lines (46 loc) · 968 Bytes
/
py.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
package py
import (
"strings"
"golang.org/x/exp/slices"
)
type Option int
const (
Normal Option = iota
Tone
NoTone
Init
)
func Py(str, sep string, options ...Option) string {
return strings.Join(Pinyin(str, options...), sep)
}
func Pinyin(str string, options ...Option) []string {
runes := []rune(str)
ret := make([]string, len(runes))
style := Normal
if len(options) > 0 {
style = options[0]
}
for i, han := range runes {
ret[i] = RuneOption(han, style, i, runes)[0]
}
return ret
}
func Pinyins(str string, options ...Option) [][]string {
runes := []rune(str)
ret := make([][]string, len(runes))
style := Normal
if len(options) > 0 {
style = options[0]
}
for i, han := range runes {
ret[i] = RuneOption(han, style, i, runes)
}
return ret
}
func Initials(str string) string {
return Py(str, "", Init)
}
func ToneIndex(tone string) int {
// return slices.Index(tones, FromStyle(tone))
return slices.Index(tones, tone)
}