Go将中文转换为拼音
最近在学习和研究Go,因此在项目上完成的功能点都想着用Go实现,以促进学习。Go将中文转换为拼音比java方便一些的是不必操心非中文字符,但二者有个通用的问题是需要自己解决多音字姓氏问题。
// Go
Maple
// An highlighted block
/*
程序目的:从文本文件中得到中文后转换为拼音
程序版本:go1.14
程序编写:枫叶
编写时间:2020年3月6日
*/
package ConvertPinYin
import (
"bufio"
"bytes"
"fmt"
"github.com/chain-zhang/pinyin"
"log"
"os"
"strings"
"time"
)
//对外接口方法
func GetFile(fileName string){
fetchChineseWord(fileName)
}
//读取文件函数并且记录转换的内容
func fetchChineseWord(fileName string) error{
file,err := os.Open(fileName)
//构造记录文件名,将转换结果写入文件
timeFile := time.Now().Format("20060102150405")
headFile := "PinYin"
tailFile := ".txt"
var bt bytes.Buffer
bt.WriteString(headFile)
bt.WriteString(timeFile)
bt.WriteString(tailFile)
recordFile := bt.String()
bt.Reset() //重置缓冲区
//构造记录文件名,将转换结果写入文件
if _, err := os.Stat(recordFile); os.IsNotExist(err) {
cf,err := os.Create(recordFile)
if err != nil {
fmt.Println("文件创建失败。")
}else {
fmt.Println("创建文件:",cf.Name())
}
closeFile(*cf)
}
wf,err := os.OpenFile(recordFile, os.O_RDWR|os.O_APPEND, 0666)
w := bufio.NewWriter(wf)
fmt.Println("准备将转换记录写入",wf.Name(),"文件...")
counter :=0
if err != nil{
log.Printf("无法打开文件,请检查: err: [%v]", err)
//defer closeFile(*file)
return err
}else{
lines := bufio.NewScanner(file)
fmt.Println("正在写入",wf.Name(),"文件...")
for lines.Scan(){
counter ++
line := lines.Text()
fmt.Println(line,"|GuangXi",toPinYin(line))
bt.WriteString(line)
bt.WriteString("|")
bt.WriteString("GuangXi")
bt.WriteString(toPinYin(line))
bt.WriteString("\n")
content := bt.String()
_,err2 := w.WriteString(content)
if err2 != nil {
return err2
}
//w.WriteString("\n")
w.Flush()
//wf.Sync()
bt.Reset() //重置缓冲区
}
fmt.Println("共读取",counter,"记录")
fmt.Println("本次转换记已经写入",recordFile,"文件")
}
closeFile(*file)
closeFile(*wf)
return nil
}
//关闭文件
func closeFile(f os.File){
if(&f != nil){
f.Close()
fmt.Println("已经关闭",f.Name(),"文件...")
}
}
//中文转拼音函数
func toPinYin(words string) string{
//fmt.Println("words",words)
var name string
flag := 0
for _,n :=range words {
flag ++
//fmt.Println("n:",strings.Replace(string(n)," ","",-1))
if (strings.Compare(strings.Replace(string(n)," ","",-1),"覃") == 0) && (flag == 1) {
name += firstNamePin(string(n))
flag = 0
}else if (strings.Compare(strings.Replace(string(n)," ","",-1),"区") == 0) && (flag == 1) {
name += firstNamePin(string(n))
flag = 0
}else if (strings.Compare(strings.Replace(string(n)," ","",-1),"牟") == 0) && (flag == 1) {
name += firstNamePin(string(n))
flag = 0
}else if (strings.Compare(strings.Replace(string(n)," ","",-1),"单") == 0) && (flag == 1) {
name += firstNamePin(string(n))
flag = 0
}else{
str,err := pinyin.New(string(n)).Split("").Mode(pinyin.InitialsInCapitals).Convert()
if err != nil {
fmt.Println("转换拼音异常...")
}else{
name +=str
}
}
}
return name
}
//多音字解决方法,尤其是姓氏
func firstNamePin(name string) string{
var n string
//fmt.Println("name:",name)
n = ""
if strings.Compare(name,"覃") == 0 {
n = "Qin"
}else if strings.Compare(name,"区") == 0{
n = "Ou"
}else if strings.Compare(name,"牟") == 0{
n = "Mu"
}else if strings.Compare(name,"单") == 0{
n = "Shan"
}
return n
}
/*
程序目的:将中文转换成拼音
程序版本:go1.14
程序编写:枫叶
编写时间:2020年3月6日
*/
package main
import (
"GoTest/ConvertPinYin"
"fmt"
"os"
"time"
)
func main(){
fmt.Println("----------中文转换为拼音程序----------")
currentPwd, _ := os.Getwd()
fmt.Println("当前工作路径:",currentPwd)
fileName := "user.txt"
fmt.Println("读取文件名为:",fileName)
//定义开始时间
startTime := time.Now()
ConvertPinYin.GetFile(fileName)
//定义结束时间
finishTime := time.Now()
fmt.Println("本次运行时间为:",finishTime.Sub(startTime),"秒")
}