Skip to content

Commit

Permalink
Merge cfe657d into aa061e5
Browse files Browse the repository at this point in the history
  • Loading branch information
ophelielacroix committed Jan 7, 2021
2 parents aa061e5 + cfe657d commit f95a525
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
22 changes: 20 additions & 2 deletions danlp/datasets/dannet.py
Expand Up @@ -140,6 +140,24 @@ def hyponyms(self, word, pos=None):

return hypernyms

def domains(self, word, pos=None):
"""
Returns the domains of `word`.
:param word: text
:param pos: (list of) part of speech tag(s) (in "Noun", "Verb", "Adjective")
:return: list of domains
"""

word_synset_ids = self._synset_ids(word, pos)
dom_synset_ids = self.relations[self.relations['synset_id'].isin(word_synset_ids) & (self.relations['relation']=='domain')]['value'].tolist()
dom_synset_ids = [val for val in dom_synset_ids if val.isdigit()]
domains_ids = self.wordsenses[self.wordsenses['synset_id'].isin(dom_synset_ids)]['word_id'].tolist()
domains = self.words[self.words['word_id'].isin(domains_ids)]['form'].tolist()

return domains

def wordnet_relations(self, word, pos=None, eurowordnet=True):
"""
Returns the name of the relations `word` is associated with.
Expand Down Expand Up @@ -185,14 +203,14 @@ def _synset_ids(self, word, pos=None):

def _word_from_id(self, word_id):

assert(type(word_id) == int or (type(word_id) == str and word_id.is_digit()))
assert(type(word_id) == int or (type(word_id) == str and word_id.isdigit()))
word_id = str(word_id)

return self.words[self.words['word_id'] == word_id]['form'].tolist()

def _synset_from_id(self, synset_id):

assert(type(synset_id) == int or (type(synset_id) == str and synset_id.is_digit()))
assert(type(synset_id) == int or (type(synset_id) == str and synset_id.isdigit()))
synset_id = str(synset_id)

synset_labels = self.synsets[self.synsets['synset_id'] == synset_id]['label'].tolist()
Expand Down
5 changes: 4 additions & 1 deletion docs/docs/datasets.md
Expand Up @@ -176,7 +176,7 @@ dannet = DanNet()
words, wordsenses, relations, synsets = dannet.load_with_pandas()
```

We also provide helper functions to search for synonyms, hyperonyms and hyponyms through the databases.
We also provide helper functions to search for synonyms, hyperonyms, hyponyms and domains through the databases.
Once you have downloaded the DanNet wrapper, you can use the following features:

```python
Expand All @@ -191,6 +191,9 @@ dannet.hypernyms(word)
# hyponyms
dannet.hyponyms(word)
""" ['hærmyre', 'skovmyre', 'pissemyre', 'tissemyre'] """
# domains
dannet.domains(word)
""" ['zoologi'] """
# meanings
dannet.meanings(word)
""" ['ca. 1 cm langt, årevinget insekt med en kraftig in ... (Brug: "Myrer på terrassen, og andre steder udendørs, kan hurtigt blive meget generende")'] """
Expand Down
1 change: 1 addition & 0 deletions tests/test_datasets.py
Expand Up @@ -176,6 +176,7 @@ def test_dannet(self):
self.assertEqual(dannet.synonyms('kat'), ['missekat', 'mis'])
self.assertEqual(dannet.hypernyms('myre', pos=['Noun']), ['årevingede insekter'])
self.assertEqual(dannet.hyponyms('myre', pos='Noun'), ['hærmyre', 'skovmyre', 'pissemyre', 'tissemyre'])
self.assertEqual(dannet.domains('myre', pos='Noun'), ['zoologi'])
self.assertEqual(dannet.pos('myre'), ['Noun'])
self.assertEqual(dannet.meanings('myre'), ['ca. 1 cm langt, årevinget insekt med en kraftig in ... (Brug: "Myrer på terrassen, og andre steder udendørs, kan hurtigt blive meget generende")'])
self.assertEqual(dannet.wordnet_relations('kat'), {'domain', 'has_holo_part', 'eq_has_synonym', 'has_hyperonym', 'role_agent', 'used_for', 'has_mero_part'})
Expand Down

0 comments on commit f95a525

Please sign in to comment.