Skip to content

Commit

Permalink
Improve KeyedVector.wmdistance (#2326)
Browse files Browse the repository at this point in the history
* Improve wmdistance

* reformat
  • Loading branch information
horpto authored and menshikh-iv committed Jan 11, 2019
1 parent 380b8bb commit 3a7760a
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions gensim/models/keyedvectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,11 +782,15 @@ def wmdistance(self, document1, document2):
# Compute distance matrix.
distance_matrix = zeros((vocab_len, vocab_len), dtype=double)
for i, t1 in dictionary.items():
if t1 not in docset1:
continue

for j, t2 in dictionary.items():
if t1 not in docset1 or t2 not in docset2:
if t2 not in docset2 or distance_matrix[i, j] != 0.0:
continue

# Compute Euclidean distance between word vectors.
distance_matrix[i, j] = sqrt(np_sum((self[t1] - self[t2])**2))
distance_matrix[i, j] = distance_matrix[j, i] = sqrt(np_sum((self[t1] - self[t2])**2))

if np_sum(distance_matrix) == 0.0:
# `emd` gets stuck if the distance matrix contains only zeros.
Expand Down

0 comments on commit 3a7760a

Please sign in to comment.