-
Notifications
You must be signed in to change notification settings - Fork 1
/
words.go
488 lines (415 loc) · 10.1 KB
/
words.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package main
import (
"os"
"fmt"
"path"
"bufio"
"strconv"
"strings"
"runtime"
"encoding/json"
"io/ioutil"
"github.com/petar/GoLLRB/llrb"
)
const (
alphaOnly = true // include/exclude words with non-alpha chars
badChars = "1234567890~`!@#$%&:;*()+=/-[]{}|\\\"^" // chars that constitute excluded words
countCutoff = 100 // words with lower counts are excluded
)
const (
ngramsDir = "/home/robert/ngrams"
ngramsBase = "grams"
totsBase = "tots"
ngramsExt = "csv"
ngramsLow = 1
ngramsHigh = 10
maxWords = 10000
jsonWords = "top.json"
)
func MarshalJsonList(file_name string, words []*Word) {
marshaled, err := json.Marshal(words)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(file_name, marshaled, os.ModePerm)
if err != nil {
panic(err)
}
}
func UnmarshalJsonList(file_name string) (words []*Word) {
data, err := ioutil.ReadFile(file_name)
if err != nil {
panic(err)
}
err = json.Unmarshal(data, &words)
if err != nil {
panic(err)
}
return
}
func TreeToWords(tree *llrb.Tree) []*Word {
words := make([]*Word, tree.Len())
count := 0
for word := range tree.IterDescend() {
words[count] = word.(*Word)
count++
}
return words
}
func WordsToTree(slice []*Word, lessFunc func(a, b interface{}) bool) *llrb.Tree {
tree := llrb.New(lessFunc)
for _, word := range slice {
tree.InsertNoReplace(word)
}
return tree
}
func TreeToXYonly(tree *llrb.Tree) []*XYonly {
words := make([]*XYonly, tree.Len())
count := 0
for word := range tree.IterDescend() {
words[count] = word.(*XYonly)
count++
}
return words
}
func XYonlyToTree(slice []*XYonly, lessFunc func(a, b interface{}) bool) *llrb.Tree {
tree := llrb.New(lessFunc)
for _, word := range slice {
tree.InsertNoReplace(word)
}
return tree
}
func NormCounts() (norm, pgnorm, bknorm map[int]int) {
fmt.Println("Loading total yearly counts.")
norm = make(map[int]int, 0)
pgnorm = make(map[int]int, 0)
bknorm = make(map[int]int, 0)
fname := totsBase + "." + ngramsExt
path := path.Join(ngramsDir, fname)
// open file and check for errors
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer file.Close()
reader := bufio.NewReader(file)
for {
line, _, err := reader.ReadLine()
if err != nil {
// probably EOF
fmt.Println(err)
break
}
pieces := strings.Split(string(line), "\t")
// skip this year if it doesn't have proper number of fields
if len(pieces) != 4 {
continue
}
year, _ := strconv.Atoi(pieces[0])
count, _ := strconv.Atoi(pieces[1])
pages, _ := strconv.Atoi(pieces[2])
books, _ := strconv.Atoi(pieces[3])
norm[year] = count
pgnorm[year] = pages
bknorm[year] = books
}
return
}
func lessWC(a, b interface{}) bool { return a.(*Word).TotalCount() <= b.(*Word).TotalCount()
}
func ProcessRaw() {
NCPU := runtime.NumCPU()
runtime.GOMAXPROCS(NCPU)
tree := llrb.New(lessWC)
ch := make(chan *Word, 100)
dead := make(chan bool)
for i := ngramsLow; i <= ngramsHigh; i++ {
fname := ngramsBase + strconv.Itoa(i) + "." + ngramsExt
path := path.Join(ngramsDir, fname)
go cleanupRawWords(path, ch, dead)
}
deadcount := 0
var done bool
for {
select {
case word := <-ch:
tree.InsertNoReplace(word)
if tree.Len() > maxWords {
tree.DeleteMin()
}
case <-dead:
deadcount++
if deadcount == ngramsHigh - ngramsLow + 1 {
done = true
}
}
if done {
break
}
}
words := TreeToWords(tree)
MarshalJsonList(jsonWords, words)
}
func cleanupRawWords(file_name string, ch chan *Word, dead chan bool) {
defer func() {dead <- true}()
fmt.Println("cleaning file ", file_name, "...")
// open file and check for errors
file, err := os.Open(file_name)
if err != nil {
panic(err)
}
defer file.Close()
reader := bufio.NewReader(file)
oldWordText := ""
badWord := ""
word := NewWord("")
for {
line, _, err := reader.ReadLine()
if err != nil {
// probably EOF
fmt.Println(err)
break
}
pieces := strings.Split(string(line), "\t")
// skip this word if it doesn't have proper number of fields
if len(pieces) != 5 {
continue
}
wordText := pieces[0]
// skip entries that correspond to wordText pre-id'ed as bad
if wordText == badWord {
continue
}
// skip words with numeric or other bad chars
if alphaOnly {
bad := false
for _, char := range badChars {
if strings.Contains(wordText, string(char)) {
badWord = wordText
bad = true;
break
}
}
if bad {continue}
}
year, _ := strconv.Atoi(pieces[1])
count, _ := strconv.Atoi(pieces[2])
pageCount, _ := strconv.Atoi(pieces[3])
bookCount, _ := strconv.Atoi(pieces[4])
// if wordText/data is a new word
if oldWordText != wordText {
oldWordText = wordText
ch <- word
word = NewWord(wordText)
}
word.AddEntry(year, count, pageCount, bookCount)
}
}
type XYonly struct {
W string // word text
X float32 // x coordinate
Y float32 // y coordinate
S float32 // DOI score
P float32 // color param
}
type Word struct {
T string // word text
C map[string] Entry // yearly count entries
tc int
}
type Entry struct {
Y int // year of count
W int // word count
P int // page count
B int // book count
}
func NewWord(text string) *Word {
word := Word{T:text}
word.C = make(map[string] Entry)
return &word
}
func (w *Word) Length() int {
return len(w.T)
}
func (w *Word) AddEntry(year, count, pageCount, bookCount int) {
w.C[strconv.Itoa(year)] = Entry {year, count, pageCount, bookCount}
}
func (w *Word) TotalPageDensity() float32 {
return float32(w.TotalCount()) / float32(w.TotalPages())
}
func (w *Word) PageDensity(year string) float32 {
_, ok := w.C[year]
if !ok {return 0}
return float32(w.C[year].W) / float32(w.C[year].P)
}
func (w *Word) TotalBookDensity() float32 {
return float32(w.TotalCount()) / float32(w.TotalBooks())
}
func (w *Word) BookDensity(year string) float32 {
_, ok := w.C[year]
if !ok {return 0}
return float32(w.C[year].W) / float32(w.C[year].B)
}
func (w *Word) Temperature(year string) float32 {
entry, ok := w.C[year]
if !ok {return 0}
return float32(entry.W) / w.MaxCount()
}
func (w *Word) MaxCount() float32 {
max := 0
for _, entry := range w.C {
if entry.W > max {
max = entry.W
}
}
return float32(max)
}
func (w *Word) TotalCount() int {
if w.tc == 0 {
for _, entry := range w.C {
w.tc += entry.W
}
}
return w.tc
}
func (w *Word) TotalPages() int {
var total int
for _, entry := range w.C {
total += entry.P
}
return total
}
func (w *Word) TotalBooks() int {
var total int
for _, entry := range w.C {
total += entry.B
}
return total
}
func (w *Word) String() string {
return fmt.Sprint(w)
}
////////////////////////////
// score calculating code //
////////////////////////////
func BuildXY(words []*Word, scores []float32, xmapper, ymapper, paramMapper func(w *Word) float32) []*XYonly {
xyonly := make([]*XYonly, len(words))
for i, w := range words {
x := xmapper(w)
y := ymapper(w)
p := paramMapper(w)
xyonly[i] = &XYonly{W:w.T, X:x, Y:y, P:p, S:scores[i]}
}
return xyonly
}
func Bk(year string) func(*Word) float32 {
return func(w *Word) float32 {
return float32(w.C[year].B)
}
}
func Pden(year string) func(*Word) float32 {
return func(w *Word) float32 {
return w.PageDensity(year)
}
}
func Bden(year string) func(*Word) float32 {
return func(w *Word) float32 {
return w.BookDensity(year)
}
}
func Tmp(year string) func(*Word) float32 {
return func(w *Word) float32 {
return w.Temperature(year)
}
}
func Cnt(year string) func(*Word) float32 {
return func(w *Word) float32 {
return float32(w.C[year].W)
}
}
func Pg(year string) func(*Word) float32 {
return func(w *Word) float32 {
return float32(w.C[year].P)
}
}
func Wlen(year string) func(*Word) float32 {
return func(w *Word) float32 {
return float32(w.Length())
}
}
type Weights struct {
Length float32
Count float32
Pages float32
Books float32
PageDen float32
Temp float32
BookDen float32
}
type Scorer func(w *Word) (float32, bool)
type ScoredWord struct {
W *Word
S float32
}
func WeightedScoreGenerator(year string, weights, maxes Weights) Scorer {
return func(w *Word) (float32, bool) {
if _, ok := w.C[year]; !ok {
return 0, false
}
var score float32 = 0
score += float32(w.Length()) / maxes.Length * weights.Length
score += float32(w.C[year].W) / maxes.Count * weights.Count
score += float32(w.C[year].P) / maxes.Pages * weights.Pages
score += float32(w.C[year].B) / maxes.Books * weights.Books
score += float32(w.PageDensity(year)) / maxes.PageDen * weights.PageDen
score += float32(w.Temperature(year)) / maxes.Temp * weights.Temp
score += float32(w.BookDensity(year)) / maxes.BookDen * weights.BookDen
return score, true
}
}
func GetScores(words []*Word, scorer Scorer) (scored []*Word, scores []float32) {
scores = make([]float32, 0)
scored = make([]*Word, 0)
NCPU := runtime.NumCPU()
runtime.GOMAXPROCS(NCPU)
percpu := int(float32(len(words)) / float32(NCPU) + 1)
ch := make(chan *ScoredWord, 100)
dead := make(chan bool)
if len(words) <= 100 {
NCPU = 1
percpu = len(words)
}
for i := 0; i < NCPU; i++ {
start := i * percpu
end := start + percpu
if end > len(words) {
end = len(words)
}
go calcScores(words[start:end], scorer, ch, dead)
}
done := false
deadcount := 0
for !done {
select {
case val := <-ch:
scores = append(scores, val.S)
scored = append(scored, val.W)
case <-dead:
deadcount++
if deadcount == NCPU {
done = true
}
}
}
return
}
func calcScores(words []*Word, scorer Scorer, ch chan *ScoredWord, dead chan bool) {
defer func() {dead <- true}()
for _, word := range words {
score, ok := scorer(word)
if ok {
sw := ScoredWord{W:word, S:score}
ch <- &sw
}
}
}