/
main.go
197 lines (157 loc) · 4.8 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 (
"bufio"
"flag"
"fmt"
"io"
"os"
"strings"
"github.com/anchepiece/suggest"
)
const (
Name = "suggest-autocorrect"
FullName = "github.com/anchepiece/suggest-autocorrect"
Usage = "suggest-autocorrect is a console based tool to return the closest match in a list"
Version = "0.1.0"
Author = "anchepiece"
Email = "anchepiece@gmail.com"
)
var (
flagQuery = flag.String("q", "", "query term to search")
flagCommands = flag.String("c", "", "comma-separated list of possible suggestions")
flagAutocorrectDisabled = flag.Bool("d", false, "disable autocorrect feature")
)
func simpleExample() {
suggester := suggest.Suggest{}
query := "fgerp"
commands := []string{"cat", "mkdir", "fgrep", "history"}
match, err := suggester.AutocorrectAgainst(query, commands)
if err == nil {
fmt.Println("Autocorrected to:", match) // "fgrep"
}
suggester.Commands = commands
if match, err := suggester.Autocorrect(query); err == nil {
fmt.Println("Also Autocorrected to:", match) // "fgrep"
}
query = "println"
commands = []string{"Fprint", "Fprintf", "Fprintln", "Sprintf", "Print", "Printf", "Println"}
suggester.Options.SimilarityMinimum = 8
fmt.Printf("Searching %v for %s\n", query, commands)
if result, err := suggester.QueryAgainst(query, commands); err == nil {
if !result.Success() {
fmt.Println("No close matches")
} else {
fmt.Println("Similar matches:", result.Matches) // [Println Fprintln]
fmt.Println("Autocorrect:", result.Autocorrect) // Println
}
}
os.Exit(0)
}
func simpleExample2() {
suggester := suggest.Suggest{}
query := "fgerp"
commands := []string{"cat", "mkdir", "fgrep", "history"}
suggester.Commands = commands
if match, err := suggester.Autocorrect("mkdri"); err == nil {
fmt.Println("Autocorrected to:", match) // "mkdir"
}
// Alternate autocorrect usage pattern
match, _ := suggester.AutocorrectAgainst(query, commands)
if match != "" {
fmt.Println("Autocorrected to:", match) // "fgrep"
}
// Alternate usage pattern
query = "printf"
commands = []string{"Fprint", "Fprintf", "Fprintln", "Sprintf", "Printf", "Println"}
suggester.Options.SimilarityMinimum = 8
fmt.Printf("Searching %v in %s\n", query, commands)
if result, err := suggester.QueryAgainst(query, commands); err == nil {
if !result.Success() {
fmt.Println("No close matches")
} else {
fmt.Println("Similar matches:", result.Matches)
// [Println Fprintln]
fmt.Println("Autocorrect:", result.Autocorrect)
// Println
}
}
}
func simpleExample3() {
// make sure our example in doc.go works
suggester := suggest.Suggest{}
suggester.Options.SimilarityMinimum = 7
suggester.Options.AutocorrectDisabled = false
query := "proflie"
commands := []string{"perfil", "profiel", "profile", "profil", "account"}
suggester.Commands = commands
if result, err := suggester.Query(query); err == nil {
if !result.Success() {
fmt.Println("No close matches")
} else {
fmt.Println("Similar matches:", result.Matches) // [profile profil profiel]
fmt.Println("Autocorrect:", result.Autocorrect) // profile
}
}
os.Exit(0)
}
func main() {
flag.Parse()
// fmt.Println(Name)
// simpleExample3()
// simpleExample2()
// simpleExample()
if *flagQuery == "" || *flagCommands == "" {
fmt.Println("Error: must specify both a query (-q) and the possible commands (-c)")
os.Exit(1)
}
fmt.Println("Entered query:", *flagQuery)
var commandList []string
for _, s := range strings.Split(*flagCommands, ",") {
commandList = append(commandList, strings.TrimSpace(s))
}
if len(commandList) > 0 {
fmt.Println("Entered possible commands:", commandList)
}
fmt.Println()
s := &suggest.Suggest{
Commands: commandList,
}
s.Options.AutocorrectDisabled = *flagAutocorrectDisabled
s.Options.SimilarityMinimum = 7
if m := s.ExactMatch(*flagQuery); m != "" {
fmt.Println("Continuing with exact match!", m)
return // Run the command!
} else {
fmt.Printf("You called a command '%s' which does not exist. \n", *flagQuery)
}
if !*flagAutocorrectDisabled {
if a, err := s.Autocorrect(*flagQuery); err == nil && a != "" {
fmt.Printf("Continuing under the assumption that you meant '%s'\n", a)
return // Run the command!
}
}
m, err := s.Query(*flagQuery)
if err != nil {
fmt.Printf("An error occurred: %v\n", err)
}
if len(m.Matches) == 1 {
fmt.Println("Did you mean this?")
}
if len(m.Matches) > 1 {
fmt.Println("Did you mean one of these?")
}
for _, suggestion := range m.Matches {
fmt.Printf("\t%s\n", suggestion)
}
}
// scan through a passed file and append all words to available commands
func scan(r io.Reader) {
// TODO: handle command line option to parse
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
word := scanner.Text()
// Match the query to input
fmt.Println(word)
}
}