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

Download this file

50 lines (36 with data), 1.3 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
# Base Dependencies
# ----------------
import numpy
# Local Dependencies
# ------------------
from models import RelationCollection
# 3rd-Party Dependencies
# ----------------------
from sklearn.base import BaseEstimator
class TokenDistanceFeature(BaseEstimator):
"""
TokenDistanceFeature
Computes the number of tokens between the two entities of a relation.
Source:
Alimova and Tutubalina (2020) - Multiple features for clinical relation extraction: A machine learning approach
"""
def __init__(self):
pass
def get_feature_names(self, input_features=None):
return ["token_dist"]
def create_token_distance_feature(
self, collection: RelationCollection
) -> numpy.array:
features = []
# max = 1
for doc in collection.middle_tokens:
features.append([len(doc)])
# if len(r.middle_context) > max:
# max = len(r.middle_context)
return numpy.array(features)
def fit(self, x: RelationCollection, y=None):
return self
def transform(self, x: RelationCollection, y=None) -> numpy.array:
return self.create_token_distance_feature(x)
def fit_transform(self, x: RelationCollection, y=None) -> numpy.array:
return self.create_token_distance_feature(x)