diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fbee1495e..fdbc1e89f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ Changes ======= +0.13.3, 2016-09-26 +* Fixed issue #743 , In word2vec's n_similarity method if atleast one empty list is passed ZeroDivisionError is raised, added test cases in test/test_word2vec.py(@pranay360, #883) + 0.13.2, 2016-08-19 * export_phrases in Phrases model changed. Fixed issue #794 and added test cases in test/test_phrases.py(@AadityaJ, [#879](https://github.com/RaRe-Technologies/gensim/pull/879)) diff --git a/gensim/models/word2vec.py b/gensim/models/word2vec.py index bf7793524e..27a9829f42 100644 --- a/gensim/models/word2vec.py +++ b/gensim/models/word2vec.py @@ -1539,9 +1539,13 @@ def n_similarity(self, ws1, ws2): True """ + if not(len(ws1) and len(ws2)): + raise ZeroDivisionError('Atleast one of the passed list is empty.') v1 = [self[word] for word in ws1] v2 = [self[word] for word in ws2] - return dot(matutils.unitvec(array(v1).mean(axis=0)), matutils.unitvec(array(v2).mean(axis=0))) + return dot(matutils.unitvec(array(v1).mean(axis=0)), + matutils.unitvec(array(v2).mean(axis=0))) + def init_sims(self, replace=False): """ diff --git a/gensim/test/test_word2vec.py b/gensim/test/test_word2vec.py index 7e0368f13f..723ed3ad59 100644 --- a/gensim/test/test_word2vec.py +++ b/gensim/test/test_word2vec.py @@ -370,9 +370,12 @@ def testSimilarities(self): model = word2vec.Word2Vec(size=2, min_count=1, sg=0, hs=0, negative=2) model.build_vocab(sentences) model.train(sentences) - + self.assertTrue(model.n_similarity(['graph', 'trees'], ['trees', 'graph'])) self.assertTrue(model.n_similarity(['graph'], ['trees']) == model.similarity('graph', 'trees')) + self.assertRaises(ZeroDivisionError, model.n_similarity, ['graph', 'trees'], []) + self.assertRaises(ZeroDivisionError, model.n_similarity, [], ['graph', 'trees']) + self.assertRaises(ZeroDivisionError, model.n_similarity, [], []) def testSimilarBy(self): """Test word2vec similar_by_word and similar_by_vector."""