-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
196 lines (165 loc) · 4.6 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
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
189
190
191
192
193
194
195
196
package main
import (
"bytes"
"encoding/gob"
"fmt"
"math/rand"
"os"
"runtime"
"time"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
)
func shuffle(data []*Sentence) {
n := len(data)
for i := n - 1; i >= 0; i-- {
j := rand.Intn(i + 1)
data[i], data[j] = data[j], data[i]
}
}
func printEvaluation(data [][]string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Sentences", "Seconds", "Accuracy"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.AppendBulk(data) // Add Bulk Data
table.Render()
}
var commandTrain = cli.Command{
Name: "train",
Usage: "Train a parsing model by easy-first algorithm",
Description: `
Train a parsing model by easy-first algorithm.
`,
Action: doTrain,
Flags: []cli.Flag{
cli.StringFlag{Name: "train-filename"},
cli.StringFlag{Name: "dev-filename"},
cli.StringFlag{Name: "model-filename"},
cli.IntFlag{Name: "max-iter", Value: 10},
},
}
var commandEval = cli.Command{
Name: "eval",
Usage: "Evaluate a parsing model by easy-first algorithm",
Description: `
Evaluate a parsing model by easy-first algorithm.
`,
Action: doEval,
Flags: []cli.Flag{
cli.StringFlag{Name: "test-filename"},
cli.StringFlag{Name: "model-filename"},
},
}
// This is an experimental feature
var commandDecode = cli.Command{
Name: "decode",
Usage: "Decode a sentence with an embeded model",
Description: `
Decode a sentence with an embeded model.
`,
Action: doDecode,
Flags: []cli.Flag{
cli.StringFlag{Name: "test-filename"},
},
}
var Commands = []cli.Command{
commandTrain,
commandEval,
commandDecode,
}
func doTrain(c *cli.Context) error {
trainFilename := c.String("train-filename")
devFilename := c.String("dev-filename")
modelFilename := c.String("model-filename")
maxIter := c.Int("max-iter")
if trainFilename == "" {
_ = cli.ShowCommandHelp(c, "train")
return cli.NewExitError("`train-filename` is a required field to train a parser.", 1)
}
if devFilename == "" {
_ = cli.ShowCommandHelp(c, "train")
return cli.NewExitError("`dev-filename` is a required field to train a parser.", 1)
}
if modelFilename == "" {
_ = cli.ShowCommandHelp(c, "train")
return cli.NewExitError("`model-filename` is a required field to train a parser.", 1)
}
goldSents, _ := ReadData(trainFilename)
devSents, _ := ReadData(devFilename)
model := NewModel()
for iter := 0; iter < maxIter; iter++ {
shuffle(goldSents)
for _, sent := range goldSents {
model.Update(sent)
}
w := model.AveragedWeight()
trainAccuracy := DependencyAccuracy(&w, goldSents)
devAccuracy := DependencyAccuracy(&w, devSents)
fmt.Println(fmt.Sprintf("%d, %0.03f, %0.03f", iter, trainAccuracy, devAccuracy))
}
w := model.AveragedWeight()
SaveModel(&w, modelFilename)
return nil
}
func doEval(c *cli.Context) error {
testFilename := c.String("test-filename")
modelFilename := c.String("model-filename")
if testFilename == "" {
_ = cli.ShowCommandHelp(c, "eval")
return cli.NewExitError("`test-filename` is a required field to evaluate a parser.", 1)
}
if modelFilename == "" {
_ = cli.ShowCommandHelp(c, "eval")
return cli.NewExitError("`model-filename` is a required field to evaluate a parser.", 1)
}
goldSents, _ := ReadData(testFilename)
weight, _ := LoadModel(modelFilename)
start := time.Now()
testAccuracy := DependencyAccuracy(weight, goldSents)
end := time.Now().Sub(start).Seconds()
data := [][]string{
{fmt.Sprintf("%d", len(goldSents)), fmt.Sprintf("%0.02f", end), fmt.Sprintf("%0.03f", testAccuracy)},
}
printEvaluation(data)
return nil
}
func loadModel(filename string) (*[]float64, error) {
var weight []float64
var b bytes.Buffer
tmp, err := Asset(filename)
if err != nil {
return nil, err
}
b.Write(tmp)
decoder := gob.NewDecoder(&b)
decoder.Decode(&weight)
return &weight, nil
}
func doDecode(c *cli.Context) error {
testFilename := c.String("test-filename")
if testFilename == "" {
_ = cli.ShowCommandHelp(c, "decode")
return cli.NewExitError("`test-filename` is a required field to decode sentences.", 1)
}
goldSents, _ := ReadData(testFilename)
weight, err := loadModel("data/model.bin")
if err != nil {
return err
}
start := time.Now()
testAccuracy := DependencyAccuracy(weight, goldSents)
end := time.Now().Sub(start).Seconds()
data := [][]string{
{fmt.Sprintf("%d", len(goldSents)), fmt.Sprintf("%0.02f", end), fmt.Sprintf("%0.03f", testAccuracy)},
}
printEvaluation(data)
return nil
}
func main() {
app := cli.NewApp()
app.Name = "easy-first"
app.Commands = Commands
runtime.GOMAXPROCS(runtime.NumCPU())
app.Run(os.Args)
}