Skip to content

Commit

Permalink
Add paths tests and similarity tests; fix path bug
Browse files Browse the repository at this point in the history
The bug was that a fake root node was not inserted if there were no
hypernym paths. Now the root is the sole node of the sole path in that
case.

Fix #64; actually this was fixed a while ago, but now it's better tested
  • Loading branch information
goodmami committed Dec 15, 2020
1 parent 835af0f commit 09420c1
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* `Synset.shortest_path()` no longer includes starting node ([#63])
* `Synset.closure()`/`Sense.closure()` may take multiple relations
([#74])
* `Synset.hypernym_paths(simulate_root=True)` returns just the fake
root node if no paths were found

### Changed

Expand Down
64 changes: 64 additions & 0 deletions tests/paths_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

import pytest

import wn


@pytest.mark.usefixtures('mini_db')
def test_hypernym_paths():
information = wn.synsets('information')[0]
example = wn.synsets('example')[0]
sample = wn.synsets('sample')[0]
random_sample = wn.synsets('random sample')[0]
assert information.hypernym_paths() == []
assert example.hypernym_paths() == [[information]]
assert sample.hypernym_paths() == [[example, information]]
assert random_sample.hypernym_paths() == [[sample, example, information]]


@pytest.mark.usefixtures('mini_db')
def test_interlingual_hypernym_paths():
información = wn.synsets('información')[0]
ejemplo = wn.synsets('ejemplo')[0]
inferred = wn.Synset.empty('*INFERRED*')
muestra_aleatoria = wn.synsets('muestra aleatoria')[0]
assert información.hypernym_paths() == []
assert ejemplo.hypernym_paths() == [[información]]
assert muestra_aleatoria.hypernym_paths() == [[inferred, ejemplo, información]]


@pytest.mark.usefixtures('mini_db')
def test_shortest_path():
information = wn.synsets('information')[0]
example = wn.synsets('example')[0]
sample = wn.synsets('sample')[0]
random_sample = wn.synsets('random sample')[0]
datum = wn.synsets('datum')[0]
exemplify = wn.synsets('exemplify')[0]
inferred_root = wn.Synset.empty('*INFERRED*')
assert information.shortest_path(information) == []
assert information.shortest_path(datum) == [datum]
assert information.shortest_path(sample) == [example, sample]
assert sample.shortest_path(information) == [example, information]
assert random_sample.shortest_path(datum) == [sample, example, information, datum]
with pytest.raises(wn.Error):
example.shortest_path(exemplify)
assert example.shortest_path(exemplify, simulate_root=True) == [
information, inferred_root, exemplify
]


@pytest.mark.usefixtures('mini_db')
def test_min_depth():
assert wn.synsets('information')[0].min_depth() == 0
assert wn.synsets('example')[0].min_depth() == 1
assert wn.synsets('sample')[0].min_depth() == 2
assert wn.synsets('random sample')[0].min_depth() == 3


@pytest.mark.usefixtures('mini_db')
def test_max_depth():
assert wn.synsets('information')[0].max_depth() == 0
assert wn.synsets('example')[0].max_depth() == 1
assert wn.synsets('sample')[0].max_depth() == 2
assert wn.synsets('random sample')[0].max_depth() == 3
37 changes: 37 additions & 0 deletions tests/similarity_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

import pytest

import wn
from wn import similarity as sim


@pytest.mark.usefixtures('mini_db')
def test_path():
information = wn.synsets('information')[0]
example = wn.synsets('example')[0]
sample = wn.synsets('sample')[0]
random_sample = wn.synsets('random sample')[0]
datum = wn.synsets('datum')[0]
exemplify = wn.synsets('exemplify')[0]
assert sim.path(information, information) == 1/1
assert sim.path(information, example) == 1/2
assert sim.path(information, sample) == 1/3
assert sim.path(information, random_sample) == 1/4
assert sim.path(random_sample, datum) == 1/5
assert sim.path(example, exemplify) == 1/4


@pytest.mark.usefixtures('mini_db')
def test_wup():
information = wn.synsets('information')[0]
example = wn.synsets('example')[0]
sample = wn.synsets('sample')[0]
random_sample = wn.synsets('random sample')[0]
datum = wn.synsets('datum')[0]
exemplify = wn.synsets('exemplify')[0]
assert sim.wup(information, information) == (2*1) / (0+0+2*1)
assert sim.wup(information, example) == (2*1) / (0+1+2*1)
assert sim.wup(information, sample) == (2*1) / (0+2+2*1)
assert sim.wup(information, random_sample) == (2*1) / (0+3+2*1)
assert sim.wup(random_sample, datum) == (2*1) / (3+1+2*1)
assert sim.wup(example, exemplify) == (2*1) / (2+1+2*1)
2 changes: 1 addition & 1 deletion wn/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ def _hypernym_paths(
root = Synset.empty(
id=_FAKE_ROOT, _lexid=self._lexid, _wordnet=self._wordnet
)
paths = [path + [root] for path in paths]
paths = [path + [root] for path in paths] or [[root]]
return paths

def hypernym_paths(self, simulate_root: bool = False) -> List[List['Synset']]:
Expand Down

0 comments on commit 09420c1

Please sign in to comment.