-
Notifications
You must be signed in to change notification settings - Fork 0
/
mention_pattern.py
349 lines (305 loc) · 12.3 KB
/
mention_pattern.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
import spacy
import utils
import pandas as pd
from os import listdir
from os.path import isfile, join, split
class AbstractedSentence(object):
def __init__(self, seq):
self._seq = 0
self._abstracted_tokens = []
self._text = None
self._parsed = None
@property
def seq(self):
return self._seq
@seq.setter
def seq(self, value):
self._seq = value
def add_token(self, t):
self._abstracted_tokens.append(t)
@property
def tokens(self):
return self._abstracted_tokens
@property
def text(self):
return self._text
@text.setter
def text(self, value):
self._text = value
def get_parsed_tree(self, nlp):
"""
use spacy instance to parse the sentence
:param nlp: a spacy instance
:return: dependency tree
"""
if self._parsed is not None:
return self._parsed
if self.text is None:
return None
self._parsed = nlp(self.text)
return self._parsed
def locate_pos(self, str):
return self._text.find(str)
def get_abstaction_by_pos(self, pos, nlp):
doc = self.get_parsed_tree(nlp)
token = None
if doc is not None:
for t in doc:
if t.idx + len(t.text) == pos:
token = t
if token is not None:
ta = TokenAbstraction(token, doc)
else:
return None
return ta
def get_related_tokens(self, t):
ret = []
for tk in self._parsed:
if tk.head == t:
ret.append(tk)
print(tk.text, tk.dep_, tk.head)
return ret
class TokenAbstraction(object):
def __init__(self, token, doc):
self._t = token
self._d = doc
self._children = []
self._root = None
self._subject = None
self._verbs = None
self._vcontext = []
self.do_abstract()
@property
def vcontext(self):
return self._vcontext
@property
def children(self):
return self._children
@property
def root(self):
return self._root
@property
def subject(self):
return self._subject
@property
def verbs(self):
return self._verbs
@property
def token(self):
return self._t
def do_abstract(self):
self._children = [t for t in self._t.children]
t = self._t
r = t
while (t.head != t) and t.dep_ not in ['ROOT', 'relcl', 'acl', 'advcl']:
t = t.head
if t.dep_ in ['ccomp']:
self._subject = [s for s in t.children if s.dep_ in [u"nsubj", 'nsubjpass', 'ROOT', 'pobj']]
if t.pos_ in ['VERB']:
self._vcontext += [s for s in t.children if s.dep_ in ["neg", 'advmod']]
r = t
if t is not None:
self._verbs = [v for v in t.children if v.pos_ == u"VERB"]
if t.dep_ in ['relcl', 'acl']:
self._subject = [t.head]
else:
if len(self._vcontext) == 0:
self._vcontext += [s for s in t.children if s.dep_ in ["neg", 'advmod']]
if self._subject is None:
self._subject = [s for s in t.children if s.dep_ in [u"nsubj", 'nsubjpass', 'ROOT']]
self._root = r
def do_abstract_waterfall(self, entity_start, entity_end):
t = self._t
seq = []
while (t.head != t) and t.dep_ not in ['ROOT', 'relcl', 'acl', 'advcl']:
t = t.head
if t.idx > entity_end or (t.idx + len(t.text) < entity_start):
seq.append((t.text, t.dep_, t.pos_))
seq.reverse()
return seq
def do_abstract_descendent(self):
return [c for c in self._t.children]
def to_dict(self):
return {'children': [t.text for t in self.children], 'root': self.root.text,
'subject': [s.text for s in self.subject], 'verbs': [v.text for v in self.verbs]}
class MentionPattern(object):
def __init__(self, pattern_folder, cui2icd, csv_file=None, ann_folder=None, in_action=False):
self._ptn_folder = pattern_folder
self._ref_good_ptns = None
self._ref_bad_ptns = None
self._csv_file = csv_file
self._cui2icd = cui2icd
self._df = None
self._nlp = get_nlp_lg()
self._ann_folder = ann_folder
self._good_ptns = None
self._bad_ptns = None
self._in_action = in_action
self.load()
def load(self):
if self._csv_file is not None:
self._df = pd.read_csv(self._csv_file)
if self._in_action:
g, b = MentionPattern.load_ref_patterns(self._ptn_folder, 'zzzz')
self._good_ptns = g
self._bad_ptns = b
@staticmethod
def load_ref_patterns(ptn_folder, ignore_chapter):
good_p = MentionPattern.load_patterns(ptn_folder, to_load=lambda f: f.find('good') > 0 and f.find(
'%s_' % ignore_chapter) != 0)
bad_p = MentionPattern.load_patterns(ptn_folder, to_load=lambda f: f.find('bad') > 0 and f.find(
'%s_' % ignore_chapter) != 0)
return good_p, bad_p
@staticmethod
def get_sent_by_pos(sents, s, e):
for sent in sents:
if sent['start'] <= s and sent['end'] >= e:
return sent
return None
def read_semehr_anns(self, doc_anns, container):
"""
doc_anns - [{'d': fk, 'ann': a, 'label': self.label}]
"""
self.read_semehr_anns_by_functions(doc_anns,
get_sent_func=lambda dd: utils.load_json_data(dd)['sentences'],
get_text_func=lambda dd: self._df[self._df['doc_id'] == dd]['text'].iloc[0],
container=container)
def read_semehr_anns_by_functions(self, doc_anns, get_sent_func, get_text_func, container):
cur_d = None
cur_sents = None
for da in doc_anns:
d = 'se_ann_%s.json' % da['d']
if d != cur_d:
cur_sents = get_sent_func(join(self._ann_folder, d))
cur_d = d
a = da['ann']
ch = self._cui2icd[a.cui]
sent = MentionPattern.get_sent_by_pos(cur_sents, a.start, a.end)
win = get_text_func(da['d'])[sent['start']:sent['end']]
container.append(
{'ch': ch, 'd': da['d'], 's': a.start, 'e': a.end, 's_s': sent['start'], 's_e': sent['end'],
'win': win})
def abstract_ann_pattern(self, ann):
abss = AbstractedSentence(2)
abss.text = ann['win']
result = abss.get_abstaction_by_pos(ann['e'] - ann['s_s'], self._nlp)
if result is not None:
# abss.get_related_tokens(result.token)
ptn = result.do_abstract_waterfall(ann['s'] - ann['s_s'], ann['e'] - ann['s_s'])
return {'pattern': ptn, "subject": result.subject, "vcontect": result.vcontext}
else:
return None
def classify_anns(self, anns):
preds = []
for ann in anns:
ret = self.abstract_ann_pattern(ann)
if ret is not None:
good_ref = self._good_ptns
bad_ref = self._bad_ptns
if not self._in_action:
good_ref, bad_ref = MentionPattern.load_ref_patterns(self._ptn_folder, ann['ch'])
good_match = MentionPattern.compute_similar_from_ref(ret, good_ref, self._nlp)
bad_match = MentionPattern.compute_similar_from_ref(ret, bad_ref, self._nlp)
# ctx = '|'.join([e[0] for e in ret['pattern']])
cls = MentionPattern.classify_by_pattern_matches(good_match, bad_match, self._nlp)
preds.append(cls)
else:
preds.append(-1)
return preds
def predict(self, doc_anns, cr=None):
anns = []
if cr is None:
self.read_semehr_anns(doc_anns, anns)
else:
# single document anns to be read by CustomisedRecoginiser
self.read_semehr_anns_by_functions(doc_anns, get_sent_func=lambda dd: cr.sentences,
get_text_func=lambda dd:cr.full_text, container=anns)
return self.classify_anns(anns)
@staticmethod
def load_patterns(ptn_folder, to_load=lambda f: True):
return [utils.load_json_data(join(ptn_folder, f)) for f in listdir(ptn_folder) if
to_load(f) and isfile(join(ptn_folder, f))]
@staticmethod
def sim_seqs(s1, s2, nlp, last_k=2):
scores = 0.0
k = min(last_k, len(s1), len(s2))
for i in range(1, k + 1):
t1, t2 = nlp(' '.join([s1[-1 * i], s2[-1 * i]]))
if t1.vector_norm > 0 and t2.vector_norm > 0:
scores += t1.similarity(t2)
return scores / k
@staticmethod
def get_pattern_group(p):
mp = p if len(p) <= 2 else p[-2:]
return '-'.join([e[2] for e in mp])
@staticmethod
def compute_similar_from_ref(ret, ref_good_ptns, nlp, threshold=0.7):
p = ret['pattern']
ctxt = '|'.join([e[0] for e in p])
# print('>>>working on %s' % ctxt)
if len(ctxt) == 0:
return None
grp = MentionPattern.get_pattern_group(p)
entried_scores = []
for ref_ptn in ref_good_ptns:
if grp in ref_ptn:
for inst in ref_ptn[grp]:
score = MentionPattern.sim_seqs([e[0] for e in p], ref_ptn[grp][inst]['list'], nlp)
if score > threshold:
entried_scores.append((score, ref_ptn[grp][inst]['freq']))
# print('\tvs %s: score %s, %s' % (inst, score, ref_good_ptns[grp][inst]['freq']))
if len(entried_scores) > 0:
total = sum([s[0] * s[1] for s in entried_scores])
supports = sum([s[1] for s in entried_scores])
avg_score = total / supports
# print('\tscore %s, support %s, %s|%s' % (avg_score, supports, ret['subject'], ret['vcontect']))
return {'score': avg_score, 'supports': supports, 'subject': [t.text for t in ret['subject']],
'context': [t.text for t in ret['vcontect']]}
else:
return None
@staticmethod
def classify_by_pattern_matches(good_match, bad_match, nlp,
bad_subjs=None,
bad_context=None):
if bad_context is None:
bad_context = ['not', 'mistakenly', 'likely', 'ie']
if bad_subjs is None:
bad_subjs = ['son', 'daughter', 'manager', 'wife', 'I', 'one', 'anyone', "questions",
"someone", "child", "neighbour", "invesitigation", "screening",
"assessment"]
if good_match is None and bad_match is None:
return -1
if good_match is None:
return 0
# elif bad_match is None:
# return 1
else:
sub = good_match['subject']
ctx = good_match['context']
if MentionPattern.lists_sim_enough(sub, bad_subjs, nlp) == 1:
return 0
if MentionPattern.lists_sim_enough(ctx, bad_context, nlp) == 1:
return 0
# return -1
if bad_match is None:
return 1
else:
return 1 if good_match['score'] * good_match['supports'] >= bad_match['score'] * bad_match[
'supports'] else 0
@staticmethod
def lists_sim_enough(l1, l2, nlp, threshold=0.8):
if len(l1) == 0 or len(l2) == 0:
return -1
d1 = nlp(' '.join(l1))
d2 = nlp(' '.join(l2))
for t1 in d1:
for t2 in d2:
if t1.similarity(t2) > threshold:
return 1
return 0
_nlp_lg = None
def get_nlp_lg():
global _nlp_lg
if _nlp_lg is None:
_nlp_lg = spacy.load('en_core_web_lg')
return _nlp_lg