This repository was archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathdoc_reader.py
297 lines (248 loc) · 8.82 KB
/
doc_reader.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
import spacy, utils
from semquery import SemEHRES
import re
from os import listdir
from os.path import isdir, isfile, join
class StreamedDocs(object):
"""
effectively an abstract class for doing streaming docs
"""
_spacy_instance = None
def get_doc_by_id(self, doc_id):
pass
def __iter__(self):
nlp = FileIterDocs.get_nlp()
for d in self.docs:
doc = nlp(self.get_doc_by_id(d))
for line in doc.sents:
tokens = [StreamedDocs.simple_tokenize(w) for w in line.text.split()]
yield [w for w in tokens if w not in StreamedDocs.get_customised_stopwords()]
@staticmethod
def get_spacy_en_stopwords():
return FileIterDocs.get_nlp().Defaults.stop_words
@staticmethod
def get_customised_stopwords():
return ['', 'the', 'a', 'an', 'these', 'that']
@staticmethod
def simple_tokenize(w):
return re.sub(r'([^a-zA-Z0-9]*)$|(^[^a-zA-Z0-9]*)', '', w)
@staticmethod
def get_nlp():
if StreamedDocs._spacy_instance is None:
print 'loading nlp instance...'
StreamedDocs._spacy_instance = spacy.load('en_core_web_sm')
return StreamedDocs._spacy_instance
class FileIterDocs(StreamedDocs):
"""
file based document iterator
SemEHR annotation to be marked before embedding learning
"""
def __init__(self, doc_ids, doc_path_template, doc_to_anns):
self.docs = doc_ids
self._path_template = doc_path_template
self._anns = None
self._need_preprocess = False
self._doc_to_anns = doc_to_anns
def add_annotations(self, anns):
self._anns = anns
@property
def need_preprocess(self):
return self._need_preprocess
@need_preprocess.setter
def need_preprocess(self, value):
self._need_preprocess = value
@staticmethod
def preprocess(text):
return re.sub(r'\s{2,}', '\n', text)
@staticmethod
def match_ann_in_text(t, a, defaultIndex):
mit = re.finditer(r"([\s\.;\,$\?!:/\('\"]|^)" + a + "([\s\.;\,\?!:/\)'\"]|$)", t, re.IGNORECASE)
poz = []
for m in mit:
if m is not None:
p = m.end(1)
poz.append([abs(p - defaultIndex), p])
poz = sorted(poz, key=lambda x: x[0])
if len(poz) > 0:
return poz[0][1]
else:
return -1
def markup_annotations(self, text):
# sort anns
anns = self._anns
anns = sorted(anns, key=lambda x: x.offset_start)
inserts = []
for ann in anns:
start = self.match_ann_in_text(text, ann.string_orig, int(ann.offset_start))
end = start + len(ann.string_orig)
if start < 0:
start = int(ann.offset_start)
end = ann.offset_end
inserts.append({'p': start, 'insert': ' ' + ann.annotator_label + '-' + ann.concept + ' '})
inserts = sorted(inserts, key=lambda x: x['p'])
prev_pos = 0
ret = ''
print inserts
for insert in inserts:
ret += text[prev_pos:insert['p']] + insert['insert']
prev_pos = insert['p']
ret += text[prev_pos:]
return ret
def ann_to_labelled_data(self, text, window=3):
# sort anns
anns = self._anns
anns = sorted(anns, key=lambda x: x.offset_start)
data_x = []
data_y = []
for ann in anns:
start = self.match_ann_in_text(text, ann.string_orig, ann.offset_start)
end = start + len(ann.string_orig)
if start < 0:
start = ann.offset_start
end = ann.offset_end
x = []
x.append(text[:start].split()[-window])
x.append(text[end:].split())[:window]
data_x.append(x)
data_y.append(ann.annotator_label + '-' + ann.concept)
return data_x, data_y
def get_doc_by_id(self, doc_id):
doc_path = self._path_template.format(**{'doc_id': doc_id})
print 'working on %s' % doc_path
text = utils.read_text_file_as_string(doc_path)
if doc_id in self._doc_to_anns:
self.add_annotations(self._doc_to_anns[doc_id])
if self.need_preprocess:
text = FileIterDocs.preprocess(text)
return self.markup_annotations(text)
class SemEHRAnn(object):
"""
semantic annotation object
"""
def __init__(self):
self._offset_start = -1
self._offset_end = -1
self._string_orig = None
self._concept = None
self._annotator_label = None
@property
def offset_start(self):
return self._offset_start
@offset_start.setter
def offset_start(self, value):
self._offset_start = value
@property
def offset_end(self):
return self._offset_end
@offset_end.setter
def offset_end(self, value):
self._offset_end = value
@property
def string_orig(self):
return self._string_orig
@string_orig.setter
def string_orig(self, value):
self._string_orig = value
@property
def annotator_label(self):
return self._annotator_label
@annotator_label.setter
def annotator_label(self, value):
self._annotator_label = value
@property
def concept(self):
return self._concept
@concept.setter
def concept(self, value):
self._concept = value
class QueryResultDocs(StreamedDocs):
"""
ES Query based streaming docs
- wraps ESDocReader to query and return fulltext of docs
"""
def __init__(self, es_config, query):
self.reader = ESDocReader(es_config)
self.docs = self.reader.query_doc_ids(query)
print '#docs: %s' % (len(self.docs))
def get_doc_by_id(self, doc_id):
return self.reader.get_doc_fulltext(doc_id)
class ESDocReader(object):
"""
elasticsearch document reader
- two steps to read ES docs:
1) query to get ids;
2) query doc fulltext for given doc id;
"""
def __init__(self, es_config):
es = utils.load_json_data(es_config)
self.semquery = SemEHRES(es['_es_host'], es['_es_index'],
es['_doc_type'], es['_concept_type'],
es['_patient_type'])
self.es = es
def query_doc_ids(self, query):
return self.semquery.search_by_scroll(query, self.es['_doc_type'], include_fields=['_id'])
def get_doc_fulltext(self, doc_id):
print 'reading %s' % doc_id
d = self.semquery.get_doc_detail(doc_id, doc_type=self.es['_doc_type'])
if d is not None:
return d['fulltext']
def load_tsv_anns(tsv_file):
doc_to_anns = {}
for l in utils.read_text_file(tsv_file):
arrs = l.split('\t')
doc = arrs[0]
anns = [] if doc not in doc_to_anns else doc_to_anns[doc]
ann = SemEHRAnn()
ann.offset_start = arrs[1]
ann.offset_end = arrs[2]
ann.string_orig = arrs[3]
ann.concept = arrs[4]
ann.annotator_label = arrs[5]
anns.append(ann)
if doc not in doc_to_anns:
doc_to_anns[doc] = anns
return doc_to_anns
def process_batched_docs(folder_path, out_folder):
if isdir(folder_path):
for f in listdir(folder_path):
if isfile(join(folder_path, f)):
t = utils.read_text_file_as_string(join(folder_path, f))
print 'processing %s' % join(folder_path, f)
print t
mit = re.finditer(r'^(\d+)\,\"', t, re.MULTILINE)
prev_pos = 0
prev_id = None
for m in mit:
if prev_pos > 0:
utils.save_string(t[prev_pos:m.start()-2], join(out_folder, prev_id))
prev_pos = m.end()
prev_id = m.string[m.start(1):m.end(1)]
if prev_id is not None:
utils.save_string(t[prev_pos:len(t) - 1], join(out_folder, prev_id))
else:
print 'ERROR!! pattern not found in %s' % join(folder_path, f)
def test():
fds = FileIterDocs(['34021'], '/Users/honghan.wu/Documents/UoE/working_folder/sample_docs/doc_{doc_id}.txt')
fds.need_preprocess = True
anns = []
ann = SemEHRAnn()
ann.offset_start = 47
ann.offset_end = 56
ann.string_orig = 'discharge'
ann.concept = 'C123124'
ann.annotator_label = 'posM'
anns.append(ann)
fds.add_annotations(anns)
for s in fds:
print s
if __name__ == "__main__":
docs_path = 'C:/Users/HWu/Desktop/validated_docs/docs'
d2anns = load_tsv_anns('C:/Users/HWu/Desktop/validated_docs/anns/anns_dumped.csv')
docs = [f for f in listdir(docs_path)]
fdocs = FileIterDocs(docs, docs_path + '/{doc_id}', d2anns)
c = 0
for d in fdocs:
print d
c += 1
if c > 200:
break