Repository: piskvorky/gensim

Issue:
LeveshteinSimilarityIndex fails when called from SparseTermSimilarityMatrix
<!--
**IMPORTANT**:

- Use the [Gensim mailing list](https://groups.google.com/forum/#!forum/gensim) to ask general or usage questions. Github issues are only for bug reports.
- Check [Recipes&FAQ](https://github.com/RaRe-Technologies/gensim/wiki/Recipes-&-FAQ) first for common answers.

Github bug reports that do not include relevant information and context will be closed without an answer. Thanks!
-->

#### Problem description

When trying to build a SparseTermSimilarityMatrix using Levenshtein it fails.

#### Steps/code/corpus to reproduce

```python
index = LevenshteinSimilarityIndex(dictionary)
SparseTermSimilarityMatrix(index, dictionary) # <- fails here
```
```
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-59-c16b89564835> in <module>
----> 1 similarity_matrix1 = SparseTermSimilarityMatrix(similarity_index1, dictionary)

~/.local/share/virtualenvs/pdftagger-LHy_2RHk/lib/python3.6/site-packages/gensim/similarities/termsim.py in __init__(self, source, dictionary, tfidf, symmetric, positive_definite, nonzero_limit, dtype)
    234                 for term, similarity in index.most_similar(t1, topn=num_rows)
    235                 if term in dictionary.token2id
--> 236             ] if num_rows > 0 else []
    237 
    238             if tfidf is None:

~/.local/share/virtualenvs/pdftagger-LHy_2RHk/lib/python3.6/site-packages/gensim/similarities/levenshtein.py in most_similar(self, t1, topn)
    151             if similarity > 0
    152         )
--> 153         return islice(most_similar, topn)

ValueError: Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize.
```

#### Versions

Linux-4.18.0-25-generic-x86_64-with-debian-buster-sid
Python 3.6.9 (default, Nov 18 2019, 15:20:23) 
[GCC 8.3.0]
NumPy 1.17.4
SciPy 1.3.3
gensim 3.8.1
FAST_VERSION 1




Discussion:

Resolving patch:
diff --git a/gensim/similarities/levenshtein.py b/gensim/similarities/levenshtein.py
--- a/gensim/similarities/levenshtein.py
+++ b/gensim/similarities/levenshtein.py
@@ -8,7 +8,7 @@
 This module provides a namespace for functions that use the Levenshtein distance.
 """
 
-from itertools import islice
+import itertools
 import logging
 from math import floor
 
@@ -150,4 +150,4 @@ def most_similar(self, t1, topn=10):
             for (similarity, t2) in sorted(similarities, reverse=True)
             if similarity > 0
         )
-        return islice(most_similar, topn)
+        return itertools.islice(most_similar, int(topn))


Test patch:
diff --git a/gensim/test/test_similarities.py b/gensim/test/test_similarities.py
--- a/gensim/test/test_similarities.py
+++ b/gensim/test/test_similarities.py
@@ -1225,6 +1225,11 @@ def test_most_similar(self):
         second_similarities = numpy.array([similarity for term, similarity in index.most_similar(u"holiday", topn=10)])
         self.assertTrue(numpy.allclose(first_similarities ** 2.0, second_similarities))
 
+        # check proper integration with SparseTermSimilarityMatrix
+        index = LevenshteinSimilarityIndex(self.dictionary, alpha=1.0, beta=1.0)
+        similarity_matrix = SparseTermSimilarityMatrix(index, dictionary)
+        self.assertTrue(scipy.sparse.issparse(similarity_matrix.matrix))
+
 
 if __name__ == '__main__':
     logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.DEBUG)