-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Fix Issue #743: Added exception handling for n_similarity method #882
Changes from all commits
cfae6b5
56743ac
1de8f4c
ad19f72
5bc70b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1539,9 +1539,15 @@ def n_similarity(self, ws1, ws2): | |
True | ||
|
||
""" | ||
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))) | ||
if len(ws1) > 0 and len(ws2) > 0: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Python, emptiness of collections is tested simply with |
||
v1 = [self[word] for word in ws1] | ||
v2 = [self[word] for word in ws2] | ||
return dot(matutils.unitvec(array(v1).mean(axis=0)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't use vertical indent; please use hanging indent (see PEP8 example). |
||
matutils.unitvec(array(v2).mean(axis=0))) | ||
else: | ||
raise ZeroDivisionError('Atleast one of the passed list is empty.') | ||
return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
|
||
def init_sims(self, replace=False): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No trailing whitespace. |
||
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([], [])) | ||
self.assertRaises(ZeroDivisionError,model.n_similarity([], ['graph', 'trees'])) | ||
|
||
def testSimilarBy(self): | ||
"""Test word2vec similar_by_word and similar_by_vector.""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look correct -- past release dates cannot change retroactively.