-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.go
69 lines (60 loc) · 1.33 KB
/
document.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
package main
import (
"bytes"
"fmt"
"os"
"strings"
"golang.org/x/net/html"
)
type Document struct {
Path string
TermsFreq map[string]uint64
TotalTermsFreq uint64
}
func NewDocument(documentPath string) (*Document, error) {
fmt.Printf("Indexing %s...\n", documentPath)
content, err := parseEntireXmlFile(documentPath)
if err != nil {
return nil, err
}
doc := &Document{Path: documentPath}
doc.TermsFreq = make(map[string]uint64)
lexer := NewLexer(content)
for {
term, err := lexer.NextToken()
if err != nil {
break
}
doc.TermsFreq[term] += 1
}
for _, freq := range doc.TermsFreq {
doc.TotalTermsFreq += freq
}
return doc, nil
}
func (d *Document) TermFrequency(term string) float32 {
return float32(d.TermsFreq[term]) / float32(d.TotalTermsFreq)
}
func parseEntireXmlFile(filePath string) (string, error) {
content, err := os.ReadFile(filePath)
if err != nil {
return "", fmt.Errorf("ERROR: could not read file %s: %s", filePath, err)
}
tokenizer := html.NewTokenizer(bytes.NewReader(content))
var result strings.Builder
for {
tt := tokenizer.Next()
if tt == html.ErrorToken {
break
}
if tt == html.TextToken {
token := tokenizer.Token()
s := token.String()
s = strings.TrimSpace(s)
if len(s) > 0 {
result.WriteString(s + " ")
}
}
}
return result.String(), nil
}