-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgenerate_classifier.go
253 lines (224 loc) · 5.68 KB
/
generate_classifier.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// +build ignore
/*
This program trains a naive bayesian classifier
provided by https://github.com/jbrukh/bayesian
on a set of source code files
provided by https://github.com/github/linguist
This file is meant by run by go generate,
refer to generate.go for its intended invokation
*/
package main
import (
"container/heap"
"fmt"
"io/ioutil"
"log"
"os"
"runtime"
"github.com/dayvonjersen/linguist/tokenizer"
"github.com/jbrukh/bayesian"
)
type sampleFile struct {
lang, fp string
tokens []string
}
func main() {
const (
sourcePath = "./linguist/samples"
outfile = "./classifier"
quiet = false
)
log.SetFlags(0)
if quiet {
log.SetOutput(ioutil.Discard)
}
// first we only read all the paths of the sample files
// and their corresponding and language names into:
sampleFiles := []*sampleFile{}
// and store all the language names into:
languages := []string{}
/*
github/linguist has directory structure:
...
├── samples
│ ├── (name of programming language)
│ │ ├── (sample file in language)
│ │ ├── (sample file in language)
│ │ └── (sample file in language)
│ ├── (name of another programming language)
│ │ └── (sample file)
...
the following hard-coded logic expects this layout
*/
log.Println("Scanning", sourcePath, "...")
srcDir, err := os.Open(sourcePath)
checkErr(err)
subDirs, err := srcDir.Readdir(-1)
checkErr(err)
for _, langDir := range subDirs {
lang := langDir.Name()
if !langDir.IsDir() {
log.Println("unexpected file:", lang)
continue
}
languages = append(languages, lang)
samplePath := sourcePath + "/" + lang
sampleDir, err := os.Open(samplePath)
checkErr(err)
files, err := sampleDir.Readdir(-1)
checkErr(err)
for _, file := range files {
fp := samplePath + "/" + file.Name()
if file.IsDir() {
// Skip subdirectories
continue
}
sampleFiles = append(sampleFiles, &sampleFile{lang, fp, nil})
}
sampleDir.Close()
}
log.Println("Found", len(languages), "languages in", len(sampleFiles), "files")
// simple progress bar
progress := 0.0
total := float64(len(sampleFiles)) * 2.0
progressBar := func() {
progress++
fmt.Printf("Processing files ... %.2f%%\r", progress/total*100.0)
}
// then we concurrently read and tokenize the samples
sampleChan := make(chan *sampleFile)
readyChan := make(chan struct{})
received := 0
tokenize := func(s *sampleFile) {
f, err := os.Open(s.fp)
checkErr(err)
contents, err := ioutil.ReadAll(f)
f.Close()
checkErr(err)
s.tokens = tokenizer.Tokenize(contents)
sampleChan <- s
}
dox := map[string][]string{}
for _, lang := range languages {
dox[lang] = []string{}
}
// this receives the processed files and stores their tokens with their language
go func() {
for {
s := <-sampleChan
dox[s.lang] = append(dox[s.lang], s.tokens...)
received++
progressBar()
if received == len(sampleFiles) {
close(readyChan)
return
}
}
}()
// this balances the workload (implementation at end of file)
requests := getRequestsChan(len(sampleFiles))
for i := range sampleFiles {
requests <- &request{
workFn: tokenize,
arg: sampleFiles[i],
}
progressBar()
}
// once that's done
<-readyChan
close(requests)
fmt.Println() // for the progress bar
// we train the classifier in the arbitrary manner that its API demands
classes := make([]bayesian.Class, 1)
documents := make(map[bayesian.Class][]string)
for _, lang := range languages {
var class = bayesian.Class(lang)
classes = append(classes, class)
documents[class] = dox[lang]
}
log.Println("Creating bayesian.Classifier ...")
clsf := bayesian.NewClassifier(classes...)
for cls, dox := range documents {
clsf.Learn(dox, cls)
}
// and write the data to disk
log.Println("Serializing and exporting bayesian.Classifier to", outfile, "...")
checkErr(clsf.WriteToFile("classifier"))
log.Println("Done.")
}
func checkErr(err error) {
if err != nil {
log.Panicln(err)
}
}
// simple load balancer from "concurrency is not parallelism" talk
type request struct {
workFn func(s *sampleFile)
arg *sampleFile
}
type worker struct {
requests chan *request
pending, index int
}
func (w *worker) work(done chan *worker) {
for {
req := <-w.requests
req.workFn(req.arg)
done <- w
}
}
type pool []*worker
func (p pool) Less(i, j int) bool { return p[i].pending < p[j].pending }
func (p pool) Len() int { return len(p) }
func (p pool) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p *pool) Push(x interface{}) { *p = append(*p, x.(*worker)) }
func (p *pool) Pop() interface{} {
old := *p
n := len(old)
x := old[n-1]
*p = old[0 : n-1]
return x
}
type balancer struct {
workers pool
done chan *worker
}
func (b *balancer) balance(work chan *request) {
for {
select {
case req, ok := <-work:
if ok {
b.dispatch(req)
} else {
return
}
case w := <-b.done:
b.completed(w)
}
}
}
func (b *balancer) dispatch(req *request) {
w := heap.Pop(&b.workers).(*worker)
w.requests <- req
w.pending++
heap.Push(&b.workers, w)
}
func (b *balancer) completed(w *worker) {
w.pending--
heap.Remove(&b.workers, w.index)
heap.Push(&b.workers, w)
}
func getRequestsChan(jobs int) chan *request {
done := make(chan *worker)
workers := make(pool, runtime.GOMAXPROCS(0)*4) // I don't know how many workers there should be
for i := 0; i < len(workers); i++ {
w := &worker{make(chan *request, jobs), 0, i}
go w.work(done)
workers[i] = w
}
heap.Init(&workers)
b := &balancer{workers, done}
requests := make(chan *request)
go b.balance(requests)
return requests
}