-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrf_entity_extractor.py
426 lines (311 loc) · 12.6 KB
/
crf_entity_extractor.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# Copyrights chatme.ai
# Author: Anna Kozlova
# Created: 17/05/2019
import os
from pathlib import Path
from collections import namedtuple
from typing import Any
from typing import Dict
from typing import Iterable
from typing import List
from typing import Union
from nltk import pos_tag
from sklearn.externals import joblib
from sklearn.metrics import accuracy_score
from sklearn_crfsuite import CRF
from nltk_splitter import NLTKSplitter
__all__ = [
'Entity',
'CrfEntityExtractor',
]
Entity = namedtuple('Entity', ['name', 'value', 'start_token', 'end_token', 'start', 'end'])
class CrfEntityExtractor:
__DIRNAME = Path(os.path.dirname(__file__))
__FEATURES_SET = [
['low', 'title', 'upper', 'pos', 'pos2'],
['low', 'word3', 'word2', 'upper', 'title', 'digit', 'pos', 'pos2'],
['low', 'title', 'upper', 'pos', 'pos2'],
]
__HALF_FEATURES_SPAN = len(__FEATURES_SET) // 2
__CONFIG = {
'max_iterations': 40,
'L1_c': 1e-3,
'L2_c': 1e-2,
}
__FEATURES_RANGE = range(-__HALF_FEATURES_SPAN, __HALF_FEATURES_SPAN + 1)
__PREFIXES = [str(i) for i in __FEATURES_RANGE]
__FUNCTION_DICT = {
'low': lambda doc: doc[0].lower(),
'title': lambda doc: doc[0].istitle(),
'word3': lambda doc: doc[0][-3:],
'word2': lambda doc: doc[0][-2:],
'word1': lambda doc: doc[0][-1:],
'pos': lambda doc: doc[1],
'pos2': lambda doc: doc[1][:2],
'bias': lambda doc: 'bias',
'upper': lambda doc: doc[0].isupper(),
'digit': lambda doc: doc[0].isdigit(),
}
def __init__(self):
self.__crf_model = None
def fit(self, train_data: Iterable[str], labels: Iterable[Iterable[str]]):
"""
:param train_data:
:param labels: labels in BIO or BILOU notation
:return:
"""
crf_dataset = self.__create_dataset(train_data, labels)
features = [
self.__convert_idata_to_features(message_data)
for message_data in crf_dataset
]
labels = [
self.__extract_labels_from_data(message_data)
for message_data in crf_dataset
]
self.__crf_model = CRF(
algorithm='lbfgs',
c1=self.__CONFIG['L1_c'],
c2=self.__CONFIG['L2_c'],
max_iterations=self.__CONFIG['max_iterations'],
all_possible_transitions=True,
)
self.__crf_model.fit(features, labels)
return self
def predict(self, text: str) -> List['Entity']:
"""Predicts entities in text.
:param text:
:return:
"""
tokens = self.__preprocess(text)
intermediate_data = self.__convert_to_idata_format(tokens)
features = self.__convert_idata_to_features(intermediate_data)
predicts = self.__crf_model.predict_marginals_single(features)
entities = []
for pred in predicts:
sorted_pred = sorted(pred.items(), key=lambda x: x[1], reverse=True)
entities.append(sorted_pred[0][0])
# entities = self.__get_entities_from_predict(
# tokens,
# predicts
# )
return entities
def evaluate(self, test_data: Iterable[str], labels: Iterable[Iterable[str]], metric: str = 'accuracy'):
"""Evaluates accuracy on test data.
:param test_data:
:param labels:
:param metric:
:return:
"""
# if self.__crf_model is None:
# self.load_model()
labels = self.__process_test_labels(labels)
predicted_entities = [self.predict(sentence) for sentence in test_data]
processed_predicted_entities = [self.__postprocess(sent_entities, self.__preprocess(sentence))
for (sent_entities, sentence) in zip(predicted_entities, test_data)]
all_predicted = self.__get_flatten_values(processed_predicted_entities)
all_labels = self.__get_flatten_values(labels)
return accuracy_score(all_predicted, all_labels)
def load_model(self, path: Path) -> 'CrfEntityExtractor':
"""Loads saved model.
:param path: path where model was saved
:return:
"""
self.__crf_model = joblib.load(path)
return self
def save_model(self, path: Path) -> None:
joblib.dump(self.__crf_model, path)
def __create_dataset(self, sentences, labels):
dataset_message_format = [
self.__convert_to_idata_format(
self.__preprocess(sentence),
sentence_labels
)
for sentence, sentence_labels in zip(sentences, labels)
]
return dataset_message_format
def __convert_to_idata_format(self, tokens, entities=None):
message_data_intermediate_format = []
for i, token in enumerate(tokens):
entity = entities[i] if (entities and len(entities) > i) else "N/A"
tag = self.__get_tag_of_token(token.value)
message_data_intermediate_format.append((token.value, tag, entity))
return message_data_intermediate_format
def __get_entities_from_predict(self, tokens, predicts):
entities = []
cur_token_ind: int = 0
while cur_token_ind < len(tokens):
end_ind, confidence, entity_label = self.__handle_bilou_label(
cur_token_ind,
predicts
)
if end_ind is not None:
current_tokens = tokens[cur_token_ind: end_ind + 1]
entity_value: str = ' '.join([token.value for token in current_tokens])
entity = Entity(
name=entity_label,
value=entity_value,
start_token=cur_token_ind,
end_token=end_ind,
start=current_tokens[0].start,
end=current_tokens[-1].end
)
entities.append(entity)
cur_token_ind = end_ind + 1
else:
cur_token_ind += 1
return entities
def __handle_bilou_label(self, token_index, predicts):
label, confidence = self.__get_most_likely_entity(token_index, predicts)
entity_label = self.__convert_to_ent_name(label)
extracted = self.__extract_bilou_prefix(label)
# if extracted == "U":
# return token_index, confidence, entity_label
if extracted == "B":
end_token_index, confidence = self.__find_bilou_end(
token_index,
predicts
)
return end_token_index, confidence, entity_label
else:
return None, None, None
def __find_bilou_end(self, token_index: int, predicts):
end_token_ind: int = token_index + 1
finished: bool = False
label, confidence = self.__get_most_likely_entity(token_index, predicts)
entity_label: str = self.__convert_to_ent_name(label)
while not finished:
label, label_confidence = self.__get_most_likely_entity(
end_token_ind,
predicts
)
confidence = min(confidence, label_confidence)
if self.__convert_to_ent_name(label) != entity_label:
if self.__extract_bilou_prefix(label) == 'L':
finished = True
if self.__extract_bilou_prefix(label) == 'I':
end_token_ind += 1
else:
finished = True
end_token_ind -= 1
else:
end_token_ind += 1
return end_token_ind, confidence
def __mark_positions_by_labels(self, entities_labels, positions, name: str):
if len(positions) == 1:
entities_labels = self.__set_label(
entities_labels, positions[0], 'U', name
)
else:
entities_labels = self.__set_label(
entities_labels, positions[0], 'B', name
)
entities_labels = self.__set_label(
entities_labels, positions[-1], 'L', name
)
for ind in positions[1: -1]:
entities_labels = self.__set_label(
entities_labels, ind, 'I', name
)
return entities_labels
def __get_example_features(self, data, example_index):
"""Exctract features from example in intermediate data format
:param data: list of examples in task specified format
:param example_index: index of central example
:return: list of special features extracted from one example and its context
"""
message_len = len(data)
example_features = {}
for futures_index in self.__FEATURES_RANGE:
if example_index + futures_index >= message_len:
example_features['EOS'] = True
elif example_index + futures_index < 0:
example_features['BOS'] = True
else:
example = data[example_index + futures_index]
shifted_futures_index = futures_index + self.__HALF_FEATURES_SPAN
prefix = self.__PREFIXES[shifted_futures_index]
features = self.__FEATURES_SET[shifted_futures_index]
for feature in features:
value = self.__FUNCTION_DICT[feature](example)
example_features[f'{prefix}:{feature}'] = value
return example_features
def __convert_idata_to_features(self, data):
"""Extract features from examples in intermediate data format
:param data: list of examples in special format
:return: list of futures extracted form each example
"""
features = []
for ind, example in enumerate(data):
example_features: Dict[str, Any] = self.__get_example_features(data, ind)
features.append(example_features)
return features
def __get_most_likely_entity(self, ind: int, predicts):
if len(predicts) > ind:
entity_probs = predicts[ind]
else:
entity_probs = None
if entity_probs:
label: str = max(entity_probs, key=lambda key: entity_probs[key])
confidence = sum([v for k, v in entity_probs.items() if k[2:] == label[2:]])
return label, confidence
else:
return '', 0.0
def __convert_to_ent_name(self, bilou_ent_name: str) -> str:
"""Get entity name from bilou label representation
:param bilou_ent_name: BILOU entity name
:return: entity name without BILOU prefix
"""
return bilou_ent_name[2:]
def __extract_bilou_prefix(self, label: str):
"""Get BILOU prefix from label
If label prefix (first label symbol) not in {B, I, U, L} return None
:param label: BILOU entity name
:return: BILOU prefix
"""
if len(label) >= 2 and label[1] == "-":
return label[0].upper()
return None
def __process_test_labels(self, test_labels):
return [self.__to_dict([label[2:]
if (label.startswith('B-') or label.startswith('I-')) else label
for label in sent_labels])
for sent_labels in test_labels]
@staticmethod
def __preprocess(text: str) -> List[str]:
"""Deletes EOS token; splits texts into token.
:param texts:
:return: tokens
"""
if text.endswith('EOS'):
return NLTKSplitter().process(text)[:-1]
else:
return NLTKSplitter().process(text)
@staticmethod
def __get_tag_of_token(token: str) -> str:
"""Gets part-of-speech tag for token.
:param token:
:return: POS tag
"""
tag = pos_tag([token])[0][1]
return tag
@staticmethod
def __extract_labels_from_data(data: Iterable) -> List[str]:
return [label for _, _, label in data]
@staticmethod
def __set_label(entities_labels, ind: int, prefix: str, name: str):
entities_labels[ind] = f'{prefix}-{name}'
return entities_labels
@staticmethod
def __to_dict(sent_labels):
return dict(enumerate(sent_labels))
@staticmethod
def __postprocess(entities, sentence):
entities_dict = {k: 'O' for k in range(len(sentence))}
for entity in entities:
for key in range(entity.start_token, entity.end_token + 1):
entities_dict[key] = entity.name
return entities_dict
@staticmethod
def __get_flatten_values(dicts):
return [word for sentence in dicts for word in sentence.values()]