Repository: piskvorky/gensim

Issue:
Documentation may have code error!
<!--
**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

[Similarity Queries](https://radimrehurek.com/gensim/auto_examples/core/run_similarity_queries.html#sphx-glr-auto-examples-core-run-similarity-queries-py)--Part--`Performing queries`
I think there is a code error. Document id error. Please see the code below.


### code

```python
sims = sorted(enumerate(sims), key=lambda item: -item[1])
for i, s in enumerate(sims):
    print(s, documents[i])  # s[0] is the document id, the 'i' is not.
```
corrected code should be
```python
sims = sorted(enumerate(sims), key=lambda item: -item[1])
for i, s in enumerate(sims):
    print(s, documents[s[0]])
```



Discussion:
The tutorial is correct – documents are identified by their position in the corpus, so in this case `i`.
Reopening because the code does look wonky. Sorry I missed this.

@mpenkov I believe the code should be

```python
# Sort documents by decreasing similarity.
sims = sorted(enumerate(sims), key=lambda item: -item[1])
for doc_position, doc_score in sims:
    print(doc_score, documents[doc_position])
```

The second enumeration was too convoluted, and also wrong. WDYT?
I agree, I think the second enumeration is erroneous.

@ChunyuPCY May I interest you in making a PR? ;)
Another user bit by this: https://groups.google.com/forum/#!topic/gensim/31RBvOeKiq4

@ChunyuPCY can you open a PR?

Resolving patch:
diff --git a/docs/src/auto_examples/core/run_similarity_queries.py b/docs/src/auto_examples/core/run_similarity_queries.py
--- a/docs/src/auto_examples/core/run_similarity_queries.py
+++ b/docs/src/auto_examples/core/run_similarity_queries.py
@@ -152,8 +152,8 @@
 # order, and obtain the final answer to the query `"Human computer interaction"`:
 
 sims = sorted(enumerate(sims), key=lambda item: -item[1])
-for i, s in enumerate(sims):
-    print(s, documents[i])
+for doc_position, doc_score in sims:
+    print(doc_score, documents[doc_position])
 
 ###############################################################################
 # The thing to note here is that documents no. 2 (``"The EPS user interface management system"``)
diff --git a/docs/src/gallery/core/run_similarity_queries.py b/docs/src/gallery/core/run_similarity_queries.py
--- a/docs/src/gallery/core/run_similarity_queries.py
+++ b/docs/src/gallery/core/run_similarity_queries.py
@@ -152,8 +152,8 @@
 # order, and obtain the final answer to the query `"Human computer interaction"`:
 
 sims = sorted(enumerate(sims), key=lambda item: -item[1])
-for i, s in enumerate(sims):
-    print(s, documents[i])
+for doc_position, doc_score in sims:
+    print(doc_score, documents[doc_position])
 
 ###############################################################################
 # The thing to note here is that documents no. 2 (``"The EPS user interface management system"``)


Test patch: