增加拼音处理

This commit is contained in:
白茶清欢 2023-06-25 18:05:43 +08:00
parent 96532aff00
commit a0e8f3b7c2
3 changed files with 211 additions and 0 deletions

1
go.mod
View File

@ -18,6 +18,7 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mozillazg/go-pinyin v0.20.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect

2
go.sum
View File

@ -11,6 +11,8 @@ github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ=
github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
github.com/mssola/user_agent v0.6.0 h1:uwPR4rtWlCHRFyyP9u2KOV0u8iQXmS7Z7feTrstQwk4=
github.com/mssola/user_agent v0.6.0/go.mod h1:TTPno8LPY3wAIEKRpAtkdMT0f8SE24pLRGPahjCH4uw=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=

208
util.go Normal file
View File

@ -0,0 +1,208 @@
// Package util ...
//
// Description : util ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-06-25 17:22
package util
import "github.com/mozillazg/go-pinyin"
// PinYinArg 设置选项方法
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:33 2023/6/25
type PinYinArg func(arg *pinyin.Args)
// 拼音风格的设置
// PinYinStyleNormal ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:37 2023/6/25
func PinYinStyleNormal() PinYinArg {
return func(arg *pinyin.Args) {
arg.Style = pinyin.Normal
}
}
// PinYinStyleToneStand 声调风格1拼音声调在韵母第一个字母上。如 zhōng guó
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:38 2023/6/25
func PinYinStyleToneStand() PinYinArg {
return func(arg *pinyin.Args) {
arg.Style = pinyin.Tone
}
}
// PinYinStyleToneAfterYunMu 声调风格2即拼音声调在各个韵母之后用数字 [1-4] 进行表示。如: zho1ng guo2
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:39 2023/6/25
func PinYinStyleToneAfterYunMu() PinYinArg {
return func(arg *pinyin.Args) {
arg.Style = pinyin.Tone2
}
}
// PinYinStyleToneAtEnding 声调风格3即拼音声调在各个拼音之后用数字 [1-4] 进行表示。如: zhong1 guo2
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:40 2023/6/25
func PinYinStyleToneAtEnding() PinYinArg {
return func(arg *pinyin.Args) {
arg.Style = pinyin.Tone3
}
}
// PinYinStyleShengMu 声母风格,只返回各个拼音的声母部分。如: zh g 。注意:不是所有的拼音都有声母
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:41 2023/6/25
func PinYinStyleShengMu() PinYinArg {
return func(arg *pinyin.Args) {
arg.Style = pinyin.Initials
}
}
// PinYinStyleFirstLetter 首字母风格,只返回拼音的首字母部分。如: z g
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:42 2023/6/25
func PinYinStyleFirstLetter() PinYinArg {
return func(arg *pinyin.Args) {
arg.Style = pinyin.FirstLetter
}
}
// PinYinStyleYunMu 韵母风格,只返回各个拼音的韵母部分,不带声调。如: ong uo
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:43 2023/6/25
func PinYinStyleYunMu() PinYinArg {
return func(arg *pinyin.Args) {
arg.Style = pinyin.Finals
}
}
// PinYinStyleToneYunMu 韵母风格1带声调声调在韵母第一个字母上。如 ōng uó
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:44 2023/6/25
func PinYinStyleToneYunMu() PinYinArg {
return func(arg *pinyin.Args) {
arg.Style = pinyin.FinalsTone
}
}
// PinYinStyleToneYunMuAtStart 韵母风格2带声调声调在各个韵母之后用数字 [1-4] 进行表示。如: o1ng uo2
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:45 2023/6/25
func PinYinStyleToneYunMuAtStart() PinYinArg {
return func(arg *pinyin.Args) {
arg.Style = pinyin.FinalsTone2
}
}
// PinYinStyleToneYunMuAtEnding 韵母风格3带声调声调在各个拼音之后用数字 [1-4] 进行表示。如: ong1 uo2
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:40 2023/6/25
func PinYinStyleToneYunMuAtEnding() PinYinArg {
return func(arg *pinyin.Args) {
arg.Style = pinyin.FinalsTone3
}
}
// 拼音风格设置结束
// PinYinStyleWithSeparator 设置拼音的分隔符
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:59 2023/6/25
func PinYinStyleWithSeparator(separator string) PinYinArg {
return func(arg *pinyin.Args) {
if len(separator) == 0 {
return
}
arg.Separator = separator
}
}
// PinYinStyleWithHeteronym 开启多音字模式
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:03 2023/6/25
func PinYinStyleWithHeteronym() PinYinArg {
return func(arg *pinyin.Args) {
arg.Heteronym = true
}
}
// PinYin 汉字转拼音
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:23 2023/6/25
type PinYin struct {
}
// Convert 汉字生成拼音
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:26 2023/6/25
func (py *PinYin) Convert(text string, argFuncList ...PinYinArg) string {
arg := pinyin.NewArgs()
arg.Separator = " "
for _, argFunc := range argFuncList {
argFunc(&arg)
}
return pinyin.Slug(text, arg)
}
// ConvertSingle 每个字一个读音, 支持多音字
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:02 2023/6/25
func (py *PinYin) ConvertSingle(text string, argFuncList ...PinYinArg) []string {
arg := pinyin.NewArgs()
arg.Separator = " "
for _, argFunc := range argFuncList {
argFunc(&arg)
}
return pinyin.LazyPinyin(text, arg)
}
// ConvertWithHeteronym 多音字模式
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:02 2023/6/25
func (py *PinYin) ConvertWithHeteronym(text string, argFuncList ...PinYinArg) [][]string {
arg := pinyin.NewArgs()
arg.Separator = " "
for _, argFunc := range argFuncList {
argFunc(&arg)
}
return pinyin.Pinyin(text, arg)
}