-
Notifications
You must be signed in to change notification settings - Fork 0
/
cedict2csv.go
67 lines (54 loc) · 1.43 KB
/
cedict2csv.go
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
package main
import (
"github.com/hermanschaaf/cedict"
"bufio"
"os"
"encoding/csv"
"flag"
"log"
)
func main () {
cedictFileName := flag.String("cedict", "", "Cedict file to parse")
outputFileName := flag.String("output", "", "Output csv")
flag.Parse()
if (flag.NFlag() != 2) {
flag.PrintDefaults()
return
}
cedictFile, cedictErr := os.Open(*cedictFileName)
checkError("Could not read cedict file", cedictErr)
defer cedictFile.Close()
cedictReader := cedict.New(bufio.NewReader(cedictFile))
csvFile, csvErr := os.Create(*outputFileName)
checkError("Could not create output file", csvErr)
defer csvFile.Close()
writer := csv.NewWriter(csvFile)
defer writer.Flush()
var data = []string{"simplified", "traditional", "pinyin", "pinyin_with_tones", "pinyin_no_tones", "definition1", "definition2"}
err := writer.Write(data)
checkError("Could not write to file", err)
for {
err = cedictReader.NextEntry()
if err != nil {
break
}
entry := cedictReader.Entry()
data = append([]string{
entry.Simplified,
entry.Traditional,
entry.Pinyin,
entry.PinyinWithTones,
entry.PinyinNoTones,
},
entry.Definitions[:]...
)
err = writer.Write(data)
checkError("Could not write to file", err)
}
}
func checkError(message string, err error) {
if err != nil {
log.Fatal(message, err)
panic(err.Error())
}
}