[735bb5]: / src / features / __init__.py

Download this file

320 lines (254 with data), 10.6 kB

  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
# Base Dependencies
# -----------------
import numpy
from typing import Dict
# Package Dependencies
# -----------------
# distance features
from .token_distance_feature import TokenDistanceFeature
from .char_distance_feature import CharDistanceFeature
from .punct_distance_feature import PunctuationFeature
from .position_feature import PositionFeature
from .relative_distance_feature import RelativeDistanceFeature
# word-based features
from .bag_of_entities_feature import BagOfEntitiesFeature
from .bag_of_words_feature import BagOfWordsFeature
from .bag_of_verbs_feature import BagOfVerbsFeature
# text representations
from .wei_text_feature import WeiTextFeature
# embeddings
from .iob_feature import IOBFeature
from .word_to_index import WordToIndex
from .entity_embedding import EntityEmbedding
from .sentence_embedding import SentenceEmbedding
# semantic
from .pos_feature import POSFeature
from .dep_feature import DEPFeature
from .negation_feature import NegationFeature
from .negated_entities_feature import NegatedEntitiesFeature
from .dependency_tree import DependencyTree
from .dep_adjancency_matrix import DependencyAdjacencyMatrix
from .sent_has_but_feature import SentHasButFeature
# others
from .character_length_feature import CharacterLengthFeature
from .token_length_feature import TokenLengthFeature
# Local Dependencies
# -------------------
from models.relation_collection import RelationCollection
from vocabulary import Vocabulary
# 3rd-Party Dependencies
# ----------------------
from sklearn.base import BaseEstimator
# RandomForestFeatures
# --------------------
class RandomForestFeatures(BaseEstimator):
"""Random Forest Features
Generates the features for the Random Forest model. This features are a subset
of those used in `Alimova and Tutubalina (2020) - Multiple features for clinical
relation extraction: A machine learning approach`
"""
def __init__(self, dataset: str):
# distance
self.token_distance_feature = TokenDistanceFeature()
self.char_distance_feature = CharDistanceFeature()
self.punctuation_feature = PunctuationFeature()
self.position_feature = PositionFeature(dataset=dataset)
# word-base
self.bag_of_entities_feature = BagOfEntitiesFeature(dataset=dataset)
self.bag_of_words_feature = BagOfWordsFeature()
def get_feature_names(self, input_features=None):
names = []
names = names + self.token_distance_feature.get_feature_names()
names = names + self.char_distance_feature.get_feature_names()
names = names + self.punctuation_feature.get_feature_names()
names = names + self.position_feature.get_feature_names()
names = names + self.bag_of_entities_feature.get_feature_names()
names = names + self.bag_of_words_feature.get_feature_names()
return names
def fit(self, x: RelationCollection, y=None):
# distance
self.token_distance_feature = self.token_distance_feature.fit(x)
self.char_distance_feature = self.char_distance_feature.fit(x)
self.punctuation_feature = self.punctuation_feature.fit(x)
self.position_feature = self.position_feature.fit(x)
# word-base
self.bag_of_entities_feature = self.bag_of_entities_feature.fit(x)
self.bag_of_words_feature = self.bag_of_words_feature.fit(x)
return self
def transform(self, x: RelationCollection) -> numpy.array:
# distance
token_distance_feature = self.token_distance_feature.transform(x)
char_distance_feature = self.char_distance_feature.transform(x)
punctuation_feature = self.punctuation_feature.transform(x)
position_feature = self.position_feature.transform(x)
# word-base
bag_of_entities_feature = self.bag_of_entities_feature.transform(x)
bag_of_words_feature = self.bag_of_words_feature.transform(x)
features = numpy.concatenate(
(
token_distance_feature,
char_distance_feature,
punctuation_feature,
position_feature,
bag_of_entities_feature,
bag_of_words_feature,
),
axis=1,
)
assert features.shape[0] == len(x)
return features
def fit_transform(self, x: RelationCollection, y=None) -> numpy.array:
# distance
token_distance_feature = self.token_distance_feature.fit_transform(x)
char_distance_feature = self.char_distance_feature.fit_transform(x)
punctuation_feature = self.punctuation_feature.fit_transform(x)
position_feature = self.position_feature.fit_transform(x)
# word-base
bag_of_entities_feature = self.bag_of_entities_feature.fit_transform(x)
bag_of_words_feature = self.bag_of_words_feature.fit_transform(x)
features = numpy.concatenate(
(
token_distance_feature,
char_distance_feature,
punctuation_feature,
position_feature,
bag_of_entities_feature,
bag_of_words_feature,
),
axis=1,
)
assert features.shape[0] == len(x)
return features
class RandomForestFeaturesNegation(RandomForestFeatures):
"""Random Forest Features with Negation"""
def __init__(self, dataset: str):
super().__init__(dataset)
# negation
# self.negation_feature = NegationFeature()
self.negated_entities = NegatedEntitiesFeature()
self.has_but = SentHasButFeature()
def get_feature_names(self, input_features=None):
names = super().get_feature_names()
# names = names + self.negation_feature.get_feature_names()
names = names + self.negated_entities.get_feature_names()
names = names + self.has_but.get_feature_names()
return names
def fit(self, x: RelationCollection, y=None):
super().fit(x)
# negation
# self.negation_feature = self.negation_feature.fit(x)
self.negated_entities = self.negated_entities.fit(x)
self.has_but = self.has_but.fit(x)
return self
def transform(self, x: RelationCollection):
features = super().transform(x)
# negation
# negation_feature = self.negation_feature.transform(x)
negated_entities = self.negated_entities.transform(x)
has_but = self.has_but.transform(x)
features = numpy.concatenate(
(features, negated_entities, has_but), # negation_feature,
axis=1,
)
return features
def fit_transform(self, x: RelationCollection):
features = super().fit_transform(x)
# negation
# negation_feature = self.negation_feature.fit_transform(x)
negated_entities = self.negated_entities.fit_transform(x)
has_but = self.has_but.fit_transform(x)
features = numpy.concatenate(
(features, negated_entities, has_but), # negation_feature,
axis=1,
)
return features
# BilstmFeatures
# --------------
class BilstmFeatures(BaseEstimator):
"""BiLSTM Features
Generates the feautes for the BiLSTM model. These features correspond to
the ones used in `Hasan et al. - Integrating Text Embedding with Traditional NLP
Features for Clinical Relation Extraction`
"""
def __init__(self, dataset: str, vocab: Vocabulary):
self.dataset = dataset
self.vocab = vocab
self.relative_distance = RelativeDistanceFeature()
self.iob = IOBFeature(dataset, vocab.pad_index)
self.pos = POSFeature(vocab.pad_index)
self.dep = DEPFeature(vocab.pad_index)
self.word2index = WordToIndex(vocab)
self.char_length = CharacterLengthFeature()
def fit(self, x: RelationCollection, y=None):
self.relative_distance = self.relative_distance.fit(x)
self.iob = self.iob.fit(x)
self.pos = self.pos.fit(x)
self.dep = self.dep.fit(x)
self.word2index = self.word2index.fit(x)
return self
def transform(self, x: RelationCollection) -> Dict:
rd1, rd2 = self.relative_distance.transform(x)
iob = self.iob.transform(x)
pos = self.pos.transform(x)
dep = self.dep.transform(x)
e1, e2, sent = self.word2index.transform(x)
seq_length = [len(s) for s in sent]
char_length = self.char_length.transform(x)
return {
"rd1": rd1,
"rd2": rd2,
"iob": iob,
"pos": pos,
"dep": dep,
"e1": e1,
"e2": e2,
"sent": sent,
"seq_length": seq_length,
"char_length": char_length,
}
def fit_transform(self, x: RelationCollection, y=None) -> Dict:
rd1, rd2 = self.relative_distance.fit_transform(x)
iob = self.iob.fit_transform(x)
pos = self.pos.fit_transform(x)
dep = self.dep.fit_transform(x)
e1, e2, sent = self.word2index.fit_transform(x)
seq_length = numpy.array([len(s) for s in sent])
char_length = self.char_length.fit_transform(x)
return {
"rd1": rd1,
"rd2": rd2,
"iob": iob,
"pos": pos,
"dep": dep,
"e1": e1,
"e2": e2,
"sent": sent,
"seq_length": seq_length,
"char_length": char_length,
}
# BertFeatures
# --------------
class BertFeatures(BaseEstimator):
"""BERT Features
Generates the features for the Bert model.
"""
def __init__(self):
self.char_length = CharacterLengthFeature()
self.token_length = TokenLengthFeature()
self.wei_text = WeiTextFeature()
def fit(self, x: RelationCollection, y=None):
return self
def transform(self, x: RelationCollection) -> Dict:
return {
"sentence": self.wei_text.transform(x),
"text": [r.text for r in x.relations],
"char_length": self.char_length.transform(x),
"seq_length": self.token_length.transform(x),
}
def fit_transform(self, x: RelationCollection, y=None) -> Dict:
return {
"sentence": self.wei_text.fit_transform(x),
"text": [r.text for r in x.relations],
"char_length": self.char_length.fit_transform(x),
"seq_length": self.token_length.fit_transform(x),
}