-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
cmd.go
168 lines (153 loc) · 4.06 KB
/
cmd.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package lattice
import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/ikawaha/kagome-dict/dict"
"github.com/ikawaha/kagome-dict/ipa"
"github.com/ikawaha/kagome-dict/uni"
"github.com/ikawaha/kagome/v2/tokenizer"
)
// subcommand property
var (
CommandName = "lattice"
Description = `lattice viewer`
UsageMessage = "%s [-userDict userdic_file] [-dict (ipa|uni)] [-mode (normal|search|extended)] [-output output_file] [-v] sentence\n"
ErrorWriter = os.Stderr
)
// options
type option struct {
userDict string
dict string
mode string
output string
verbose bool
input string
flagSet *flag.FlagSet
}
// ContinueOnError ErrorHandling // Return a descriptive error.
// ExitOnError // Call os.Exit(2).
// PanicOnError // Call panic with a descriptive error.flag.ContinueOnError
func newOption(_ io.Writer, eh flag.ErrorHandling) (o *option) {
o = &option{
flagSet: flag.NewFlagSet(CommandName, eh),
}
// option settings
o.flagSet.StringVar(&o.userDict, "userDict", "", "user dict")
o.flagSet.StringVar(&o.dict, "dict", "ipa", "dict type (ipa|uni)")
o.flagSet.StringVar(&o.mode, "mode", "normal", "tokenize mode (normal|search|extended)")
o.flagSet.StringVar(&o.output, "output", "", "output file")
o.flagSet.BoolVar(&o.verbose, "v", false, "verbose mode")
return
}
func (o *option) parse(args []string) error {
if err := o.flagSet.Parse(args); err != nil {
return err
}
// validations
if o.flagSet.NArg() == 0 {
return fmt.Errorf("input is empty")
}
if o.dict != "" && o.dict != "ipa" && o.dict != "uni" {
return fmt.Errorf("invalid argument: -dict %v", o.dict)
}
if o.mode != "" && o.mode != "normal" && o.mode != "search" && o.mode != "extended" {
return fmt.Errorf("invalid argument: -mode %v", o.mode)
}
o.input = strings.Join(o.flagSet.Args(), " ")
return nil
}
// OptionCheck receives a slice of args and returns an error if it was not successfully parsed
func OptionCheck(args []string) error {
opt := newOption(ioutil.Discard, flag.ContinueOnError)
if err := opt.parse(args); err != nil {
return fmt.Errorf("%v, %v", CommandName, err)
}
return nil
}
func selectDict(name string) (*dict.Dict, error) {
switch name {
case "ipa":
return ipa.Dict(), nil
case "uni":
return uni.Dict(), nil
}
return nil, fmt.Errorf("unknown name type, %v", name)
}
func selectMode(mode string) tokenizer.TokenizeMode {
switch mode {
case "normal":
return tokenizer.Normal
case "search":
return tokenizer.Search
case "extended":
return tokenizer.Extended
}
return tokenizer.Normal
}
// command main
func command(opt *option) error {
d, err := selectDict(opt.dict)
if err != nil {
return err
}
udict := tokenizer.Nop()
if opt.userDict != "" {
d, err := dict.NewUserDict(opt.userDict)
if err != nil {
return err
}
udict = tokenizer.UserDict(d)
}
t, err := tokenizer.New(d, udict)
if err != nil {
return err
}
out := os.Stdout
if opt.output != "" {
var err error
out, err = os.OpenFile(opt.output, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0o666)
if err != nil {
return err
}
defer func() {
_ = out.Close()
}()
}
mode := selectMode(opt.mode)
tokens := t.AnalyzeGraph(out, opt.input, mode)
if opt.verbose {
for i, size := 1, len(tokens); i < size; i++ {
tok := tokens[i]
f := tok.Features()
if tok.Class == tokenizer.DUMMY {
fmt.Fprintf(ErrorWriter, "%s\n", tok.Surface)
} else {
fmt.Fprintf(ErrorWriter, "%s\t%v\n", tok.Surface, strings.Join(f, ","))
}
}
}
return nil
}
// Run receives the slice of args and executes the lattice tool
func Run(args []string) error {
opt := newOption(ErrorWriter, flag.ExitOnError)
if err := opt.parse(args); err != nil {
Usage()
PrintDefaults(flag.ExitOnError)
return fmt.Errorf("%v, %v", CommandName, err)
}
return command(opt)
}
// Usage provides information on the use of the lattice tool
func Usage() {
fmt.Fprintf(ErrorWriter, UsageMessage, CommandName)
}
// PrintDefaults prints out the default flags
func PrintDefaults(eh flag.ErrorHandling) {
o := newOption(ErrorWriter, eh)
o.flagSet.PrintDefaults()
}