1 Star 0 Fork 0

ZhangRui/pinyin

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
pinyin.go 871 Bytes
一键复制 编辑 原始数据 按行查看 历史
ZhangRui 提交于 2020-08-07 17:37 . Initial commit
package pinyin
import (
"strings"
"unicode/utf8"
)
var dict = make(map[string][]string)
type Pinyin struct {
string
FullPinyin []string
Initials []string
}
func init() {
parts := strings.Split(dictData, "|")
for _, i := range parts {
idx := strings.Split(i, ":")
dict[idx[0]] = strings.Split(idx[1], ",")
}
}
func New(s string) *Pinyin {
full, initials := make([]string, utf8.RuneCountInString(s)), make([]string, utf8.RuneCountInString(s))
for _, word := range s {
ph := " "
rn := utf8.RuneLen(rune(word))
if rn > 1 {
for idx, chars := range dict {
for _, char := range chars {
if string(word) == char {
ph = idx
}
}
}
} else if rn == 1 {
ph = string(word)
}
full = append(full, ph)
initials = append(initials, string([]rune(ph)[0]))
}
return &Pinyin{string: s, FullPinyin: full, Initials: initials}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/zhangrui-git/pinyin.git
[email protected]:zhangrui-git/pinyin.git
zhangrui-git
pinyin
pinyin
master

搜索帮助