-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcqlizer.go
382 lines (335 loc) · 13.5 KB
/
cqlizer.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Copyright 2024 Tomas Machalek <[email protected]>
// Copyright 2024 Institute of the Czech National Corpus,
// Faculty of Arts, Charles University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:generate pigeon -o ./cql/grammar.go ./cql/grammar.peg
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/czcorpus/cnc-gokit/logging"
"github.com/czcorpus/cqlizer/cnf"
"github.com/czcorpus/cqlizer/models/rf"
"github.com/czcorpus/cqlizer/stats"
"github.com/fatih/color"
"github.com/gin-gonic/gin"
randomforest "github.com/malaschitz/randomForest"
"github.com/rs/zerolog/log"
)
type action string
func (a action) String() string {
return string(a)
}
func (a action) validate() error {
if a != actionServer && a != actionImport && a != actionCorpsizes && a != actionBenchmark &&
a != actionReplay && a != actionEvaluate && a != actionLearn && a != actionLearnNDW &&
a != actionVersion && a != actionNormalize {
return fmt.Errorf("unknown action: %s", a)
}
return nil
}
const (
actionServer action = "server"
actionImport action = "import"
actionCorpsizes action = "corpsizes"
actionBenchmark action = "benchmark"
actionReplay action = "replay"
actionEvaluate action = "evaluate"
actionLearn action = "learn"
actionLearnNDW action = "learn-ndw"
actionVersion action = "version"
actionNormalize action = "normalize"
)
var (
version string
buildDate string
gitCommit string
)
// VersionInfo provides a detailed information about the actual build
type VersionInfo struct {
Version string `json:"version"`
BuildDate string `json:"buildDate"`
GitCommit string `json:"gitCommit"`
}
func getRequestOrigin(ctx *gin.Context) string {
currOrigin, ok := ctx.Request.Header["Origin"]
if ok {
return currOrigin[0]
}
return ""
}
func additionalLogEvents() gin.HandlerFunc {
return func(ctx *gin.Context) {
logging.AddLogEvent(ctx, "userAgent", ctx.Request.UserAgent())
logging.AddLogEvent(ctx, "corpusId", ctx.Param("corpusId"))
ctx.Next()
}
}
func loadModel(conf *cnf.Conf, statsDB *stats.Database, trainingID int) (randomforest.Forest, float64, error) {
threshold, err := statsDB.GetTrainingThreshold(trainingID)
if err != nil {
return randomforest.Forest{},
0.0,
fmt.Errorf("failed to load model for training %d \u25B6 %w", trainingID, err)
}
log.Info().Int("trainingId", trainingID).Msg("found required training")
tdata, err := statsDB.GetTrainingData(trainingID)
if err != nil {
return randomforest.Forest{},
threshold,
fmt.Errorf("failed to load model for training %d \u25B6 %w", trainingID, err)
}
vdata, err := statsDB.GetTrainingValidationData(trainingID)
if err != nil {
return randomforest.Forest{},
threshold,
fmt.Errorf("failed to load model for training %d \u25B6 %w", trainingID, err)
}
eng := rf.NewEngine(conf, statsDB)
model, err := eng.TrainReplay(threshold, tdata, vdata)
if err != nil {
return randomforest.Forest{},
threshold,
fmt.Errorf("failed to load model for training %d \u25B6 %w", trainingID, err)
}
return model, threshold, err
}
func cleanVersionInfo(v string) string {
return strings.TrimLeft(strings.Trim(v, "'"), "v")
}
func parseTrainingIdOrExit(v string) int {
if v == "" {
return 0
}
trainingID, err := strconv.ParseInt(v, 10, 64)
if err != nil {
color.New(errColor).Fprintln(os.Stderr, err)
os.Exit(1)
}
return int(trainingID)
}
func topLevelUsage() {
fmt.Fprintf(os.Stderr, "CQLIZER - a CQL analysis tool\n")
fmt.Fprintf(os.Stderr, "-----------------------------\n\n")
fmt.Fprintf(os.Stderr, "Commands:\n")
fmt.Fprintf(os.Stderr, "\t%s\t\tshow version info\n", actionVersion.String())
fmt.Fprintf(os.Stderr, "\t%s\t\tstart HTTP API server\n", actionServer.String())
fmt.Fprintf(os.Stderr, "\t%s\t\timport queries from KonText logs\n", actionImport.String())
fmt.Fprintf(os.Stderr, "\t%s\timport corpora sizes (currently unused)\n", actionCorpsizes.String())
fmt.Fprintf(os.Stderr, "\t%s\trun benchmarks on stored queries\n", actionBenchmark.String())
fmt.Fprintf(os.Stderr, "\t%s\t\treplay stored training\n", actionReplay.String())
fmt.Fprintf(os.Stderr, "\t%s\tevaluate queries with `trainingExclude` flag\n", actionEvaluate.String())
fmt.Fprintf(os.Stderr, "\t%s\t\tlearn rf model using benchmarked queries (ones without `trainingExclude` flag)\n", actionLearn.String())
fmt.Fprintf(os.Stderr, "\t%s\tlearn ndw model using benchmarked queries (ones without `trainingExclude` flag)\n", actionLearnNDW.String())
fmt.Fprintf(os.Stderr, "\nUse `cqlizer command -h` for information about a specific action\n\n")
}
func setupConfAndLogging(cmd *flag.FlagSet, idx int) *cnf.Conf {
conf := cnf.LoadConfig(cmd.Arg(idx))
logging.SetupLogging(conf.LogFile, conf.LogLevel)
cnf.ValidateAndDefaults(conf)
return conf
}
func main() {
version := VersionInfo{
Version: cleanVersionInfo(version),
BuildDate: cleanVersionInfo(buildDate),
GitCommit: cleanVersionInfo(gitCommit),
}
cmdServer := flag.NewFlagSet(actionServer.String(), flag.ExitOnError)
cmdServer.Usage = func() {
fmt.Fprintf(
os.Stderr,
"Usage:\t%s %s [options] config.json [trainingID]\n\t",
filepath.Base(os.Args[0]), actionServer.String())
fmt.Fprintf(os.Stderr, "\nOptions:\n")
cmdServer.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nStart HTTP API Server for CQL analysis\n")
}
cmdBenchmark := flag.NewFlagSet(actionBenchmark.String(), flag.ExitOnError)
overwriteAll := cmdBenchmark.Bool("overwrite-all", false, "If set, then all the queries will be benchmarked even if they already have a result attached")
cmdBenchmark.Usage = func() {
fmt.Fprintf(
os.Stderr,
"Usage:\t%s %s [options] config.json\n\t",
filepath.Base(os.Args[0]), actionBenchmark.String())
fmt.Fprintf(os.Stderr, "\nOptions:\n")
cmdBenchmark.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nBenchmark available queries.\n")
}
cmdCorpsizes := flag.NewFlagSet(actionCorpsizes.String(), flag.ExitOnError)
addToTrainingSet := cmdCorpsizes.Bool("add-to-training", false, "If set, than all the imported records will become part of the training&validation set")
cmdEvaluate := flag.NewFlagSet(actionEvaluate.String(), flag.ExitOnError)
numSamples := cmdEvaluate.Int("num-samples", 5, "Number of samples for the validation action")
sampleSize := cmdEvaluate.Int("sample-size", 300, "Sample size for the validation action")
allowTrainingRecs := cmdEvaluate.Bool("allow-training-records", false, "If set, then even records used for training the model may occur in validation sets")
anyCorpusSearch2 := cmdEvaluate.Bool("any-corpus", false, "Do not restrict to queries to syn* corpora")
cmdEvaluate.Usage = func() {
fmt.Fprintf(
os.Stderr,
"Usage:\t%s %s [options] config.json [trainingID]\n\t",
filepath.Base(os.Args[0]), actionEvaluate.String())
fmt.Fprintf(os.Stderr, "\nArguments:\n")
fmt.Fprintf(os.Stderr, "\tconfig.json\ta path to a config file\n")
fmt.Fprintf(os.Stderr, "\ttrainingID\tAn ID of a training used as a base for running service. If omitted, the latest training ID will be used\n")
fmt.Fprintf(os.Stderr, "\nOptions:\n")
cmdEvaluate.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nEvaluate stored queries (the ones with the `trainingExclude` flag set). This is intended for the \"real word\" model testing.\n")
}
cmdImport := flag.NewFlagSet(actionImport.String(), flag.ExitOnError)
cmdImport.Usage = func() {
fmt.Fprintf(
os.Stderr,
"Usage:\t%s %s [options] config.json tsource_file\n\t",
filepath.Base(os.Args[0]), actionImport.String())
fmt.Fprintf(os.Stderr, "\nArguments:\n")
fmt.Fprintf(os.Stderr, "\tconfig.json\ta path to a config file\n")
fmt.Fprintf(os.Stderr, "\tsource_file\tA KonText log file to import training/validation user queries from")
fmt.Fprintf(os.Stderr, "\nOptions:\n")
cmdImport.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nImport queries from a KonText application log file\n")
}
cmdLearn := flag.NewFlagSet(actionLearn.String(), flag.ExitOnError)
anyCorpusSearch := cmdLearn.Bool("any-corpus", false, "Do not restrict to queries to syn* corpora")
ratioOfTrues := cmdLearn.Float64("ratio-of-trues", 0.1, "Ratio of values above the threshold")
cmdLearn.Usage = func() {
fmt.Fprintf(
os.Stderr,
"Usage:\t%s %s [options] config.json threshold\n\t",
filepath.Base(os.Args[0]), actionLearn.String())
fmt.Fprintf(os.Stderr, "\nArguments:\n")
fmt.Fprintf(os.Stderr, "\tconfig.json\ta path to a config file\n")
fmt.Fprintf(os.Stderr, "\tthreshold\tA threshold value (in seconds) for what is considered a problematic query in a benchmark environment\n")
fmt.Fprintf(os.Stderr, "\nOptions:\n")
cmdLearn.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nLearn a new model based on queries stored in database (the ones without the `trainingExclude` flag)\n")
}
cmdLearnNDW := flag.NewFlagSet(actionLearn.String(), flag.ExitOnError)
anyCorpusSearchNDW := cmdLearnNDW.Bool("any-corpus", false, "Do not restrict to queries to syn* corpora")
ratioOfTruesNDW := cmdLearnNDW.Float64("ratio-of-trues", 0.1, "Ratio of values above the threshold")
cmdLearnNDW.Usage = func() {
fmt.Fprintf(
os.Stderr,
"Usage:\t%s %s [options] config.json threshold\n\t",
filepath.Base(os.Args[0]), actionLearn.String())
fmt.Fprintf(os.Stderr, "\nArguments:\n")
fmt.Fprintf(os.Stderr, "\tconfig.json\ta path to a config file\n")
fmt.Fprintf(os.Stderr, "\tthreshold\tA threshold value (in seconds) for what is considered a problematic query in a benchmark environment\n")
fmt.Fprintf(os.Stderr, "\nOptions:\n")
cmdLearnNDW.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nLearn a new model based on queries stored in database (the ones without the `trainingExclude` flag)\n")
}
cmdReplay := flag.NewFlagSet(actionReplay.String(), flag.ExitOnError)
cmdReplay.Usage = func() {
fmt.Fprintf(
os.Stderr,
"Usage:\t%s %s [options] config.json [training_ID]\n\t",
filepath.Base(os.Args[0]), actionReplay.String())
fmt.Fprintf(os.Stderr, "\nArguments:\n")
fmt.Fprintf(os.Stderr, "\tconfig.json\ta path to a config file\n")
fmt.Fprintf(os.Stderr, "\ttraining_ID\tAn ID of a training used as a base for running service. If omitted, the latest training ID will be used\n")
fmt.Fprintf(os.Stderr, "\nOptions:\n")
cmdLearn.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nReplay learn and validation for the specified training set.\n")
}
cmdNormalize := flag.NewFlagSet(actionNormalize.String(), flag.ExitOnError)
cmdNormalize.Usage = func() {
fmt.Fprintf(
os.Stderr,
"Usage:\t%s %s [options] config.json\n\t",
filepath.Base(os.Args[0]), actionNormalize.String())
fmt.Fprintf(os.Stderr, "\nGenerate normalized versions for queries.\n")
}
if len(os.Args) < 2 {
topLevelUsage()
os.Exit(10)
}
action := action(os.Args[1])
if err := action.validate(); err != nil {
fmt.Println(err)
os.Exit(11)
}
switch action {
case actionVersion:
fmt.Printf("CQLizer %s\nbuild date: %s\nlast commit: %s\n", version.Version, version.BuildDate, version.GitCommit)
return
case actionServer:
var trainingID int64
var err error
cmdServer.Parse(os.Args[2:])
if cmdServer.Arg(1) != "" {
trainingID, err = strconv.ParseInt(cmdServer.Arg(1), 10, 64)
if err != nil {
color.New(errColor).Fprintln(os.Stderr, err)
os.Exit(1)
}
}
conf := setupConfAndLogging(cmdServer, 0)
runApiServer(conf, int(trainingID))
case actionImport:
cmdImport.Parse(os.Args[2:])
conf := setupConfAndLogging(cmdImport, 0)
runKontextImport(conf, cmdImport.Arg(1), *addToTrainingSet)
case actionCorpsizes:
cmdCorpsizes.Parse(os.Args[2:])
conf := setupConfAndLogging(cmdCorpsizes, 0)
runSizesImport(conf, cmdCorpsizes.Arg(1))
case actionBenchmark:
cmdBenchmark.Parse(os.Args[2:])
conf := setupConfAndLogging(cmdBenchmark, 0)
runBenchmark(conf, *overwriteAll)
case actionReplay:
cmdReplay.Parse(os.Args[2:])
conf := setupConfAndLogging(cmdReplay, 0)
trainingID := parseTrainingIdOrExit(cmdReplay.Arg(1))
runTrainingReplay(conf, trainingID)
case actionEvaluate:
cmdEvaluate.Parse(os.Args[2:])
conf := setupConfAndLogging(cmdEvaluate, 0)
//trainingID := parseTrainingIdOrExit(cmdEvaluate.Arg(1))
thr, err := strconv.ParseFloat(cmdEvaluate.Arg(1), 64)
if err != nil {
color.New(errColor).Fprintln(os.Stderr, err)
os.Exit(1)
}
runEvaluation2(conf, thr, *numSamples, *sampleSize, *allowTrainingRecs, *anyCorpusSearch2)
case actionLearn:
cmdLearn.Parse(os.Args[2:])
conf := setupConfAndLogging(cmdLearn, 0)
thr, err := strconv.ParseFloat(cmdLearn.Arg(1), 64)
if err != nil {
color.New(errColor).Fprintln(os.Stderr, err)
os.Exit(1)
}
runLearning(conf, thr, *ratioOfTrues, !*anyCorpusSearch)
case actionLearnNDW:
cmdLearnNDW.Parse(os.Args[2:])
conf := setupConfAndLogging(cmdLearnNDW, 0)
thr, err := strconv.ParseFloat(cmdLearnNDW.Arg(1), 64)
if err != nil {
color.New(errColor).Fprintln(os.Stderr, err)
os.Exit(1)
}
runLearningNDW(conf, thr, *ratioOfTruesNDW, !*anyCorpusSearchNDW)
case actionNormalize:
cmdNormalize.Parse(os.Args[2:])
conf := setupConfAndLogging(cmdNormalize, 0)
runQueryNormalization(conf)
default:
log.Fatal().Msgf("Unknown action %s", action)
}
}