-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
124 lines (109 loc) · 2.45 KB
/
main.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
)
type Radicals map[string][]string
type Sounds map[string]string
func loadRadicals(file string) (Radicals, error) {
r := Radicals{}
b, err := ioutil.ReadFile(file)
if err != nil {
return r, err
}
err = json.Unmarshal(b, &r)
return r, err
}
func loadSounds(file string) (Sounds, error) {
s := Sounds{}
b, err := ioutil.ReadFile(file)
if err != nil {
return s, err
}
err = json.Unmarshal(b, &s)
return s, err
}
func main() {
// characters => radicals
r, err := loadRadicals("radicals.json")
if err != nil {
panic(err)
}
// pinyin => characters
s, err := loadSounds("sounds.json")
if err != nil {
panic(err)
}
// radical => characters
radicalLookup := map[string]string{}
for ch, rads := range r {
for _, rad := range rads {
if !strings.Contains(radicalLookup[rad], ch) {
radicalLookup[rad] += ch
}
}
}
radicals := []string{}
usedRads := map[string]string{} // character => radicals used in filter
possibleChars := ""
i := 0
for {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter pinyin: ")
text, _ := reader.ReadString('\n')
text = strings.Trim(text, "\n")
if chars, ok := s[text]; ok {
radicals = append(radicals, "")
for _, c := range chars {
for _, rads := range r[string(c)] {
for _, rad := range rads {
if !strings.Contains(strings.Join(radicals, ""), string(rad)) {
radicals[i] += string(rad)
}
if i == 0 {
allPossibleChars := radicalLookup[string(rad)]
possibleChars += allPossibleChars
for _, apc := range allPossibleChars {
usedRads[string(apc)] = string(rad)
}
}
}
}
}
// reduce number of possibilities
if i >= 1 {
remainingChars := ""
for _, c := range possibleChars {
foundAny := false
// get radicals for possible character
chRads := r[string(c)]
filter:
for _, rad := range radicals[i] {
// see if character has radical in latest searched list of radicals
for _, cr := range chRads {
if cr == string(rad) && !strings.Contains(usedRads[string(c)], string(rad)) {
foundAny = true
usedRads[string(c)] = string(rad)
break filter
}
}
}
if foundAny {
remainingChars += string(c)
}
}
possibleChars = remainingChars
}
fmt.Println(radicals)
fmt.Println(possibleChars)
i++
}
if text == "" {
break
}
}
}