forked from AjaykumarAI/keyword_extraction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_per.py
299 lines (242 loc) · 11.6 KB
/
get_per.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import boto3
import json
import pandas as pd
import re
import os
def process_text_analysis(bucket, document):
s3_connection = boto3.resource("s3")
client = boto3.client('s3')
result = client.get_object(Bucket=bucket, Key=document)
text = result['Body'].read().decode('utf-8')
res = json.loads(text)
left_cor = []
top_cor = []
width_cor = []
height_cor = []
page = []
line_text = []
for response in res:
blocks = response["Blocks"]
for block in blocks:
if block["BlockType"] == "LINE":
left_cor.append(float("{:.3f}".format(block["Geometry"]["BoundingBox"]["Left"])))
top_cor.append(float("{:.3f}".format(block["Geometry"]["BoundingBox"]["Top"])))
width_cor.append(float("{:.3f}".format(block["Geometry"]["BoundingBox"]["Width"])))
height_cor.append(float("{:.3f}".format(block["Geometry"]["BoundingBox"]["Height"])))
line_text.append(block["Text"])
page.append(block["Page"])
df_line = pd.DataFrame(list(zip(left_cor, top_cor, width_cor, height_cor, line_text, page)),
columns=["xmin", "ymin", "width_cor", "height_cor", "line_text", "page"])
df_line["xmax"] = (df_line["xmin"] + df_line["width_cor"])
df_line["ymax"] = (df_line["ymin"] + df_line["height_cor"])
left_cor = []
top_cor = []
width_cor = []
height_cor = []
page = []
word_text = []
for response in res:
blocks = response["Blocks"]
for block in blocks:
if block["BlockType"] == "WORD":
left_cor.append(float("{:.3f}".format(block["Geometry"]["BoundingBox"]["Left"])))
top_cor.append(float("{:.3f}".format(block["Geometry"]["BoundingBox"]["Top"])))
width_cor.append(float("{:.3f}".format(block["Geometry"]["BoundingBox"]["Width"])))
height_cor.append(float("{:.3f}".format(block["Geometry"]["BoundingBox"]["Height"])))
word_text.append(block["Text"])
page.append(block["Page"])
df_word = pd.DataFrame(list(zip(left_cor, top_cor, width_cor, height_cor, word_text, page)),
columns=["xmin", "ymin", "width_cor", "height_cor", "word_text", "page"])
df_word["xmax"] = (df_word["xmin"] + df_word["width_cor"])
df_word["ymax"] = (df_word["ymin"] + df_word["height_cor"])
pages = df_line.page.unique().tolist()
text_dict = {}
for p in pages:
dfp = df_line[df_line.page == p]
txt_list = dfp.line_text.tolist()
txt = " ".join(txt_list)
text_dict[p] = txt
return text_dict, res, df_line, df_word
def get_reference_page(text_dict):
reference_page = None
pattern_security_details = r'(Security\s*Details?\s*:?)|(SECURITY\s*DETAILS?\s*:?)|(Security\s*details?\s*:?)|(SIGNED\s*UNDERWRITERS?\s*:?)|(Reinsured\s*Signing\s*Page?\s*:?)|(Reinsurer\s*Signing\s*Page?\s*:?)'
pattern_wr = r'(WRITTEN\s*LINES\s*:?)|(Written\s*:?)'
pattern_tmk = r'(TMK1880\s*:?)'
pattern_kln = r'(KLN510\s*:?)'
pattern_lloyd = r'(Lloyd’s?\s*:?)|(Lloyd’s\s*Underwriter\s*Synd.?\s*:?)'
pattern = r'[A-Za-z]{1}\s*[A-Za-z]{1}\s*[A-Za-z]{1}\s*\d{1}\s*\d{1}\s*\d{1}\s*[A-Za-z]{1}\s*\d{1}\s*\d{1}\s*[A-Za-z]{1}\s*[A-Za-z]{1}'
for page_no, page_text in text_dict.items():
match_sec = re.search(pattern_security_details, page_text)
match_wr = re.search(pattern_wr, page_text)
match_tmk = re.search(pattern_tmk, page_text)
match_kln = re.search(pattern_kln, page_text)
match_lloyd = re.search(pattern_lloyd, page_text)
match_pattern = re.search(pattern, page_text)
if (match_sec and match_wr and match_pattern) or (
match_sec and match_tmk and match_kln and match_pattern) or (
match_sec and match_tmk and match_kln and match_lloyd and match_pattern) or (
match_wr and match_tmk and match_kln and match_pattern):
reference_page = page_no
break
return reference_page
def get_refernce_json_page(bucket, document, output_json, page_no):
s3_connection = boto3.resource("s3")
client = boto3.client('s3')
result = client.get_object(Bucket=bucket, Key=document)
text = result['Body'].read().decode('utf-8')
res = json.loads(text)
page_blocks = []
for response in res:
blocks = response["Blocks"]
for block in blocks:
if 'Page' in block and block['Page'] == page_no:
page_blocks.append(block)
page_data = {'Blocks': page_blocks}
with open(output_json, 'w') as f:
json.dump(page_data, f)
def add_space(reference_id):
reference_id = ' '.join(reference_id)
return reference_id
def get_coordinates_of_string(bucket, file_path, search_string):
s3_connection = boto3.resource("s3")
client = boto3.client('s3')
result = client.get_object(Bucket=bucket, Key=file_path)
text = result['Body'].read().decode('utf-8')
textract_output = json.loads(text)
try:
for res in textract_output:
for block in res['Blocks']:
if block['BlockType'] in ['WORD', 'LINE'] and block['Text'] == search_string:
bounding_box = block['Geometry']['BoundingBox']
coordinates = {
'x': bounding_box['Left'],
'y': bounding_box['Top'],
'w': bounding_box['Width'],
'h': bounding_box['Height'],
}
return coordinates
elif block['BlockType'] in ['WORD', 'LINE'] and block['Text'] == add_space(search_string):
bounding_box = block['Geometry']['BoundingBox']
coordinates = {
'x': bounding_box['Left'],
'y': bounding_box['Top'],
'w': bounding_box['Width'],
'h': bounding_box['Height'],
}
return coordinates
except Exception as e:
print('An error occurred:', e)
return None
def extract_reference_ids_and_draw_bounding_boxes_from_json(json_file, regex_pattern):
with open(json_file, "r") as file:
response = json.load(file)
blocks = response['Blocks']
text = ''
for item in response['Blocks']:
if item["BlockType"] == 'LINE':
text += item['Text'] + ' '
reference_ids = []
reference_id_coordinates = []
for block in blocks:
if block['BlockType'] == 'LINE':
text = block['Text']
match = re.search(regex_pattern, text)
if match:
reference_id = match.group(0)
reference_ids.append(reference_id)
reference_id_coordinates.append(block['Geometry']['BoundingBox'])
x = reference_id_coordinates[0]['Left']
y = reference_id_coordinates[0]['Top']
w = reference_id_coordinates[0]['Width']
h = reference_id_coordinates[0]['Height']
return x, y, w, h
def get_surrounding_text(file_path, x, y, w, h):
with open(file_path, 'r') as f:
textract_output = json.load(f)
left_texts, right_texts, top_texts, bottom_texts = [], [], [], []
percentage_pattern = r'\b(?!(?:80|20)%)\d+(?:\.\d+)?\s*%'
for block in textract_output['Blocks']:
if block['BlockType'] in ['WORD', 'LINE']:
left = block['Geometry']['BoundingBox']['Left']
top = block['Geometry']['BoundingBox']['Top']
width = block['Geometry']['BoundingBox']['Width']
height = block['Geometry']['BoundingBox']['Height']
if left < x and (y <= top <= y + h) or (y - 0.4 * h <= top <= y + 1 + h):
left_texts.append(block['Text'])
if left + width > x + w and (y <= top <= y + h):
right_texts.append(block['Text'])
if top < y and (x <= left <= x + w):
top_texts.append(block['Text'])
if top + height > y + h and (x <= left <= x + w):
bottom_texts.append(block['Text'])
left_percentages = [re.findall(percentage_pattern, text) for text in left_texts]
right_percentages = [re.findall(percentage_pattern, text) for text in right_texts]
top_percentages = [re.findall(percentage_pattern, text) for text in top_texts]
bottom_percentages = [re.findall(percentage_pattern, text) for text in bottom_texts]
left_percentages = [percentage for sublist in left_percentages for percentage in sublist]
right_percentages = [percentage for sublist in right_percentages for percentage in sublist]
top_percentages = [percentage for sublist in top_percentages for percentage in sublist]
bottom_percentages = [percentage for sublist in bottom_percentages for percentage in sublist]
return left_percentages
def merge_1(json_file):
pattern_reference = r'[A-Za-z]{1}\s*[A-Za-z]{1}\s*[A-Za-z]{1}\s*\d{1}\s*\d{1}\s*\d{1}\s*[A-Za-z]{1}\s*\d{1}\s*\d{1}\s*[A-Za-z]{1}\s*[A-Za-z]{1}'
x, y, w, h = extract_reference_ids_and_draw_bounding_boxes_from_json(json_file, pattern_reference)
left_percentages = get_surrounding_text(json_file, x, y, w, h)
pattern_lloyd = r"Lloyd's\sUnderwriter"
x1, y1, w1, h1 = extract_reference_ids_and_draw_bounding_boxes_from_json(json_file, pattern_lloyd)
left_percentages2 = get_surrounding_text(json_file, x1, y1, w1, h1)
final_per = left_percentages + left_percentages2
if final_per == []:
return None
else:
return final_per[0]
def add_spaces(stri):
stri = ' '.join(stri)
return stri
def final_fn(bucket_name, json_path, reference_id):
list1 = []
text_dict, _, _, df_word = process_text_analysis(bucket_name, json_path)
page = get_reference_page(text_dict)
try:
if page != None:
text_page = text_dict[page]
print(text_page)
for i in range(len(reference_id)):
search_string = reference_id[i]
search_string = add_spaces(search_string)
coordinates = get_coordinates_of_string(bucket_name, json_path, search_string)
get_refernce_json_page(bucket_name, json_path, "AMU.json", page)
per = merge_1("AMU.json")
pattern_date = r'\b(?:\d{1,2}/\d{1,2}/\d{2,4}|\d{4}-\d{2}-d{2}|(?:\d{1,2}|(?:(?:3[01]|[12][0-9]|0?[1-9])th)) \w+\d{2,4})\b'
matches_date = re.findall(pattern_date, text_page)
percentage_line = per.replace('%', '')
written_line = ''
entity = str(510)
section_type = str("NON EEA")
if coordinates != None:
dict1 = {
reference_id[i]: {
"overall_written_line": percentage_line,
'written_line': written_line,
'entity': entity,
"written_date": str(matches_date[0]),
'Risk_Code': [],
'page_number': str(page),
'coordinates': coordinates,
'Section_Type': section_type
}
}
list1.append(dict1)
else:
print("Coordinates not found for reference number:", reference_id[i])
os.remove("AMU.json")
except Exception as e:
print('An error occurred:', e)
tuple_dict = (list1,)
return tuple_dict
# Example usage
bucket_name = "your_bucket_name"
json_path = "path_to_your_json_file"
reference_numbers = ["ABC12345", "DEF67890"]
result = final_fn(bucket_name, json_path, reference_numbers)
print(result)