-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfinal_report_cleaning.py
507 lines (478 loc) · 15.8 KB
/
final_report_cleaning.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
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#Final Report Clean-up
#Remove Non-clinically relevant (and potentially confounding) keywords from lines; Remove redaction artifacts and empty lines
import glob, os
import pickle
import pandas as pd
import shutil
import re
from collections import Counter
import matplotlib.pyplot as plt
import seaborn as sns
#Create new directory for output
prev_dir = 'post_table_and_handwriting_removal/'
new_dir = 'post_keyword_and_empty_filtering/'
if not os.path.exists(new_dir):
os.makedirs(new_dir)
#Data to be processed
current_files = glob.glob(prev_dir+'*.p')
print('Number of pages to process:',len(current_files))
#List of terms to match and remove
list_to_remove = ['\\*',
'^end[ ]*of[ ]*report$', #footer
'^\(end[ ]*of[ ]*report\)$', #footer
'page[ ]*(\S+)[ ]*of[ ]*(\S+)', #footer
'continued[ ]*on[ ]*next[ ]*page.*', #footer
'Translated[ ]*by.*', #footer
'^Edited by.*', #footer
'.*CONTACT[ ]*YOUR[ ]*DOCTOR[ ]*WITH[ ]*THIS[ ]*REPORT.*', #footer
'.*MM/DD/YYYY.*', #remove entire line containing this expression
'.*Ref[\. ]*Number.*', #header
'.*DOB.*', #header
'^D\.O\.B\..*', #header
'^B.?Day.*',#header
'.*Internal[ ]*Sample[ ]*ID.*', #header
'^Surg[ ]*Path[ ]*Final[ ]*Report.*$',
'^Surg\.[ ]*Path\.[ ]*No[ ]*.?:?#?$',
'^Surg[ ]*Path.?:?#?$',
'^Surgical[ ]*Pathology[ ]*.?:?#?$',
'^Surgical[ ]*Pathology[ ]*Report[ ]*.?:?#?$',
'^Surgical[ ]*Pathology[ ]*Final[ ]*.?:?#?[ ]*Report[ ]*.?:?#?$',
'^Surgical[ ]*Pathology[ ]*Consultation[ ]*Report[ ]*.?:?#?$',
'^Surgical[ ]*Pathology[ ]*.?:?#?[ ]*\[OrderID[ ]*.?:?#?$',
'^Surgical[ ]*Date[ ]*.?:?#?$',
'^Sample[ ]*.?:?#?.?:?#?$',
'^Sample[ ]*Number[ ]*.?:?#?$',
'^Sample[ ]*ID[ ]*.?:?#?$',
'^Sample Procurement:?[ ]*Date.*',
'^TCGA.*', #TCGA barcode remnants; accounting for variations encountered due to OCR mis-translation
'Redacted.*',
'^UUID.*',
'^JUID.*',
'^UID:.*',
'^JID:.*',
'^IID:.*',
'^ICGA.*',
'^ID[ ]*.?:?#?$',
'^D:\S+\-?\S+\-?\S+\-?\S+\-?\S+$',
'^D[ ]*:[ ]*\d+$',
'^ID[ ]*:[ ]*\S+\-?\S+\-?\S+\-?\S+\-?\S+$',
'^TCSA.*',
'^CGA.*',
'.*PATH\.[ ]*NO.?:?#?.*', #header
'^TSS.*', #header
'^[ ]*Tissue[ ]*Source[ ]*Site[ ]*.?:?#?', #header
'^Case[ ]*#.?.*',
'^Case.?:?#?[ ]*$',
'^Age \\(years\\).*',
'^Age.?:?$', #header
'.*Age/Sex.*', #header
'^Sex/Race.*$',#header
'^Tumor[ ]*ID.?:?#?$', #header
'^Date[ ]*.?:?#?$', #header
'^Date[ ]*Ordered[ ]*.?:?#?$',
'^Date[ ]*Signed[ ]*.?:?#?$',
'^Compliance[ ]*validated[ ]*by[ ]*.?:?#?$',
'^Collect[ ]*date.?:?#?',#header
'^Date[ ]*of[ ]*Procedure.?:?#?', #header
'.*Date[ ]*of[ ]*procurement[ ]*.?:?#?', #header
'.*Date[ ]*of[ ]*tumor procurement.*',#header
'^Date[ ]*of[ ]*report.*', #header
'^Date[ ]*of[ ]*Surgery.*', #header
'^Date[ ]*Reported.*',#header
'^Date[ ]*of.?[ ]*$',
'^Procurement[ ]*Date[ ]*.?:?#?',
'^Date[ ]*of[ ]*Receipt.*',#header
'.*Specimen[ ]*Date/Time[ ]*.?',#header
'^Unique[ ]*Patient[ ]*Identifier.*', #header
'MRN:.*',#header
'<Sign[ ]*Out[ ]*Dr\.[ ]*Signature>.*',
'^Accession[ ]*.?:?#?[ ]*$', #header
'^Acc#.*', #header
'^PHYSICIAN[ ]*&[ ]*PAGER[ ]*#.*',#header
'^Examination[ ]*No[ ]*\..?:?#?', #header
'^Patient[ ]*Name[ ]*.?:?#?[ ]*$', #header
'^Patient\'s[ ]*Name[ ]*.?:?#?[ ]*$', #header
'^Patient[ ]*Identification[ ]*.?:?#?$', #header
'^Patient[ ]*Results[ ]*:?#?[ ]*$', #header
'^Procurement[ ]*.?:?#?$$',#header
'Name[ ]*of[ ]*Pathologist.*',#header
'^Assistant[ ]*.?:?#?$', #header
'^Pathology[ ]*Assistant[ ]*.?:?#?$',
'^Pathologists\'[ ]*Assistant[ ]*.?:?#?$',
'^Performed[ ]*by[ ]*Pathologist\'s[ ]*Assistant[ ]*.?:?#?$',
'.*MED\.[ ]*REC\.[ ]*N.*', #header
'.*Med\.[ ]*Rec\.[ ]*#.*', #header
'^Admit[ ]*Protocol[ ]*.?:?#?$',
'\\(\\)', #empty parentheses
'\[[ ]*\]',
'Printed[ ]*by[ ]*.?:?#?$', #footer
'Printed[ ]*on[ ]*.?:?#?$', #footer
'^Verified[ ]*By.*',#footer
'Report[ ]*Electronically[ ]*Signed.*',#footer
'^History[ ]*Case[ ]*Pathology.*', #header
'^File[ ]*under.*', #header
'^Admitting Physician.*',
'^Result[ ]*Type.?:?#?$', #header
'^Result[ ]*Title.*', #header
'^Result[ ]*Status.?:?#?$', #header
'Patient:[ ]*XXX', #header
'^Examination performed on.*', #header
'^Internal[ ]*invoice.*', #header
'PESEL:[ ]*XXX.*',
'^OPER[ ]*.?:?#?$',
'^Service.?:?#?$',
'^Ref.?[ ]*Physician.*',
'^Account[ ]*#.?$',
'^OUTPATIENT.?:?#?$',
'^Patient[ ]*Address.*',
'^Billing.*$',
'.*Additional Copy to.*',
'^Ref\.[ ]*Source.*',
'^INPATIENT.?:?#?$',
'^Other[ ]*Related[ ]*Data.?:?#?$',
'^Financial[ ]*Number.*',
'^Performing[ ]*Clinician.?:?#?$',
'^Street[ ]*address.*',
'^Internal[ ]*postal[ ]*address.*',
'^Patient[ ]*Key.*$',
'^E\-mail.*$',
'^Concerning.?:?#?$',
'^Direct[ ]*dial[ ]*.?:?#?$',
'^Path.?\-[ ]*No.?.?[ ]*$',
'^Mobile[ ]*.?:?#?$',
'^Postal[ ]*address[ ]*.?:?#?$',
'^Pt\.[ ]*Phone[ ]*no[ ]*.?:?#?$',
'^PATIENT[ ]*PHONE[ ]*NO[ ]*.?:?#?$',
'^HCN[ ]*.?:?#?$',
'.*AP[ ]*Report[ ]*.?:?#?$',
'^Final[ ]*Report[ ]*.?:?#?$',
'^Document[ ]*Type[ ]*.?:?#?$',
'^Document[ ]*Status[ ]*.?:?#?$',
'^Document[ ]*.?:?#?$',
'^Auth[ ]*\\(Verified\\)',
'^Document[ ]*Title[ ]*.?:?#?$',
'^Anatomic[ ]*Pathology[ ]*Report[ ]*.?:?#?$',
'^Encounter[ ]*info[ ]*.?:?#?$',
'^Value[ ]*of[ ]*diagnostic[ ]*procedure[ ]*.?:?#?$',
'^Result[ ]*.?:?#?$',
'^Received?[ ]*.?.?:?#?$' ,
'^Entry[ ]*Information[ ]*.?:?#?$',
'^Seen[ ]*in[ ]*consultation[ ]*with[ ]*.?:?#?$',
'^continued[ ]*next[ ]*.?:?#?$',
'^Entry[ ]*Date[ ]*and[ ]*Time.?[ ]*$',
'^Lab[ ]*Status[ ]*.?:?#?$',
'^Entered[ ]*by[ ]*.?:?#?$',
'^Final[ ]*result[ ]*.?:?#?$',
'^Reported[ ]*by[ ]*.?:?#?$',
'^Report[ ]*.?:?#?$',
'^Physician[ ]*.?:?#?$',
'^Collected[ ]*.?:?#?$',
'^Specimen[ ]*Received*.?:?#?$',
'^Pathology[ ]*Number[ ]*.?:?#?$',
'^Inquiry[ ]*Number.?[ ]*.?:?#?$',
'^Received[ ]*Time[ ]*.?:?#?$',
'^Received[ ]*Date[ ]*.?:?#?$',
'^Time[ ]*Collected[ ]*.?:?#?$',
'^Time[ ]*Received[ ]*.?:?#?$',
'^Time[ ]*Reported[ ]*.?:?#?$',
'^Time[ ]*Transmitted[ ]*.?:?#?$',
'^Collected[ ]*Time[ ]*.?:?#?$',
'^CC:$',
'^Final[ ]*.?:?#?$',
'^Relevant Information[ ]*.?:?#?$',
'^Location[ ]*.?:?#?$',
'^Copied[ ]*To[ ]*.?:?#?$',
'^Report[ ]*Patient[ ]*.?:?#?$',
'^Name[ ]*.?:?#?$',
'^Accession[ ]*Number[ ]*.?:?#?$',
'^Reviewed[ ]*.?:?#?$',
'^Reviewed[ ]*and[ ]*electronically.*',
'^Reviewed[ ]*.?:?#?$',
'^Reviewed[ ]*by .*',
'^Reviewed[ ]*By[ ]*:?[ ]*List.*',
'^Grossed[ ]*by[ ]*:?.*',
'^Reviewed[ ]*by[ ]*dr.*',
'^Lab[ ]*Information[ ]*.?:?#?$',
'^Parent[ ]*Order[ ]*.?:?#?$',
'^Child[ ]*Order[ ]*.?:?#?$',
'^Result[ ]*Information[ ]*.?:?#?$',
'^Result[ ]*and Time[ ]*.?:?#?$',
'^Edited[ ]*.?:?#?$',
'^Entry[ ]*Date[ ]*.?:?#?$',
'^Component[ ]*Results[ ]*.?:?#?$',
'^Taken[ ]*.?:?#?$',
'^Physician\\(s\\)[ ]*.?:?#?$',
'^Reported[ ]*.?:?#?$',
'^Reported[ ]*Date[ ]*.?:?#?$',
'^BIRTH[ ]*.?:?#?$',
'^PAT[ ]*TYPE[ ]*.?:?#?$',
'^ADM[ ]*.?:?#?[ ]*.?:?#?$',
'^Unit[ ]*in[ ]*charge[ ]*.?:?#?$',
'^Physician[ ]*in[ ]*charge[ ]*.?:?#?$',
'^Material[ ]*collected[ ]*on[ ]*.?:?#?$',
'^Material[ ]*received[ ]*on[ ]*.?:?#?$',
'^Address[ ]*.?:?#?$',
'^MR[ ]*No[ ]*.?:?#?$',
'^Taken/Received[ ]*.?:?#?$',
'^Service[ ]*.?:?#?$',
'^Surgeon/physician[ ]*.?:?#?$',
'^Attending[ ]*Surgeon[ ]*.?:?#?$',
'^Surgeons?[ ]*.?.?:?#?$',
'^Gender[ ]*.?.?:?#?$',
'^Sex[ ]*.?.?:?#?$',
'^Case[ ]*Number[ ]*.?:?#?$',
'^Phone[ ]*.?.?:?#?$',
'^Fax[ ]*.?.?:?#?$',
'^\\(Continued\\)[ ]*.?:?#?$',
'^Tel[ ]*.?:?#?$',
'^Surname[ ]*.?:?#?$',
'^Surname[ ]*and[ ]*Name.?:?#?$',
'^Forename.?\\(s\\)[ ]*.?:?#?$',
'^Unit[ ]*.?:?#?$',
'^Request Date[ ]*.?:?#?$',
'^This[ ]*Copy[ ]*For[ ]*.?:?#?$',
'^Path#[ ]*.?:?#?$',
'^Staff[ ]*Pathologist[ ]*.?:?#?$',
'^Other[ ]*Pathologists[ ]*/[ ]*PAs[ ]*.?:?#?$',
'^Dictated[ ]*by[ ]*.?:?#?$',
'^MR[ ]*.?:?#?$',
'^Signature[ ]*.?:?#?$',
'^COMMITTEE.*',
'^Requested[ ]*by[ ]*.?:?#?$',
'^AP[ ]*Surgical[ ]*Pathology[ ]*.?:?#?$',
'^Performed[ ]*by[ ]*.?:?#?$',
'^Performed[ ]*by[ ]*the[ ]*staff[ ]*pathologist[ ]*.?:?#?$',
'^MED.?[ ]*REC[ ]*.?:?#?$',
'PATIENT[ ]*PHONE[ ]*.?:?#?$',
'^CONF[ ]*.?:?#?$',
'^Collection.*',
'^Print[ ]*time.*',
'^professor[ ]*of[ ]*pathology[ ]*.?:?#?$',
'Filed[ ]*automatically[ ]*on.*',
'^by[ ]*.?:?#?$',
'Report[ ]*electroni.*',
'^Electronic.*',
'Excision[ ]*date.*',
'Formatted[ ]*Path[ ]*Reports?[ ]*.?:?#?$',
'Compliance[ ]*vali[ ]*by.*',
'^Status[ ]*:[ ]*.?:?#?complete$',
'^Status[ ]*.?:?#?$',
'^Status[ ]*History[ ]*.?:?#?$',
'^Status[ ]*.?:?#?[ ]*Signed.*$',
'^Status[ ]*.?:?#?[ ]*Corrected$',
'^Status[ ]*:[ ]*.?:?#?supplemental$',
'^STATUS[ ]*:[ ]*SOUT[ ]*.?:?#?$',
'^Status[ ]*:[ ]*N/A[ ]*.?:?#?$',
'^Status[ ]*:[ ]*simed[ ]*Out[ ]*.?:?#?$',
'^Status[ ]*:[ ]*Ordered[ ]*.?:?#?$',
'^Status[ ]*Final[ ]*result[ ]*.?:?#?$',
'^Status:[ ]*Siman[ ]*Out[ ]*.?:?#?$',
'^Status[ ]*\\(Last[ ]*updated[ ]*Date/Time\\)[ ]*.?:?#?$',
'^Status:[ ]*Complete[ ]*as[ ]*of[ ]*.?:?#?$',
'^Status:[ ]*Final[ ]*resuit[ ]*.?:?#?$',
'^Status:[ ]*Final[ ]*result[ ]*.?:?#?$',
'^Status[ ]*of[ ]*Proceduze:[ ]*Signed[ ]*Out[ ]*.?:?#?$',
'^Status[ ]*:[ ]*DIS[ ]*IN[ ]*.?:?#?$',
'^Department[ ]*of[ ]*Cancer Pathology.*',
'^Department[ ]*of[ ]*Pathology[ ]*.?:?#?$',
'Submitting[ ]*Physician.*',
'^Resulting[ ]*Agency[ ]*.?:?#?$',
'^Provider[ ]*Information[ ]*.?:?#?$',
'Accessioned[ ]*on.*',
'CASE NO[ ]*.?:?#?$',
'^OpenClinica.*',
'^Order.*',
'^Test[ ]*performed[ ]*at.*',
'Addendum[ ]*Report[ ]*Issued.*',
'^MRN[ ]*.?:?#?[ ]*.?:?#?[ ]*.?:?#$',
'This[ ]*report[ ]*has[ ]*been[ ]*finalized.*',
'^FMP/SSN.*',
'^SSN.*',
'^Referring[ ]*Physician[ ]*.?:?#?$',
'^Subm[ ]*Dr[ ]*.?:?#?.?:?#?$',
'^Acct[ ]*.?:?#?$',
'^Reg[ ]*.?:?#?$',
'^Disch[ ]*.?:?#?$',
'^Att[ ]*Dr.*',
'^Req[ ]*.?:?#?$',
'^Ref[ ]*.?:?#?$',
'^Hosp[ ]*.?:?#?$',
'^Prov[ ]*.?:?#?$',
'^\\(FINAL\\)[ ]*.?:?#?$',
'^Patient[ ]*Na[ ]*.?:?#?$',
'Visit[ ]*.?:?#?$',
'^Path[ ]*.?:?#?$',
'^Primary[ ]*Path[ ]*.?:?#?$',
'^Resident[ ]*Path[ ]*.?:?#?$',
'^Copath[ ]*[ ]*.?:?#?$',
'^Department[ ]*:[ ]*PATH[ ]*.?:?#?$',
'^Formatted[ ]*Path[ ]*.?:?#?$',
'Copy[ ]*To.*',
'^Transcribed[ ]*by.*',
'^Surgery[ ]*.?:?#?$',
'^XX[ ]*.?:?#?$',
'^XXX+[ ]*.?:?#?$',
'^Signed[ ]*.?:?#?$',
'^Signed[ ]*out[ ]*.?:?#?$',
'^Signed[ ]*by[ ]*.?:?#?$',
'^Signed[ ]*out[ ]*by[ ]*.?:?#?$',
'^\[][ ]*.?:?#?$',
'Surg[ ]*Date.*',
'^TBCF#[ ]*.?:?#?$:',
'^Gross[ ]*Dictation.*',
'^Unique[ ]*Patient[ ]*.?:?#?$',
'^Hospital[ ]*.?:?#?$',
'^Patient[ ]*Type[ ]*.?:?#?$',
'^Physician[ ]*Copy[ ]*.?:?#?$',
'^PATHOLOGY[ ]*REPORT[ ]*.?:?#?$',
'^Component[ ]*Results[ ]*.?:?#?$',
'Patient[ ]*Number[ ]*.?:?#?$',
'Room/Bed.*',
'Ethnicity \\(Race\\)[ ]*.?:?#?$',
'^Cancer[ ]*Sample[ ]*.?:?#?$',
'^Page[ ]*.?:?#?$',
'^Page:[ ]*(S+)$',
'^Pathologist[ ]*.?:?#?$',
'^Patient[ ]*.?:?#?$',
'^PAT[ ]*TYPE[ ]*.?:?#?$',
'.*1974.*',#PRIVACY ACT OF 1974
'^No[ ]*.?:?#?$',
'^Yes[ ]*.?:?#?$',
'^\.+$',
'^,+$',
'^:+$',
'^-+$',
'^Collect[ ]*.?:?#?$',
'^[:\.,\)\(\?\*;]?[ ]*[:\.,\)\(\?\*;-]?[ ]*[:\.,\)\(\*;]?[ ]*[:\.,\)\(\?\*;-]?[ ]*[:\.,\)\(\*;-]?$'] #Matches if a line contains only punctuation (with any number of spaces in between)
#List of terms that require capitalization for specific-matching
full_cap_list=['.*DOB.*','^D\.O\.B\..*',
'^DOB.*',
'^UUID.*',
'^JUID.*',
'^UID:.*',
'^JID:.*',
'^ICGA.*',
'^ID[ ]*.?:?#?$',
'^TCSA.*',
'^CGA.*',
'^TCGA.*',
'^HCN[ ]*.?:?#?$',
'^TSS.*']
#List of terms that require specifically the first letter to be capitalized for matching
capitalize_first_letter = ['^Case[ ]*#.?.*',
'^Case.?:?#?[ ]*$',
'^Patient[ ]*.?:?#?$',
'^Patient[ ]*Name[ ]*.?:?#?[ ]*$', #header
'^Patient\'s[ ]*Name[ ]*.?:?#?[ ]*$',
'^Patient[ ]*Identification[ ]*.?:?#?$', #header
'^Patient[ ]*Results[ ]*:?#?[ ]*$', #header
'PATIENT[ ]*PHONE[ ]*.?:?#?$',
'^PATIENT[ ]*PHONE[ ]*NO[ ]*.?:?#?$',
'Patient:[ ]*XXX', #header
'^Patient[ ]*Na[ ]*.?:?#?$',
'^Patient[ ]*Address.*',
'^Patient[ ]*Key.*$',
'^Patient[ ]*Type[ ]*.?:?#?$',
'Patient[ ]*Number[ ]*.?:?#?$',
'^Sample[ ]*.?:?#?.?:?#?$',
'^Sample[ ]*Number[ ]*.?:?#?$',
'^Sample[ ]*ID[ ]*.?:?#?$',
'^Sample Procurement:?[ ]*Date.*',
'^Taken[ ]*.?:?#?$',
'^Reported[ ]*.?:?#?$',
'^Reported[ ]*by[ ]*.?:?#?$',
'^Reported[ ]*Date[ ]*.?:?#?$',
'^Order.*',
'^Collection.*',
'^Procurement[ ]*.?:?#?$',
'^Procurement[ ]*Date[ ]*.?:?#?$',
'^Status[ ]*:[ ]*.?:?#?complete$',
'^Status[ ]*.?:?#?$',
'^Status[ ]*History[ ]*.?:?#?$',
'^Status[ ]*.?:?#?[ ]*Signed.*$',
'^Status[ ]*.?:?#?[ ]*Corrected$',
'^Status[ ]*:[ ]*.?:?#?supplemental$',
'^STATUS[ ]*:[ ]*SOUT[ ]*.?:?#?$',
'^Status[ ]*:[ ]*N/A[ ]*.?:?#?$',
'^Status[ ]*:[ ]*simed[ ]*Out[ ]*.?:?#?$',
'^Status[ ]*:[ ]*Ordered[ ]*.?:?#?$',
'^Status[ ]*Final[ ]*result[ ]*.?:?#?$',
'^Status:[ ]*Siman[ ]*Out[ ]*.?:?#?$',
'^Status[ ]*\\(Last[ ]*updated[ ]*Date/Time\\)[ ]*.?:?#?$',
'^Status:[ ]*Complete[ ]*as[ ]*of[ ]*.?:?#?$',
'^Status:[ ]*Final[ ]*resuit[ ]*.?:?#?$',
'^Status:[ ]*Final[ ]*result[ ]*.?:?#?$',
'^Status[ ]*of[ ]*Proceduze:[ ]*Signed[ ]*Out[ ]*.?:?#?$',
'^Status[ ]*:[ ]*DIS[ ]*IN[ ]*.?:?#?$',
'^Reviewed[ ]*.?:?#?$',
'^Reviewed[ ]*and[ ]*electronically.*',
'^Reviewed[ ]*.?:?#?$',
'^Reviewed[ ]*by .*',
'^Reviewed[ ]*By[ ]*:?[ ]*List.*',
'^Surgeon/physician[ ]*.?:?#?$',
'^Surgeons?[ ]*.?.?:?#?$',
'^Pathologist[ ]*.?:?#?$',
'^Surgical[ ]*Pathology[ ]*.?:?#?$',
'^Surgical[ ]*Pathology[ ]*Report[ ]*.?:?#?$',
'^Surgical[ ]*Pathology[ ]*Final[ ]*.?:?#?[ ]*Report[ ]*.?:?#?$',
'^Surgical[ ]*Pathology[ ]*Consultation[ ]*Report[ ]*.?:?#?$',
'^Surgical[ ]*Pathology[ ]*.?:?#?[ ]*\[OrderID[ ]*.?:?#?$',
'^Surgical[ ]*Date[ ]*.?:?#?$']
#Define function for line removal, specifying capitalization conditions for keywords as outlined above
def process_line(new_line, list_to_remove, keyword_lines_removed_dict):
for element in list_to_remove:
old_line = new_line
prev = len(old_line)
if element in full_cap_list:
if element == '.*DOB.*':
if 'ENDOBRONCHIAL' not in new_line:
new_line = re.sub(element, "", new_line).strip()
else:
new_line = re.sub(element, "", new_line).strip()
elif element in capitalize_first_letter:
if len(new_line) > 0:
if new_line[0].isupper():
new_line = re.sub(element, "", new_line, flags=re.IGNORECASE).strip()
else:
if element in ['^No[ ]*.?:?#?$','^Yes[ ]*.?:?#?$']:
if (i != 0) and lines[i-1].strip()[-1]==':': #Ammending yes/no removal
pass
else:
new_line = re.sub(element, "", new_line, flags=re.IGNORECASE).strip()
new_line = re.sub(element, "", new_line, flags=re.IGNORECASE).strip()
if len(new_line) != prev:
keyword_lines_removed_dict[element].append(old_line)
return new_line, keyword_lines_removed_dict
#Track lines removed (and which keywords trigger removal)
keyword_lines_removed_dict = {elem:[] for elem in list_to_remove}
n_lines_removed_dict = {}
#Track number of empty pages (removed)
n_empty_pages = 0
#Note: The below code may take some time to run
for j in range(len(current_files)):
p = current_files[j]
n_lines_removed = 0
new_lines = []
lines = pickle.load(open(p, "rb"))
for i in range(len(lines)):
len_line = len(lines[i])
new_line = lines[i].strip() #Remove leading and trailing whitespace
new_line = re.sub(' {2,}', ' ', new_line) #Remove >1 white space between words
#3x keyword removal
for cycle in range(3):
new_line, keyword_lines_removed_dict = process_line(new_line, list_to_remove, keyword_lines_removed_dict)
if (len_line != 0) and (len(new_line)==0):
n_lines_removed +=1
#Remove lines that were originally empty or whitespace, or that are now whitespace
elif (len_line != 0) and (len(lines[i].strip())!=0) and (len(new_line.strip())!= 0):
new_lines.append(new_line)
#Only save non-empty pages
if len(new_lines) > 0:
pickle.dump(new_lines, open(new_dir + p.replace(prev_dir,""),"wb"))
else:
n_empty_pages +=1
n_lines_removed_dict[p.replace(prev_dir,'').replace('_lines.p','')] = n_lines_removed
#Track progress
if j % 2000 == 0:
print(j,' pages finished.')
print(n_empty_pages,' empty pages, removed.')