From b588bd7d5a9ea5db3538c4bcd62d3eb9243739ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Novotn=C3=BD?= Date: Fri, 17 May 2019 23:32:09 +0200 Subject: [PATCH] Make SparseTermSimilarityMatrix constructor defensive about most_similar(topn=0) --- gensim/similarities/termsim.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/gensim/similarities/termsim.py b/gensim/similarities/termsim.py index bc7bd836f0..d7a06d6b1d 100644 --- a/gensim/similarities/termsim.py +++ b/gensim/similarities/termsim.py @@ -231,8 +231,9 @@ def __init__(self, source, dictionary=None, tfidf=None, symmetric=True, positive num_rows = nonzero_limit - num_nonzero most_similar = [ (dictionary.token2id[term], similarity) - for term, similarity in index.most_similar(t1, num_rows) - if term in dictionary.token2id] + for term, similarity in index.most_similar(t1, topn=num_rows) + if term in dictionary.token2id + ] if num_rows > 0 else [] if tfidf is None: rows = sorted(most_similar)