forked from Georgetown-IR-Lab/QuickUMLS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquickumls.py
374 lines (299 loc) · 12.1 KB
/
quickumls.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
# future statements for Python 2 compatibility
from __future__ import (
unicode_literals, division, print_function, absolute_import)
# built in modules
import os
import sys
import datetime
from six.moves import xrange
# installed modules
import spacy
import nltk
from unidecode import unidecode
# project modules
try:
import toolbox
import constants
except ImportError:
from . import toolbox
from . import constants
class QuickUMLS(object):
def __init__(
self, quickumls_fp,
overlapping_criteria='score', threshold=0.7, window=5,
similarity_name='jaccard', min_match_length=3,
accepted_semtypes=constants.ACCEPTED_SEMTYPES,
verbose=False):
self.verbose = verbose
valid_criteria = {'length', 'score'}
err_msg = (
'"{}" is not a valid overlapping_criteria. Choose '
'between {}'.format(
overlapping_criteria, ', '.join(valid_criteria)
)
)
assert overlapping_criteria in valid_criteria, err_msg
self.overlapping_criteria = overlapping_criteria
valid_similarities = {'dice', 'jaccard', 'cosine', 'overlap'}
err_msg = ('"{}" is not a valid similarity name. Choose between '
'{}'.format(similarity_name, ', '.join(valid_similarities)))
assert not(valid_similarities in valid_similarities), err_msg
self.similarity_name = similarity_name
simstring_fp = os.path.join(quickumls_fp, 'umls-simstring.db')
cuisem_fp = os.path.join(quickumls_fp, 'cui-semtypes.db')
self.valid_punct = constants.UNICODE_DASHES
self.negations = constants.NEGATIONS
self.window = window
self.ngram_length = 3
self.threshold = threshold
self.min_match_length = min_match_length
self.to_lowercase_flag = os.path.exists(
os.path.join(quickumls_fp, 'lowercase.flag')
)
self.normalize_unicode_flag = os.path.exists(
os.path.join(quickumls_fp, 'normalize-unicode.flag')
)
language_fp = os.path.join(quickumls_fp, 'language.flag')
# download stopwords if necessary
try:
nltk.corpus.stopwords.words()
except LookupError:
nltk.download('stopwords')
if os.path.exists(language_fp):
with open(language_fp) as f:
self.language_flag = f.read().strip()
else:
self.language_flag = 'ENG'
if self.language_flag not in constants.LANGUAGES:
raise ValueError('Language "{}" not supported'.format(self.language_flag))
elif constants.LANGUAGES[self.language_flag] is None:
self._stopwords = set()
spacy_lang = 'XXX'
else:
self._stopwords = set(
nltk.corpus.stopwords.words(constants.LANGUAGES[self.language_flag])
)
spacy_lang = constants.SPACY_LANGUAGE_MAP[self.language_flag]
# domain specific stopwords
self._stopwords = self._stopwords.union(constants.DOMAIN_SPECIFIC_STOPWORDS)
self._info = None
self.accepted_semtypes = accepted_semtypes
self.ss_db = toolbox.SimstringDBReader(
simstring_fp, similarity_name, threshold
)
self.cuisem_db = toolbox.CuiSemTypesDB(cuisem_fp)
try:
self.nlp = spacy.load(spacy_lang)
except OSError:
msg = (
'Model for language "{}" is not downloaded. Please '
'run "python -m spacy download {}" before launching '
'QuickUMLS'
).format(
self.language_flag,
constants.SPACY_LANGUAGE_MAP.get(self.language_flag, 'xx')
)
raise OSError(msg)
def get_info(self):
return self.info
def get_accepted_semtypes(self):
return self.accepted_semtypes
@property
def info(self):
# useful for caching of respnses
if self._info is None:
self._info = {
'threshold': self.threshold,
'similarity_name': self.similarity_name,
'window': self.window,
'ngram_length': self.ngram_length,
'min_match_length': self.min_match_length,
'accepted_semtypes': sorted(self.accepted_semtypes),
'negations': sorted(self.negations),
'valid_punct': sorted(self.valid_punct)
}
return self._info
def _is_valid_token(self, tok):
return not(
tok.is_punct or tok.is_space or
tok.pos_ == 'ADP' or tok.pos_ == 'DET' or tok.pos_ == 'CONJ'
)
def _is_valid_start_token(self, tok):
return not(
tok.like_num or
(self._is_stop_term(tok) and tok.lemma_ not in self.negations) or
tok.pos_ == 'ADP' or tok.pos_ == 'DET' or tok.pos_ == 'CONJ'
)
def _is_stop_term(self, tok):
return tok.text in self._stopwords
def _is_valid_end_token(self, tok):
return not(
tok.is_punct or tok.is_space or self._is_stop_term(tok) or
tok.pos_ == 'ADP' or tok.pos_ == 'DET' or tok.pos_ == 'CONJ'
)
def _is_valid_middle_token(self, tok):
return (
not(tok.is_punct or tok.is_space) or
tok.is_bracket or
tok.text in self.valid_punct
)
def _is_ok_semtype(self, target_semtypes):
if self.accepted_semtypes is None:
ok = True
else:
ok = any(sem in self.accepted_semtypes for sem in target_semtypes)
return ok
def _is_longer_than_min(self, span):
return (span.end_char - span.start_char) >= self.min_match_length
def _make_ngrams(self, sent):
sent_length = len(sent)
# do not include teterminers inside a span
skip_in_span = {token.i for token in sent if token.pos_ == 'DET'}
# invalidate a span if it includes any on these symbols
invalid_mid_tokens = {
token.i for token in sent if not self._is_valid_middle_token(token)
}
for i in xrange(sent_length):
tok = sent[i]
if not self._is_valid_token(tok):
continue
# do not consider this token by itself if it is
# a number or a stopword.
if self._is_valid_start_token(tok):
compensate = False
else:
compensate = True
span_end = min(sent_length, i + self.window) + 1
# we take a shortcut if the token is the last one
# in the sentence
if (
i + 1 == sent_length and # it's the last token
self._is_valid_end_token(tok) and # it's a valid end token
len(tok) >= self.min_match_length # it's of miminum length
):
yield(tok.idx, tok.idx + len(tok), tok.text)
for j in xrange(i + 1, span_end):
if compensate:
compensate = False
continue
if sent[j - 1] in invalid_mid_tokens:
break
if not self._is_valid_end_token(sent[j - 1]):
continue
span = sent[i:j]
if not self._is_longer_than_min(span):
continue
yield (
span.start_char, span.end_char,
''.join(token.text_with_ws for token in span
if token.i not in skip_in_span).strip()
)
def _get_all_matches(self, ngrams):
matches = []
for start, end, ngram in ngrams:
ngram_normalized = ngram
if self.normalize_unicode_flag:
ngram_normalized = unidecode(ngram_normalized)
# make it lowercase
if self.to_lowercase_flag:
ngram_normalized = ngram_normalized.lower()
# if the term is all uppercase, it might be the case that
# no match is found; so we convert to lowercase;
# however, this is never needed if the string is lowercased
# in the step above
if not self.to_lowercase_flag and ngram_normalized.isupper():
ngram_normalized = ngram_normalized.lower()
prev_cui = None
ngram_cands = list(self.ss_db.get(ngram_normalized))
ngram_matches = []
for match in ngram_cands:
cuisem_match = sorted(self.cuisem_db.get(match))
for cui, semtypes, preferred in cuisem_match:
match_similarity = toolbox.get_similarity(
x=ngram_normalized,
y=match,
n=self.ngram_length,
similarity_name=self.similarity_name
)
if match_similarity == 0:
continue
if not self._is_ok_semtype(semtypes):
continue
if prev_cui is not None and prev_cui == cui:
if match_similarity > ngram_matches[-1]['similarity']:
ngram_matches.pop(-1)
else:
continue
prev_cui = cui
ngram_matches.append(
{
'start': start,
'end': end,
'ngram': ngram,
'term': toolbox.safe_unicode(match),
'cui': cui,
'similarity': match_similarity,
'semtypes': semtypes,
'preferred': preferred
}
)
if len(ngram_matches) > 0:
matches.append(
sorted(
ngram_matches,
key=lambda m: m['similarity'] + m['preferred'],
reverse=True
)
)
return matches
@staticmethod
def _select_score(match):
return (match[0]['similarity'], (match[0]['end'] - match[0]['start']))
@staticmethod
def _select_longest(match):
return ((match[0]['end'] - match[0]['start']), match[0]['similarity'])
def _select_terms(self, matches):
sort_func = (
self._select_longest if self.overlapping_criteria == 'length'
else self._select_score
)
matches = sorted(matches, key=sort_func, reverse=True)
intervals = toolbox.Intervals()
final_matches_subset = []
for match in matches:
match_interval = (match[0]['start'], match[0]['end'])
if match_interval not in intervals:
final_matches_subset.append(match)
intervals.append(match_interval)
return final_matches_subset
def _make_token_sequences(self, parsed):
for i in range(len(parsed)):
for j in xrange(
i + 1, min(i + self.window, len(parsed)) + 1):
span = parsed[i:j]
if not self._is_longer_than_min(span):
continue
yield (span.start_char, span.end_char, span.text)
def _print_verbose_status(self, parsed, matches):
if not self.verbose:
return False
print(
'[{}] {:,} extracted from {:,} tokens'.format(
datetime.datetime.now().isoformat(),
sum(len(match_group) for match_group in matches),
len(parsed)
),
file=sys.stderr
)
return True
def match(self, text, best_match=True, ignore_syntax=False):
parsed = self.nlp(u'{}'.format(text))
if ignore_syntax:
ngrams = self._make_token_sequences(parsed)
else:
ngrams = self._make_ngrams(parsed)
matches = self._get_all_matches(ngrams)
if best_match:
matches = self._select_terms(matches)
self._print_verbose_status(parsed, matches)
return matches