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

Download this file

52 lines (37 with data), 1.4 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
# Base Dependencies
# ----------------
import numpy
# Local Dependencies
# ------------------
from models.relation_collection import RelationCollection
# 3rd-Party Dependencies
# ----------------------
from sklearn.base import BaseEstimator
class SentHasButFeature(BaseEstimator):
"""
SentHasBut Feature
Determines if a relation contains the word "but".
Source:
Chowdhury and Lavelli (2013) - Exploiting the Scope of Negations and Heterogeneous Features for Relation
Extraction: A Case Study for Drug-Drug Interaction Extraction
"""
def __init__(self):
pass
def get_feature_names(self, input_features=None):
return ["has_but"]
def compute_sent_has_but(self, collection: RelationCollection) -> numpy.array:
features = []
for doc in collection.tokens:
feature = 0
for token in doc:
if token.text == "but":
feature = 1
break
features.append([feature])
return numpy.array(features)
def fit(self, x: RelationCollection, y=None):
return self
def transform(self, x: RelationCollection, y=None) -> numpy.array:
return self.compute_sent_has_but(x)
def fit_transform(self, x: RelationCollection, y=None) -> numpy.array:
return self.compute_sent_has_but(x)