forked from jsfenfen/whatwordwhere
-
Notifications
You must be signed in to change notification settings - Fork 5
/
document_parser.py
53 lines (37 loc) · 1.57 KB
/
document_parser.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
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
Lazily read an hocr document, returning pages until it's done.
Pages are returned as cStringIO objects. Expects strict hocr adherence.
</html> must appear on it's own line to end a page...
Also, note that some hocr that's supposed to be utf-8 is actually latin-1.
https://groups.google.com/forum/#!topic/tesseract-ocr/UiyIMUWMzsU
Pass in the encoding explicitly if it's different.
"""
from cStringIO import StringIO
class document_parser(object):
def __init__(self, file_path, encoding='utf-8'):
self.file_source = open(file_path, 'r')
self.pages_read = 0
self.is_err = False
self.encoding = encoding
def __iter__(self):
return self
def next(self):
try:
return self.next_document()
except Exception:
raise StopIteration
def next_document(self):
page_to_return = StringIO()
end_of_page_found=False
while not end_of_page_found:
this_line = self.file_source.next().strip()
# Destroy all namespaces, meta, whatever. Much easier than trying to do this with lxml.
if this_line.startswith("<html"):
this_line = "<html>"
if this_line.startswith("</html>") :
end_of_page_found = True
if self.encoding != 'utf-8':
this_line = this_line.decode(self.encoding).encode('utf-8')
page_to_return.write(this_line)
self.pages_read += 1
return page_to_return