-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplumber.py
More file actions
492 lines (434 loc) · 18.5 KB
/
plumber.py
File metadata and controls
492 lines (434 loc) · 18.5 KB
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import re, json
from datetime import datetime
from document_classifier import Classify
import FileReader
def convert_date_format(line):
dateObj = None
if re.match(r"^\d{4}(\d{2})\d{2}$", line):
if int(re.match(r"^\d{4}(\d{2})\d{2}$", line).groups()[0]) < 12:
dateObj = datetime.strptime(line, '%Y%m%d')
else:
dateObj = datetime.strptime(line, '%d%m%Y')
elif re.match(r"^\d{1,2}/", line):
dateObj = datetime.strptime(line, '%m/%d/%Y')
elif re.match(r"^[a-z]{3}", line, re.IGNORECASE):
dateObj = datetime.strptime(line, '%b %d %Y')
elif re.match(r"^\d{1,2} [a-z]{3}", line, re.IGNORECASE):
dateObj = datetime.strptime(line, '%d %b %Y')
elif re.match(r"^\d{4}-\d{1,2}-\d{1,2}", line, re.IGNORECASE):
dateObj = datetime.strptime(line, '%Y-%m-%d')
elif re.match(r"^\d{1,2}-[a-z]+-\d{4}", line, re.IGNORECASE):
dateObj = datetime.strptime(line, '%d-%b-%Y')
elif re.match(r"^\d{1,2}-\d{1,2}-\d{4}", line, re.IGNORECASE):
dateObj = datetime.strptime(line, '%d-%m-%Y')
return dateObj.strftime('%d-%m-%Y')
Acronym_Map = {'DIGI': 'DIGIT', 'TW': 'TWO WHEELER', 'PC': 'Private Car', 'COMM': "Passenger Carrying",'GOODS': "Goods Carrying",'TP':'TP','PKG':'Package'}
INSURANCE_TYPE = {
'DIGI_TW': ['digit', 'two', 'wheeler', 'policy'],
'DIGI_PC': ['digit', 'private', 'car', 'policy'],
'DIGI_COMM': ['digit', 'commercial', 'passenger', 'policy'],
'DIGI_GOODS': ['digit', 'commercial', 'goods', 'policy']
}
class Digit_RickShaw:
def __init__(self, raw_data,pdf_path):
self.pdf_path = pdf_path
self.raw_data = raw_data
self.raw_text = self._get_all_page_data()
self.json_data = json.load(open("res/policy_class_helper.json", 'r'))
def _get_all_page_data(self):
tmp_txt = ""
for page in self.raw_data.pages:
tmp_txt = tmp_txt + "\n" + page.extract_text()
return tmp_txt
def get_policy_type(self):
if "bajaj" in self.raw_text:
return "Bajaj"
elif "digit" in self.raw_text:
return "digit"
else:
return "Unknown"
def get_policy(self):
for policy in self.json_data["policies"]:
match_list = [True]
for identifier in self.json_data["policies"][policy]['identifier']:
match_list.append(identifier in self.raw_text)
if len(set(match_list)) == 1:
return policy
return "Unknown"
def get_customer_name(self):
found_name = False
customer_name = None
registration_num = None
name_pattern = ".*name(.*)vehicle registration no.\s([a-z0-9]+)\s*.*"
for page in self.raw_data.pages:
raw_text = page.extract_text()
raw_text_list = raw_text.lower().split("\n")
for row in raw_text_list:
match = re.match(name_pattern, row)
if match is not None:
found_name == True
customer_name = match.groups()[0].strip().upper()
registration_num = match.groups()[1].strip().upper()
return customer_name, registration_num
break
if found_name:
break
if not found_name:
registration_num = self.just_get_registrations()
customer_name = self.just_get_name()
return customer_name, registration_num
def just_get_name(self):
customer_name = None
for page in self.raw_data.pages:
raw_text = page.extract_text()
raw_text_list = raw_text.lower().split("\n")
for row in raw_text_list:
if 'name' in row:
if 'partner' in row:
p_in = row.index('partner')
rw = row[:p_in]
else:
rw = row
m = re.match(".*name:(.*)", rw)
if m is not None:
customer_name = m.groups()[0].strip().upper()
return customer_name
return None
def just_get_number(self):
customer_name = None
for page in self.raw_data.pages:
raw_text = page.extract_text()
raw_text_list = raw_text.lower().split("\n")
for row in raw_text_list:
if 'mobile' in row:
if 'partner' in row:
p_in = row.index('partner')
rw = row[:p_in]
else:
rw = row
m = re.match(".*mobile.*:(.*)", rw)
if m is not None:
customer_name = m.groups()[0].strip().upper()
return customer_name
return None
def just_get_registrations(self):
customer_name = None
for page in self.raw_data.pages:
raw_text = page.extract_text()
raw_text_list = raw_text.lower().split("\n")
for row in raw_text_list:
if 'registration' in row:
if 'partner' in row:
p_in = row.index('partner')
rw = row[:p_in]
else:
rw = row
m = re.match(".*registration.*:(.*)", rw)
if m is not None:
customer_name = m.groups()[0].strip().upper()
return customer_name
return None
def get_policy_num(self):
found_policy = False
policy_num = None
invoice_date = None
policy_pattern = ".*policy no.\s+([a-z0-9]+)\s*/\s*(\d{8})\s*.*"
for page in self.raw_data.pages:
raw_text = page.extract_text()
raw_text_list = raw_text.lower().split("\n")
for row in raw_text_list:
match = re.match(policy_pattern, row)
if match is not None and 'previous' not in row:
found_policy = True
# print(row)
policy_num = match.groups()[0].strip().upper()
# print(match.groups())
invoice_date = convert_date_format(match.groups()[1].strip().upper())
break
if found_policy:
break
if not found_policy:
policy_pattern = ".*policy no:\s+([a-z0-9]+)\s*.*"
for page in self.raw_data.pages:
raw_text = page.extract_text()
raw_text_list = raw_text.lower().split("\n")
for row in raw_text_list:
match = re.match(policy_pattern, row)
if match is not None and 'previous' not in row:
found_policy == True
# print(row)
policy_num = match.groups()[0].strip().upper()
invoice_date = self.get_invoice_date()
break
if found_policy:
break
return policy_num, invoice_date
def get_invoice_date(self):
found_invoice_date = False
invoice_date = None
policy_pattern = ".*invoice number invoice date gross premium.*"
check_invoice = False
for page in self.raw_data.pages:
raw_text = page.extract_text()
raw_text_list = raw_text.lower().split("\n")
for row in raw_text_list:
if found_invoice_date:
break
if not check_invoice:
match = re.match(policy_pattern, row)
if match is not None:
check_invoice = True
# print("header found")
else:
split_row = row.split()
# print("Checking the invoice ", len(split_row))
if len(split_row) == 9:
found_invoice_date = True
invoice_date = convert_date_format(split_row[1])
# convert_date_format()
check_invoice = False
break
if found_invoice_date:
break
return invoice_date
def get_policy_expiry(self):
found_policy_expiry = False
policy_expiry = None
expiry_pattern = ".*period of policy to\s*(\d{2}-[a-z]{3,4}-\d{4})\s*(\d{2}:\d{2}:\d{2}).*"
for page in self.raw_data.pages:
raw_text = page.extract_text()
raw_text_list = raw_text.lower().split("\n")
for row in raw_text_list:
match = re.match(expiry_pattern, row)
if match is not None:
found_policy_expiry = True
policy_expiry = match.groups()[0].strip().upper()
break
if found_policy_expiry:
break
if not found_policy_expiry:
check_next = False
for page in self.raw_data.pages:
raw_text = page.extract_text()
raw_text_list = raw_text.lower().split("\n")
for row in raw_text_list:
if not check_next:
if 'period of policy' in row:
check_next = True
else:
expiry_pattern = 'to\s*(\d{2}-[a-z]{3,4}-\d{4})\s*(\d{2}:\d{2}:\d{2}).*'
match = re.match(expiry_pattern, row)
if match is not None:
found_policy_expiry = True
policy_expiry = match.groups()[0].strip().upper()
break
if found_policy_expiry:
break
if policy_expiry is None:
rd = FileReader.Digit(self.pdf_path)
prem = rd.get_policy_expiry()
if prem is not None:
return prem
return policy_expiry
def get_mobile_num(self):
found_number = False
customer_number = None
name_pattern = "(.*mobile\s*)([a-x0-9]{10})\s*"
for page in self.raw_data.pages:
raw_text = page.extract_text()
raw_text_list = raw_text.lower().split("\n")
for row in raw_text_list:
match = re.match(name_pattern, row)
if match is not None:
found_number == True
customer_number = match.groups()[1].strip().upper()
return customer_number
break
if found_number:
break
if not found_number:
customer_number = self.just_get_number()
return customer_number
def get_insurance_type(self):
for page in self.raw_data.pages:
raw_text = page.extract_text()
raw_text_list = raw_text.lower().split("\n")
for row in raw_text_list:
for policy_type, attrib in INSURANCE_TYPE.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"
else:
policy_type = policy_type + "_PKG"
return Acronym_Map[policy_type.split('_')[0]],Acronym_Map[policy_type.split('_')[1]],Acronym_Map[policy_type.split('_')[2]]
return None
def get_make(self):
# make = None
make = None
tpd = FileReader.Digit(self.pdf_path)
make = tpd.get_make()
if make is not None:
return make
for page in self.raw_data.pages:
raw_text = page.extract_text()
raw_text_list = raw_text.split("\n")
for row in raw_text_list:
if 'Make' in row:
if 'Model' in row:
p_in = row.index('Model')
row = row[:p_in]
elif 'Trailer' in row:
p_in = row.index('Trailer')
row = row[:p_in]
elif 'Vehicle' in row:
# print('Reached here........')
# print(row)
rw = row.split()
row = ""
for im in rw:
if 'Vehicle' in im:
break
else:
row = row + " " + im
elif 'Variant' in row:
# print('Reached here........')
# print(row)
rw = row.split()
row = ""
for im in rw:
if 'Variant' in im:
break
else:
row = row + " " + im
# temp_match=re.match("(.*)([^\s])/Vehicle.*",row)
# if temp_match is not None:
# row = temp_match.groups()[0]
else:
pass
m = re.match('.*Make\s*(.*)', row)
if m is not None:
make = m.groups()[0].strip().upper()
return make
return make
def get_model(self):
# model = None
model = None
tpd = FileReader.Digit(self.pdf_path)
model = tpd.get_model()
if model is not None:
return model
for page in self.raw_data.pages:
raw_text = page.extract_text()
raw_text_list = raw_text.split("\n")
for row in raw_text_list:
if re.match(".*Model/*.*\s([^\s]+)/.*", row) is not None:
# print(row)
return re.match(".*Model/*.*\s([^\s]+)/.*", row).groups()[0]
elif re.match(".*Variant.*\s+([^\s]+)/.*", row) is not None:
# print(row)
return re.match(".*Variant.*\s+([^\s]+)/.*", row).groups()[0]
return None
def get_final_premium(self):
final_premium = None
for page in self.raw_data.pages:
raw_text = page.extract_text()
raw_text_list = raw_text.lower().split("\n")
for row in raw_text_list:
match = re.match(".*final premium.*\(.*\).*\s+([+-]?([0-9]*[.])?[0-9]+).*", row)
if match is not None:
return match.groups()[0]
else:
match = re.match(".*total premium.*\(.*\).*\s+([+-]?([0-9]*[.])?[0-9]+).*", row)
if match is not None:
return match.groups()[0]
rd = FileReader.Digit(self.pdf_path)
prem = rd.get_final_premium()
if prem is not None:
return prem
return None
def get_net_premium(self):
TP_Price = None
tpd = FileReader.Digit(self.pdf_path)
TP_Price = tpd.get_net_premium()
if TP_Price is not None:
return TP_Price
final_premium = None
for page in self.raw_data.pages:
raw_text = page.extract_text()
raw_text_list = raw_text.lower().split("\n")
for row in raw_text_list:
match = re.match(".*net premium.*\s*\(.*\).*\s+([+-]?([0-9]*[.])?[0-9]+).*", row)
if match is not None:
print(row)
return re.findall("[-+]?\d*\.\d+|\d+", row)[0]
else:
match = re.match(".*total premium\s*\(.*\).*\s+([+-]?([0-9]*[.])?[0-9]+).*", row)
print(row)
if match is not None:
return re.findall(r"[-+]?\d*\.\d+|\d+", row)[0]
return None
def get_code(self):
TP_Price = None
tpd = FileReader.Digit(self.pdf_path)
TP_Price = tpd.get_partner_name()
if TP_Price is not None:
return TP_Price
def get_payout(self):
insurance = self.get_insurance_type()
if insurance is not None:
if insurance[1]=="TWO WHEELER" and insurance[2]=="TP":
return "O"
if insurance[1]=="TWO WHEELER":
return 'N'
if insurance[1]=="Private Car" and insurance[2]=="TP":
return "O"
if insurance[1]=="Private Car":
return "O"
return "O"
def get_od_premium_old(self):
final_premium = None
insurance= self.get_insurance_type()[2]
if insurance=='TP':
return '0'
for page in self.raw_data.pages:
raw_text = page.extract_text()
raw_text_list = raw_text.lower().split("\n")
for row in raw_text_list:
match = re.match(".*od premium.*\s*\(.*\).*\s+([+-]?([0-9]*[.])?[0-9]+).*", row)
if match is not None:
#return match.groups()[0]
all_floats=re.findall(r"[-+]?\d*\.\d+", row)
stub_end=row.index('od premium') + len('od premium')
closest_float=None
smallest_index= None
for each_float in all_floats:
if row.index(each_float) - stub_end > 0:
if closest_float is None:
closest_float=each_float
smallest_index=row.index(each_float)
else:
if row.index(each_float) < smallest_index:
smallest_index = row.index(each_float)
closest_float = each_float
return closest_float
else:
match = re.match(".*total premium\s*\(.*\).*\s+([+-]?([0-9]*[.])?[0-9]+).*", row)
if match is not None:
return match.groups()[0]
return None
def get_tp_premium(self):
rd = FileReader.Digit(self.pdf_path)
insurance = self.get_insurance_type()[2]
prem = rd.get_tp_premium(Policy_Type=insurance)
if prem is not None:
return prem
def get_od_premium(self):
final_premium = None
insurance = self.get_insurance_type()[2]
if insurance == 'TP':
return '0'
rd = FileReader.Digit(self.pdf_path)
prem = rd.get_od_premium()
if prem is not None:
return prem[0]