-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
cmd.go
188 lines (172 loc) · 4.47 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package tokenize
import (
"bufio"
"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
const (
CommandName = "tokenize"
Description = `command line tokenize`
usageMessage = "%s [-file input_file] [-dict dic_file] [-userdict userdic_file] [-sysdict (ipa|uni)] [-simple false] [-mode (normal|search|extended)]\n"
)
// ErrorWriter writes to stderr
var (
ErrorWriter = os.Stderr
)
// options
type option struct {
file string
dict string
udict string
sysdict string
simple bool
mode 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(w io.Writer, eh flag.ErrorHandling) (o *option) {
o = &option{
flagSet: flag.NewFlagSet(CommandName, eh),
}
// option settings
o.flagSet.SetOutput(w)
o.flagSet.StringVar(&o.file, "file", "", "input file")
o.flagSet.StringVar(&o.dict, "dict", "", "dict")
o.flagSet.StringVar(&o.udict, "udict", "", "user dict")
o.flagSet.StringVar(&o.sysdict, "sysdict", "ipa", "system dict type (ipa|uni)")
o.flagSet.BoolVar(&o.simple, "simple", false, "display abbreviated dictionary contents")
o.flagSet.StringVar(&o.mode, "mode", "normal", "tokenize mode (normal|search|extended)")
return
}
func (o *option) parse(args []string) error {
if err := o.flagSet.Parse(args); err != nil {
return err
}
// validations
if nonFlag := o.flagSet.Args(); len(nonFlag) != 0 {
return fmt.Errorf("invalid argument: %v", nonFlag)
}
if o.mode != "" && o.mode != "normal" && o.mode != "search" && o.mode != "extended" {
return fmt.Errorf("invalid argument: -mode %v", o.mode)
}
if o.sysdict != "" && o.sysdict != "ipa" && o.sysdict != "uni" {
return fmt.Errorf("invalid argument: -sysdict %v", o.sysdict)
}
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(path, sysdict string, shurink bool) (*dict.Dict, error) {
if path != "" {
if shurink {
return dict.LoadShrink(path)
}
return dict.LoadDictFile(path)
}
switch sysdict {
case "ipa":
if shurink {
return ipa.DictShrink(), nil
}
return ipa.Dict(), nil
case "uni":
if shurink {
return uni.DictShrink(), nil
}
return uni.Dict(), nil
}
return nil, fmt.Errorf("unknown dict type, %v", sysdict)
}
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, opt.sysdict, opt.simple)
if err != nil {
return err
}
udict := tokenizer.Nop()
if opt.udict != "" {
d, err := dict.NewUserDict(opt.udict)
if err != nil {
return err
}
udict = tokenizer.UserDict(d)
}
t, err := tokenizer.New(d, udict)
if err != nil {
return err
}
fp := os.Stdin
if opt.file != "" {
var err error
fp, err = os.Open(opt.file)
if err != nil {
return err
}
defer func() {
_ = fp.Close()
}()
}
mode := selectMode(opt.mode)
scanner := bufio.NewScanner(fp)
for scanner.Scan() {
line := scanner.Text()
tokens := t.Analyze(line, mode)
for i, size := 1, len(tokens); i < size; i++ {
tok := tokens[i]
c := tok.Features()
if tok.Class == tokenizer.DUMMY {
fmt.Printf("%s\n", tok.Surface)
} else {
fmt.Printf("%s\t%v\n", tok.Surface, strings.Join(c, ","))
}
}
}
return scanner.Err()
}
// Run receives the slice of args and executes the tokenize 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 tokenize 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()
}