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

Download this file

61 lines (43 with data), 1.5 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
# Base Dependencies
# ----------------
from typing import Optional
# Local Dependencies
# ------------------
from models import RelationCollection
# 3rd-Party Dependencies
# ----------------------
from sklearn.base import BaseEstimator
# Constants
# ---------
from constants import U_POS_TAGS
class POSFeature(BaseEstimator):
"""
PoS Tagging
Obtains the universal POS tag 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 ["POS"]
def create_pos_feature(self, collection: RelationCollection) -> list:
all_pos = []
for doc in collection.tokens:
r_pos = []
for t in doc:
r_pos.append(self.pos_index(t.pos_))
all_pos.append(r_pos)
return all_pos
def pos_index(self, pos_tag: str):
"""
Computes the index of the corresponding POS tag
"""
idx = U_POS_TAGS.index(pos_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_pos_feature(x)
def fit_transform(self, x: RelationCollection, y=None) -> list:
return self.create_pos_feature(x)