-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats.go
150 lines (112 loc) · 3.75 KB
/
stats.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
package main
import (
"fmt"
"math"
"time"
)
func featureStats(input <-chan *PageContainer, n int) {
containers := make([]*PageContainer, 0, n)
for i := 0; i < n; i++ {
containers = append(containers, <-input)
}
fmt.Println("Standard Deviation of features:")
var avg float64
var stdDev float64
stdDev, avg = calcStats(containers, func(page *PageContainer) float64 {
return float64(page.NumLinks)
})
fmt.Printf("# of Links: %.3f (avg %.3f)\n", stdDev, avg)
stdDev, avg = calcStats(containers, func(page *PageContainer) float64 {
return page.LinkDensity
})
fmt.Printf("Link Density: %.3f (avg %.3f)\n", stdDev, avg)
stdDev, avg = calcStats(containers, func(page *PageContainer) float64 {
return float64(page.NumExternalLinks)
})
fmt.Printf("# of External Links: %.3f (avg %.3f)\n", stdDev, avg)
stdDev, avg = calcStats(containers, func(page *PageContainer) float64 {
return float64(page.NumHeadings)
})
fmt.Printf("# of Headings: %.3f (avg %.3f)\n", stdDev, avg)
stdDev, avg = calcStats(containers, func(page *PageContainer) float64 {
return float64(page.NumRefs)
})
fmt.Printf("# of Refrences: %.3f (avg %.3f)\n", stdDev, avg)
stdDev, avg = calcStats(containers, func(page *PageContainer) float64 {
return float64(page.NumCategories)
})
fmt.Printf("# of Categories: %.3f (avg %.3f)\n", stdDev, avg)
stdDev, avg = calcStats(containers, func(page *PageContainer) float64 {
return float64(page.NumSentences)
})
fmt.Printf("# of Sentences: %.3f (avg %.3f)\n", stdDev, avg)
stdDev, avg = calcStats(containers, func(page *PageContainer) float64 {
return page.AvgSentenceLen
})
fmt.Printf("Avg Sentence Length: %.3f (avg %.3f)\n", stdDev, avg)
stdDev, avg = calcStats(containers, func(page *PageContainer) float64 {
return float64(page.NumWords)
})
fmt.Printf("# of Words: %.3f (avg %.3f)\n", stdDev, avg)
stdDev, avg = calcStats(containers, func(page *PageContainer) float64 {
return page.AvgWordLen
})
fmt.Printf("Avg Word Length: %.3f (avg %.3f)\n", stdDev, avg)
stdDev, avg = calcStats(containers, func(page *PageContainer) float64 {
return float64(len(page.Unigrams))
})
fmt.Printf("# of Unigrams: %.3f (avg %.3f)\n", stdDev, avg)
stdDev, avg = calcStats(containers, func(page *PageContainer) float64 {
return float64(len(page.Bigrams))
})
fmt.Printf("# of Bigrams: %.3f (avg %.3f)\n", stdDev, avg)
stdDev, avg = calcStats(containers, func(page *PageContainer) float64 {
return float64(len(page.Trigrams))
})
fmt.Printf("# of Trigrams: %.3f (avg %.3f)\n", stdDev, avg)
}
func testTiming(iterations int, f func()) {
t0 := time.Now()
for i := 0; i < iterations; i++ {
f()
}
t1 := time.Now()
fmt.Printf("It took %v for %d iterations. (Avg. %.3fs)\n", t1.Sub(t0), iterations, t1.Sub(t0).Seconds()/float64(iterations))
return
}
// Returns stdDeviation and Avg
func calcStats(containers []*PageContainer, f func(*PageContainer) float64) (float64, float64) {
vals := make([]float64, len(containers))
mean := float64(0)
for i, page := range containers {
if page.IsRedirect {
i -= 1
continue
} else {
vals[i] = f(page)
mean += f(page)
}
}
mean = mean / float64(len(vals))
devSum := float64(0)
for _, v := range vals {
devSum += (float64(v) - mean) * (float64(v) - mean)
}
avg := float64(0)
for _, v := range containers {
avg += float64(f(v))
}
return math.Sqrt(devSum / float64(len(vals))), avg / float64(len(containers))
}
func calcStdDev(nums []float64) (float64, float64) {
mean := float64(0)
for _, v := range nums {
mean += v
}
mean = mean / float64(len(nums))
devSum := float64(0)
for _, v := range nums {
devSum += (float64(v) - mean) * (float64(v) - mean)
}
return math.Sqrt(devSum / float64(len(nums))), mean
}