-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract.go
More file actions
86 lines (79 loc) · 1.94 KB
/
Copy pathextract.go
File metadata and controls
86 lines (79 loc) · 1.94 KB
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
package main
import (
"bytes"
"strings"
"unicode"
"log"
"golang.org/x/net/html"
"github.com/kljensen/snowball"
)
// to apply same string modification to input and extracted words
func stringMod(sent string) []string {
isPunc := func (let rune) bool {
return !unicode.IsLetter(let) && !unicode.IsNumber(let)
}
words := strings.FieldsFunc(strings.ToLower(sent), isPunc) // removes punctuation
for idx, word := range words {
if stemmed, err := snowball.Stem(word, "english", true); err == nil {
words[idx] = stemmed
} else {
log.Fatalf("Stemmed Failed: %v\n", err)
}
}
return words
}
func extract(dl *DownloadResult, chOut chan ExtractResult) {
words := []string{}
hrefs := []string{}
imgs := make(map[string]string)
var title string
doc, err := html.Parse(bytes.NewReader(dl.body))
if err != nil {
log.Fatalf("Could not parse doc: %v\n", err)
}
var f func(*html.Node)
f = func(n *html.Node) {
switch n.Type {
case html.ElementNode:
if n.Data == "img" {
var src string
for _, attr := range n.Attr {
if attr.Key == "src" {
src = attr.Val
}
if attr.Key == "alt" {
imgs[src] = attr.Val
}
}
}
// extracting hrefs
for _, attr := range n.Attr {
if attr.Key == "href" {
hrefs = append(hrefs, attr.Val)
}
}
// extracting title node
if n.Data == "title" && n.Parent.Data == "head" {
title = n.FirstChild.Data
}
case html.TextNode:
p := n.Parent
if p.Type == html.ElementNode && (p.Data != "style" && p.Data != "script") {
toks := stringMod(n.Data)
words = append(words, toks...)
}
}
// go through the child nodes recursively
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(doc)
urls := []string{}
for _, href := range hrefs {
if cleanUrl, ok := clean(dl.url, href); ok {
urls = append(urls, cleanUrl)
}
}
chOut <- ExtractResult{dl.url, title, words, urls, imgs}
}