-
Notifications
You must be signed in to change notification settings - Fork 0
/
bajaj_parser.py
390 lines (349 loc) · 15 KB
/
bajaj_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
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
import re
Acronym_Map_BJ = {'BAJAJ': 'BAJAJ', 'TW': 'TWO WHEELER', 'PC': 'Private Car', 'COMM': "Passenger Carrying", 'TP': 'TP',
'PKG': 'Package', 'OD':'OD'}
INSURANCE_TYPE_OLD = {
'BAJAJ_TW': ['transcript', 'two', 'wheeler'],
'BAJAJ_PC': ['transcript', 'private', 'car'],
'BAJAJ_COMM': ['transcript', 'commercial', 'vehicle', 'policy']
}
INSURANCE_TYPE_NEW = {
'BAJAJ_TW': ['two', 'wheeler'],
'BAJAJ_PC': ['private', 'car'],
'BAJAJ_COMM': ['commercial', 'vehicle', 'policy']
}
POLICY_MAP = {
'OLD': INSURANCE_TYPE_OLD,
'NEW': INSURANCE_TYPE_NEW
}
OLD_VS_NEW = {
'TW': ['TP'],
'PC': ['TP'],
'COMM': ['TP']
}
class BajajPolicy:
def __init__(self, raw_data,file_path,logger):
logger.setPlainText('Converting format for Bajaj')
self.raw_data = raw_data
self.raw_text = self._get_all_page_data()
# print(self.raw_text)
self.policy_version = self.checkifnew()
self.insurance_type = self.get_insurance_type()
print(self.checkifnew())
print(self.get_insurance_type())
self.get_all_policy_details()
def checkifnew(self):
if 'facebook' in self.raw_text:
return 'NEW'
return 'OLD'
def _get_all_page_data(self):
tmp_txt = ""
for page in self.raw_data.pages:
tmp_txt = tmp_txt + "\n" + self.get_good_text(page.extract_text(x_tolerance=1))
return tmp_txt
def get_good_text(self, xx):
GoodTExt = ""
for x in xx.split('\n'):
if x != '' and x != '(cid:3)': # merely to compact the output
abc = re.findall(r'\(cid\:\d+\)', x)
if len(abc) > 0:
for cid in abc: x = x.replace(cid, self.cidToChar(cid))
GoodTExt += "\n" + repr(x).strip("'")
return GoodTExt
def cidToChar(self, cidx):
return chr(int(re.findall(r'\(cid\:(\d+)\)', cidx)[0]) + 29)
def get_insurance_type(self):
for page in self.raw_data.pages:
raw_text = page.extract_text(x_tolerance=1)
raw_text_list = raw_text.lower().split("\n")
for row in raw_text_list:
for policy_type, attrib in POLICY_MAP[self.policy_version].items():
check_list = set([attr in row for attr in attrib])
if len(check_list) == 1 and list(check_list)[0]:
if 'liability' in row:
policy_type = policy_type + "_TP"
elif 'PACKAGE POLICY' in row.upper():
policy_type = policy_type + "_PKG"
else:
policy_type = policy_type + "_OD"
if policy_type.split('_')[1]=='COMM':
m = re.match('.*Vehicle Type\s+(.*)',row.lower())
if m is not None:
use_string =m.groups()[0]
if 'passenger' in use_string.lower():
vtype= 'Passanger Carrying'
else:
if 'pubic' in use_string.lower():
vtype = 'Goods - Public Carrier'
else:
vtype = 'Goods - PVT Carrier'
return Acronym_Map_BJ[policy_type.split('_')[0]],vtype,Acronym_Map_BJ[policy_type.split('_')[2]]
return Acronym_Map_BJ[policy_type.split('_')[0]], Acronym_Map_BJ[policy_type.split('_')[1]], \
Acronym_Map_BJ[policy_type.split('_')[2]]
return None
def get_all_policy_details(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': None,
'S': None,
'T': None,
'U': None,
'V': None,
'W': None,
'X': None
}
if self.policy_version == "OLD":
policy_obj = BAJAJ_OLD_POLCY(self.raw_data, self.raw_text, self.insurance_type)
print('Customer Name is ', policy_obj.just_get_name())
print('Customer Mobile Number is ', policy_obj.just_get_number())
print('Expiry Date is ', policy_obj.get_expiry_date())
print('Issue Date is ', policy_obj.get_policy_issue_date())
print('Policy Number is ', policy_obj.get_policy_num())
print('Make Info is below : ', policy_obj.get_registration_make())
policy_obj.get_prices()
data_dict['A'] = policy_obj.get_policy_issue_date()
data_dict['B'] = policy_obj.just_get_name()
data_dict['C'],data_dict['J'],data_dict['K'] = policy_obj.get_registration_make()
data_dict['D'] = policy_obj.get_policy_num()
data_dict['E'] = policy_obj.get_expiry_date()
data_dict['F'] = policy_obj.just_get_number()
if data_dict['F'].strip()!='NA':
data_dict['F'] = int(data_dict['F'].replace('0-',''))
data_dict['G'] = policy_obj.insurance_type[1]
data_dict['H'] = policy_obj.insurance_type[2]
data_dict['I'] = policy_obj.insurance_type[0]
data_dict['L'],data_dict['M'],data_dict['N'],data_dict['O'] = policy_obj.get_prices()
data_dict['Q'] = policy_obj.get_code()
return data_dict
class BAJAJ_OLD_POLCY:
def __init__(self, raw_data, raw_text, insurance_type):
self.raw_data = raw_data
self.raw_text = raw_text
# print(self.raw_text)
self.insurance_type = insurance_type
def get_good_text(self, xx):
GoodTExt = ""
for x in xx.split('\n'):
if x != '' and x != '(cid:3)': # merely to compact the output
abc = re.findall(r'\(cid\:\d+\)', x)
if len(abc) > 0:
for cid in abc: x = x.replace(cid, self.cidToChar(cid))
GoodTExt += "\n" + repr(x).strip("'")
return GoodTExt
def just_get_name(self):
customer_name = None
raw_text_list = self.raw_text.lower().split("\n")
for row in raw_text_list:
m = re.match("\d+.*proposer\s*name\s*:\s*(.*)", row.strip().lower())
if m is not None:
customer_name = m.groups()[0].strip().upper()
return customer_name
return None
def cidToChar(self, cidx):
return chr(int(re.findall(r'\(cid\:(\d+)\)', cidx)[0]) + 29)
def just_get_number(self):
customer_name = None
raw_text_list = self.raw_text.lower().split("\n")
for row in raw_text_list:
m = re.match("\d+.*proposer\s*mobile\s*number\s*:\s*(.*)", row.strip().lower())
if m is not None:
customer_name = m.groups()[0].strip().upper()
return customer_name
return None
def get_expiry_date(self):
customer_name = None
raw_text_list = self.raw_text.lower().split("\n")
for row in raw_text_list:
m = re.match(".*(\d{2}-[a-z]+-\d{4})\s*midnight.*", row.strip().lower())
if m is not None:
customer_name = m.groups()[0].strip().upper()
return customer_name
return None
def get_policy_issue_date(self):
customer_name = None
raw_text_list = self.raw_text.lower().split("\n")
for row in raw_text_list:
m = re.match(".*policy\s*issued\s*on\s*(\d{2}-[a-z]+-\d{4}).*", row.strip().lower())
if m is not None:
customer_name = m.groups()[0].strip().upper()
return customer_name
return None
def get_policy_num(self):
customer_name = None
raw_text_list = self.raw_text.lower().split("\n")
for row in raw_text_list:
m = re.match(".*policy\s*number\s*([a-z0-9-]+\d+).*", row.strip().lower())
if m is not None:
customer_name = m.groups()[0].strip().upper()
return customer_name
return None
def get_clean_text(self, txt):
return txt.replace('\n', ' ').replace('- ', '')
def get_make_table(self):
customer_name = None
Make_Page = None
for page in self.raw_data.pages:
raw_text = self.get_good_text(page.extract_text(x_tolerance=1))
raw_text_list = raw_text.lower().split("\n")
for row in raw_text_list:
m = re.match("\d+.*proposer\s*name\s*:\s*(.*)", row.strip().lower())
if m is not None:
Make_Page = page
print('Found page')
break
if Make_Page is not None:
break
make_table = None
if Make_Page is not None:
page_tables = Make_Page.extract_tables({'text_x_tolerance': 1})
for table in page_tables:
# print(table)
clean_table = []
assign_table = False
for row in table:
clean_row = [self.get_clean_text(data.upper()) for data in row]
clean_table.append(clean_row)
if 'REGISTRATION NUMBER' in clean_row:
assign_table = True
if assign_table:
make_table = clean_table
return make_table
def get_table_row(self, tb, key):
row_num = -1
column_num = -1
found = False
values = None
for row in tb:
row_num += 1
for cell in row:
column_num += 1
if key == cell.strip():
found = True
break
if found:
break
if found:
row_num_local = -1
column_num_local = -1
for row in tb:
row_num_local += 1
if row_num_local > row_num:
if row[column_num] != None:
value = row[column_num]
return value
return None
def get_registration_make(self):
mk_table = self.get_make_table()
if mk_table is not None:
r_num = self.get_table_row(mk_table, 'REGISTRATION NUMBER')
v_make = self.get_table_row(mk_table, 'VEHICLE MAKE')
v_model = self.get_table_row(mk_table, 'VEHICLE MODEL')
return r_num,v_make,v_model
return None,None,None
def get_code(self):
code_pattern_1 = "generated\s*by\s*(.*)@general.*"
code_pattern_2 = ".*\(web\)\s*\((.*)\).*"
for page in self.raw_data.pages:
raw_text = self.get_good_text(page.extract_text(x_tolerance=1))
raw_text_list = raw_text.lower().split("\n")
code=None
for row in raw_text_list:
m = re.match(code_pattern_1,row)
n = re.match(code_pattern_2, row)
if not m is None:
code= m.groups()[0]
return code
elif not n is None:
code = n.groups()[0]
return code
def get_price_table(self):
customer_name = None
price_Page = None
for page in self.raw_data.pages:
raw_text = page.extract_text(x_tolerance=1).upper()
# raw_text_list = raw_text.lower().split("\n")
if 'OWN DAMAGE' in raw_text and 'FINAL PREMIUM' in raw_text:
price_Page = page
break
if price_Page is not None:
sc_res = price_Page.search(".*(OWN Damage).*", regex=True, case=False, x_tolerance=1)
if len(sc_res) != 0:
top_y = sc_res[0]['top'] - 2 + sc_res[0]['top'] - sc_res[0]['bottom']
x0 = 10
if top_y + 150 < price_Page.height:
bottom_y = price_Page.height
# bottom_y = top_y + 150
else:
bottom_y = price_Page.height
x1 = price_Page.width
new_page = price_Page.crop((x0, top_y, x1, bottom_y))
tbls = new_page.extract_tables({'text_x_tolerance': 1})
for tbl in tbls:
found_check_list = []
for row in tbl:
for cell in row:
if 'own damage' in str(cell).lower():
found_check_list.append(1)
if 1 in found_check_list:
clean_table = []
for row in tbl:
clean_row = [self.get_clean_text(str(data).upper()) for data in row]
clean_table.append(clean_row)
return clean_table
return None
def get_next_not_none(self,remaining_row):
for cell in remaining_row:
if cell.strip() != 'NONE':
return cell
return None
def get_value_from_price_table(self,table,key,repeat=False):
TotalValue = None
for row in table:
for cell_index, cell in enumerate(row):
m= re.match(key,str(cell.strip()))
if m is not None:
value = self.get_next_not_none(row[cell_index+1:])
if value is not None and len(value.strip())!=0:
if TotalValue is None:
TotalValue=0
TotalValue+=float(value.replace(',',''))
if repeat:
pass
else:
return TotalValue
return TotalValue
def get_prices(self):
pc_table = self.get_price_table()
if pc_table is not None:
'''
print('OD PRIMUIM ',self.get_value_from_price_table(pc_table,'.*OWN DAMAGE PREMIUM:*(?!\()'))
print('TOTAL GST ', self.get_value_from_price_table(pc_table,'.*GST.*',True))
print('Final Premium ', self.get_value_from_price_table(pc_table,'.*FINAL\s*PREMIUM.*'))
print('TP Premium ', self.get_value_from_price_table(pc_table,'.*BASIC\s*THIRD\s*PARTY\s*LIABILITY.*'))
'''
ODP = self.get_value_from_price_table(pc_table, '.*OWN DAMAGE PREMIUM:*(?!\()')
GST= self.get_value_from_price_table(pc_table, '.*GST.*', True)
FP = self.get_value_from_price_table(pc_table, '.*FINAL\s*PREMIUM.*')
TP = self.get_value_from_price_table(pc_table, '.*BASIC\s*THIRD\s*PARTY\s*LIABILITY.*')
if TP is None:
TP=0
if ODP is None:
ODP=0
NetPremium = FP - GST
return float(FP),float(TP),float(ODP),float(NetPremium)
return None,None,None,None