def analogy(self, pos1, neg1, pos2,N=10,mult=True):
wvecs, vocab = self._vecs, self._vocab
p1 = vocab.index(pos1)
p2 = vocab.index(pos2)
n1 = vocab.index(neg1)
if mult:
p1,p2,n1 = [(1+wvecs.dot(wvecs[i]))/2 for i in (p1,p2,n1)]
if N == 1:
return max(((v,w) for v,w in izip((p1 * p2 / n1),vocab) if w not in [pos1,pos2,neg1]))
return heapq.nlargest(N,((v,w) for v,w in izip((p1 * p2 / n1),vocab) if w not in [pos1,pos2,neg1]))
else:
p1,p2,n1 = [(wvecs.dot(wvecs[i])) for i in (p1,p2,n1)]
if N == 1:
return max(((v,w) for v,w in izip((p1 + p2 - n1),vocab) if w not in [pos1,pos2,neg1]))
return heapq.nlargest(N,((v,w) for v,w in izip((p1 + p2 - n1),vocab) if w not in [pos1,pos2,neg1]))
评论列表
文章目录