-
Notifications
You must be signed in to change notification settings - Fork 0
/
gloomy.go
168 lines (154 loc) · 5 KB
/
gloomy.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
// Copyright 2017 Tomas Machalek <[email protected]>
//
// 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.
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/tomachalek/gloomy/index/builder"
"github.com/tomachalek/gloomy/index/extras"
"github.com/tomachalek/gloomy/index/gconf"
"github.com/tomachalek/gloomy/service"
)
const (
createIndexAction = "create-index"
extractNgramsAction = "extract-ngrams"
searchServiceAction = "search-service"
searchAction = "search"
appVersion = "0.1.0"
)
func help(topic string) {
if topic == "" {
fmt.Print("Missing action to help with. Select one of the:\n\tcreate-index, extract-ngrams, search-service, search")
}
fmt.Printf("HELP on [%s]:\n", topic)
}
func createIndex(conf *gconf.IndexBuilderConf, ngramSize int) {
if conf.InputFilePath == "" {
fmt.Println("Vertical file not specified")
os.Exit(1)
}
log.Println("Importing vertical file ", conf.InputFilePath)
if conf.OutDirectory == "" {
conf.OutDirectory = filepath.Dir(conf.InputFilePath)
}
fmt.Println("Output directory: ", conf.OutDirectory)
t0 := time.Now()
builder.CreateGloomyIndex(conf, ngramSize)
fmt.Printf("DONE in %s\n", time.Since(t0))
}
func extractNgrams(conf *gconf.IndexBuilderConf, ngramSize int) {
if conf.InputFilePath == "" {
fmt.Println("Vertical file not specified")
os.Exit(1)
}
log.Println("Processing vertical file ", conf.InputFilePath)
if conf.OutDirectory == "" {
conf.OutDirectory = filepath.Dir(conf.InputFilePath)
}
fmt.Println("Output directory: ", conf.OutDirectory)
t0 := time.Now()
extras.ExtractUniqueNgrams(conf, ngramSize)
fmt.Printf("DONE in %s\n", time.Since(t0))
}
func loadSearchConf(confBasePath string) *gconf.SearchConf {
if confBasePath == "" {
var err error
confBasePath, err = os.Getwd()
if err != nil {
panic(err)
}
confBasePath = filepath.Join(confBasePath, "gloomy.json")
}
return gconf.LoadSearchConf(confBasePath)
}
func searchCLI(confBasePath string, corpus string, query string, attrs []string, offset int, limit int, queryType int) {
conf := loadSearchConf(confBasePath)
t1 := time.Now()
args := service.SearchArgs{
CorpusID: corpus,
Phrase: query,
QueryType: queryType,
Attrs: attrs,
Offset: offset,
Limit: limit,
}
ans, err := service.Search(conf.DataPath, args)
if err != nil {
log.Printf("Srch error: %s", err)
}
t2 := time.Since(t1)
for i := 0; ans.HasNext(); i++ {
v := ans.Next()
log.Printf("res[%d]: %s (count: %d, meta: %s)", i, v.Ngram, v.Count, v.Args)
}
log.Printf("Search time: %s", t2)
}
func startSearchService(confBasePath string) {
conf := loadSearchConf(confBasePath)
service.Serve(conf, appVersion)
}
func parseAttrs(attrStr string) []string {
if len(attrStr) == 0 {
return []string{}
}
return strings.Split(attrStr, ",")
}
func main() {
ngramSize := flag.Int("ngram-size", 2, "N-gram size, 2: bigram (default), ...")
srchConfPath := flag.String("conf-path", "", "Path to the gloomy.conf (by default, working dir is used")
metadataAttrs := flag.String("attrs", "", "Metadata attributes separated by comma")
resultLimit := flag.Int("limit", -1, "Result limit")
resultOffset := flag.Int("offset", 0, "Result offset (starting from zero)")
queryType := flag.String("qtype", "default", "Query type (0 = default, 1 = regexp)")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Gloomy - an n-gram database >>>\n\nUsage:\n\t%s [options] [action] [config.json]\n\nAavailable actions:\n\tsearch, search-service, create-index, extract-ngrams\n\nOptions:\n", filepath.Base(os.Args[0]))
flag.PrintDefaults()
}
flag.Parse()
if len(flag.Args()) == 0 {
fmt.Println("Missing action, try -h for help")
os.Exit(1)
} else {
switch flag.Arg(0) {
case "help":
help(flag.Arg(1))
case createIndexAction:
conf := gconf.LoadIndexBuilderConf(flag.Arg(1))
createIndex(conf, *ngramSize)
case extractNgramsAction:
conf := gconf.LoadIndexBuilderConf(flag.Arg(1))
extractNgrams(conf, *ngramSize)
case searchServiceAction:
startSearchService(*srchConfPath)
case searchAction:
if flag.Arg(1) == "" || flag.Arg(2) == "" {
log.Fatal("Missing argument (both corpus and query must be specified)")
}
qtype := service.ImportQueryType(*queryType)
if qtype < 0 {
panic(fmt.Sprintf("Unknown query type: %s", *queryType))
}
searchCLI(*srchConfPath, flag.Arg(1), flag.Arg(2), parseAttrs(*metadataAttrs),
*resultOffset, *resultLimit, qtype)
default:
fmt.Printf("Unknown action %s\n", flag.Arg(0))
os.Exit(1)
}
}
}