forked from angelpaulml17/DeIDClinic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNER_CRF.py
321 lines (290 loc) · 11.3 KB
/
NER_CRF.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
"""
Copyright 2020 ICES, University of Manchester, Evenset Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
#Code by Nikola Milosevic
import os
import sklearn_crfsuite
import pickle
from nltk.tokenize.treebank import TreebankWordTokenizer
from ner_plugins.NER_abstract import NER_abstract
from utils.spec_tokenizers import tokenize_fa
class NER_CRF(NER_abstract):
"""
The class for executing CRF labelling based on i2b2 dataset (2014).
"""
def __init__(self):
filename = 'Models/crf_baseline_model.sav'
self.crf_model = sklearn_crfsuite.CRF(
algorithm='lbfgs',
c1=0.1,
c2=0.05,
max_iterations=200,
all_possible_transitions=True
)
self._treebank_word_tokenizer = TreebankWordTokenizer()
if os.path.exists(filename):
self.crf_model = pickle.load(open(filename, 'rb'))
else:
self.crf_model = None
pass
def shape(self,word):
shape = ""
for letter in word:
if letter.isdigit():
shape = shape + "d"
elif letter.isalpha():
if letter.isupper():
shape = shape + "W"
else:
shape = shape + "w"
else:
shape = shape + letter
return shape
def word2features(self,sent, i):
"""
Transforms words into features that are fed into CRF model
:param sent: a list of tokens in a single sentence
:param i: position of a transformed word in a given sentence (token sequence)
:type i: int
"""
word = sent[i][0]
#postag = sent[i][1]
features = {
'bias': 1.0,
'word.lower()': word.lower(),
'word.isupper()': word.isupper(),
'word.istitle()': word.istitle(),
'word.isdigit()': word.isdigit(),
'word.shape()':self.shape(word),
'word.isalnum()':word.isalnum(),
'word.isalpha()':word.isalpha(),
# 'postag': postag,
# 'postag[:2]': postag[:2],
}
if i > 0:
word1 = sent[i - 1][0]
#postag1 = sent[i - 1][1]
features.update({
'-1:word.lower()': word1.lower(),
'-1:word.istitle()': word1.istitle(),
'-1:word.isupper()': word1.isupper(),
'-1:word.isdigit()': word1.isdigit(),
'-1:word.isalnum()':word1.isalnum(),
'-1:word.isalpha()':word1.isalpha(),
# '-1:postag': postag1,
# '-1:postag[:2]': postag1[:2],
})
else:
features['BOS'] = True
if i > 1:
word2 = sent[i - 2][0]
#postag2 = sent[i - 2][1]
features.update({
'-2:word.lower()': word2.lower(),
'-2:word.istitle()': word2.istitle(),
'-2:word.isupper()': word2.isupper(),
'-2:word.isdigit()': word2.isdigit(),
'-2:word.isalnum()': word2.isalnum(),
'-2:word.isalpha()': word2.isalpha(),
# '-2:postag': postag2,
# '-2:postag[:2]': postag2[:2],
})
else:
features['BOS1'] = True
if i > 2:
word3 = sent[i - 3][0]
#postag3 = sent[i - 3][1]
features.update({
'-3:word.lower()': word3.lower(),
'-3:word.istitle()': word3.istitle(),
'-3:word.isupper()': word3.isupper(),
'-3:word.isdigit()': word3.isdigit(),
'-3:word.isalnum()': word3.isalnum(),
'-3:word.isalpha()': word3.isalpha(),
# '-3:postag': postag3,
# '-3:postag[:2]': postag3[:2],
})
else:
features['BOS2'] = True
if i > 3:
word4 = sent[i - 4][0]
#postag4 = sent[i - 4][1]
features.update({
'-4:word.lower()': word4.lower(),
'-4:word.istitle()': word4.istitle(),
'-4:word.isupper()': word4.isupper(),
'-4:word.isdigit()': word4.isdigit(),
'-4:word.isalnum()': word4.isalnum(),
'-4:word.isalpha()': word4.isalpha(),
# '-4:postag': postag4,
# '-4:postag[:2]': postag4[:2],
})
else:
features['BOS2'] = True
if i < len(sent) - 1:
word1 = sent[i + 1][0]
features.update({
'+1:word.lower()': word1.lower(),
'+1:word.istitle()': word1.istitle(),
'+1:word.isupper()': word1.isupper(),
'+1:word.isdigit()': word1.isdigit(),
'+1:word.isalnum()': word1.isalnum(),
'+1:word.isalpha()': word1.isalpha(),
# '+1:postag': postag1,
# '+1:postag[:2]': postag1[:2],
})
else:
features['EOS'] = True
if i < len(sent) - 2:
word12 = sent[i + 2][0]
#postag12 = sent[i + 2][1]
features.update({
'+2:word.lower()': word12.lower(),
'+2:word.istitle()': word12.istitle(),
'+2:word.isupper()': word12.isupper(),
'+2:word.isdigit()': word12.isdigit(),
'+2:word.isalnum()': word12.isalnum(),
'+2:word.isalpha()': word12.isalpha(),
# '+2:postag': postag12,
# '+2:postag[:2]': postag12[:2],
})
else:
features['EOS2'] = True
if i < len(sent) - 3:
word13 = sent[i + 3][0]
#postag13 = sent[i + 3][1]
features.update({
'+3:word.lower()': word13.lower(),
'+3:word.istitle()': word13.istitle(),
'+3:word.isupper()': word13.isupper(),
'+3:word.isdigit()': word13.isdigit(),
'+3:word.isalnum()': word13.isalnum(),
'+3:word.isalpha()': word13.isalpha(),
# '+3:postag': postag13,
# '+3:postag[:2]': postag13[:2],
})
else:
features['EOS2'] = True
if i < len(sent) - 4:
word14 = sent[i + 4][0]
#postag14 = sent[i + 4][1]
features.update({
'+4:word.lower()': word14.lower(),
'+4:word.istitle()': word14.istitle(),
'+4:word.isupper()': word14.isupper(),
'+4:word.isdigit()': word14.isdigit(),
'+4:word.isalnum()': word14.isalnum(),
'+4:word.isalpha()': word14.isalpha(),
# '+4:postag': postag14,
# '+4:postag[:2]': postag14[:2],
})
else:
features['EOS2'] = True
return features
def doc2features(self,sent):
"""
Transforms a sentence to a sequence of features
:param sent: a set of tokens that will be transformed to features
:type language: list
"""
return [self.word2features(sent['tokens'], i) for i in range(len(sent['tokens']))]
def word2labels(self, sent):
return sent[1]
def sent2tokens(self,sent):
return [token for token, postag,capitalized, label in sent]
def prepare_features(self):
pass
def save_model(self,path):
pickle.dump(self.crf_model, open(path, 'wb'))
def transform_sequences(self,tokens_labels):
"""
Transforms sequences into the X and Y sets. For X it creates features, while Y is list of labels
:param tokens_labels: Input sequences of tuples (token,lable)
:return:
"""
X_train = []
y_train = []
for seq in tokens_labels:
features_seq = []
labels_seq = []
for i in range(0, len(seq)):
features_seq.append(self.word2features(seq, i))
labels_seq.append(self.word2labels(seq[i]))
X_train.append(features_seq)
y_train.append(labels_seq)
return X_train,y_train
def learn(self,X,Y,epochs =1):
"""
Function for training CRF algorithm
:param X: Training set input tokens and features
:param Y: Training set expected outputs
:param epochs: Epochs are basically used to calculate max itteration as epochs*200
:return:
"""
self.crf_model = sklearn_crfsuite.CRF(
algorithm='lbfgs',
c1=0.1,
c2=0.05,
max_iterations=(epochs*200),
all_possible_transitions=True
)
self.crf_model.fit(X, Y)
def save(self,model_path):
"""
Function that saves the CRF model using pickle
:param model_path: File name in Models/ folder
:return:
"""
filename = "Models/"+model_path+"1.sav"
pickle.dump(self.crf_model, open(filename, 'wb'))
def evaluate(self,X,Y):
"""
Function that takes testing data and evaluates them by making classification report matching predictions with Y argument of the function
:param X: Input sequences of words with features
:param Y: True labels
:return: Prints the classification report
"""
from sklearn import metrics
Y_pred = self.crf_model.predict(X)
labels = list(self.crf_model.classes_)
labels.remove('O')
Y_pred_flat = [item for sublist in Y_pred for item in sublist]
Y_flat = [item for sublist in Y for item in sublist]
print(metrics.classification_report(Y_pred_flat, Y_flat,labels))
def perform_NER(self,text):
"""
Implemented function that performs named entity recognition using CRF. Returns a sequence of tuples (token,label).
:param text: text over which should be performed named entity recognition
:type language: str
"""
X_test = []
documents = [text]
sequences = tokenize_fa(documents)
word_sequences = []
for seq in sequences:
features_seq = []
labels_seq = []
sentence = []
for i in range(0, len(seq)):
features_seq.append(self.word2features(seq, i))
labels_seq.append(self.word2labels(seq[i]))
sentence.append(seq[i][0])
X_test.append(features_seq)
word_sequences.append(sentence)
y_pred = self.crf_model.predict(X_test)
final_sequences = []
for i in range(0,len(y_pred)):
sentence = []
for j in range(0,len(y_pred[i])):
sentence.append((word_sequences[i][j],y_pred[i][j]))
final_sequences.append(sentence)
return final_sequences