Go pinyin
Contents
Go 汉字转拼音库
汉语拼音转换工具 Go 版。
1、安装
go get -u github.com/mozillazg/go-pinyin
安装cli工具
go get -u github.com/mozillazg/go-pinyin/cmd/pinyin
$ pinyin 中国人
zhōng guó rén
2、简单使用
package main
import (
"fmt"
"github.com/mozillazg/go-pinyin"
)
func main() {
hans := "中国人"
// 默认
a := pinyin.NewArgs()
fmt.Println(pinyin.Pinyin(hans, a))
// [[zhong] [guo] [ren]]
// 包含声调
a.Style = pinyin.Tone
fmt.Println(pinyin.Pinyin(hans, a))
// [[zhōng] [guó] [rén]]
// 声调用数字表示
a.Style = pinyin.Tone2
fmt.Println(pinyin.Pinyin(hans, a))
// [[zho1ng] [guo2] [re2n]]
// 开启多音字模式
a = pinyin.NewArgs()
a.Heteronym = true
fmt.Println(pinyin.Pinyin(hans, a))
// [[zhong zhong] [guo] [ren]]
a.Style = pinyin.Tone2
fmt.Println(pinyin.Pinyin(hans, a))
// [[zho1ng zho4ng] [guo2] [re2n]]
fmt.Println(pinyin.LazyPinyin(hans, pinyin.NewArgs()))
// [zhong guo ren]
fmt.Println(pinyin.Convert(hans, nil))
// [[zhong] [guo] [ren]]
fmt.Println(pinyin.LazyConvert(hans, nil))
// [zhong guo ren]
}
输出调试
package main
import (
"fmt"
"github.com/mozillazg/go-pinyin"
)
func main() {
hans := "音乐"
// 默认
a := pinyin.NewArgs()
p1 := pinyin.Pinyin(hans, a)
fmt.Println(p1)
// [[zhong] [guo] [ren]]
// 包含声调
a.Style = pinyin.Tone
p2 := pinyin.Pinyin(hans, a)
fmt.Println(p2)
// [[zhōng] [guó] [rén]]
// 声调用数字表示
a.Style = pinyin.Tone2
p3 := pinyin.Pinyin(hans, a)
fmt.Println(p3)
// [[zho1ng] [guo2] [re2n]]
// 开启多音字模式
a = pinyin.NewArgs()
a.Heteronym = true
p4 := pinyin.Pinyin(hans, a)
fmt.Println(p4)
// [[zhong zhong] [guo] [ren]]
a.Style = pinyin.Tone2
p5 := pinyin.Pinyin(hans, a)
fmt.Println(p5)
// [[zho1ng zho4ng] [guo2] [re2n]]
p6 := pinyin.LazyPinyin(hans, pinyin.NewArgs())
fmt.Println(p6)
// [zhong guo ren]
p7 := pinyin.Convert(hans, nil)
fmt.Println(p7)
// [[zhong] [guo] [ren]]
p8 := pinyin.LazyConvert(hans, nil)
fmt.Println(p8)
// [zhong guo ren]
}