6

Here is the code:

from pyemd import emd

print("sentence 1:")
print(input_document_lower[0])
print("sentence 2:")
print(input_document_lower[1])
print("similarity:")
model_w2v.wmdistance(input_document_lower[0], input_document_lower[1])

Here's the error:

sentence 1:
incorrect batch number printed primary label pbn
sentence 2:
unconfirmed oos met vial washing qualification sample per 
similarity:

ImportErrorTraceback (most recent call last)
<ipython-input-201-50af089a2354> in <module>()
      4 print(input_document_lower[1])
      5 print("similarity:")
----> 6 model_w2v.wmdistance(input_document_lower[0], input_document_lower[1])

C:\ProgramData\Anaconda2\lib\site-packages\gensim\models\word2vec.pyc in wmdistance(self, document1, document2)
   1308         Refer to the documentation for `gensim.models.KeyedVectors.wmdistance`
   1309         """
-> 1310         return self.wv.wmdistance(document1, document2)
   1311 
   1312     def most_similar_cosmul(self, positive=None, negative=None, topn=10):

C:\ProgramData\Anaconda2\lib\site-packages\gensim\models\keyedvectors.pyc in wmdistance(self, document1, document2)
    386 
    387         if not PYEMD_EXT:
--> 388             raise ImportError("Please install pyemd Python package to compute WMD.")
    389 
    390         # Remove out-of-vocabulary words.

ImportError: Please install pyemd Python package to compute WMD.

It is being installed properly so I really have no clue as to what is going wrong. Have any of your encountered this?

gojomo
  • 52,260
  • 14
  • 86
  • 115
madsthaks
  • 2,091
  • 6
  • 25
  • 46
  • 1
    Just to be documented. Note, that pyemd at this time supports python 3.6 or lower. I tried to install it on 3.7 and got the error Command "/Users/myuser/Documents/projects/scoring/venv/bin/python3.7 -u -c "import setuptools, tokenize;__file__='/private/var/folders/p4/dcysb3fj7y12vc89m5jymp_40000gn/T/pip-install-luqazulu/pyemd/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /private/var/folders/p4/dcysb3fj7y12vc89m5jymp_40000gn/T/pip-record-xde7ar9c/install-record.txt --single-version-ext – Yuri Tinyukov Jun 05 '19 at 08:34

7 Answers7

6

I had the same error, and for me the solution was to swap:

from gensim.similarities import WmdSimilarity
from pyemd import emd

into

from pyemd import emd
from gensim.similarities import WmdSimilarity

Don't ask me why it works.

wessel
  • 534
  • 5
  • 15
2

Solved this as following -

If your pip install is not working use conda install instead by using this command

conda install -c conda-forge pyemd

I believe that now you have successfully installed pyemd. Now when it comes to thowing error even after importing successfully do as following - import pyemd before any gensim module. I mean pyemd should be on top.

from pyemd import emd
from gensim.similarities import WmdSimilarity
from gensim.models.doc2vec import LabeledSentence
from gensim.models.doc2vec import TaggedLineDocument

I hope you are done with this concern :)

Niraj D Pandey
  • 151
  • 1
  • 7
0

I had some issues, I installed this package in my virtual env, but it doesn't work. Then I rebooted my computer, and after that it works.

Vladimir
  • 387
  • 4
  • 15
0

Add the line shown below

global PYEMD_EXT

just above the try:except code block

try:
    from pyemd import emd
    PYEMD_EXT = True
except ImportError:
    PYEMD_EXT = False
0

Go to the keyedvectors.py file and delete the try catch block.

This:

  try:
        from pyemd import emd
        PYEMD_EXT = True
    except ImportError:
        PYEMD_EXT = False

Remove the import error prompt

if not PYEMD_EXT:
            raise ImportError("Please install pyemd Python package to compute WMD.")

add this

from pyemd import emd

To know more about the bug check, https://github.com/RaRe-Technologies/gensim/pull/2229/files

Even after this code throws an error. One possible problem is you'll running in jupyter, so restart the whole karnel it will do!!!

vijay athithya
  • 1,529
  • 1
  • 10
  • 16
0

On Mac and Linux, the pip install pyemd works fine.

However, on Windows, you may experience an issue in installing this.

This error occurs due to

pyemd needing some C++ dependencies during build time.

This can be resolved by installing the Build Tools for Visual Studio 2019 as follows:

  • Download "Build Tools for Visual Studio" https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019

  • Run the .exe file and select to install C++ build tools

  • Check that among the suggested packages to install it is also selected "Windows 10 SDK" (the newest version is fine) as this is the critical dependency

  • Once the installation has finished reopen your PowerShell/Command Prompt and retry to install the library with the original pip instruction

Nayantara Jeyaraj
  • 2,624
  • 7
  • 34
  • 63
-1

Check the documentation of wmdistance method

Warnings -------- This method only works if pyemd <https://pypi.org/project/pyemd/>_ is installed.

If one of the documents have no words that exist in the vocab, `float('inf')` (i.e. infinity)
will be returned.

Raises
------
ImportError
    If `pyemd <https://pypi.org/project/pyemd/>`_  isn't installed.

so before using the wmdistance install package pyemd

pip install pyemd

manju h
  • 141
  • 2
  • 9