Golang 中文转拼音

发布时间 2023-06-22 16:07:17作者: 仁扬

翻遍整个 GitHub , Golang 中文转拼音类库, 怎么就这么难找呢? 于是我造了一个轮子: 中文转拼音类库. 目前来说应该是最好用的了.

GitHub 传送门: https://github.com/Lofanmi/pinyin-golang

如果说基于汉字拼音字典, 逐个汉字替换, 也是可以转换的, 但是碰到多音字就很麻烦了. 而基于词库, 最起码可以解决大多数的多音字的转换, 人名姓氏的转换.

最开始我用了 CC-CEDICT 词典, 基于词组的长度, 以及英文释义的丰富程度, 来决定替换的优先级, 词组越长优先替换, 英文解释越丰富, 说明它越常用, 拥有更高的优先级, 后来发现它很多汉字都没有收录, 更别说生僻字了.

现在我把 安正超 的 PHP 开源项目 overtrue/pinyin 中的词库搬过来, 整理成一个 []string 放在 go 文件里面, 978K , 编译完也不需要依赖词库了, 非常符合 Go 的气质.

当然也不能说它可以解决 100% 的转换, 多多少少肯定会有瑕疵, 但是问题不大, 完善好词库, 对付一般的转换是绝对没问题的. 如果想完全解决, 词库定会无比庞大...

用法很简单, 接口都很清晰, 不再赘述.

INSTALL

go get -u -v github.com/Lofanmi/pinyin-golang/pinyin

DEMO

package main

import (
	"fmt"

	"github.com/Lofanmi/pinyin-golang/pinyin"
)

func main() {
	dict := pinyin.NewDict()

	// ----
	// 简单用法
	// ----

	// Redis shì yí gè Key-Value cún chǔ xì tǒng.
	str := `Redis 是一个 Key-Value 存储系统。`
	fmt.Println(dict.Sentence(str).Unicode())

	s := ""

	// wǒ hé shí néng bào fù
	s = dict.Convert(`我,何時能暴富?`, " ").Unicode()
	fmt.Println(s)
	// wǒ, hé shí néng bào fù?
	s = dict.Sentence(`我,何時能暴富?`).Unicode()
	fmt.Println(s)

	// ----
	// 转换接口: Dict.Convert
	// ----

	// 输入繁体中文, 输出为带 空格 分隔的拼音字符串
	// ASCII 格式显示
	// wo3 he2 shi2 neng2 bao4 fu4
	s = dict.Convert(`我,何時能暴富?`, " ").ASCII()
	fmt.Println(s)

	// 输入简体中文, 输出为带 连字符- 分隔的拼音字符串
	// Unicode 格式显示
	// wǒ-hé-shí-néng-bào-fù
	s = dict.Convert(`我,何时能暴富?`, "-").Unicode()
	fmt.Println(s)

	// 转换简体中文和繁体中文, 转换为带 斜杆/ 分隔的拼音字符串
	// 不显示声调
	// wo/he/shi/neng/bao/fu
	s = dict.Convert(`我,何时能暴富?`, "/").None()
	fmt.Println(s)

	// ----
	// 句子接口: Dict.Sentence
	// ----

	// 输入繁体中文, 输出为带 空格 分隔的拼音字符串
	// ASCII 格式显示
	// wo3, he2 shi2 neng2 bao4 fu4?
	s = dict.Sentence(`我,何時能暴富?`).ASCII()
	fmt.Println(s)

	// 输入简体中文, 输出为带 空格 分隔的拼音字符串
	// Unicode 格式显示
	// wǒ, hé shí néng bào fù?
	s = dict.Sentence(`我,何时能暴富?`).Unicode()
	fmt.Println(s)

	// 转换简体中文和繁体中文, 转换为带 空格 分隔的拼音字符串
	// 不显示声调
	// wo, he shi neng bao fu?
	s = dict.Sentence(`我,何时能暴富?`).None()
	fmt.Println(s)

	// ----
	// 转换人名: Dict.Name
	// ----

	// 输入繁体中文, 输出为带 空格 分隔的人名拼音字符串
	// ASCII 格式显示
	// mo4 qi2 wo4 xi3 huan1 chi1 suan1 nai3
	s = dict.Name(`万俟沃喜欢吃酸奶`, " ").ASCII()
	fmt.Println(s)

	// 输入简体中文, 输出为带 连字符- 分隔的人名拼音字符串
	// Unicode 格式显示
	// mò-qí-wò-xǐ-huan-chī-suān-nǎi
	s = dict.Name(`万俟沃喜欢吃酸奶`, "-").Unicode()
	fmt.Println(s)

	// 转换简体中文和繁体中文, 转换为带 斜杆/ 分隔的人名拼音字符串
	// 不显示声调
	// mo/qi/wo/xi/huan/chi/suan/nai
	s = dict.Name(`万俟沃喜欢吃酸奶`, "/").None()
	fmt.Println(s)

	// ----
	// 转换拼音简写: Dict.Abbr
	// ----

	// 转换简体中文和繁体中文, 输出为带 连字符- 分隔的拼音字符串首字符
	// m-q-w-x-h-c-s-n
	s = dict.Abbr(`万俟沃喜欢吃酸奶`, "-")
	fmt.Println(s)

	// ----
	// 转换为字符串 slice: ToSlice
	// ----
	// wo3 he2 shi2 neng2 bao4 fu4
	s = dict.Convert(`我,何時能暴富?`, " ").ASCII()
	fmt.Println(s)

	// [wo3 he2 shi2 neng2 bao4 fu4]
	fmt.Printf("%v", pinyin.ToSlice(s))

	// $ go run main.go
	// Redis shì yí gè Key-Value cún chǔ xì tǒng.
	// wǒ hé shí néng bào fù
	// wǒ, hé shí néng bào fù?
	// wo3 he2 shi2 neng2 bao4 fu4
	// wǒ-hé-shí-néng-bào-fù
	// wo/he/shi/neng/bao/fu
	// wo3, he2 shi2 neng2 bao4 fu4?
	// wǒ, hé shí néng bào fù?
	// wo, he shi neng bao fu?
	// mo4 qi2 wo4 xi3 huan1 chi1 suan1 nai3
	// mò-qí-wò-xǐ-huān-chī-suān-nǎi
	// mo/qi/wo/xi/huan/chi/suan/nai
	// m-q-w-x-h-c-s-n
	// wo3 he2 shi2 neng2 bao4 fu4
	// [wo3 he2 shi2 neng2 bao4 fu4]
}

Contribution

欢迎提意见及完善词库

License

MIT


文章来源于本人博客,发布于 2018-06-02,原文链接:https://imlht.com/archives/159/