-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathannotate.py
More file actions
294 lines (272 loc) · 10.1 KB
/
annotate.py
File metadata and controls
294 lines (272 loc) · 10.1 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
import re
import pandas as pd
alias_lists = [
['Asthma', ], # asztma
['CAD', # szívkoszorúér betegség [elmeszesedés]
# 'coronary (?:(?:artery|heart)? ?disease|atherosclerosis)',
# 'coronary (?:atherosclerosis)', # remove is better
'coronary angioplasty',
'Percutaneous coronary intervention',
'PCI', # FIXME intuitive
'Atherosclerotic disease',
'chd',
'coronary artery bypass',
'coronary artery disease' #added
# 'LAD',
],
['CHF', # kongesztív szívbetegség
'(?:congestive )?(?:heart|cardiac) failure',
'CCF',
'dchf',
'(?:dilated|(?:non-?)?ischa?emic) cardiomyopathy',
# 'cardiomyopathy',
# 'CMP',
# 'Pulmonary edema',
],
['Depression', # depresszió
'depressive?',
],
['Diabetes', # cukorbetegség
'DM',
'N?IDDM', # = diabetes mellitus
'diabetic',
],
['Gallstones', # epekő (nem betegség
'gall ?stones?',
'Cholelithiasis',
# 'pancreatitis', # NOTE epekő következménye
'biliary colic', # NOTE tünet
'Cholecystectomy', # NOTE műtét
'Gallbladder removal', # NOTE műtét
'CCY',
'gallbladder (?:[a-z-\/]+ )*stones',
# 'pancreatic duct',
#,'Cholestasis', # NOTE epekő következménye
],
['GERD', # reflux
'reflux (?:disease|disorder|esophagitis)',
'(?:gastro(?:-o)?)?esophageal reflux',
'G-?E Reflux',
'gord',
's esophagitis', # barretts's esophagitis
# 'reflux',
],
['Gout',
'gouty',
'metabolic arthritis',
], # köszvény
['Hypercholesterolemia', # hypercholesterinaemia
'hyperchol\w*',
'(?:high|elev(?:ated)?|increased) (?:chol(?:esterol)?|lipids?)',
'(?:chol(?:esterol)?|lipids?)(?:\w|-| )+(?:high|elev(?:ated)?|increased)',
# 'hyperlipi?demia', # NOTE utal rá
# 'hyperlipoproteinemia' , # NOTE utal rá
# 'dyslipidemia', # NOTE általánosabb
'hyperli?pi?d\w*', # NOTE utal rá
# 'hyperlipoproteinemia' , # NOTE utal rá
# 'dyslipidemia', # NOTE általánosabb
'dysli?pi?d\w+', # NOTE általánosabb
],
['Hypertension', # magasvérnyomás
'htn',
# 'elevated bp',
],
['Hypertriglyceridemia', # u.a.
'Hyper(?:tri)?glycerid[a-z]*',
'hyper tg',
'(?:high|elev(?:ated)?|increased) trig(?:lyceride)?s',
't(?:g|rig(?:lyceride)?s?) (?:[a-z0-9\-\/]+ )*(?:high|elev(?:ated)?|increased)',
],
['OA', # rheumatoid arthritis
'(?:Osteo)?arthritis',
'degenerative (arthritis|joint disease)',
'DJD',
],
['Obesity', # elhízás
'obes\w+',
# 'weight gain', # tul sok FP
],
['OSA',
'(obstructive )?sleep apnea', ], # alvási apnoe [horkolás]
['PVD', # Végtagi érelmeszesedés [verőérszűkület]
'Peripheral (?:vascular|arterial)(?: occlusive)? Disease',
'peripherovascular disease',
# 'peripheral arterial disease',
# 'PAD',
],
['Venous Insufficiency', #visszér betegség
'venostasis', # NOTE alfaj
'(veno)?stasis dermatitis', # NOTE alfaj
'venous (?:stasis|return)',
],
];
# lowercase!
# no active parens '(' in regex! only '(?:' allowed
fake_aliases = {
'Depression' : [
'resp(?:iratory)? depr[a-z]*',
's(?:t|cd) (?:segment )?depr[a-z]*',
],
'Diabetes' : [
'(?:gestional|chemical) diabetes',
'diabetic diet',
],
'OA' : [
'(?:rheumatoid|septic|psoriatic|idiopathic) arthritis',
'oa7'
],
'Asthma' : [
# 'asthma flare',
'cardiac asthma',
],
'Hypertension' : [
'pulmonary hypertension',
],
};
intuitive_alias_lists =[
[ 'Depression' ,
'(?:prozac|zoloft|wellbutrin|plaxil|lexapro|effexor|cymbalta|elavil|celexa|desyrel|Pamelor|Tofranil)',
'(?:ssri|snri)s?',
'antidepressants?',
],
[ 'Hypertension',
'(?:ACE inhibitors|antihypertensive)s?',
'(?:Angiotensin|b(?:eta)?(?: |-)blocker)',
'(blood pressure|bp)( of | )(1[4-9][0-9]|[2-9]\d{2,})/(9[0-9]|[1-9]\d{2,})'
],
[ 'GERD',
'ESOPHAGITIS',
'(?:carafate|Sucralfate)',
],
[ 'CAD',
'Cardiac catheterization',
],
[ 'Hypercholesterolemia',
'[a-z]+statins?',
],
# [ 'Hypertriglyceridemia',
# 'fibrates?',
# '(?:Bezafibrate|Bezalip|Ciprofibrate|Modalim|Clofibrate|Gemfibrozil|Lopid|Fenofibrate|Tricor)',
# ],
[ 'Obesity',
'Pannicul(?:ectomy|itis)',
],
[ 'OA',
'LUMBAR DIS[kc] DISEASE',
'hip replacement',
'knee replacement',
],
[ 'Gout',
'Allopurinol',
],
[ 'Venous Insufficiency',
'(?:lower extremity|leg) ulcers',
],
# [ 'Hypertriglyceridemia',
# 'hyperglycerida?emia', # NOTE általánosabb
# ],
];
fake_negatives = (
'gram negative',
'no further',
'not able to be',
'no increase',
'not suspicious',
'not certain',
'not clearly',
'not certain whether',
'not certain if',
'not necessarily',
'not optimal',
'not rule out',
'without any further',
'without difficulty',
'without further',
'not correlate',
'no correlataion',
#'no family history', # unstripped family history segments
'no prior',
);
alias_lists = alias_lists+intuitive_alias_lists
aliases={}
for dis in alias_lists:
tag = dis[0]
for alias in dis:
alias = alias.lower()
alias = re.sub(' ','[ _]',alias)
aliases[alias] = tag
def findQpos(doc):
line=re.finditer(r'((?:(?:\b(?:not certain if|versus|vs|rule out|be ruled out|r\/o|presumed|suggest(?:\b|[^i]|i[^v])|possib|scree?n for|question|consider|whether or not|may have|study for))(?:[^\.\,;:)(\?]+))|\?(?: ?[a-z\/\-]+){1,4}|(?:[a-z\-\/]+ )+(?:versus|vs|screen)[a-z -]+)', doc.lower())
fea_sen = [(x.span(),x.group(0)) for x in line]
fea = pd.DataFrame(columns=['disease','dis_pos','dis_alias','sen_pos','sentence','tail'])
# print(fea_sen)
for sen in fea_sen:
s = sen[1]
pos = sen[0]
tail = ''
if not re.search(r'(?:versus|vs|screen)',sen[1]):
rs=re.search(r'^(.+?)((?:\b(?:no |and|when|with|given|if|besides|because|related|component|from|since|secondary|regard|due|disposing)\b|\b-\B|\B-\b|\-\-|\*\*).*)',s)
if rs:
s = rs.group(1)
pos = [x+pos[0] for x in rs.span(1)]
tail = rs.group(2)
# print(s)
for alias, tag in aliases.items():
fake = '(?:' + '|'.join(fake_aliases[tag]) + ')|' if tag in fake_aliases else ''
match_dis = re.finditer(r'(?:'+fake+r'(?:\b|[0-9])('+alias+r')(?:\b|[0-9]))',s)
fea_words = [(dis.span(),dis.group(0)) for dis in match_dis if dis.group(1)]
for f in fea_words:
fea.loc[len(fea)] = [tag,f[0],f[1],sen[0],sen[1],tail]
doc = doc[:pos[0]]+' '*(pos[1]-pos[0])+doc[pos[1]:]
# print(pos,s)
return fea, doc
def findNpos(doc):
line=re.finditer(r'(\b(?:no|not|denie[sd]|denying|rule[sd] out|neg(?:ative)?|w(?:ithout|-\B|\/o\b)|did not exhibit|declined|(?:\w|\s|-)+ neg(?:ative)?) (?:[^\.\,\;\:\)\(\?\*]+)|non-[a-z-]+)', doc.lower())
fea_sen = [(x.span(),x.group(0)) for x in line]
fea = pd.DataFrame(columns=['disease','dis_pos','dis_alias','sen_pos','sentence','tail'])
for sen in fea_sen:
s = sen[1]
pos = sen[0]
tail = ''
blk=' '*(pos[1]-pos[0])
rs=re.search(r'((?:\b(?:when|given|taken|takes?|from|further|but|taking|during|as|besides|more|who|after|complications? of|because|since|due to|unless|regarding|secondary)\b|\b-\B|\B-\b|\-\-|\*\*).*)',s)
if rs:
tail=rs.group(1)
tpos=rs.span(1)
s=re.sub(r'((?:\b(?:when|given|taken|takes?|from|further|but|taking|during|as|besides|more|who|after|complications? of|because|since|due to|unless|regarding|secondary)\b|\b-\B|\B-\b|\-\-|\*\*).*)','',s)
blk = blk[:tpos[0]]+tail+blk[tpos[1]:]
# print(s,tail)
if any([re.search(fake,s) for fake in fake_negatives]):
continue
for alias, tag in aliases.items():
fake = '(?:' + '|'.join(fake_aliases[tag]) + ')|' if tag in fake_aliases else ''
match_dis = re.finditer(r'('+fake+r'(?:\b|[0-9])('+alias+r')(?:\b|[0-9]))',s)
fea_words = [(dis.span(),dis.group(0)) for dis in match_dis if dis.group(2)]
for f in fea_words:
fea.loc[len(fea)] = [tag,f[0],f[1],sen[0],sen[1],tail]
doc = doc[:pos[0]]+blk+doc[pos[1]:]
return fea,doc
def findPpos(doc):
fea = pd.DataFrame(columns=['disease','dis_pos','dis_alias'])
for alias, tag in aliases.items():
fake = '(?:' + '|'.join(fake_aliases[tag]) + ')|' if tag in fake_aliases else ''
match_dis = re.finditer(r'('+fake+r'(?:\b|[0-9])('+alias+r')(?:\b|[0-9]))',doc.lower())
# print('('+fake+r'(?:\b|[0-9])('+alias+r')(?:\b|[0-9]))')
# print([(x.group(0),x.group(1),x.group(2)) for x in match_dis])
fea_words = [(dis.span(),dis.group(0)) for dis in match_dis if dis.group(2)]
for f in fea_words:
fea.loc[len(fea)] = [tag,f[0],f[1]]
return fea
corp_name='Obesity_data/ObesitySen_remove_familiy_history.dms'
f = open(corp_name,'r')
content = f.read()
f.close()
records = content.strip().split('RECORD #')
for i in range(1,1250):
print('processing the %dth document'%(i))
Qfea,doc=findQpos(records[i])
Qfea.to_csv('featurepos/Question/Qpos_%d.csv'%(i))
Nfea,doc=findNpos(doc)
Nfea.to_csv('featurepos/Negated/Npos_%d.csv'%(i))
Pfea=findPpos(doc)
Pfea.to_csv('featurepos/Positive/Ppos_%d.csv'%(i))