Files
servicebase/pkg/common/pinyin/pinyin.go
2025-11-18 17:48:20 +08:00

149 lines
3.2 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 pinyin
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"unicode/utf8"
)
type vowel int32
var (
ys = []vowel{'ā', 'ē', 'ī', 'ō', 'ū', 'ǖ', 'Ā', 'Ē', 'Ī', 'Ō', 'Ū', 'Ǖ'} // 单韵母 一声
es = []vowel{'á', 'é', 'í', 'ó', 'ú', 'ǘ', 'Á', 'É', 'Í', 'Ó', 'Ú', 'Ǘ'} // 单韵母 二声
ss = []vowel{'ǎ', 'ě', 'ǐ', 'ǒ', 'ǔ', 'ǚ', 'Ǎ', 'Ě', 'Ǐ', 'Ǒ', 'Ǔ', 'Ǚ'} // 单韵母 三声
fs = []vowel{'à', 'è', 'ì', 'ò', 'ù', 'ǜ', 'À', 'È', 'Ì', 'Ò', 'Ù', 'Ǜ'} // 单韵母 四声
ws = []vowel{'a', 'e', 'i', 'o', 'u', 'v', 'A', 'E', 'I', 'O', 'U', 'V'} // 单韵母 无声调
)
var (
pinyinTemp map[vowel]interface{}
toneTemp map[vowel]vowel
)
const (
Tone string = "带声调的拼音" // 带声调的拼音 例如Cào
InitialsInCapitals string = "首字母大写不带声调" // 首字母大写不带声调 例如Cao
None string = "" //如果匹配不到汉字,就靠大家维护下 【匹配失败手动添加代码到pinyin.txt】
)
func LoadingPYFile(filename string) {
f, err := os.Open(filename)
if err != nil {
fmt.Println(err)
}
if err != nil {
panic(err)
}
pinyinTemp = make(map[vowel]interface{}, 0)
toneTemp = make(map[vowel]vowel)
for i, t := range ys {
toneTemp[t] = ws[i]
}
for i, t := range es {
toneTemp[t] = ws[i]
}
for i, t := range ss {
toneTemp[t] = ws[i]
}
for i, t := range fs {
toneTemp[t] = ws[i]
}
defer func() {
_ = f.Close()
}()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
//单行分割取出拼音
str := strings.Split(scanner.Text(), "=>")
if len(str) < 2 {
continue
}
i, err := strconv.ParseInt(str[0], 16, 32)
if err != nil {
continue
}
pinyinTemp[vowel(i)] = str[1]
//fmt.Println(pinyinTemp[vowel(i)])
}
}
func PY(source string) (string, error) {
return PYSplit(source, "", "")
}
func PYSplit(source string, split string, types string) (string, error) {
hz := []vowel(source)
words := make([]string, 0)
for _, s := range hz {
word, err := _vowel(s, types)
if err != nil {
return None, err
}
if len(word) > 0 {
words = append(words, word)
}
}
return strings.Join(words, split), nil
}
func _vowel(source vowel, types string) (string, error) {
switch types {
case Tone:
return getTone(source), nil
case InitialsInCapitals:
return getInitialsInCapitals(source), nil
default:
return getDefault(source), nil
}
}
func getTone(source vowel) string {
if pinyinTemp[source] != nil {
return pinyinTemp[source].(string)
}
return string(source)
}
func getInitialsInCapitals(source vowel) string {
def := getDefault(source)
var result string
if def == "" {
return def
}
str := []vowel(def)
if str[0] > 32 {
str[0] = str[0] - 32
}
for _, v := range str {
result += string(v)
}
return result
}
func getDefault(source vowel) string {
tone := getTone(source)
var result string
if tone == "" {
return None
}
resultLen := make([]vowel, utf8.RuneCountInString(tone))
count := 0
for _, t := range tone {
changes, ok := toneTemp[vowel(t)]
if ok {
resultLen[count] = changes
} else {
resultLen[count] = vowel(t)
}
count++
}
for _, v := range resultLen {
result += string(v)
}
return result
}