🚧 dinglehopper: Extract text while retaining segment id info

pull/38/head
Gerber, Mike 5 years ago
parent 6d0db229fa
commit bc1002b1e6

@ -148,21 +148,35 @@ def page_text(tree):
return page_extract(tree).text
def text(filename):
"""Read the text from the given file.
def plain_extract(filename):
with open(filename, 'r') as f:
return ExtractedText(
(ExtractedTextSegment('line %d' % no, line) for no, line in enumerate(f.readlines())),
'\n'
)
def plain_text(filename):
return plain_extract(filename).text
def extract(filename):
"""Extract the text from the given file.
Supports PAGE, ALTO and falls back to plain text.
"""
try:
tree = ET.parse(filename)
except XMLSyntaxError:
with open(filename, 'r') as f:
return f.read()
return plain_extract(filename)
try:
return page_text(tree)
return page_extract(tree)
except ValueError:
return alto_text(tree)
return alto_extract(tree)
def text(filename):
return extract(filename).text
if __name__ == '__main__':

Loading…
Cancel
Save