-
Notifications
You must be signed in to change notification settings - Fork 0
/
stork.go
416 lines (348 loc) · 9.26 KB
/
stork.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
// package stork implements an algorithm of html content extraction.
//
// It claims to bring a simple, robust, accurate and language-independent solution
// for extracting the main content of an HTML-formatted Web page and for
// removing additional content such as navigation menus, functional
// and design elements, and commercial advertisements.
//
// This method creates a text density graph of a given Web page and then
// selects the region of the Web page with the highest density.
//
// For more information about the original method, please have a look
// at the following paper.
//
// https://github.com/lobre/stork/raw/master/Language_Independent_Content_Extraction.pdf
//
// It provides here an implementation of the method given in the paper
// but is not affiliated with the research.
//
// Before analysing the html document, the process first applies some simple techniques
// to simplify the content.
// - strip everything that is not in the body tag
// - strip some unwanted tags
// - apply a simple whitespace removal strategy
package stork
import (
"errors"
"fmt"
"io"
"strings"
"text/tabwriter"
"github.com/guptarohit/asciigraph"
"golang.org/x/net/html"
)
// leashParams represents parameters needed
// to calculate a leash from a text size.
type leashParams struct {
minLength, maxLength float64
minLeash, maxLeash float64
}
// default values for leash calculation
var defaultLeashParams = leashParams{0, 400, 0, 40}
var htmlSkeleton string = "<!DOCTYPE html><html><head><meta charset=\"utf-8\" /><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" /></head><body></body></html>"
// blockText stores the textual representation of
// a structural block element on an html page.
// It aims to be used in a slice to
// calculate the density.
type blockText struct {
// blocks holds all html text nodes
// contained in the corresponding text
blocks []*html.Node
text string
}
// Article contains all the extracted values of an html document.
// It should be created using the From() method.
type Article struct {
// A header image to use for the article
Thumbnail *Image
// Images contained in the extracted article
Images []*Image
// Links contained in the extracted article
Links []string
// Metadata taken from the html document
Meta struct {
Lang string
Canonical string
Title string
Favicon string
Description string
Keywords string
OpenGraph map[string]string
}
density []blockText
output *html.Node
}
// Image contains information taken from a <img> html tag.
type Image struct {
Src string
Width uint
Height uint
node *html.Node
}
// From parses an html document from an io.Reader
// and extracts the content into an Article.
func From(r io.Reader) (*Article, error) {
var a Article
doc, err := html.Parse(r)
if err != nil {
return nil, err
}
// search body
var body *html.Node
iterate(doc, func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "body" {
body = n
}
})
if body == nil {
return nil, errors.New("body not found")
}
// TODO
if err := a.extractMetadata(doc); err != nil {
return nil, err
}
// TODO
if err := a.extractThumbnail(doc); err != nil {
return nil, err
}
if err := a.clean(body); err != nil {
return nil, err
}
// TODO
// this should create
if err := a.calculateDensity(body); err != nil {
return nil, err
}
// TODO
if err := a.extractContent(body); err != nil {
return nil, err
}
// TODO parse article images
// TODO assert if really an article
return &a, nil
}
func (a *Article) extractThumbnail(doc *html.Node) error {
return nil
}
func (a *Article) extractMetadata(doc *html.Node) error {
return nil
}
// clean will apply a first layer of cleaning to the parsed html.
//
// It will:
// - remove unwanted tags
// - remove comments
// - apply a whitespace removal strategy
func (a *Article) clean(body *html.Node) error {
iterate(body, func(n *html.Node) {
switch n.Type {
case html.CommentNode:
remove(n)
case html.ElementNode:
if IgnoreTags[n.Data] {
remove(n)
}
// remove class
var keep []html.Attribute
for _, attr := range n.Attr {
if attr.Key != "class" {
keep = append(keep, attr)
}
}
n.Attr = keep
}
})
minify(body)
return nil
}
func (a *Article) calculateDensity(body *html.Node) error {
a.density = nil
a.density = append(a.density, blockText{nil, ""})
idx := 0
iterate(body, func(n *html.Node) {
switch n.Type {
case html.ElementNode:
if blockTags[n.Data] {
a.density = append(a.density, blockText{[]*html.Node{}, ""})
idx++
}
case html.TextNode:
a.density[idx].text += n.Data
a.density[idx].blocks = append(a.density[idx].blocks, n)
}
})
return nil
}
// body parameter is temporary while the density is not implemented
func (a *Article) extractContent(body *html.Node) error {
if len(a.density) <= 0 {
return errors.New("wrong density")
}
// find longest text
smax, maxl := 0, 0
for i, d := range a.density {
if len(d.text) > maxl {
smax = i
maxl = len(d.text)
}
}
// high density region
hdr := []int{smax}
restart := true
for restart {
restart = false
for i, d := range a.density {
add := false
for _, j := range hdr {
// already exists
if i == j {
add = false
break
}
leash := calculateLeash(defaultLeashParams, len(d.text))
if abs(i-j) < leash {
add = true
}
}
if add {
hdr = append(hdr, i)
restart = true
}
}
}
start, end := smax, smax
for _, i := range hdr {
if i < start {
start = i
}
if i > end {
end = i
}
}
if err := a.assembleOutput(start, end); err != nil {
return err
}
return nil
}
func (a *Article) assembleOutput(start, end int) error {
reader := strings.NewReader(htmlSkeleton)
var err error
a.output, err = html.Parse(reader)
if err != nil {
return err
}
// search head and body
var root, head, body *html.Node
iterate(a.output, func(n *html.Node) {
if n.Type == html.ElementNode {
switch n.Data {
case "html":
root = n
case "head":
head = n
case "body":
body = n
}
}
})
if root == nil || head == nil || body == nil {
return errors.New("error parsing html skeleton")
}
if a.Meta.Lang != "" {
root.Attr = append(root.Attr, html.Attribute{Key: "lang", Val: a.Meta.Lang})
}
if a.Meta.Description != "" {
head.AppendChild(createNode("meta", "", []html.Attribute{
html.Attribute{Key: "name", Val: "description"},
html.Attribute{Key: "content", Val: a.Meta.Description},
}))
}
if a.Meta.Keywords != "" {
head.AppendChild(createNode("meta", "", []html.Attribute{
html.Attribute{Key: "name", Val: "keywords"},
html.Attribute{Key: "content", Val: a.Meta.Keywords},
}))
}
for k, v := range a.Meta.OpenGraph {
head.AppendChild(createNode("meta", "", []html.Attribute{
html.Attribute{Key: "property", Val: "og:" + k},
html.Attribute{Key: "content", Val: v},
}))
}
if a.Meta.Title != "" {
head.AppendChild(createNode("title", a.Meta.Title, nil))
body.AppendChild(createNode("h1", a.Meta.Title, nil))
}
if a.Meta.Canonical != "" {
head.AppendChild(createNode("link", "", []html.Attribute{
html.Attribute{Key: "rel", Val: "canonical"},
html.Attribute{Key: "href", Val: a.Meta.Canonical},
}))
}
if a.Meta.Favicon != "" {
head.AppendChild(createNode("link", "", []html.Attribute{
html.Attribute{Key: "rel", Val: "shortcut icon"},
html.Attribute{Key: "href", Val: a.Meta.Favicon},
html.Attribute{Key: "type", Val: "image/x-icon"},
}))
}
if a.Thumbnail != nil {
// shallow copy
thumb := *a.Thumbnail.node
thumb.Parent, thumb.PrevSibling, thumb.NextSibling = nil, nil, nil
body.AppendChild(&thumb)
}
// append article content
firstBlock := a.density[start].blocks[0]
lastBlock := a.density[end].blocks[len(a.density[end].blocks)-1]
firstBlock, lastBlock = ancestorsWithSameParent(firstBlock, lastBlock)
cur := firstBlock
for {
// shallow copy
block := *cur
block.Parent, block.PrevSibling, block.NextSibling = nil, nil, nil
body.AppendChild(&block)
if cur == lastBlock {
break
}
cur = cur.NextSibling
}
return nil
}
// Density returns the content from the density table
// with index and text length prepended to each text item.
// This function is for debug purposes.
func (a *Article) Density() string {
var b strings.Builder
writer := tabwriter.NewWriter(&b, 0, 8, 1, '\t', 0)
for i, d := range a.density {
leash := calculateLeash(defaultLeashParams, len(d.text))
fmt.Fprintf(writer, "%d\tlen: %d\tleash: %d\t%s\n", i, len(d.text), leash, d.text)
}
writer.Flush()
return b.String()
}
// Plot will draw the density graph calculated
// from the extracted article.
//
// It will generate a graph similar to the one on figure 2 at page 3 of the paper.
// https://github.com/lobre/stork/raw/master/Language_Independent_Content_Extraction.pdf
func (a *Article) Plot() string {
var data []float64
for _, t := range a.density {
data = append(data, float64(len(t.text)))
}
return asciigraph.Plot(data, asciigraph.Height(30))
}
func calculateLeash(lp leashParams, length int) int {
var res float64
res = (((lp.maxLeash - lp.minLeash) * (float64(length) - lp.minLength)) /
(lp.maxLength - lp.minLength)) + lp.minLeash
if res < lp.minLeash {
res = lp.minLeash
}
if res > lp.maxLeash {
res = lp.maxLeash
}
return int(res)
}