-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
76 lines (64 loc) · 1.7 KB
/
main.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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"github.com/PageDash/boilertext/pkg/boilertext"
"github.com/PageDash/boilertext/pkg/extractor"
)
func main() {
splitPtr := flag.String("split", "word", "a string")
extractorPtr := flag.String("extractor", "shallow", "a string")
flag.Parse()
file, err := os.Open("sample/" + os.Args[len(os.Args)-1])
if err != nil {
log.Fatal("Failed to open file")
}
var blocks []*boilertext.TextBlock
genBlocksBasedOnFlag := func(file *os.File) {
defer file.Close()
if *splitPtr == "word" {
blocks, err = boilertext.GenerateTextBlocks(file, bufio.ScanWords)
if err != nil {
log.Fatal(err)
}
} else if *splitPtr == "rune" {
blocks, err = boilertext.GenerateTextBlocks(file, bufio.ScanRunes)
if err != nil {
log.Fatal(err)
}
} else {
log.Fatal("Missing split argument")
}
}
var ex boilertext.Extractor
if *extractorPtr == "shallow" {
ex = extractor.ShallowTextExtractor{}
genBlocksBasedOnFlag(file)
} else {
// Returns all text
ex = &extractor.AllTextExtractor{}
genBlocksBasedOnFlag(file)
// Calculate percentage of words that are links
linkCount := 0
linkWordCount := 0
wordCount := 0
for _, block := range blocks {
linkWordCount += block.NumOfAnchorWords
wordCount += block.NumOfWords
if block.NumOfAnchorWords > 0 {
linkCount++
}
fmt.Println("BLOCK", block)
}
fmt.Println("Percentage of words are links:", float64(linkWordCount)/float64(wordCount)*100.0, "%")
fmt.Println("Percentage of blocks containing links:", float64(linkCount)/float64(len(blocks))*100.0, "%")
}
res, err := ex.Process(blocks)
if err != nil {
log.Fatal("Extractor failed")
}
fmt.Println("RESULT", res)
}