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

Download this file

63 lines (46 with data), 1.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
# Base Dependencies
# ----------------
from typing import Optional
# Local Dependencies
# ------------------
from models import RelationCollection
from nlp_pipeline import get_pipeline
# 3rd-Party Dependencies
# ----------------------
from sklearn.base import BaseEstimator
# Constants
# ---------
from constants import DEP_TAGS
class DEPFeature(BaseEstimator):
"""
Dependency Tagging
Obtains the DEP tags of each token in the relation's sentence
"""
def __init__(self, padding_idx: Optional[int] = None):
self.padding_idx = padding_idx
def get_feature_names(self, input_features=None):
return ["DEP"]
def create_dep_feature(self, collection: RelationCollection) -> list:
all_dep = []
NLP = get_pipeline()
parser = NLP.get_pipe("parser")
for doc in parser.pipe(collection.tokens):
r_dep = []
for t in doc:
r_dep.append(self.dep_index(t.dep_))
all_dep.append(r_dep)
return all_dep
def dep_index(self, dep_tag: str):
"""
Computes the index of the corresponding POS tag
"""
idx = DEP_TAGS.index(dep_tag)
if self.padding_idx is not None and idx >= self.padding_idx:
idx += 1
return idx
def fit(self, x: RelationCollection, y=None):
return self
def transform(self, x: RelationCollection) -> list:
return self.create_dep_feature(x)
def fit_transform(self, x: RelationCollection, y=None) -> list:
return self.create_dep_feature(x)