Skip to content

Commit

Permalink
Add Sense.relation_map() and tests
Browse files Browse the repository at this point in the history
Previous commit only added the method for Synsets.

Fixes #167
Fixes #216
  • Loading branch information
goodmami committed Dec 4, 2024
1 parent e197dad commit 730a5ae
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
## [Unreleased][unreleased]

## Index

* Added `oewn:2024` ([#221])

## Added

* `Sense.relation_map()` method ([#216])
* `Synset.relation_map()` method ([#167], [#216])


## [v0.10.1]

Expand Down Expand Up @@ -681,6 +687,7 @@ abandoned, but this is an entirely new codebase.
[#155]: https://github.com/goodmami/wn/issues/155
[#156]: https://github.com/goodmami/wn/issues/156
[#157]: https://github.com/goodmami/wn/issues/157
[#167]: https://github.com/goodmami/wn/issues/167
[#168]: https://github.com/goodmami/wn/issues/168
[#169]: https://github.com/goodmami/wn/issues/169
[#177]: https://github.com/goodmami/wn/issues/177
Expand All @@ -696,4 +703,5 @@ abandoned, but this is an entirely new codebase.
[#213]: https://github.com/goodmami/wn/issues/213
[#214]: https://github.com/goodmami/wn/issues/214
[#215]: https://github.com/goodmami/wn/issues/215
[#216]: https://github.com/goodmami/wn/issues/216
[#221]: https://github.com/goodmami/wn/issues/221
2 changes: 2 additions & 0 deletions tests/data/mini-lmf-1.0.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ Spanish:
<Lemma partOfSpeech="v" writtenForm="illustrate" />
<Sense id="test-en-illustrate-v-0003-01" synset="test-en-0003-v" >
<SenseRelation relType="derivation" target="test-en-illustration-n-0002-01" />
<SenseRelation relType="other" target="test-en-illustration-n-0002-01" dc:type="result" />
<SenseRelation relType="other" target="test-en-illustration-n-0002-01" dc:type="event" />
</Sense>
<SyntacticBehaviour senses="test-en-illustrate-v-0003-01" subcategorizationFrame="Somebody ----s something" />
<SyntacticBehaviour senses="test-en-illustrate-v-0003-01" subcategorizationFrame="Something ----s something" />
Expand Down
34 changes: 34 additions & 0 deletions tests/relations_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,37 @@ def test_synset_relations_issue_169():
def test_synset_relations_issue_177():
# https://github.com/goodmami/wn/issues/177
assert 'hyponym' in wn.synset('test-es-0001-n').relations()


@pytest.mark.usefixtures('mini_db')
def test_sense_relation_map():
en = wn.Wordnet('test-en')
assert en.sense('test-en-information-n-0001-01').relation_map() == {}
relmap = en.sense('test-en-illustrate-v-0003-01').relation_map()
# only sense-sense relations by default
assert len(relmap) == 3
assert all(isinstance(tgt, wn.Sense) for tgt in relmap.values())
assert {rel.name for rel in relmap} == {'derivation', 'other'}
assert {rel.target_id for rel in relmap} == {'test-en-illustration-n-0002-01'}
# sense relations targets should always have same ids as resolved targets
assert all(rel.target_id == tgt.id for rel, tgt in relmap.items())


@pytest.mark.usefixtures('mini_db')
def test_synset_relation_map():
en = wn.Wordnet('test-en')
assert en.synset('test-en-0003-v').relation_map() == {}
relmap = en.synset('test-en-0002-n').relation_map()
assert len(relmap) == 2
assert {rel.name for rel in relmap} == {'hypernym', 'hyponym'}
assert {rel.target_id for rel in relmap} == {'test-en-0001-n', 'test-en-0004-n'}
# synset relation targets have same ids as resolved targets in same lexicon
assert all(rel.target_id == tgt.id for rel, tgt in relmap.items())

# interlingual synset relation targets show original target ids
es = wn.Wordnet('test-es', expand='test-en')
relmap = es.synset('test-es-0002-n').relation_map()
assert len(relmap) == 2
assert {rel.name for rel in relmap} == {'hypernym', 'hyponym'}
assert {rel.target_id for rel in relmap} == {'test-en-0001-n', 'test-en-0004-n'}
assert all(rel.target_id != tgt.id for rel, tgt in relmap.items())
3 changes: 3 additions & 0 deletions wn/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,9 @@ def get_related_synsets(self, *args: str) -> list[Synset]:
synset for _, synset in self._iter_sense_synset_relations(*args)
)

def relation_map(self) -> dict[SenseRelation, 'Sense']:
return dict(self._iter_sense_relations())

def _iter_sense_relations(self, *args: str) -> Iterator[tuple[SenseRelation, 'Sense']]:
iterable = get_sense_relations(self._id, args, self._get_lexicon_ids())
for relname, rellexid, relrowid, _, sid, eid, ssid, lexid, rowid in iterable:
Expand Down

0 comments on commit 730a5ae

Please sign in to comment.