首页 > 其他分享 >Golang 中文转拼音

Golang 中文转拼音

时间:2023-06-20 23:55:43浏览次数:41  
标签:---- 中文 拼音 fmt ng Golang dict Println

翻遍整个 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-08-08,原文链接:https://imlht.com/archives/159/

标签:----,中文,拼音,fmt,ng,Golang,dict,Println
From: https://www.cnblogs.com/lofanmi/p/17495194.html

相关文章

  • Golang之map
    Golang之Map什么是Golang中的Map?在Golang编程语言中,Map(映射)是一种关联容器,用于存储键值对。它提供了一种非常高效的方式来存储和检索数据。Map是一种无序的数据结构,其中的键(key)是唯一的,而值(value)则可以重复。通过使用键来访问和修改值,我们可以在Map中快速地查找和更新数据。Gola......
  • 解决方案 | xpdf4.04支持中文pdf识别与转换(pdf转txt)
    一、下载地址按照参考链接将已经打包好,下载链接如下:https://www.123pan.com/s/9Rn9-eEQpH.html提取码:6666二、使用方法win+r输入cmd,使用cd切换到,pdftotext.exe所在文件夹....xpdf-tools-win-4.04\xpdf-tools-win-4.04\bin64按照如下命令即可完成转换。pdftotextinput.p......
  • 西门子828D 840Dsl数控程序PLC西门子数控程序中文注释,详细解释介绍。
    西门子828D840Dsl数控程序PLC西门子数控程序中文注释,详细解释介绍。对于维修人员,或者想学习PLC编程的工程师,初学者。西门子828D和840Dsl是西门子公司生产的数控系统,用于控制机床的运动和加工过程。这些系统具有强大的功能和灵活性,适用于各种工业领域。PLC(可编程逻辑控制器)是一种......
  • Palera1n越狱简体中文,iOS15.0~16.5越狱支持Mac/Linux/U盘多平台
    Palera1n越狱简体中文,iOS15.0~16.5越狱支持Mac/Linux/U盘多平台中文教程:https://dkxuanye.cn/?p=6813官方文档:https://github.com/palera1n/palera1n/releases......
  • A Practical Methodology, HSM, Handler,Service,Model, for Golang Backend Developm
    EverybodyisfamiliarwiththewidelyadoptedMVC(Model-View-Controller)pattern,whichhasbeenusedformanyyearsacrossvariouslanguagesandframeworks.MVChasproventobeapracticalpatternfororganizingprogramswithuserinterfacesandmultip......
  • gjson - Golang 解析 JSON
    文章目录简介主要类型TypeResult方法gjsonresultPath修饰符示例介绍自定义备用简介Github地址go安装:goget-ugithub.com/tidwall/gjson主要类型Type说明说明:解析的数据类型(实际是int类型)功能:用于解析和输出时做判断包括:-True-False-String-JSON-Number......
  • Golang - kafka 的使用
    producerpackagemainimport( "fmt" "github.com/Shopify/sarama" "log" "strconv")const( BROKER="ip:port" TOPIC="xx")//sendMsg发送到kfkfuncsendMsg(clientsarama.SyncProducer,ms......
  • Golang - net/http 笔记
    Serverpackagemainimport( "fmt" "log" "net/http")//模拟实现Handler接口typeBarstruct{}func(bBar)ServeHTTP(whttp.ResponseWriter,req*http.Request){ tgt:=req.URL.Query().Get("name") fmt.Fprintf(w,......
  • Golang - 日志
    官方Log包方法输出到logger.out:log.Print(),log.Printf(),log.Println()输出到logger.out,再执行os.Exit(1):log.Fatal(),log.Fatalln(),log.Fatalf()输出到logger.out,再执行panic():log.Panic(),log.Panicln(),log.Panicf()logger结构体typeLoggerstruc......
  • Golang - Structs 包的使用
    packagemain////主要用于struct转map//还可以判断结构体是否有空属性等功能//import( "fmt" "github.com/fatih/structs")//struct-->maptypeStustruct{ Namestring Ageint}funcmain(){ //创建一个Age属性为空的struct实例 u1:=Stu{......