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

Download this file

66 lines (48 with data), 2.2 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
# Base Dependencies
# ----------------
import numpy
# Local Dependencies
# ------------------
from models.relation_collection import RelationCollection
# 3rd-Party Dependencies
# ----------------------
from sklearn.base import BaseEstimator
class NegationFeature(BaseEstimator):
"""
NegationFeature
Determines if a relation:
1. does not contain `no`, `n't` or `not`.
2. doen't contain any of the following phrases: "not recommended", "should not be", "must not be"
3. No target entity mention appears in the sentence after “no”, “n’t” or “not”
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 ["no_word", "no_phrase", "no_target"]
def compute_not_feature(self, collection: RelationCollection) -> numpy.array:
features = []
for i in range(len(collection)):
feature = [1, 1, 1]
# 1. does not contain “no”, “n’t” or “not”
for token in collection.tokens[i]:
if token.lemma_ in ["no", "not"]:
feature[0] = 0
# 2. hasn't any of the following phrases
for phrase in ["not recommended", "should not be", "must not be"]:
if phrase in collection.relations[i].text:
feature[1] = 0
# 3. No target entity mention appears in the sentence after “no”, “n’t” or “not”
for token in collection.left_tokens[i]:
if token.lemma_ in ["no", "not"]:
feature[2] = 0
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_not_feature(x)
def fit_transform(self, x: RelationCollection, y=None) -> numpy.array:
return self.compute_not_feature(x)