-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
70 lines (51 loc) · 2.3 KB
/
test.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from bs4 import BeautifulSoup
import difflib
import numpy as np
hocr_file = "/media/leon/22CEF525CEF4F241/LEONLAH/CityUHack/CityU-Hackathon-2019/Training/1/hocr/1-74.hocr"
shape = (164,1)
hocr_report1 = np.chararray(shape)
# for i in (0,164):
# hocr_report+
search_terms = ('financial', 'year')
def parse_hocr(search_terms=None, hocr_file=None, regex=None):
"""Parse the hocr file and find a reasonable bounding box for each of the strings
in search_terms. Return a dictionary with values as the bounding box to be used for
extracting the appropriate text.
inputs:
search_terms = Tuple, A tuple of search terms to look for in the HOCR file.
outputs:
box_dict = Dictionary, A dictionary whose keys are the elements of search_terms and values
are the bounding boxes where those terms are located in the document.
"""
# Make sure the search terms provided are a tuple.
if not isinstance(search_terms,tuple):
raise ValueError('The search_terms parameter must be a tuple')
# Make sure we got a HOCR file handle when called.
if not hocr_file:
raise ValueError('The parser must be provided with an HOCR file handle.')
# Open the hocr file, read it into BeautifulSoup and extract all the ocr words.
hocr = open(hocr_file,'r').read()
soup = BeautifulSoup(hocr,'html.parser')
words = soup.find_all('span',class_='ocrx_word')
result = dict()
# Loop through all the words and look for our search terms.
for word in words:
w = word.get_text().lower()
for s in search_terms:
# If the word is in our search terms, find the bounding box
if len(w) > 1 and difflib.SequenceMatcher(None, s, w).ratio() > .5:
bbox = word['title'].split(';')
bbox = bbox[0].split(' ')
bbox = tuple([int(x) for x in bbox[1:]])
count = bbox.count(s)
# Update the result dictionary or raise an error if the search term is in there twice.
if s not in result.keys():
result.update({s:bbox})
else:
result.update({s+"("+str(count)+")":bbox})
else:
pass
return result
print('\n')
print(hocr_file)
print(parse_hocr(search_terms, hocr_file, ))