Repository: goodmami/wn

Issue:
Synset.relations() for some lexicons uses synset id as relation name
Compare the difference in the relation name for the `oewn` versus the `omw-fr` lexicons:

```pycon
>>> import wn
>>> wn.synsets('dog', pos='n', lexicon='oewn')[0].relations('hypernym')
{'hypernym': [Synset('oewn-02085998-n'), Synset('oewn-01320032-n')]}
>>> wn.synsets('chien', pos='n', lexicon='omw-fr')[0].relations('hypernym')
{'omw-fr-10753546-n': [Synset('omw-fr-10753546-n')]}
```

For OEWN, the relation name is `hypernym` while for `omw-fr` it is `omw-fr-10753546-n`. I suspect this is the case for relations obtained from expand lexicons.

Comments:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4a066dc..e61eab7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@
   `Wordnet` object ([#157], [#168])
 * `Sense.word()` now creates a `Word` properly linked to the same
   `Wordnet` object ([#157])
+* `Synset.relations()` uses the correct relation type for those
+  obtained from expand lexicons ([#169])
 
 
 ## [v0.9.1]
@@ -589,3 +591,4 @@ abandoned, but this is an entirely new codebase.
 [#156]: https://github.com/goodmami/wn/issues/156
 [#157]: https://github.com/goodmami/wn/issues/157
 [#168]: https://github.com/goodmami/wn/issues/168
+[#169]: https://github.com/goodmami/wn/issues/169
diff --git a/wn/_core.py b/wn/_core.py
index 0d89f42..b5c8246 100644
--- a/wn/_core.py
+++ b/wn/_core.py
@@ -693,10 +693,11 @@ class Synset(_Relatable):
 
         # first get relations from the current lexicon(s)
         if self._id != NON_ROWID:
-            relations = list(get_synset_relations({self._id}, args, lexids))
-            targets.extend((row[0], Synset(*row[2:], self._wordnet))
-                           for row in relations
-                           if row[5] in lexids)
+            targets.extend(
+                (row[0], Synset(*row[2:], self._wordnet))
+                for row in get_synset_relations({self._id}, args, lexids)
+                if row[5] in lexids
+            )
 
         # then attempt to expand via ILI
         if self._ili is not None and self._wordnet and self._wordnet._expanded_ids:
@@ -705,26 +706,31 @@ class Synset(_Relatable):
             # get expanded relation
             expss = find_synsets(ili=self._ili, lexicon_rowids=expids)
             rowids = {rowid for _, _, _, _, rowid in expss} - {self._id, NON_ROWID}
-            relations = list(get_synset_relations(rowids, args, expids))
-            ilis = {row[4] for row in relations} - {None}
+            relations: Dict[str, Set[str]] = {reltype: set() for reltype in args}
+            for rel_row in get_synset_relations(rowids, args, expids):
+                rel_type, ili = rel_row[0], rel_row[4]
+                if ili is not None:
+                    relations[rel_type].add(ili)
 
             # map back to target lexicons
             seen = {ss._id for _, ss in targets}
-            for row in get_synsets_for_ilis(ilis, lexicon_rowids=lexids):
-                if row[-1] not in seen:
-                    targets.append((row[0], Synset(*row, self._wordnet)))
+            for rel_type, ilis in relations.items():
+                for row in get_synsets_for_ilis(ilis, lexicon_rowids=lexids):
+                    if row[-1] not in seen:
+                        targets.append((rel_type, Synset(*row, self._wordnet)))
 
             # add empty synsets for ILIs without a target in lexids
-            unseen_ilis = ilis - {tgt._ili for _, tgt in targets}
-            for rel_row in relations:
-                if rel_row[4] in unseen_ilis:
+            seen_ilis = {tgt._ili for _, tgt in targets}
+            for rel_type, ilis in relations.items():
+                unseen_ilis = ilis - seen_ilis
+                for ili in unseen_ilis:
                     ss = Synset.empty(
                         id=_INFERRED_SYNSET,
-                        ili=rel_row[4],
+                        ili=ili,
                         _lexid=self._lexid,
                         _wordnet=self._wordnet
                     )
-                    targets.append((rel_row[0], ss))
+                    targets.append((rel_type, ss))
 
         return targets
 


Test patch:
diff --git a/tests/relations_test.py b/tests/relations_test.py
index 8a335d3..1295075 100644
--- a/tests/relations_test.py
+++ b/tests/relations_test.py
@@ -109,6 +109,16 @@ def test_extension_relations():
 
 @pytest.mark.usefixtures('mini_db_1_1')
 def test_sense_synset_issue_168():
+    # https://github.com/goodmami/wn/issues/168
     ja = wn.Wordnet(lexicon='test-ja', expand='')
     assert ja.synset('test-ja-0001-n').get_related() == []
     assert ja.sense('test-ja-情報-n-0001-01').synset().get_related() == []
+
+
+@pytest.mark.usefixtures('mini_db')
+def test_synset_relations_issue_169():
+    # https://github.com/goodmami/wn/issues/169
+    en = wn.Wordnet('test-en')
+    assert list(en.synset("test-en-0001-n").relations('hyponym')) == ['hyponym']
+    es = wn.Wordnet('test-es', expand='test-en')
+    assert list(es.synset("test-es-0001-n").relations('hyponym')) == ['hyponym']