We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN 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



--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `gensim/similarities/levenshtein.py`
Content:
```
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright (C) 2018 Vit Novotny <witiko@mail.muni.cz>
5 # Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html
6 
7 """
8 This module provides a namespace for functions that use the Levenshtein distance.
9 """
10 
11 from itertools import islice
12 import logging
13 from math import floor
14 
15 from gensim.similarities.termsim import TermSimilarityIndex
16 
17 logger = logging.getLogger(__name__)
18 
19 
20 def levdist(t1, t2, max_distance=float("inf")):
21     """Get the Levenshtein distance between two terms.
22 
23     Return the Levenshtein distance between two terms. The distance is a
24     number between <1.0, inf>, higher is less similar.
25 
26     Parameters
27     ----------
28     t1 : {bytes, str, unicode}
29         The first compared term.
30     t2 : {bytes, str, unicode}
31         The second compared term.
32     max_distance : {int, float}, optional
33         If you don't care about distances larger than a known threshold, a more
34         efficient code path can be taken. For terms that are clearly "too far
35         apart", we will not compute the distance exactly, but we will return
36         `max(len(t1), len(t2))` more quickly, meaning "more than
37         `max_distance`".
38         Default: always compute distance exactly, no threshold clipping.
39 
40     Returns
41     -------
42     int
43         The Levenshtein distance between `t1` and `t2`.
44 
45     """
46     import Levenshtein
47 
48     distance = Levenshtein.distance(t1, t2)
49     if distance > max_distance:
50         return max(len(t1), len(t2))
51     return distance
52 
53 
54 def levsim(t1, t2, alpha=1.8, beta=5.0, min_similarity=0.0):
55     """Get the Levenshtein similarity between two terms.
56 
57     Return the Levenshtein similarity between two terms. The similarity is a
58     number between <0.0, 1.0>, higher is more similar.
59 
60     Parameters
61     ----------
62     t1 : {bytes, str, unicode}
63         The first compared term.
64     t2 : {bytes, str, unicode}
65         The second compared term.
66     alpha : float, optional
67         The multiplicative factor alpha defined by Charlet and Damnati (2017).
68     beta : float, optional
69         The exponential factor beta defined by Charlet and Damnati (2017).
70     min_similarity : {int, float}, optional
71         If you don't care about similarities smaller than a known threshold, a
72         more efficient code path can be taken. For terms that are clearly "too
73         far apart", we will not compute the distance exactly, but we will
74         return zero more quickly, meaning "less than `min_similarity`".
75         Default: always compute similarity exactly, no threshold clipping.
76 
77     Returns
78     -------
79     float
80         The Levenshtein similarity between `t1` and `t2`.
81 
82     Notes
83     -----
84     This notion of Levenshtein similarity was first defined in section 2.2 of
85     `Delphine Charlet and Geraldine Damnati, "SimBow at SemEval-2017 Task 3:
86     Soft-Cosine Semantic Similarity between Questions for Community Question
87     Answering", 2017 <http://www.aclweb.org/anthology/S/S17/S17-2051.pdf>`_.
88 
89     """
90     assert alpha >= 0
91     assert beta >= 0
92 
93     max_lengths = max(len(t1), len(t2))
94     if max_lengths == 0:
95         return 1.0
96 
97     min_similarity = float(max(min(min_similarity, 1.0), 0.0))
98     max_distance = int(floor(max_lengths * (1 - (min_similarity / alpha) ** (1 / beta))))
99     distance = levdist(t1, t2, max_distance)
100     similarity = alpha * (1 - distance * 1.0 / max_lengths)**beta
101     return similarity
102 
103 
104 class LevenshteinSimilarityIndex(TermSimilarityIndex):
105     """
106     Computes Levenshtein similarities between terms and retrieves most similar
107     terms for a given term.
108 
109     Notes
110     -----
111     This is a naive implementation that iteratively computes pointwise Levenshtein similarities
112     between individual terms. Using this implementation to compute the similarity of all terms in
113     real-world dictionaries such as the English Wikipedia will take years.
114 
115     Parameters
116     ----------
117     dictionary : :class:`~gensim.corpora.dictionary.Dictionary`
118         A dictionary that specifies the considered terms.
119     alpha : float, optional
120         The multiplicative factor alpha defined by Charlet and Damnati (2017).
121     beta : float, optional
122         The exponential factor beta defined by Charlet and Damnati (2017).
123     threshold : float, optional
124         Only terms more similar than `threshold` are considered when retrieving
125         the most similar terms for a given term.
126 
127     See Also
128     --------
129     :func:`gensim.similarities.levenshtein.levsim`
130         The Levenshtein similarity.
131     :class:`~gensim.similarities.termsim.SparseTermSimilarityMatrix`
132         Build a term similarity matrix and compute the Soft Cosine Measure.
133 
134     """
135     def __init__(self, dictionary, alpha=1.8, beta=5.0, threshold=0.0):
136         self.dictionary = dictionary
137         self.alpha = alpha
138         self.beta = beta
139         self.threshold = threshold
140         super(LevenshteinSimilarityIndex, self).__init__()
141 
142     def most_similar(self, t1, topn=10):
143         similarities = (
144             (levsim(t1, t2, self.alpha, self.beta, self.threshold), t2)
145             for t2 in self.dictionary.values()
146             if t1 != t2
147         )
148         most_similar = (
149             (t2, similarity)
150             for (similarity, t2) in sorted(similarities, reverse=True)
151             if similarity > 0
152         )
153         return islice(most_similar, topn)
154
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

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 @@
             for (similarity, t2) in sorted(similarities, reverse=True)
             if similarity > 0
         )
-        return islice(most_similar, topn)
+        return itertools.islice(most_similar, int(topn))

{"golden_diff": "diff --git a/gensim/similarities/levenshtein.py b/gensim/similarities/levenshtein.py\n--- a/gensim/similarities/levenshtein.py\n+++ b/gensim/similarities/levenshtein.py\n@@ -8,7 +8,7 @@\n This module provides a namespace for functions that use the Levenshtein distance.\n \"\"\"\n \n-from itertools import islice\n+import itertools\n import logging\n from math import floor\n \n@@ -150,4 +150,4 @@\n             for (similarity, t2) in sorted(similarities, reverse=True)\n             if similarity > 0\n         )\n-        return islice(most_similar, topn)\n+        return itertools.islice(most_similar, int(topn))\n", "issue": "LeveshteinSimilarityIndex fails when called from SparseTermSimilarityMatrix\n<!--\r\n**IMPORTANT**:\r\n\r\n- 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.\r\n- Check [Recipes&FAQ](https://github.com/RaRe-Technologies/gensim/wiki/Recipes-&-FAQ) first for common answers.\r\n\r\nGithub bug reports that do not include relevant information and context will be closed without an answer. Thanks!\r\n-->\r\n\r\n#### Problem description\r\n\r\nWhen trying to build a SparseTermSimilarityMatrix using Levenshtein it fails.\r\n\r\n#### Steps/code/corpus to reproduce\r\n\r\n```python\r\nindex = LevenshteinSimilarityIndex(dictionary)\r\nSparseTermSimilarityMatrix(index, dictionary) # <- fails here\r\n```\r\n```\r\n---------------------------------------------------------------------------\r\nValueError                                Traceback (most recent call last)\r\n<ipython-input-59-c16b89564835> in <module>\r\n----> 1 similarity_matrix1 = SparseTermSimilarityMatrix(similarity_index1, dictionary)\r\n\r\n~/.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)\r\n    234                 for term, similarity in index.most_similar(t1, topn=num_rows)\r\n    235                 if term in dictionary.token2id\r\n--> 236             ] if num_rows > 0 else []\r\n    237 \r\n    238             if tfidf is None:\r\n\r\n~/.local/share/virtualenvs/pdftagger-LHy_2RHk/lib/python3.6/site-packages/gensim/similarities/levenshtein.py in most_similar(self, t1, topn)\r\n    151             if similarity > 0\r\n    152         )\r\n--> 153         return islice(most_similar, topn)\r\n\r\nValueError: Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize.\r\n```\r\n\r\n#### Versions\r\n\r\nLinux-4.18.0-25-generic-x86_64-with-debian-buster-sid\r\nPython 3.6.9 (default, Nov 18 2019, 15:20:23) \r\n[GCC 8.3.0]\r\nNumPy 1.17.4\r\nSciPy 1.3.3\r\ngensim 3.8.1\r\nFAST_VERSION 1\r\n\r\n\n", "before_files": [{"content": "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n#\n# Copyright (C) 2018 Vit Novotny <witiko@mail.muni.cz>\n# Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html\n\n\"\"\"\nThis module provides a namespace for functions that use the Levenshtein distance.\n\"\"\"\n\nfrom itertools import islice\nimport logging\nfrom math import floor\n\nfrom gensim.similarities.termsim import TermSimilarityIndex\n\nlogger = logging.getLogger(__name__)\n\n\ndef levdist(t1, t2, max_distance=float(\"inf\")):\n    \"\"\"Get the Levenshtein distance between two terms.\n\n    Return the Levenshtein distance between two terms. The distance is a\n    number between <1.0, inf>, higher is less similar.\n\n    Parameters\n    ----------\n    t1 : {bytes, str, unicode}\n        The first compared term.\n    t2 : {bytes, str, unicode}\n        The second compared term.\n    max_distance : {int, float}, optional\n        If you don't care about distances larger than a known threshold, a more\n        efficient code path can be taken. For terms that are clearly \"too far\n        apart\", we will not compute the distance exactly, but we will return\n        `max(len(t1), len(t2))` more quickly, meaning \"more than\n        `max_distance`\".\n        Default: always compute distance exactly, no threshold clipping.\n\n    Returns\n    -------\n    int\n        The Levenshtein distance between `t1` and `t2`.\n\n    \"\"\"\n    import Levenshtein\n\n    distance = Levenshtein.distance(t1, t2)\n    if distance > max_distance:\n        return max(len(t1), len(t2))\n    return distance\n\n\ndef levsim(t1, t2, alpha=1.8, beta=5.0, min_similarity=0.0):\n    \"\"\"Get the Levenshtein similarity between two terms.\n\n    Return the Levenshtein similarity between two terms. The similarity is a\n    number between <0.0, 1.0>, higher is more similar.\n\n    Parameters\n    ----------\n    t1 : {bytes, str, unicode}\n        The first compared term.\n    t2 : {bytes, str, unicode}\n        The second compared term.\n    alpha : float, optional\n        The multiplicative factor alpha defined by Charlet and Damnati (2017).\n    beta : float, optional\n        The exponential factor beta defined by Charlet and Damnati (2017).\n    min_similarity : {int, float}, optional\n        If you don't care about similarities smaller than a known threshold, a\n        more efficient code path can be taken. For terms that are clearly \"too\n        far apart\", we will not compute the distance exactly, but we will\n        return zero more quickly, meaning \"less than `min_similarity`\".\n        Default: always compute similarity exactly, no threshold clipping.\n\n    Returns\n    -------\n    float\n        The Levenshtein similarity between `t1` and `t2`.\n\n    Notes\n    -----\n    This notion of Levenshtein similarity was first defined in section 2.2 of\n    `Delphine Charlet and Geraldine Damnati, \"SimBow at SemEval-2017 Task 3:\n    Soft-Cosine Semantic Similarity between Questions for Community Question\n    Answering\", 2017 <http://www.aclweb.org/anthology/S/S17/S17-2051.pdf>`_.\n\n    \"\"\"\n    assert alpha >= 0\n    assert beta >= 0\n\n    max_lengths = max(len(t1), len(t2))\n    if max_lengths == 0:\n        return 1.0\n\n    min_similarity = float(max(min(min_similarity, 1.0), 0.0))\n    max_distance = int(floor(max_lengths * (1 - (min_similarity / alpha) ** (1 / beta))))\n    distance = levdist(t1, t2, max_distance)\n    similarity = alpha * (1 - distance * 1.0 / max_lengths)**beta\n    return similarity\n\n\nclass LevenshteinSimilarityIndex(TermSimilarityIndex):\n    \"\"\"\n    Computes Levenshtein similarities between terms and retrieves most similar\n    terms for a given term.\n\n    Notes\n    -----\n    This is a naive implementation that iteratively computes pointwise Levenshtein similarities\n    between individual terms. Using this implementation to compute the similarity of all terms in\n    real-world dictionaries such as the English Wikipedia will take years.\n\n    Parameters\n    ----------\n    dictionary : :class:`~gensim.corpora.dictionary.Dictionary`\n        A dictionary that specifies the considered terms.\n    alpha : float, optional\n        The multiplicative factor alpha defined by Charlet and Damnati (2017).\n    beta : float, optional\n        The exponential factor beta defined by Charlet and Damnati (2017).\n    threshold : float, optional\n        Only terms more similar than `threshold` are considered when retrieving\n        the most similar terms for a given term.\n\n    See Also\n    --------\n    :func:`gensim.similarities.levenshtein.levsim`\n        The Levenshtein similarity.\n    :class:`~gensim.similarities.termsim.SparseTermSimilarityMatrix`\n        Build a term similarity matrix and compute the Soft Cosine Measure.\n\n    \"\"\"\n    def __init__(self, dictionary, alpha=1.8, beta=5.0, threshold=0.0):\n        self.dictionary = dictionary\n        self.alpha = alpha\n        self.beta = beta\n        self.threshold = threshold\n        super(LevenshteinSimilarityIndex, self).__init__()\n\n    def most_similar(self, t1, topn=10):\n        similarities = (\n            (levsim(t1, t2, self.alpha, self.beta, self.threshold), t2)\n            for t2 in self.dictionary.values()\n            if t1 != t2\n        )\n        most_similar = (\n            (t2, similarity)\n            for (similarity, t2) in sorted(similarities, reverse=True)\n            if similarity > 0\n        )\n        return islice(most_similar, topn)\n", "path": "gensim/similarities/levenshtein.py"}], "after_files": [{"content": "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n#\n# Copyright (C) 2018 Vit Novotny <witiko@mail.muni.cz>\n# Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html\n\n\"\"\"\nThis module provides a namespace for functions that use the Levenshtein distance.\n\"\"\"\n\nimport itertools\nimport logging\nfrom math import floor\n\nfrom gensim.similarities.termsim import TermSimilarityIndex\n\nlogger = logging.getLogger(__name__)\n\n\ndef levdist(t1, t2, max_distance=float(\"inf\")):\n    \"\"\"Get the Levenshtein distance between two terms.\n\n    Return the Levenshtein distance between two terms. The distance is a\n    number between <1.0, inf>, higher is less similar.\n\n    Parameters\n    ----------\n    t1 : {bytes, str, unicode}\n        The first compared term.\n    t2 : {bytes, str, unicode}\n        The second compared term.\n    max_distance : {int, float}, optional\n        If you don't care about distances larger than a known threshold, a more\n        efficient code path can be taken. For terms that are clearly \"too far\n        apart\", we will not compute the distance exactly, but we will return\n        `max(len(t1), len(t2))` more quickly, meaning \"more than\n        `max_distance`\".\n        Default: always compute distance exactly, no threshold clipping.\n\n    Returns\n    -------\n    int\n        The Levenshtein distance between `t1` and `t2`.\n\n    \"\"\"\n    import Levenshtein\n\n    distance = Levenshtein.distance(t1, t2)\n    if distance > max_distance:\n        return max(len(t1), len(t2))\n    return distance\n\n\ndef levsim(t1, t2, alpha=1.8, beta=5.0, min_similarity=0.0):\n    \"\"\"Get the Levenshtein similarity between two terms.\n\n    Return the Levenshtein similarity between two terms. The similarity is a\n    number between <0.0, 1.0>, higher is more similar.\n\n    Parameters\n    ----------\n    t1 : {bytes, str, unicode}\n        The first compared term.\n    t2 : {bytes, str, unicode}\n        The second compared term.\n    alpha : float, optional\n        The multiplicative factor alpha defined by Charlet and Damnati (2017).\n    beta : float, optional\n        The exponential factor beta defined by Charlet and Damnati (2017).\n    min_similarity : {int, float}, optional\n        If you don't care about similarities smaller than a known threshold, a\n        more efficient code path can be taken. For terms that are clearly \"too\n        far apart\", we will not compute the distance exactly, but we will\n        return zero more quickly, meaning \"less than `min_similarity`\".\n        Default: always compute similarity exactly, no threshold clipping.\n\n    Returns\n    -------\n    float\n        The Levenshtein similarity between `t1` and `t2`.\n\n    Notes\n    -----\n    This notion of Levenshtein similarity was first defined in section 2.2 of\n    `Delphine Charlet and Geraldine Damnati, \"SimBow at SemEval-2017 Task 3:\n    Soft-Cosine Semantic Similarity between Questions for Community Question\n    Answering\", 2017 <http://www.aclweb.org/anthology/S/S17/S17-2051.pdf>`_.\n\n    \"\"\"\n    assert alpha >= 0\n    assert beta >= 0\n\n    max_lengths = max(len(t1), len(t2))\n    if max_lengths == 0:\n        return 1.0\n\n    min_similarity = float(max(min(min_similarity, 1.0), 0.0))\n    max_distance = int(floor(max_lengths * (1 - (min_similarity / alpha) ** (1 / beta))))\n    distance = levdist(t1, t2, max_distance)\n    similarity = alpha * (1 - distance * 1.0 / max_lengths)**beta\n    return similarity\n\n\nclass LevenshteinSimilarityIndex(TermSimilarityIndex):\n    \"\"\"\n    Computes Levenshtein similarities between terms and retrieves most similar\n    terms for a given term.\n\n    Notes\n    -----\n    This is a naive implementation that iteratively computes pointwise Levenshtein similarities\n    between individual terms. Using this implementation to compute the similarity of all terms in\n    real-world dictionaries such as the English Wikipedia will take years.\n\n    Parameters\n    ----------\n    dictionary : :class:`~gensim.corpora.dictionary.Dictionary`\n        A dictionary that specifies the considered terms.\n    alpha : float, optional\n        The multiplicative factor alpha defined by Charlet and Damnati (2017).\n    beta : float, optional\n        The exponential factor beta defined by Charlet and Damnati (2017).\n    threshold : float, optional\n        Only terms more similar than `threshold` are considered when retrieving\n        the most similar terms for a given term.\n\n    See Also\n    --------\n    :func:`gensim.similarities.levenshtein.levsim`\n        The Levenshtein similarity.\n    :class:`~gensim.similarities.termsim.SparseTermSimilarityMatrix`\n        Build a term similarity matrix and compute the Soft Cosine Measure.\n\n    \"\"\"\n    def __init__(self, dictionary, alpha=1.8, beta=5.0, threshold=0.0):\n        self.dictionary = dictionary\n        self.alpha = alpha\n        self.beta = beta\n        self.threshold = threshold\n        super(LevenshteinSimilarityIndex, self).__init__()\n\n    def most_similar(self, t1, topn=10):\n        similarities = (\n            (levsim(t1, t2, self.alpha, self.beta, self.threshold), t2)\n            for t2 in self.dictionary.values()\n            if t1 != t2\n        )\n        most_similar = (\n            (t2, similarity)\n            for (similarity, t2) in sorted(similarities, reverse=True)\n            if similarity > 0\n        )\n        return itertools.islice(most_similar, int(topn))\n", "path": "gensim/similarities/levenshtein.py"}]}