-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
executable file
·197 lines (190 loc) · 4.74 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
197
package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
"github.com/codegangsta/cli"
"github.com/firstrow/tcp_server"
"github.com/schollz/goagrep/goagrep"
)
var VersionNum string
var Build string
var BuildTime string
func main() {
if len(Build) > 6 {
Build = Build[0:6]
}
app := cli.NewApp()
app.Name = "goagrep"
app.Usage = "Fuzzy matching of big strings.\n Before use, make sure to make a data file (goagrep build)."
app.Version = VersionNum + " (" + Build + ")"
var wordlist, subsetSize, outputFile, searchWord string
var verbose, listAll bool
var tcpServer bool
var port string
goagrep.Normalize = true
app.Commands = []cli.Command{
{
Name: "match",
Aliases: []string{"m"},
Usage: "fuzzy match word",
Flags: []cli.Flag{
cli.StringFlag{
Name: "database, d",
Usage: "input database name (built using 'goagrep build')",
Destination: &wordlist,
},
cli.StringFlag{
Name: "word, w",
Usage: "word to use",
Destination: &searchWord,
},
cli.BoolFlag{
Name: "all, a",
Usage: "list all matches",
Destination: &listAll,
},
},
Action: func(c *cli.Context) error {
if len(wordlist) == 0 || len(searchWord) == 0 {
cli.ShowCommandHelp(c, "match")
} else {
if listAll {
words, scores, err := goagrep.GetMatches(strings.ToLower(searchWord), wordlist)
if err != nil {
fmt.Printf("Not found|||-1")
fmt.Println(err)
fmt.Println(words, scores)
} else {
for i, word := range words {
fmt.Printf("%v|||%v\n", word, scores[i])
}
}
} else {
word, score, err := goagrep.GetMatch(strings.ToLower(searchWord), wordlist)
if err != nil {
fmt.Printf("Not found|||-1")
} else {
fmt.Printf("%v|||%v", word, score)
}
}
}
return nil
},
},
{
Name: "build",
Aliases: []string{"b"},
Usage: "builds the database subsequent fuzzy matching",
Flags: []cli.Flag{
cli.StringFlag{
Name: "list, l",
Usage: "wordlist to use, seperated by newlines",
Destination: &wordlist,
},
cli.StringFlag{
Name: "database, d",
Usage: "output database name (default: words.db)",
Destination: &outputFile,
},
cli.StringFlag{
Name: "size, s",
Usage: "subset size (default: 3)",
Destination: &subsetSize,
},
cli.BoolFlag{
Name: "verbose, v",
Usage: "show more output",
Destination: &verbose,
},
},
Action: func(c *cli.Context) error {
if len(subsetSize) == 0 {
subsetSize = "3"
}
if len(outputFile) == 0 {
outputFile = "words.db"
}
if len(wordlist) == 0 {
cli.ShowCommandHelp(c, "build")
} else {
fmt.Println("Generating '" + outputFile + "' from '" + wordlist + "' with subset size " + subsetSize)
tupleLength, _ := strconv.Atoi(subsetSize)
goagrep.GenerateDB(wordlist, outputFile, tupleLength, verbose)
fmt.Println("Finished building db")
}
return nil
},
},
{
Name: "serve",
Aliases: []string{"s"},
Usage: "serve database on TCP for subsequent fuzzy matching",
Flags: []cli.Flag{
cli.StringFlag{
Name: "list, l",
Usage: "wordlist to use, seperated by newlines",
Destination: &wordlist,
},
cli.StringFlag{
Name: "size, s",
Usage: "subset size (default: 3)",
Destination: &subsetSize,
},
cli.StringFlag{
Name: "port, p",
Usage: "port to use (default: 3334)",
Destination: &port,
},
cli.BoolFlag{
Name: "verbose, v",
Usage: "show more output",
Destination: &verbose,
},
},
Action: func(c *cli.Context) error {
if len(subsetSize) == 0 {
subsetSize = "3"
}
if len(port) == 0 {
port = "3334"
}
if len(wordlist) == 0 {
cli.ShowCommandHelp(c, "serve")
} else {
tcpServer = true
}
return nil
},
},
}
app.Run(os.Args)
if !tcpServer {
os.Exit(0)
}
tupleLength, _ := strconv.Atoi(subsetSize)
words, tuples := goagrep.GenerateDBInMemory(wordlist, tupleLength, verbose)
start := time.Now()
server := tcp_server.New("localhost:9992")
server.OnNewMessage(func(c *tcp_server.Client, message string) {
if verbose {
start = time.Now()
}
matches, scores, _ := goagrep.GetMatchesInMemoryInParallel(message, words, tuples, tupleLength, true)
if verbose {
elapsed := time.Since(start)
log.Printf("Searched for '%s' in %s", strings.TrimSpace(message), elapsed)
for i := range matches {
fmt.Printf("%d: '%s'\n", scores[i], matches[i])
if i > 10 {
break
}
}
}
c.Send(matches[0])
})
server.Listen()
}