Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed pyemd import error, pythonic equality and typo #2228 #2240

Merged
merged 5 commits into from
Jan 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions gensim/models/keyedvectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,6 @@
except ImportError:
from Queue import Queue, Empty # noqa:F401

# If pyemd C extension is available, import it.
# If pyemd is attempted to be used, but isn't installed, ImportError will be raised in wmdistance
try:
from pyemd import emd
PYEMD_EXT = True
except (ImportError, ValueError):
PYEMD_EXT = False

from numpy import dot, float32 as REAL, empty, memmap as np_memmap, \
double, array, zeros, vstack, sqrt, newaxis, integer, \
ndarray, sum as np_sum, prod, argmax, divide as np_divide
Expand Down Expand Up @@ -752,8 +744,10 @@ def wmdistance(self, document1, document2):
If `pyemd <https://pypi.org/project/pyemd/>`_ isn't installed.

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

# If pyemd C extension is available, import it.
# If pyemd is attempted to be used, but isn't installed, ImportError will be raised in wmdistance
from pyemd import emd

# Remove out-of-vocabulary words.
len_pre_oov1 = len(document1)
Expand All @@ -765,7 +759,7 @@ def wmdistance(self, document1, document2):
if diff1 > 0 or diff2 > 0:
logger.info('Removed %d and %d OOV words from document 1 and 2 (respectively).', diff1, diff2)

if len(document1) == 0 or len(document2) == 0:
if not document1 or not document2:
logger.info(
"At least one of the documents had no words that were in the vocabulary. "
"Aborting (returning inf)."
Expand Down