-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (83 loc) · 2.22 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
package main
import (
"flag"
"fmt"
"os"
"github.com/antonyho/go-congkit/engine"
)
var (
version int
simplified bool
easy bool
prediction bool
db string
)
const (
DefaultDB = "congkit.db"
DefaultCongkitVersion = engine.CongkitV5
)
const (
HelpUsage = "Print usages"
VersionUsage = "Congkit version(3/5)"
SimplifiedUsage = "Output simplified Chinese word"
EasyIMUsage = "Use 'Easy' input method"
PredicationUsage = "Predict the possible typing word"
DBUsage = "Custom database file path"
)
func init() {
flag.IntVar(&version, "version", int(DefaultCongkitVersion), VersionUsage)
flag.IntVar(&version, "v", int(DefaultCongkitVersion), VersionUsage)
flag.BoolVar(&simplified, "simplified", false, SimplifiedUsage)
flag.BoolVar(&simplified, "s", false, SimplifiedUsage)
flag.BoolVar(&easy, "easy", false, EasyIMUsage)
flag.BoolVar(&easy, "e", false, EasyIMUsage)
flag.BoolVar(&prediction, "prediction", false, PredicationUsage)
flag.BoolVar(&prediction, "p", false, PredicationUsage)
flag.StringVar(&db, "database", DefaultDB, DBUsage)
flag.StringVar(&db, "d", DefaultDB, DBUsage)
flag.BoolFunc("help", HelpUsage, helpFunc)
flag.BoolFunc("h", HelpUsage, helpFunc)
}
func main() {
flag.Parse()
if flag.NArg() < 1 {
fmt.Printf("%s [congkit_radicals]\n\n", os.Args[0])
flag.Usage()
os.Exit(0)
}
options := []engine.Option{engine.WithDatabase(db)}
switch engine.CongkitVersion(version) {
case engine.CongkitV3:
options = append(options, engine.WithCongkitV3())
case engine.CongkitV5:
options = append(options, engine.WithCongkitV5())
default:
options = append(options, engine.WithCongkitV5())
}
if simplified {
options = append(options, engine.WithSimplified())
}
if easy {
options = append(options, engine.WithEasy())
}
if prediction {
options = append(options, engine.WithPrediction())
}
eng := engine.New(options...)
defer eng.Close()
result, err := eng.Encode(flag.Arg(0))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resultStrings := make([]string, len(result))
for i, r := range result {
resultStrings[i] = string(r)
}
fmt.Println(resultStrings)
}
func helpFunc(_ string) error {
flag.Usage()
os.Exit(0)
return nil
}