/
page_container.go
287 lines (243 loc) · 6.55 KB
/
page_container.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
package main
import (
"bytes"
"fmt"
"github.com/agonopol/go-stem/stemmer"
"github.com/jcla1/matrix"
"strings"
)
var (
pipelineFuncs = []FeatureFunc{
Counter,
(*PageContainer).SetIsRedirect,
(*PageContainer).SetIsFeatured,
(*PageContainer).SetPlainText,
(*PageContainer).SetSentences,
(*PageContainer).SetNumSentences,
(*PageContainer).SetAvgSentenceLen,
(*PageContainer).SetWords,
(*PageContainer).SetNumWords,
(*PageContainer).SetAvgWordLen,
(*PageContainer).SetUnigrams,
(*PageContainer).SetBigrams,
(*PageContainer).SetTrigrams,
(*PageContainer).SetNumHeadings,
(*PageContainer).SetNumExternalLinks,
(*PageContainer).SetNumLinks,
(*PageContainer).SetLinkDensity,
(*PageContainer).SetNumRefs,
(*PageContainer).SetNumCategories,
}
cleanFuncs = []CleanFunc{
replaceNbsps,
removeFiles,
removeComments,
removeTextStyling,
removeQuotes,
removeRefs,
removeTemplates,
removeTags,
removeCategories,
removeLinks,
removeExternalLinks,
removeHeadings,
removeListsSpaces,
//replaceDoubleLines,
}
)
type PageContainer struct {
PlainText string
Sentences []string
Words [][]string
AvgSentenceLen float64
AvgWordLen float64 // not quite sure if I should use this, it's always around 5.1 chars
LinkDensity float64
NumLinks int
NumExternalLinks int
NumHeadings int
NumRefs int
NumCategories int
NumSentences int
NumWords int
Unigrams map[string]int
Bigrams map[string]int
Trigrams map[string]int
IsFeatured bool
IsRedirect bool
NNOutput *matrix.Matrix
// actual data
Page *Page
}
func (container *PageContainer) String() string {
return fmt.Sprintf(`
Title: %s
# of Links: %d
Link Density: %f
# of external Links: %d
# of Headings: %d
# of Refrences: %d
# of Categories: %d
# of Sentences: %d
Avg Sentence Length: %f
# of Words: %d
Avg Word Length: %f
# of Unigrams: %d
# of Bigrams: %d
# of Trigrams: %d`,
container.Page.Title,
container.NumLinks,
container.LinkDensity,
container.NumExternalLinks,
container.NumHeadings,
container.NumRefs,
container.NumCategories,
container.NumSentences,
container.AvgSentenceLen,
container.NumWords,
container.AvgWordLen,
len(container.Unigrams),
len(container.Bigrams),
len(container.Trigrams),
)
}
// The function to use with the pipeline is:
// (*PageContainer).MethodName
// Signiture: func(*PageContainer)
// Which is the same as out FeatureFunc from pipeline
var (
c = 0
)
func Counter(p *PageContainer) {
c += 1
if c%100 == 0 {
fmt.Println("Processed over:", c, "articles")
}
}
func (container *PageContainer) SetIsFeatured() {
container.IsFeatured = strings.Contains(container.Page.Text(), "{{featured article}}")
}
func (container *PageContainer) SetIsRedirect() {
container.IsRedirect = (container.Page.Redirect != nil)
}
func (container *PageContainer) SetPlainText() {
pageText := container.Page.Text()
for _, f := range cleanFuncs {
pageText = f(pageText)
}
container.PlainText = pageText
}
func (container *PageContainer) SetSentences() {
sentences := sentenceRegex.Split(container.PlainText, -1)
removeIndicies := make([]int, 0)
for i, v := range sentences {
if len(v) < 2 {
removeIndicies = append(removeIndicies, i)
}
}
counter := 0
for _, i := range removeIndicies {
sentences = append(sentences[:i-counter], sentences[i-counter+1:]...)
counter += 1
}
container.Sentences = sentences
}
func (container *PageContainer) SetNumSentences() {
container.NumSentences = len(container.Sentences)
}
func (container *PageContainer) SetAvgSentenceLen() {
sum := 0
for _, v := range container.Sentences {
// may want to change this to # of words
sum += len(v)
}
container.AvgSentenceLen = float64(sum) / float64(len(container.Sentences))
}
func (container *PageContainer) SetWords() {
var words []string
var removeIndicies []int
var counter int
for _, v := range container.Sentences {
words = strings.Fields(v)
removeIndicies = make([]int, 0)
for i, w := range words {
words[i] = punctuationRegex.ReplaceAllString(w, "")
if len(words[i]) == 0 {
removeIndicies = append(removeIndicies, i)
} else {
//words[i] = strings.ToLower(words[i])
words[i] = string(stemmer.Stem([]byte(strings.ToLower(words[i]))))
}
}
counter = 0
for _, i := range removeIndicies {
words = append(words[:i-counter], words[i-counter+1:]...)
counter += 1
}
container.Words = append(container.Words, words)
}
}
func (container *PageContainer) SetNumWords() {
for _, words := range container.Words {
container.NumWords += len(words)
}
}
func (container *PageContainer) SetAvgWordLen() {
sum := 0
for _, words := range container.Words {
for _, w := range words {
sum += len(w)
}
}
container.AvgWordLen = float64(sum) / float64(container.NumWords)
}
func (container *PageContainer) SetUnigrams() {
container.Unigrams = make(map[string]int)
for _, words := range container.Words {
for _, w := range words {
container.Unigrams[w] += 1
}
}
}
func (container *PageContainer) SetBigrams() {
container.Bigrams = make(map[string]int)
var w string
for _, words := range container.Words {
for i := 0; i < len(words)-1; i++ {
w = words[i] + " " + words[i+1]
container.Bigrams[w] += 1
}
}
}
func (container *PageContainer) SetTrigrams() {
container.Trigrams = make(map[string]int)
var w string
for _, words := range container.Words {
for i := 0; i < len(words)-2; i++ {
w = words[i] + " " + words[i+1] + " " + words[i+2]
container.Trigrams[w] += 1
}
}
}
func (container *PageContainer) SetNumExternalLinks() {
container.NumExternalLinks = len(externalLinkCountingRegex.FindAllString(container.Page.Text(), -1))
}
func (container *PageContainer) SetNumLinks() {
container.NumLinks = len(linkRegex.FindAllString(container.Page.Text(), -1))
}
func (container *PageContainer) SetNumHeadings() {
container.NumHeadings = len(headingRegex.FindAllString(container.Page.Text(), -1))
}
func (container *PageContainer) SetNumRefs() {
container.NumRefs = strings.Count(container.Page.Text(), "</ref>")
}
func (container *PageContainer) SetNumCategories() {
container.NumCategories = strings.Count(container.Page.Text(), "[[Category:")
}
// Just takes internal Links into account
func (container *PageContainer) SetLinkDensity() {
buf := bytes.NewBufferString("")
for _, v := range linkRegex.FindAllStringSubmatch(container.Page.Text(), -1) {
buf.WriteString(" " + v[1] + " ")
}
container.LinkDensity = float64(len(strings.Fields(buf.String()))) / float64(container.NumWords)
}