Skip to content

Commit

Permalink
Synset path_similarity commutativity (nltk#2650)
Browse files Browse the repository at this point in the history
* add test

* need_root

* update

* fix tab

* fix tab

* wup similarity
  • Loading branch information
snrore authored Jan 2, 2021
1 parent 0c5288c commit b005e7a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 5 additions & 3 deletions nltk/corpus/reader/wordnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ def path_similarity(self, other, verbose=False, simulate_root=True):
"""

distance = self.shortest_path_distance(
other, simulate_root=simulate_root and self._needs_root()
other, simulate_root=simulate_root and (self._needs_root() or other._needs_root())
)
if distance is None or distance < 0:
return None
Expand Down Expand Up @@ -934,13 +934,15 @@ def wup_similarity(self, other, verbose=False, simulate_root=True):
the two senses can be found, None is returned.
"""
need_root = self._needs_root() or other._needs_root()

need_root = self._needs_root()
# Note that to preserve behavior from NLTK2 we set use_min_depth=True
# It is possible that more accurate results could be obtained by
# removing this setting and it should be tested later on
subsumers = self.lowest_common_hypernyms(
other, simulate_root=simulate_root and need_root, use_min_depth=True
other,
simulate_root=simulate_root and need_root,
use_min_depth=True
)

# If no LCS was found return None
Expand Down
4 changes: 4 additions & 0 deletions nltk/test/unit/test_wordnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,16 @@ def test_wordnet_similarities(self):
# Path based similarities.
self.assertAlmostEqual(S('cat.n.01').path_similarity(S('cat.n.01')), 1.0)
self.assertAlmostEqual(S('dog.n.01').path_similarity(S('cat.n.01')), 0.2)
self.assertAlmostEqual(S('car.n.01').path_similarity(S('automobile.v.01')),
S('automobile.v.01').path_similarity(S('car.n.01')))
self.assertAlmostEqual(
S('dog.n.01').lch_similarity(S('cat.n.01')), 2.028, places=3
)
self.assertAlmostEqual(
S('dog.n.01').wup_similarity(S('cat.n.01')), 0.8571, places=3
)
self.assertAlmostEqual(S('car.n.01').wup_similarity(S('automobile.v.01')),
S('automobile.v.01').wup_similarity(S('car.n.01')))
# Information Content similarities.
brown_ic = wnic.ic('ic-brown.dat')
self.assertAlmostEqual(
Expand Down

0 comments on commit b005e7a

Please sign in to comment.