-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
250 lines (216 loc) · 6.31 KB
/
process.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
package giawarc
import (
"bufio"
"io"
"log"
"net/http"
"sort"
"strings"
"github.com/paracrawl/giawarc/cld2"
// "github.com/jmhodges/gocld3/cld3"
"github.com/paracrawl/go-warc/warc"
"github.com/spaolacci/murmur3"
)
// This structure implements the reading side of the WARC preprocessor.
type WARCPreProcessor struct {
wf *warc.WARCFile
tw TextWriter
inputHashes map[uint32]struct{}
outputHashes map[uint32]struct{}
inputHashReader GzOrXzReader
outputHashWriter ZipWriter
inputHashing bool
outputHashing bool
langDetection string
// cld3Model cld3.LanguageIdentifier
Filename string
TextRecords int // records claiming to be text
LangRecords int // records where we can tell the language
TotalRecords int // total records
ContentCounts map[string]int // statistics about content types
TextBytes int // bytes claiming to be text
LangBytes int // bytes claiming to be text where we know the language
TotalBytes int // total bytes
}
// Create a preprocessor given a readable buffer containing a (gzipped) WARC file.
// The second argument, the TextRecord channel is where texts that are found will
// be sent. It will be closed when the file is done.
func NewWARCPreProcessor(rc io.ReadCloser, tw TextWriter, inputHashReader GzOrXzReader, outputHashWriter ZipWriter, langDetection string) (wp *WARCPreProcessor, err error) {
var p WARCPreProcessor
p.ContentCounts = make(map[string]int)
p.wf, err = warc.NewWARCFile(rc)
if err != nil {
return
}
p.tw = tw
p.inputHashReader = inputHashReader
p.outputHashWriter = outputHashWriter
p.inputHashing = (inputHashReader != (GzOrXzReader{}))
p.outputHashing = (outputHashWriter != (ZipWriter{}))
p.inputHashes = make(map[uint32]struct{})
p.outputHashes = make (map[uint32]struct{})
p.langDetection = langDetection
wp = &p
return
}
// Loop through each record and process it
func (p *WARCPreProcessor) Process() {
if p.inputHashing {
p.inputHashes = p.inputHashReader.ReadHashes()
}
/*
if p.langDetection == "cld3" {
langIdModel, err := cld3.NewLanguageIdentifier(0,1024)
if err != nil {
log.Println("Error creating cld3 model")
return
}
p.cld3Model = langIdModel
defer cld3.FreeLanguageIdentifier(p.cld3Model)
}
*/
reader := p.wf.GetReader()
reader.Iterate(p.processRecord)
if p.outputHashing {
p.outputHashWriter.WriteHashes(p.outputHashes)
}
}
// Callback from the WARC reader Iterate function
func (p *WARCPreProcessor) processRecord(wr *warc.WARCRecord, err error) {
if err != nil {
if err != io.EOF {
log.Printf("Error reading WARC record: %v", err)
}
return
}
warc_type := wr.GetHeader().GetType()
if warc_type == "warcinfo" {
p.Filename, _ = wr.GetHeader().Get("WARC-Filename")
log.Printf("Processing %v", p.Filename)
}
// content type of the WARC record not the payload
content_type, _ := wr.GetHeader().Get("Content-Type")
if !strings.Contains(content_type, "application/http") || !strings.Contains(content_type, "response") {
// nothing to do
// log.Printf("Ignoring WARC record (not application/http)")
return
}
content_length := wr.GetHeader().GetContentLength()
date, _ := wr.GetHeader().Get("WARC-Date")
// record some statistics
p.TotalRecords += 1
p.TotalBytes += content_length
uri, _ := wr.GetHeader().Get("WARC-Target-URI")
// skip robots.txt
if strings.HasSuffix(uri, "robots.txt") {
return
}
// get HTTP response out of the WARC file, and parse it
payload := wr.GetPayload()
resp, err := http.ReadResponse(bufio.NewReader(payload.GetReader()), nil)
reader := payload.GetReader()
if err == nil {
content_type, _ = CleanContentType(resp.Header.Get("Content-Type"))
// record some statistics
count, ok := p.ContentCounts[content_type]
if !ok {
p.ContentCounts[content_type] = 1
} else {
p.ContentCounts[content_type] = count + 1
}
// here is where we would do, is it a PDF? transform to text and then continue,
// is it a doc? transform to text and continue
content_type, iszip := IsZip(content_type, uri)
if iszip {
reader, err = ReadZipPayload(content_type, resp.Body)
if err != nil {
return
}
} else if IsText(content_type) {
reader = resp.Body
} else {
return
}
}
p.TextRecords += 1
p.TextBytes += content_length
// transform to UTF-8 and normalise, strip HTML stuff
text, err := CleanText(reader, content_type)
if err != nil {
log.Printf("Error reading content for %v: %v (read %d bytes)", uri, err, len(text))
return
}
var lang string
var ok bool
/*
if p.langDetection == "cld3" {
res := p.cld3Model.FindLanguage(text)
lang = res.Language
ok = res.IsReliable
} else {
*/
lang, ok = cld2.DetectLang(text)
/*
}
*/
if !ok {
return
}
tidied := CleanSpaces(text) // clean up excess whitespace
if p.outputHashing || p.inputHashing {
// hash clean text
newhash := murmur3.Sum32([]byte (tidied))
_, exists := p.inputHashes[newhash]
if exists { return }
_, exists = p.outputHashes[newhash]
if exists { return }
// store new hash and continue
p.outputHashes[newhash] = Empty
}
// record some statistics
p.LangRecords += 1
p.LangBytes += content_length
recid := wr.GetHeader().GetRecordId()
recid = strings.TrimPrefix(recid, "<urn:uuid:")
recid = strings.TrimSuffix(recid, ">")
// send off a TextRecord to whatever will write it
rec := TextRecord{
Source: p.Filename,
Date: date,
RecordId: recid,
URI: uri,
ContentType: content_type,
Lang: lang,
Text: tidied,
}
_, err = p.tw.WriteText(&rec)
}
// Utility to get statistics about content types for printing out.
func (p *WARCPreProcessor) ContentTypeStats() ContentStats {
cts := make(ContentStats, len(p.ContentCounts))
for k, v := range p.ContentCounts {
s := ContentStat{ContentType: k, Prevalence: float64(v) / float64(p.TotalRecords)}
cts = append(cts, s)
}
sort.Sort(cts)
return cts
}
// A statistic about a content type
type ContentStat struct {
ContentType string
Prevalence float64
}
// An array of content-type statistics, with a new name so that we
// can sort it by prevalence
type ContentStats []ContentStat
func (cts ContentStats) Len() int {
return len(cts)
}
func (cts ContentStats) Less(i, j int) bool {
return cts[i].Prevalence > cts[j].Prevalence
}
func (cts ContentStats) Swap(i, j int) {
tmp := cts[i]
cts[i] = cts[j]
cts[j] = tmp
}