改为使用来自 pinyin-data 的拼音数据

This commit is contained in:
mozillazg
2016-03-12 00:09:44 +08:00
parent 67be434e7f
commit c347d8403b
6 changed files with 41318 additions and 76169 deletions
+3
View File
@@ -0,0 +1,3 @@
[submodule "tools/pinyin-data"]
path = tools/pinyin-data
url = https://github.com/mozillazg/pinyin-data.git
+5
View File
@@ -1,6 +1,11 @@
# Changelog
## 0.5.0 (2016-mm-dd)
* **CHANGE** 改为使用来自 [pinyin-data](https://github.com/mozillazg/pinyin-data) 的拼音数据。
## 0.4.0 (2016-01-29)
* **NEW** `Args` 结构体新增 field: `Fallback func(r rune, a Args) []string`
+12
View File
@@ -0,0 +1,12 @@
help:
@echo "test run test"
@echo "gen_pinyin_dict gen pinyin dict"
.PHONY: test
test:
@echo "run test"
@go test -v -cover
.PHONY: gen_pinyin_dict
gen_pinyin_dict:
@go run tools/gen_pinyin_dict.go tools/pinyin-data/pinyin.txt pinyin_dict.go
+41217 -76169
View File
File diff suppressed because it is too large Load Diff
+80
View File
@@ -0,0 +1,80 @@
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"strings"
)
type cmdArgs struct {
inputFile string
outputFile string
}
func gen_code(inFile *os.File, outFile *os.File) {
rd := bufio.NewReader(inFile)
output := `package pinyin
// Warning: Auto-generated file, don't edit.
var PinyinDict = map[int]string{
`
lines := []string{}
for {
line, err := rd.ReadString('\n')
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
// line: `U+4E2D: zhōng,zhòng # 中`
dataSlice := strings.Split(line, " #")
dataSlice = strings.Split(dataSlice[0], ": ")
// 0x4E2D
hexCode := strings.Replace(dataSlice[0], "U+", "0x", 1)
// zhōng,zhòng
pinyin := dataSlice[1]
lines = append(lines, fmt.Sprintf("\t%s: \"%s\",", hexCode, pinyin))
}
output += strings.Join(lines, "\n")
output += "\n}\n"
outFile.WriteString(output)
return
}
func parseCmdArgs() cmdArgs {
flag.Parse()
inputFile := flag.Arg(0)
outputFile := flag.Arg(1)
return cmdArgs{inputFile, outputFile}
}
func main() {
args := parseCmdArgs()
usage := "gen_pinyin_dict INPUT OUTPUT"
inputFile := args.inputFile
outputFile := args.outputFile
if inputFile == "" || outputFile == "" {
fmt.Println(usage)
os.Exit(1)
}
inFp, err := os.Open(inputFile)
if err != nil {
fmt.Printf("open file %s error", inputFile)
panic(err)
}
outFp, err := os.Create(outputFile)
if err != nil {
fmt.Printf("open file %s error", outputFile)
panic(err)
}
defer inFp.Close()
defer outFp.Close()
gen_code(inFp, outFp)
}
+1
Submodule tools/pinyin-data added at 01d866e543