Skip to content

Commit

Permalink
Retain undocumented WordEmbeddingsKeyedVectors.most_similar(..topn=None)
Browse files Browse the repository at this point in the history
  • Loading branch information
Witiko committed Jan 25, 2019
1 parent f4a2d00 commit 06781e2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 3 additions & 1 deletion gensim/models/keyedvectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ def most_similar(self, positive=None, negative=None, topn=10, restrict_vocab=Non
Sequence of (word, similarity).
"""
if topn < 1:
if topn is not None and topn < 1:
return []

if positive is None:
Expand Down Expand Up @@ -553,6 +553,8 @@ def most_similar(self, positive=None, negative=None, topn=10, restrict_vocab=Non

limited = self.vectors_norm if restrict_vocab is None else self.vectors_norm[:restrict_vocab]
dists = dot(limited, mean)
if topn is None:
return dists
best = matutils.argsort(dists, topn=topn + len(all_words), reverse=True)
# ignore (don't return) words from the input
result = [(self.index2word[sim], float(dists[sim])) for sim in best if sim not in all_words]
Expand Down
3 changes: 3 additions & 0 deletions gensim/test/test_keyedvectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ def test_most_similar_topn(self):
self.assertEqual(len(self.vectors.most_similar('war', topn=5)), 5)
self.assertEqual(len(self.vectors.most_similar('war', topn=10)), 10)

predicted = self.vectors.most_similar('war', topn=None)
self.assertEqual(len(predicted), len(self.vectors.vocab))

def test_relative_cosine_similarity(self):
"""Test relative_cosine_similarity returns expected results with an input of a word pair and topn"""
wordnet_syn = [
Expand Down

0 comments on commit 06781e2

Please sign in to comment.