-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
227 lines (194 loc) · 5.62 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
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
package main
import (
"bufio"
"database/sql"
"fmt"
"log"
"maps"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/hbollon/go-edlib"
_ "github.com/mattn/go-sqlite3"
"github.com/spf13/pflag"
)
func main() {
paths := handleCLI()
// connect to DB
db, err := sql.Open("sqlite3", "tokens.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Define the regex to match tokens
wordRegex := regexp.MustCompile(`\b(?:_?)([_a-zA-Z]\w*)`) // match the words we want to match
tokenCaseRegex := regexp.MustCompile(`([A-Z][a-z']+|[a-z']+|[A-Z']{3,})`) // match the cases within the matched words
// mapping for full word check
tokenMap := make(map[string]bool)
maps.Copy(tokenMap, getTokensFromDB(db, "Solidity", 1e-8))
maps.Copy(tokenMap, getTokensFromDB(db, "English", 0))
// mapping for compound word check
compoundMap := make(map[string]bool)
maps.Copy(compoundMap, getTokensFromDB(db, "Solidity", 1e-3))
maps.Copy(compoundMap, getTokensFromDB(db, "English", 0))
// get a word list for edit distance and typo fix suggestion
wordSuggestionList := make([]string, len(compoundMap))
i := 0
for k := range compoundMap {
wordSuggestionList[i] = k
i++
}
// Process the paths slice
for _, file := range paths {
processFile(file, wordRegex, tokenCaseRegex, tokenMap, compoundMap, wordSuggestionList)
}
}
func processFile(path string, wordRegex *regexp.Regexp, tokenRegex *regexp.Regexp, tokenMap map[string]bool, compoundMap map[string]bool, wordList []string) {
file, err := os.Open(path)
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
lineNo := 0
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
lineNo++
words := wordRegex.FindAllStringSubmatch(line, -1)
for _, word := range words {
tokens := tokenRegex.FindAllStringSubmatch(word[1], -1)
for _, token := range tokens {
// check whether the full word is part of the map
if !tokenMap[strings.ToLower(token[1])] {
// check whether the word in compounds is part of the map
_, success := parseCompoundWords(strings.ToLower(token[1]), compoundMap)
// also failed to build compound word, a typo then
if !success {
fmt.Printf("Typo in %s:%d: %s", path, lineNo, token[1])
// find most similar words in word list for fix suggestion
res, err := edlib.FuzzySearchSetThreshold(token[1], wordList, 3, 0.7, edlib.OSADamerauLevenshtein)
if err != nil {
fmt.Println(err)
} else if strings.Join(res, "") == "" {
fmt.Printf("\n")
} else {
fmt.Printf(" => %s\n", strings.Join(res, " "))
}
}
}
}
}
}
}
func parseCompoundWords(word string, knownWords map[string]bool) ([]string, bool) {
// Base case: empty string or only a known word with at least 2 characters
if word == "" || (len(word) >= 2 && knownWords[word]) {
return []string{word}, true
}
// Recursive case: check potential tokens with length at least 2 and their remaining parts
for i := 2; i < len(word); i++ {
candidate := word[:i]
if len(candidate) >= 2 && knownWords[candidate] {
remaining, success := parseCompoundWords(word[i:], knownWords)
if success {
return append([]string{candidate}, remaining...), true
}
}
}
// No successful split found
return nil, false
}
func getTokensFromDB(db *sql.DB, language string, minFrequency float32) map[string]bool {
query := `
WITH LanguageTotal AS (
SELECT language, SUM(count) as total_count
FROM tokens
GROUP BY language
)
SELECT t.language, t.token, t.count, t.count / CAST(lt.total_count as FLOAT) as relative_count
FROM tokens as t
JOIN LanguageTotal lt ON t.language = lt.language
WHERE t.language = ? AND relative_count > ?
ORDER BY relative_count DESC;
`
rows, err := db.Query(query, language, minFrequency)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
// Create a map to store tokens
tokensMap := make(map[string]bool)
for rows.Next() {
var language string
var token string
var count int
var relativeCount float32
err = rows.Scan(&language, &token, &count, &relativeCount)
if err != nil {
log.Fatal(err)
}
// Add the token to the map
tokensMap[token] = true
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
return tokensMap
}
func handleCLI() []string {
scopeFile := pflag.StringP("scope", "s", "", "File containing additional file paths")
pflag.Parse()
basePath := pflag.Args()[0]
// Ensure positional argument is provided
if basePath == "" {
fmt.Println("Missing positional argument: directory or file")
os.Exit(1)
}
basePathInfo, err := os.Stat(basePath)
if err != nil {
fmt.Println("Error accessing positional argument:", err)
os.Exit(1)
}
paths := []string{}
// Read paths from scope file if provided
if *scopeFile != "" {
if !basePathInfo.IsDir() {
fmt.Println("Positional argument must be a directory when --scope is used")
os.Exit(1)
}
scopeBytes, err := os.ReadFile(*scopeFile)
if err != nil {
fmt.Println("Error reading scope file:", err)
os.Exit(1)
}
scopePaths := strings.Split(string(scopeBytes), "\n")
for _, scopePath := range scopePaths {
scopePath = strings.TrimSpace(scopePath)
if scopePath != "" {
paths = append(paths, filepath.Join(basePath, scopePath))
}
}
} else {
if basePathInfo.IsDir() {
err = filepath.Walk(basePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
paths = append(paths, path)
}
return nil
})
if err != nil {
fmt.Println("Error walking folder:", err)
os.Exit(1)
}
} else {
paths = append(paths, basePath)
}
}
return paths
}