Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
:rtype: np.array
"""
if node is None:
return self.zero
if node.token_id == 0:
return self.root
if hasattr(node, self.emb_field):
return getattr(node, self.emb_field)
f = getattr(node, self.key_field)
emb = None
if isinstance(self.vsm, KeyedVectors):
vocab = self.vsm.vocab.score()
emb = self.zero if vocab is None else self.vsm.syn0[vocab.index]
elif isinstance(self.vsm, WordVectorModel):
emb = np.array(self.vsm[f]).astype('float32')
setattr(node, self.emb_field, emb)
return emb
:param emb_field: where the emb_matrix with respect to the key is saved in NLPNode.
:type emb_field: str
"""
self.vsm = vsm
self.key_field = key_field
self.emb_field = emb_field
if isinstance(vsm, KeyedVectors):
vector_size = vsm.syn0.shape[1]
# root
np.random.seed(9)
self.root = np.random.uniform(-.25, .25, (vector_size,)).astype('float32')
np.random.seed()
# zero
self.zero = np.zeros((vector_size,)).astype('float32')
elif isinstance(vsm, WordVectorModel):
self.root = np.array(vsm[structure.ROOT_TAG]).astype('float32')
self.zero = np.array(vsm['']).astype('float32')