-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
executable file
·72 lines (66 loc) · 2.55 KB
/
predict.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
import os,sys,json
from flair.data import Sentence
import time
import flair
from date_sanity import checkfirst,check_all
from datetime import datetime
from dateutil.parser import parse
def predict(model, fields,docOCR, textOCR=None):
output={}
if textOCR:
text_sent = Sentence(textOCR, use_tokenizer=True)
# time_to_predict=time.time()
model.predict(text_sent)
textout = find_vals(text_sent, fields)
doc_sent = Sentence(docOCR, use_tokenizer=True)
model.predict(doc_sent)
docout = find_vals(doc_sent, fields)
for k in docout.keys():
# if len(textout[k])>len(docout[k]):
# output[k]=textout[k]
# else:
output[k]=docout[k]
else:
doc_sent = Sentence(docOCR, use_tokenizer=True)
model.predict(doc_sent)
docout = find_vals(doc_sent,fields)
output=docout
# print(output)
return output
def find_vals(sent,fields):
# start_time=
out1={}
out={}
# print(fields)
# print("bfusfusu")
for fname in fields:
out1[fname]=[]
for word in sent.tokens:
# print(word.tags["ner"].value)
if word.tags["ner"].value=="B-PER":
# print(word)
out1['firstName'].append(word.text)
elif word.tags["ner"].value=="I-PER":
out1['lastName'].append(word.text)
elif word.tags["ner"].value == "B-NUM":
# print("###################")
out1['number'].append(word.text)
elif word.tags["ner"].value == "B-DOB":
out1['dob'].append(word.text)
elif word.tags["ner"].value == "B-DOI":
out1['issueDate'].append(word.text)
elif word.tags["ner"].value == "B-DOE":
out1['expiryDate'].append(word.text)
elif word.tags["ner"].value == "B-ADD":
out1['address'].append(word.text)
print (out1)
out1=checkfirst(out1)
try:
if (len(out1['dob'])) and (len(out1['issueDate'])) and (len(out1['expiryDate'])) and (not (out1['dob'][0],out1['issueDate'][0],out1['expiryDate'][0]==sorted([out1['dob'][0],out1['issueDate'][0],out1['expiryDate'][0]], key=lambda x: datetime.strptime(x, '%d/%m/%Y')))):
out1['dob'],out1['issueDate'],out1['expiryDate']=check_all(out1['dob'],out1['issueDate'],out1['expiryDate'])
except Exception as e:
out1['dob'],out1['issueDate'],out1['expiryDate']=check_all(out1['dob'],out1['issueDate'],out1['expiryDate'])
for fname in fields:
out[fname]=' '.join(out1[fname])
# print(out)
return out