-
Notifications
You must be signed in to change notification settings - Fork 0
/
TataPolcity.py
5029 lines (404 loc) · 21 KB
/
TataPolcity.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import fitz
import re
import json
import TataTables
Tata_Policy_Map = {
'OD': [['damage'], []],
'TP': [['liability', 'only'], ['damage']],
'Package': [['package'], ['liability']]
}
class TataPolicy:
def __init__(self, pdf_file):
self.doc = fitz.Document(pdf_file)
self.policy_version = None
self.policy_type = None
def get_policy_version(self):
version_page = None
version = None
make_model = None
insurance_date = None
expiry_date = None
for page in self.doc.pages():
search_res = page.search_for('Registered Owner of the Motor Vehicle')
if len(search_res) != 0:
version_page = page
break
if version_page is not None:
for line in page.get_text().splitlines():
if re.match('.*vehicle\s+details\s*:\s+please\s*refer.*', line.lower().strip()) is not None:
version = 'VER_1'
break
elif re.match('.*vehicle\s+details\s*:.*', line.lower().strip()) is not None:
m = re.match('.*vehicle\s+details\s*:(.*)', line.lower().strip())
version = 'VER_2'
make_model = m.groups()[0].strip()
break
for line in page.get_text().splitlines():
m = re.match('.*insurance\s*desired\s*from.*:\s*(.*)to\s*midnight\s*of(.*)', line.lower().strip())
if m is not None:
# print(line)
insurance_date = m.groups()[0].strip()
expiry_date = m.groups()[1].strip()
break
return [version, make_model, insurance_date, expiry_date]
# print(page.get_text("blocks"))
def get_insurer_details(self):
POI = None
name = None
policy_num = None
issue_date = None
for page in self.doc.pages():
search_res = page.search_for('Sincerely')
if len(search_res) != 0:
POI = page
break
if POI is not None:
for line in page.get_text().splitlines():
name_pattern = '.*dear(.*),'
m = re.match(name_pattern, line.lower())
if m is not None:
name = m.groups()[0].strip().replace(u'\xa0', u' ')
break
for line in page.get_text().splitlines():
policy_pattern = '.*policy\s*number.*:(.*)'
m = re.match(policy_pattern, line.lower())
if m is not None:
policy_num = m.groups()[0].strip().replace(u'\xa0', u' ')
break
for line in page.get_text().splitlines():
issue_date_pattern = '.*date\s*:(.*)'
m = re.match(issue_date_pattern, line.lower())
if m is not None:
issue_date = m.groups()[0].strip().replace(u'\xa0', u' ')
break
return [name, policy_num,issue_date]
def get_POI(self, pattern):
POI = None
code = None
agent_code = None
for page in self.doc.pages():
for line in page.get_text().splitlines():
# page_text = page.get_text()
m = re.match(pattern, line.lower())
if m is not None:
POI = page
# print('found')
break
if POI is not None:
break
return POI
def get_agent_code(self):
POI = None
code = None
agent_code = None
for page in self.doc.pages():
Agent_Pattern = 'agent.*name\s*:\s*(\S+).*'
for line in page.get_text().splitlines():
# page_text = page.get_text()
m = re.match(Agent_Pattern, line.lower())
if m is not None:
POI = page
# print('found')
agent_code = m.groups()[0].strip()
if ';' in agent_code:
print('Unwanted Found')
break
if POI is not None:
break
return agent_code
def get_policy_type(self):
policy_type_string = None
vehicle_type = None
policytype = None
POI = self.get_POI('.*schedule\s*of\s*premium.*')
s1 = (POI.search_for('Policy Type'))[0]
print(" ".join(POI.get_textbox([s1.x0, s1.y0, 9999, s1.y1]).split()))
# print(POI.get_text())
if POI is not None:
search1 = POI.search_for('Policy Type', hit_max=1)
if not search1:
return None
s1 = search1[0]
policy_type_string = " ".join(POI.get_textbox([s1.x0, s1.y0, 9999, s1.y1]).split())
# for line in POI.get_text().splitlines():
# policy_type_pattern = '.*auto\s*secure\s+(.*)'
# m = re.match(policy_type_pattern, line.lower())
#
# #print(line)
# if m is not None:
# policy_type_string = m.groups()[0].strip().replace(u'\xa0', u' ')
# break
#print('policy_type_string ',policy_type_string)
if policy_type_string is not None:
for policy_type, attrib in Tata_Policy_Map.items():
check_list_true = set([attr in policy_type_string.lower() for attr in attrib[0]])
check_list_false = set([attr in policy_type_string.lower() for attr in attrib[1]])
if not True in check_list_false and not False in check_list_true:
# print('Policy Type is {0}'.format(policy_type))
policytype = policy_type
break
vehicle = None
if 'commercial' in policy_type_string.lower():
vehicle = 'Commercial'
elif 'private' in policy_type_string.lower() and 'car' in policy_type_string.lower():
vehicle = 'Private Car'
else:
vehicle = 'UnKnown'
self.policy_type = policytype
self.vehicle_type = vehicle
return vehicle, policytype
def get_price(self, pattern, page):
Own_Damage_Dict = {}
mode = 'Title'
# pattern = '.*own\s*damage\s*premium.*'
valuepattern = '[+-]?([0-9]*[.])?[0-9]+'
currentTitle = None
blocks = page.get_text("blocks")
blocktextlist = []
# blocks= POI.get_text("blocks",flags = fitz.fitz.TEXT_PRESERVE_LIGATURES | fitz.fitz.TEXT_PRESERVE_WHITESPACE)
blocks.sort(key=lambda b: (b[3], b[0]))
start_append = False
for bl in blocks:
# if 'premium' in bl[4].lower():
if not start_append:
m = re.match('.*schedule\s*of\s*premium.*', bl[4].lower())
if m is not None:
start_append = True
if start_append and '<image' not in bl[4]:
blocktextlist.extend(bl[4].replace(u'\xa0', u' ').strip().split("\n"))
# print(blocktextlist)
for line in blocktextlist:
if mode == 'Title':
m = re.match(pattern, line.lower())
if m is not None:
currentTitle = line
mode = 'value'
if mode == 'value':
m = re.match(valuepattern, line.lower())
if m is not None:
currentvalue = float(line.replace(',', ''))
Own_Damage_Dict[currentTitle.strip().replace(u'\xa0', u' ')] = currentvalue
mode = 'Title'
else:
currentTitle = line
return Own_Damage_Dict
def get_net_premium(self, page):
total_gst_pattern = '.*total\s*gst\s*amount.*'
gst_dict = self.get_price(total_gst_pattern, page)
net_pattern = '.*net\s*premium.*'
net_p_dict = self.get_price(net_pattern, page)
# print(blocktext)
road_side_pattern = '.*road\s*side\s*assistance.*'
road_side_p_dict = self.get_price(road_side_pattern, page)
total_premium = self.get_total_policy_premium(page)
net_premium = 0
total_gst_value = 0
road_side_value = 0
if len(road_side_p_dict.keys()) != 0:
road_side_value = max(road_side_p_dict.values())
if len(gst_dict.keys()) != 0:
total_gst_value = max(gst_dict.values())
net_premium = total_premium - total_gst_value + road_side_value
if len(net_p_dict.keys()) != 0:
net_premium = max(net_p_dict.values()) + road_side_value
return net_premium,total_premium + road_side_value
print(gst_dict)
print(net_p_dict)
def get_total_policy_premium(self, page):
total_primum_pattern_1 = '.*total\s+policy\s*premium.*'
prem_dict_1 = self.get_price(total_primum_pattern_1, page)
total_primum_pattern_2 = '.*total\s+premium.*'
prem_dict_2 = self.get_price(total_primum_pattern_2, page)
#print('...........Total premium............')
maximum_pre1 = 0
if len(prem_dict_1.keys()) != 0:
maximum_pre1 = max(prem_dict_1.values())
maximum_pre2 = 0
if len(prem_dict_2.keys()) != 0:
maximum_pre2 = max(prem_dict_2.values())
return max([maximum_pre1,maximum_pre2])
def get_own_damage(self, page):
od_primum_pattern_1 = '.+own\s*damage\s*premium.*'
od_prem_dict_1 = self.get_price(od_primum_pattern_1, page)
#print(od_prem_dict_1)
add_on = 0
if self.policy_type=='Package' or self.policy_type=='OD':
add_on_pattern = '.*total\s*add\s*on\s*premium.*'
add_on_dict_1 = self.get_price(add_on_pattern, page)
if len(add_on_dict_1.keys()) != 0:
add_on = max(add_on_dict_1.values())
maximum_od = 0
if len(od_prem_dict_1.keys()) != 0:
return add_on + max(od_prem_dict_1.values())
return maximum_od + add_on
def get_liabilty_premium(self, page):
laibility_primum_pattern_1 = '.*basic.*'
tp_prem_dict_1 = self.get_price(laibility_primum_pattern_1, page)
if self.policy_type =='OD':
return 0
maximum_basic = 0
ValuList = []
if len(tp_prem_dict_1.keys()) !=0:
for k in tp_prem_dict_1.keys():
if 'od' not in k.lower():
ValuList.append(tp_prem_dict_1[k])
if len(ValuList)!= 0:
return max(ValuList)
return maximum_basic
def get_reg_num_make(self,make,model):
POI = self.get_POI('.*schedule\s*of\s*premium.*')
if make is None or model is None:
regnum, make, model = TataTables.get_registration(POI,'Registration','SCHEDULE OF PREMIUM',None)
else:
regnum, make, model = TataTables.get_registration(POI, 'Registration', 'SCHEDULE OF PREMIUM', [make,model])
return regnum, make, model
def get_premium_details(self):
POI = self.get_POI('.*schedule\s*of\s*premium.*')
table_title = "SCHEDULE OF PREMIUM"
search1 = POI.search_for(table_title, hit_max=1)
rect1 = search1[0] # the rectangle that surrounds the search string
ymin = rect1.y1
tmp_txt = POI.get_textbox([0, ymin, 9999, 9999])
# print(self.get_own_damage(tmp_txt,'.*own\s*damage\s*premium.*',POI))
# print(self.get_own_damage(tmp_txt,'.*total\s*premium.*',POI))
# print(self.get_own_damage(tmp_txt,'.*liability\s*premium.*',POI))
net_prem,total_prem = self.get_net_premium(POI)
#print(self.get_total_policy_premium(POI))
OD = self.get_own_damage(POI)
basic_tp = self.get_liabilty_premium(POI)
# blocks= POI.get_text("blocks")
# # blocks= POI.get_text("blocks",flags = fitz.fitz.TEXT_PRESERVE_LIGATURES | fitz.fitz.TEXT_PRESERVE_WHITESPACE)
# blocks.sort(key=lambda b: (b[3], b[0]))
# for bl in blocks:
# #if 'premium' in bl[4].lower():
# print(bl[4])
return total_prem,OD,basic_tp,net_prem
def get_policy_info(self):
data_dict = {
'A': None,
'B': None,
'C': None,
'D': None,
'E': None,
'F': None,
'G': None,
'H': None,
'I': None,
'J': None,
'K': None,
'L': None,
'M': None,
'N': None,
'O': None,
'P': None,
'Q': None,
'R': "",
'S': "",
'T': None,
'U': None,
'V': None,
'W': None,
'X': None
}
version, make_model, _, expiry_date = self.get_policy_version()
mk =None
mdl=None
if version =='VER_2':
mk = make_model.split("/")[0]
mdl = make_model.split("/")[1]
insurer_name, policy_num, issue_date = self.get_insurer_details()
regnum, make,model = self.get_reg_num_make(mk,mdl)
agent_code = self.get_agent_code()
vt, polity_type = self.get_policy_type()
FP,OD,TP,NP= self.get_premium_details()
vehicle_type = "Not Found"
if issue_date is not None and TP !=0:
day,month,year = issue_date.split('/')
print(day,int(month),year)
cs = classifyTP(TP,int(month),int(year))
self.vehicle_type = cs.policy_type
if vt == 'Private Car':
self.vehicle_type = vt
print("Issue Date: ",issue_date)
print("Insurer Name: ",insurer_name)
print("RegName: ",regnum)
print("POLICY Number: ",policy_num)
print("Expiry Date: ", expiry_date)
print("Contact Number: ", 'NA')
print("VEHILCE TYPE: ", self.vehicle_type)
print("POLICY TYPE: ", polity_type)
print("Make: ", make)
print("Model: ", model)
print("Final Premium: ", FP)
print("TP Premium: ", TP)
print("OD Premium: ", OD)
print("NET Premium: ", NP)
data_dict['A'] = issue_date
data_dict['B'] = insurer_name
data_dict['C'] = regnum
data_dict['D'] = policy_num
data_dict['E'] = expiry_date
data_dict['F'] = 'NA'
data_dict['H'] = polity_type
data_dict['I'] = 'TATA'
data_dict['J'] = make
data_dict['K'] = model
data_dict['L'] = float(FP)
data_dict['M'] = float(TP)
data_dict['N'] = float(OD)
data_dict['O'] = float(NP)
data_dict['P'] = data_dict['O']
data_dict['Q'] = agent_code
if self.vehicle_type == "Private Car" or 'PVT' in self.vehicle_type:
if polity_type == 'TP' or polity_type == 'OD':
data_dict['P'] = data_dict['O']
else:
data_dict['P'] = data_dict['N']
if self.vehicle_type == 'Not Found':
self.vehicle_type=None
data_dict['G'] = self.vehicle_type
self.doc.close()
return data_dict
class classifyTP:
def __init__(self, tp, month, year=2022):
self.policy_type_verbose = 'Not Found'
self.policy_type_acro = 'None'
self.json_data = json.load(open(r"res/tata_tp_rate_helper.json", 'r'))
self.tp_rate = tp
self.month = month
self.year = year
@property
def policy_type(self):
for key, value in self.json_data.items():
if "TP_Group" in key:
if self.month in value['month'] and self.year in value['year']:
TP_Rates = value['TP_Rates']
for vehicle_type, tp_rates in TP_Rates.items():
if tp_rates[0] <= self.tp_rate <= tp_rates[1]:
self.policy_type_verbose = self.json_data['acro_map'][vehicle_type]
self.policy_type_acro = vehicle_type
return self.policy_type_verbose
return self.policy_type_verbose
# for
# file_name = r"E:\NewDownloads\DemoDownloads\tata policy\Tata AIG Motor Policy Schedule_3184_6200366991-00.pdf"
# pl = TataPolicy(file_name)
# pl.get_policy_info()
# print(pl.get_policy_version())
# print(pl.get_insurer_details())
# print(pl.get_agent_code())
# print(pl.get_policy_type())
# pl.get_premium_details()
# import os
#
# from pathlib import Path
#
# dir_path = r"E:\NewDownloads\DemoDownloads\tata policy"
# extensions = set()
#
# for _, _, files in os.walk(dir_path):
# for f in files:
# ext = Path(f).suffix.lower()
# if 'pdf' in ext:
# print(f)
# pl = TataPolicy(os.path.join(dir_path, f))
# pl.get_policy_info()