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

Download this file

67 lines (49 with data), 2.0 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
66
# Base Dependencies
# ----------------
import numpy as np
from typing import List, Tuple
# Local Dependencies
# ------------------
from models import RelationCollection
# 3rd-Party Dependencies
# ----------------------
from gensim.models import KeyedVectors
from spacy.tokens import Doc
from sklearn.base import BaseEstimator
# Constants
# ---------
from constants import DATASETS
class EntityEmbedding(BaseEstimator):
"""
Entity Embedding
Obtains the vectors indexes of the two entities in the relation.
Source:
Alimova and Tutubalina (2020) - Multiple features for clinical relation extraction: A machine learning approach
"""
def __init__(self, dataset: str, model: KeyedVectors):
if dataset not in DATASETS:
raise ValueError("unsupported dataset '{}'".format(dataset))
self.dataset = dataset
self.model = model
def get_feature_names(self, input_features=None):
return ["ent_emb"]
def create_entity_embedding(
self, collection: RelationCollection
) -> Tuple[np.array, np.array]:
e1_embs = []
e2_embs = []
entities1: List[Doc] = collection.entities1_tokens
entities2: List[Doc] = collection.entities2_tokens
assert len(entities1) == len(entities2)
for e1, e2 in zip(entities1, entities2):
e1_tokens: List[str] = list(map(lambda t: t.text.lower(), e1))
e2_tokens: List[str] = list(map(lambda t: t.text.lower(), e2))
e1_embs.append(self.model.get_mean_vector(e1_tokens))
e2_embs.append(self.model.get_mean_vector(e2_tokens))
return np.array(e1_embs), np.array(e2_embs)
def fit(self, x: RelationCollection, y=None):
return self
def transform(self, x: RelationCollection) -> Tuple[np.array, np.array]:
return self.create_entity_embedding(x)
def fit_transform(self, x: RelationCollection, y=None) -> Tuple[np.array, np.array]:
return self.create_entity_embedding(x)