/
main.go
207 lines (165 loc) · 3.76 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
package main
import (
"github.com/andrebq/gas"
"io/ioutil"
"strings"
"bufio"
"bytes"
"fmt"
"os"
)
type TrieNode struct {
terminator bool
children [26]*TrieNode
}
type Word struct {
start int
length int
step int
}
var dictionary *TrieNode
var content []byte
var foundWords map[string][]*Word
func main() {
loadDictionary();
if len( os.Args ) < 2 {
fmt.Println("Usage: code-finder [file to search]")
os.Exit(0)
}
fmt.Println("Processing...")
filename := os.Args[1]
unfilteredContent, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println(err)
os.Exit(0)
}
content = make([]byte, 0, len(unfilteredContent))
for _, b := range unfilteredContent {
if b >= 'A' && b <= 'Z' {
content = append(content, b)
} else if b >= 'a' && b <= 'z' {
content = append(content, b - 'a' + 'A')
}
}
foundWords = make(map[string][]*Word)
// Gather words.
for step := -1000; step < 1000; step++ {
if step < 0 || step > 1 {
examineStep(step)
}
}
writeTemplate()
}
func writeTemplate() {
jsonWords := ""
nWords := len(foundWords)
wid := 0
for k, v := range foundWords {
if wid % 100 == 0 {
fmt.Printf( "compiling: %d / %d\n", wid, nWords );
}
wid++
jsonWords += fmt.Sprintf("\"%s\": [\n", k);
for _, w := range v {
jsonWords += fmt.Sprintf("\t[ %d, %d, %d ],\n", w.start, w.step, w.length)
}
jsonWords += "],\n"
}
template, err := gas.ReadFile("github.com/thingalon/code-finder/template.html")
if err != nil {
fmt.Println(err)
os.Exit(0)
}
template = bytes.Replace(template, []byte("[filename]"), []byte(os.Args[1]), 1)
template = bytes.Replace(template, []byte("[content]"), content, 1)
template = bytes.Replace(template, []byte("[words]"), []byte(jsonWords), 1)
outfile := os.Args[1] + ".html"
ioutil.WriteFile(outfile, template, 0644)
fmt.Println("Written to " + outfile)
}
func examineStep(step int) {
started := 0
done := make(chan bool, 100)
for i := 0; i < len(content); i++ {
go examineStepPos(done, step, i)
started++
}
for started > 0 {
<-done
started--
}
}
func examineStepPos(done chan bool, step int, start int) {
length := 0
cursor := dictionary
pos := start
for pos >= 0 && pos < len(content) {
b := content[pos]
index := int(b) - int('A')
if cursor.children[index] == nil {
done <- true
return
}
cursor = cursor.children[index]
length += 1
if length >= 3 && cursor.terminator {
word := getWord(step, start, length)
if _, ok := foundWords[word]; ! ok {
foundWords[word] = make([]*Word, 0, 1)
}
if len( foundWords[word] ) < 50 {
foundWords[word] = append(foundWords[word], &Word{
step: step,
start: start,
length: length,
})
}
}
pos += step
}
done <- true
}
func getWord(step int, pos int, length int) string {
word := make([]byte, length, length)
for i := 0; i < length; i++ {
word[i] = content[pos]
pos += step
}
return string(word)
}
func loadDictionary() {
file, err := gas.Open("github.com/thingalon/code-finder/dictionary.txt")
if err != nil {
fmt.Println("Couldn't open dictionary")
os.Exit(0)
}
defer file.Close()
dictionary = &TrieNode{};
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := []byte(strings.ToUpper(scanner.Text()))
addToDictionary(line);
}
}
func addToDictionary(line []byte) {
cursor := dictionary
for _, b := range line {
index := int(b) - int('A');
if cursor.children[index] == nil {
cursor.children[index] = &TrieNode{}
}
cursor = cursor.children[index]
}
cursor.terminator = true
}
func wordInDictionary(word []byte) bool {
cursor := dictionary
for _, b := range word {
index := int(b) - int('A')
if cursor.children[index] == nil {
return false
}
cursor = cursor.children[index]
}
return cursor.terminator
}