-
Notifications
You must be signed in to change notification settings - Fork 2
/
newparser.py
348 lines (307 loc) · 10.1 KB
/
newparser.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
import re
from fuzzywuzzy import fuzz
from fuzzywuzzy import process
import xml.etree.cElementTree as ET
from xml.etree import ElementTree
import xml.dom.minidom as xmd
from django.conf import settings
import os
data_forms_mapping={
'name':['name of the patient','name of patient','patient name','claimant name','patient','name','claimant'],
'referred':['referred by','ref. by','refby','refd.by.','doctor', 'dr.' ,'md:' ,'dr:' , 'from whom was treatment' ],
'age':['age'],
'date':['date received','date of collection','date'],
'dob':['date of birth','dob'],
'sex':['sex','gender'],
'sino':['file no'],
'labno':['lab no'],
'history':['history'],
"aadhar":["aadhar number" , "aadharnumber", "aadharnum" , "aadharid", "aadhar_id" , "aadhar"]
}
worddict={}
seperators = [':' , "-" , "|" , "_"]
numarray = [str(i) for i in range(100,0,-1)]
def num(line):
for age in numarray:
if(age in line):
return age
return ""
def preprocessor(input_data):
"""
This function cleans the OCR data
:param input_data:
return pre_processed_data
"""
final_data = ""
for line in input_data.splitlines():
final_data += line.strip() + "\n"
input_data = final_data
#Push Arbitrary Spacing to new line i.e 2 or more
input_data = re.sub(r" [' ']+","\n",input_data)
input_data = re.sub(r'[^\x00-\x7f]',r'', input_data)
input_data_list = input_data.splitlines()
#Collapsing the new lines into one new line
input_data = re.sub(r"\n+","\n",input_data).strip()
return input_data.lower()
################################################
############# GETS TO GENERATE #################
############# DICTIONARY KEYS #################
################################################
def get_name(input_data):
input_data = input_data.split("\n")
name = ["",0]
value = 0
indexer = 0
#print input_data
#extracted the line from which has name or previous line
for line in input_data:
flag = 0
for tag in data_forms_mapping['name']:
if(tag in line):
name[0] = line
name[1] = indexer
flag = 1
if(flag):
break
curr_value = max(fuzz.ratio(line, tags) for tags in data_forms_mapping['name'])
if(curr_value > value):
value = curr_value
name[0] = line
name[1] = indexer
indexer+=1
#print name
for tag in data_forms_mapping['name']:
name[0]=name[0].replace(tag , "")
name[0] = name[0].strip()
for i in seperators:
name[0]=name[0].replace(i, "")
if(name[0] == "" or len(name[0])<= 3):
name[0] = input_data[name[1]+1]
return name[0]
def get_doctor(input_data):
input_data = input_data.split("\n")
while('.' in input_data):
input_data.remove('.')
for i in seperators:
while(i in input_data):
input_data.remove(i)
doctor = ["",0]
value = 0
indexer = 0
#extracted the line from which has name or previous line
for line in input_data:
flag = 0
for tag in data_forms_mapping['referred']:
if(tag in line):
doctor[0] = line
doctor[1] = indexer
flag = 1
break
if(flag):
break
curr_value = max(fuzz.ratio(line, tags) for tags in data_forms_mapping['referred'])
if(curr_value > value):
value = curr_value
doctor[0] = line
doctor[1] = indexer
indexer+=1
#print doctor
for tag in data_forms_mapping['referred']:
doctor[0]=doctor[0].replace(tag , "")
doctor[0] = doctor[0].strip()
for i in seperators:
doctor[0]=doctor[0].replace(i, "")
if(doctor[0] == "" or len(doctor[0]) == 1 ):
doctor[0] = input_data[doctor[1]+1]
if(doctor[0].count(" ")>=3):
return ""
return doctor[0]
def get_age(input_data):
input_data = input_data.split("\n")
age_ends = ["months" , "yrs" , "yr" , "days"]
counter = 0
age = ""
for line in input_data:
if('age' in line):
break
counter+=1
for i in range(counter+1,len(input_data)):
age = num(input_data[i])
if(age!=""):
break
return age
def get_date(input_data):
#coz there are mutliple dates
date_arr = re.findall(r"[0-9][0-9][\/|\-][0-9][0-9][\/|\-][1-9][0-9]+" , input_data)
return ' '.join(date_arr)
def get_sex(input_data):
input_data = input_data.split("\n")
gender = ""
indexer = 0
#extracted the line from which has name or previous line
flag=0
for line in input_data:
if "sex" in line or "gender" in line:
flag=1
break
indexer+=1
if flag==0:
return gender
if "sex" in input_data[indexer] :
gender = input_data[indexer].replace("sex","")
elif "gender" in input_data[indexer] :
gender = input_data[indexer].replace("gender","")
for i in seperators:
gender=gender.replace(i, "")
gender = gender.strip()
if len(gender)==0:
gender = input_data[indexer+1]
return gender
def get_sino(input_data):
input_data = input_data.split("\n")
sino = ""
indexer = 0
flag=0
for line in input_data:
if "file no" in line:
flag=1
break
indexer+=1
if flag==0:
return sino
sino = input_data[indexer+1]
for i in seperators:
sino=sino.replace(i, "")
return sino.strip()
def get_labno(input_data):
input_data = input_data.split("\n")
labno = ""
indexer = 0
flag=0
for line in input_data:
if "lab no" in line:
flag=1
break
indexer+=1
if flag==0:
return labno
labno = input_data[indexer+1]
for i in seperators:
labno=labno.replace(i, "")
return labno.strip()
def create_ccd(worddict):
print("in ccd=",worddict)
composition=ET.Element("composition")
if 'date' in worddict:
ET.SubElement(composition,"date", value=worddict["date"])
else:
ET.SubElement(composition,"date", value="")
ET.SubElement(composition,"title", value="Continuity of Care Document")
ET.SubElement(composition,"status", value="preliminary")
subject=ET.SubElement(composition,"subject")
if 'name' in worddict:
ET.SubElement(subject,"display", value=worddict["name"])
else:
ET.SubElement(subject,"display", value="e")
author=ET.SubElement(composition,"author")
if 'doctor' in worddict:
ET.SubElement(author,"display", value=worddict["doctor"])
else:
ET.SubElement(author,"display", value="e")
attester=ET.SubElement(composition,"attester")
ET.SubElement(attester,"mode",value="legal")
party=ET.SubElement(attester,"party")
if 'doctor' in worddict:
ET.SubElement(party,"display", value=worddict["doctor"])
else:
ET.SubElement(party,"display", value="")
section=ET.SubElement(composition,"section")
ET.SubElement(section,"title",value="report")
tree=ET.ElementTree(composition)
#name=worddict["name"]
#name+=".xml"
name="output.xml"
filepath="/home/chinu/datadigitizer-project/outputdocs/"
filepath=os.path.join(filepath,name)
tree.write(filepath)
xmlstr = ElementTree.tostring(composition, encoding='utf8', method='xml')
dom=xmd.parseString(xmlstr)
pretty_xml=dom.toprettyxml()
print("xml created successfuly")
return pretty_xml
def get_aadhar(input_data):
input_data = input_data.split("\n")
aadhar = ["",0]
value = 0
indexer = 0
#print input_data
#extracted the line from which has name or previous line
for line in input_data:
flag = 0
for tag in data_forms_mapping['aadhar']:
if(tag in line):
aadhar[0] = line
aadhar[1] = indexer
flag = 1
if(flag):
break
curr_value = max(fuzz.ratio(line, tags) for tags in data_forms_mapping['aadhar'])
if(curr_value > value):
value = curr_value
aadhar[0] = line
aadhar[1] = indexer
indexer+=1
#print name
for tag in data_forms_mapping['aadhar']:
aadhar[0]=aadhar[0].replace(tag , "")
aadhar[0] = aadhar[0].strip()
for i in seperators:
aadhar[0]=aadhar[0].replace(i, "")
if(aadhar[0] == "" or len(aadhar[0])<= 3):
aadhar[0] = input_data[aadhar[1]+1]
return aadhar[0]
def ocr_data_parser(filename):
"""
This will generate the keys in data_required_and_their_forms_mapping dictionary
:param file_name:
return key:value pairs
"""
file_handle = open(filename+".txt" , "r")
#file_handle1 = open( filename+"p.txt", "r")
input_data = file_handle.read()
pre_processed_data = preprocessor(input_data)
#file_handle1.write(pre_processed_data)
name = get_name(pre_processed_data)
print(name)
doctor = get_doctor(pre_processed_data)
print(doctor)
age = get_age(pre_processed_data)
print(age)
date = get_date(pre_processed_data)
print(date)
sex = get_sex(pre_processed_data)
print(sex)
sino = get_sino(pre_processed_data)
print(sino)
labno = get_labno(pre_processed_data)
print(labno)
aadhar = get_aadhar(pre_processed_data)
print(aadhar)
# we have not implemented history
#history = get_history(pre_processed_data)
print("#########")
worddict = {'name':name ,'aadhar':aadhar ,'doctor':doctor , 'age':age , 'sex':sex, 'sino':sino , 'labno': labno ,'date':date}
print(worddict)
fhir={}
fhir['patient']=worddict
fhir['report']={}
fhir['report']['location'] = os.path.join(settings.BASE_DIR,"output.txt")
tree=create_ccd(worddict)
parser_out={}
parser_out['jsonout']=fhir
parser_out['tree']=tree
print(parser_out)
return parser_out
#ocr_data_parser(r"adhaar")
#file_names = ['27_handwritten', 'sdd', 'omega', 'dorm', 'medical', '28_handwritten']
#for i in file_names: