-
Notifications
You must be signed in to change notification settings - Fork 0
/
embedding.py
25 lines (21 loc) · 886 Bytes
/
embedding.py
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
import numpy as np
from gensim.models import KeyedVectors
class Embedding:
def __init__(self, general_model):
self.general_model = KeyedVectors.load(general_model, mmap='r')
def get_embeddings(self, token, sentence):
try:
return np.mean(self.general_model[token], axis=0)
# return np.mean(self.hate_model.get_vector(token))
except KeyError:
try:
return np.mean(self.general_model[token], axis=0)
except KeyError:
embedded = []
for snt in sentence:
try:
embedded.append(np.mean(self.general_model[snt]))
# embedded.append(np.mean(self.hate_model.get_vector(snt)))
except KeyError:
embedded.append(0.0)
return np.mean(embedded)