def nearest_words(self, word, top=20, display=False):
"""
Find the nearest words to the word
according to the cosine similarity.
"""
W = self.W / np.linalg.norm(self.W, axis=0)
if (type(word)==str):
vec = self.word_vector(word, W)
else:
vec = word / np.linalg.norm(word)
cosines = (vec.T).dot(W)
args = np.argsort(cosines)[::-1]
nws = []
for i in xrange(1, top+1):
nws.append(self.inv_vocab[args[i]])
if (display):
print self.inv_vocab[args[i]], round(cosines[args[i]],3)
return nws
评论列表
文章目录