From a0e8f3b7c22e35cbe2b6aeb59ab11196df13d92b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 25 Jun 2023 18:05:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8B=BC=E9=9F=B3=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 2 + util.go | 208 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 211 insertions(+) create mode 100644 util.go diff --git a/go.mod b/go.mod index f37080e..e654957 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 5b898a0..80ce228 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/util.go b/util.go new file mode 100644 index 0000000..8456fdd --- /dev/null +++ b/util.go @@ -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) +}