From b005e7af5f671b31b45229d92208572c8d378c38 Mon Sep 17 00:00:00 2001 From: snrore <35712574+snrore@users.noreply.github.com> Date: Fri, 1 Jan 2021 19:33:13 -0500 Subject: [PATCH] Synset path_similarity commutativity (#2650) * add test * need_root * update * fix tab * fix tab * wup similarity --- nltk/corpus/reader/wordnet.py | 8 +++++--- nltk/test/unit/test_wordnet.py | 4 ++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/nltk/corpus/reader/wordnet.py b/nltk/corpus/reader/wordnet.py index 7d20e6f10c..1dab0eb729 100644 --- a/nltk/corpus/reader/wordnet.py +++ b/nltk/corpus/reader/wordnet.py @@ -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 @@ -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 diff --git a/nltk/test/unit/test_wordnet.py b/nltk/test/unit/test_wordnet.py index 97d5d4d92f..b58c845b05 100644 --- a/nltk/test/unit/test_wordnet.py +++ b/nltk/test/unit/test_wordnet.py @@ -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(