-
Notifications
You must be signed in to change notification settings - Fork 0
/
radicals.go
87 lines (80 loc) · 1.49 KB
/
radicals.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package radicals
import (
"bufio"
"os"
"strconv"
"strings"
)
type Radical struct {
StrokeCount int
Kanji []string
}
type RadkfileParser map[string]Radical
type Kanji struct {
Radicals []string
}
type KradfileParser map[string]Kanji
func ParseRadkfile(filename string) (RadkfileParser, error) {
r := RadkfileParser{}
radkfile, err := os.Open(filename)
if err != nil {
return r, err
}
defer radkfile.Close()
scanner := bufio.NewScanner(radkfile)
var rad Radical
var cur = ""
var strokes = 0
for scanner.Scan() {
t := scanner.Text()
switch t[0] {
case '#':
continue
case '$':
s := strings.Split(t, " ")
cur = s[1]
strokes, err = strconv.Atoi(s[2])
if err != nil {
return r, err
}
rad.StrokeCount = strokes
r[cur] = rad
default:
s := strings.Split(t, "")
rad := r[cur]
k := r[cur].Kanji
k = append(r[cur].Kanji, s...)
rad.Kanji = k
r[cur] = rad
}
}
if err = scanner.Err(); err != nil {
return r, err
}
return r, err
}
func ParseKradfile(filename string) (KradfileParser, error) {
k := KradfileParser{}
kradfile, err := os.Open(filename)
if err != nil {
return k, err
}
defer kradfile.Close()
scanner := bufio.NewScanner(kradfile)
for scanner.Scan() {
t := scanner.Text()
switch t[0] {
case '#':
continue
default:
s := strings.Split(t, " : ")
kanji := Kanji{}
kanji.Radicals = strings.Split(s[1], " ")
k[s[0]] = kanji
}
}
if err = scanner.Err(); err != nil {
return k, err
}
return k, err
}