forked from dkrotx/ts-idx-2016
-
Notifications
You must be signed in to change notification settings - Fork 6
/
docreader.py
executable file
·40 lines (31 loc) · 1.13 KB
/
docreader.py
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
#!/usr/bin/env python
import argparse
import document_pb2
import struct
import gzip
import sys
class DocumentStreamReader:
def __init__(self, paths):
self.paths = paths
def open_single(self, path):
return gzip.open(path, 'rb') if path.endswith('.gz') else open(path, 'rb')
def __iter__(self):
for path in self.paths:
with self.open_single(path) as stream:
while True:
sb = stream.read(4)
if sb == '':
break
size = struct.unpack('i', sb)[0]
msg = stream.read(size)
doc = document_pb2.document()
doc.ParseFromString(msg)
yield doc
def parse_command_line():
parser = argparse.ArgumentParser(description='compressed documents reader')
parser.add_argument('files', nargs='+', help='Input files (.gz or plain) to process')
return parser.parse_args()
if __name__ == '__main__':
reader = DocumentStreamReader(parse_command_line().files)
for doc in reader:
print "%s\t%d bytes" % (doc.url, len(doc.text))