Wordnet lch_similarity method raises a ZeroDivisionError when synset is compared with itself #301

Closed
dan-blanchard opened this Issue Oct 8, 2012 · 2 comments

Comments

Projects
None yet
3 participants
@dan-blanchard
Contributor

dan-blanchard commented Oct 8, 2012

The current implementation of Leacock & Chodorow's WordNet similarity metric does not correctly handle comparing synsets with themselves. According to the method's docstring, "If a Synset is compared with itself, the maximum score is returned, which varies depending on the taxonomy depth.", but currently it will just raise a ZeroDivisionError.

For example:

>>> from nltk.corpus import wordnet
>>> tardy_synsets = wordnet.synsets('tardy')
>>> tardy_synsets
[Synset('belated.s.01')]
>>> tardy_synsets[0].lch_similarity(tardy_synsets[0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/python/2.7/lib/python2.7/site-packages/nltk/corpus/reader/wordnet.py", line 650, in lch_similarity
    return -math.log((distance + 1) / (2.0 * depth))
ZeroDivisionError: float division by zero
@michelleful

This comment has been minimized.

Show comment
Hide comment
@michelleful

michelleful Jun 25, 2013

Contributor

This should be resolved with pull request #421. The issue is not, strictly speaking, with comparing a synset to itself, but when that synset additionally has no hypernyms, as is the case with "tardy". With a synset that does have hypernyms, the function behaves appropriately.

>>> from nltk.corpus import wordnet
>>> cow_synsets = wordnet.synsets('cow')
>>> cow_synsets[0].lch_similarity(cow_synsets[0])
3.6375861597263857

With the change in pull request #421, the function returns None when the synset has zero hypernyms.

Contributor

michelleful commented Jun 25, 2013

This should be resolved with pull request #421. The issue is not, strictly speaking, with comparing a synset to itself, but when that synset additionally has no hypernyms, as is the case with "tardy". With a synset that does have hypernyms, the function behaves appropriately.

>>> from nltk.corpus import wordnet
>>> cow_synsets = wordnet.synsets('cow')
>>> cow_synsets[0].lch_similarity(cow_synsets[0])
3.6375861597263857

With the change in pull request #421, the function returns None when the synset has zero hypernyms.

@stevenbird

This comment has been minimized.

Show comment
Hide comment
@stevenbird

stevenbird Jun 25, 2013

Member

Yes, and here's what happens with "tardy".

>>> from nltk.corpus import wordnet
>>> tardy_synsets = wordnet.synsets('tardy')
>>> tardy_synsets[0].hypernyms()
[]
>>> print tardy_synsets[0].lch_similarity(tardy_synsets[0])
None
Member

stevenbird commented Jun 25, 2013

Yes, and here's what happens with "tardy".

>>> from nltk.corpus import wordnet
>>> tardy_synsets = wordnet.synsets('tardy')
>>> tardy_synsets[0].hypernyms()
[]
>>> print tardy_synsets[0].lch_similarity(tardy_synsets[0])
None

@stevenbird stevenbird closed this Jun 25, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment