File: gen_pinyin_dict.go

package info (click to toggle)
golang-github-mozillazg-go-pinyin 0.20.0%2Bds.1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,024 kB
  • sloc: python: 452; makefile: 57; sh: 12
file content (85 lines) | stat: -rw-r--r-- 1,611 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main

import (
	"bufio"
	"flag"
	"fmt"
	"io"
	"os"
	"strings"
)

type cmdArgs struct {
	inputFile  string
	outputFile string
}

func genCode(inFile *os.File, outFile *os.File) {
	rd := bufio.NewReader(inFile)
	output := `package pinyin

// PinyinDict is data map
// 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)
		}
		if strings.HasPrefix(line, "#") {
			continue
		}

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

	genCode(inFp, outFp)
}