util/util.go

209 lines
5.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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)
}