Repository: piskvorky/gensim

Issue:
Possible bug in WmdSimilarity
<!--
**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

I wanted to check if WmdSimilarity is any better (it should not be) from multiprocessing wmdistance, but it did not work at all. 
So, I made a little example and the result was exactly the same: AttributeError: 'WmdSimilarity' object has no attribute 'w2v_model'


#### Steps/code/corpus to reproduce

```python
from gensim.models import Word2Vec
from gensim.similarities import WmdSimilarity

a = "Trump’s decision not to join the Trans-Pacific Partnership (TPP) came as little surprise. During his election campaign he railed against international trade deals, blaming them for job losses and focusing anger in the industrial heartland. Obama had argued that this deal would provide an effective counterweight to China in the region."
b = "Trump's decision to withdraw the US from TPP is also a first step in the administration's efforts to amass a governing coalition to push the new President's agenda, one that includes the blue-collar workers who defected from Democrats and flocked to Trump's candidacy in November."
c = "Although the Trans-Pacific Partnership had not been approved by Congress, Mr. Trump’s decision to withdraw not only doomed former President Barack Obama’s signature trade achievement, but also carried broad geopolitical implications in a fast-growing region. The deal, which was to link a dozen nations from Canada and Chile to Australia and Japan in a complex web of trade rules, was sold as a way to permanently tie the United States to East Asia and create an economic bulwark against a rising China."

def w2v(corpus):
    w2v_model = Word2Vec(corpus, alpha=0.025, vector_size=50,
         window=5, min_count=1, workers=2, epochs=50, sg = 0)
    return w2v_model

corpus = "\n".join([a,b,c])
model = w2v(corpus)
print(model, type(model))
print(WmdSimilarity([a,b,c], model))
```

RESULTS:

```
Word2Vec(vocab=47, vector_size=50, alpha=0.025) <class 'gensim.models.word2vec.Word2Vec'>
Traceback (most recent call last):
  File "/home/tedo/diagnose/proba.py", line 18, in <module>
    print(WmdSimilarity([a,b,c], model))
  File "/usr/local/lib/python3.9/dist-packages/gensim/similarities/docsim.py", line 1099, in __str__
    return "%s<%i docs, %i features>" % (self.__class__.__name__, len(self), self.w2v_model.wv.syn0.shape[1])
AttributeError: 'WmdSimilarity' object has no attribute 'w2v_model'
```

#### Versions

Please provide the output of:

```python
import platform; print(platform.platform())
import sys; print("Python", sys.version)
import struct; print("Bits", 8 * struct.calcsize("P"))
import numpy; print("NumPy", numpy.__version__)
import scipy; print("SciPy", scipy.__version__)
import gensim; print("gensim", gensim.__version__)
from gensim.models import word2vec;print("FAST_VERSION", word2vec.FAST_VERSION)
```
Linux-5.10.0-9-amd64-x86_64-with-glibc2.31
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110]
Bits 64
NumPy 1.21.2
SciPy 1.7.1
gensim 4.1.1
FAST_VERSION 1


Discussion:
Thanks for reporting Tedo!

Seems like an omission during the WmdDistance refactoring in #2698, where `self.w2v_model` was renamed to `self.wv`. And `__str__` is not covered by CI tests so this wasn't caught automatically.
@tvrbanec Thank you for reporting this. Are you interested in making a PR to fix the problem?
@mpenkov I'd like to, but I do not know how to use Github. It has not come to my agenda yet. :(
@mpenkov  Hi! I am a new user of gensim and I am eager to contribute to the community. Do you still need people to fix this problem? 

My intended solution is to fix the self.__str__() function by changing self.w2v_model.wv.syn0.shape[1] into self.wv.wv.syn0.shape[1], something like this. I will submit a PR solving this problem.

Thank you so much! 
Sure, @DingQK , just make sure you have tests that demonstrate the correctness of your solution

Resolving patch:
diff --git a/gensim/similarities/docsim.py b/gensim/similarities/docsim.py
--- a/gensim/similarities/docsim.py
+++ b/gensim/similarities/docsim.py
@@ -1015,7 +1015,7 @@ class WmdSimilarity(interfaces.SimilarityABC):
         >>>
         >>> model = Word2Vec(common_texts, vector_size=20, min_count=1)  # train word-vectors
         >>>
-        >>> index = WmdSimilarity(common_texts, model)
+        >>> index = WmdSimilarity(common_texts, model.wv)
         >>> # Make query.
         >>> query = ['trees']
         >>> sims = index[query]
@@ -1096,7 +1096,7 @@ def get_similarities(self, query):
         return result
 
     def __str__(self):
-        return "%s<%i docs, %i features>" % (self.__class__.__name__, len(self), self.w2v_model.wv.syn0.shape[1])
+        return "%s<%i docs, %i features>" % (self.__class__.__name__, len(self), self.wv.vectors.shape[1])
 
 
 class SparseMatrixSimilarity(interfaces.SimilarityABC):


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
@@ -373,6 +373,11 @@ def test_iter(self):
             self.assertTrue(numpy.alltrue(sims >= 0.0))
             self.assertTrue(numpy.alltrue(sims <= 1.0))
 
+    @unittest.skipIf(PYEMD_EXT is False, "pyemd not installed")
+    def test_str(self):
+        index = self.cls(TEXTS, self.w2v_model)
+        self.assertTrue(str(index))
+
 
 class TestSoftCosineSimilarity(_TestSimilarityABC):
     def setUp(self):