-
Notifications
You must be signed in to change notification settings - Fork 0
/
hypernyms.py
34 lines (29 loc) · 1.29 KB
/
hypernyms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python
"""A function generating a noun-restricted taxonomy of hypernyms for the input string"""
import warnings
from nltk.corpus import wordnet as wn
warnings.filterwarnings("ignore") # discards warnings about redundant hypernym searches
def taxonomy(word: str) -> list:
all_hypernyms = []
if wn.synsets(
word, pos=wn.NOUN
): # checks if there are any synsets to begin with.
synsets = wn.synsets(word, pos=wn.NOUN)
for synset in synsets:
hypernym_list = []
hyper = (
lambda s: s.hypernyms()
) # a function to iterate up the hypernym taxonomy
if (
list(synset.closure(hyper, depth=1)) == synset.hypernyms()
): # validates closure over the hypernym hierarchy
hypernyms = list(
synset.closure(hyper)
) # generates a list of hypernym synsets
for synset in hypernyms:
for lemma in synset.lemmas():
hypernym_list.append(lemma.name().replace("_", " "))
all_hypernyms.append(hypernym_list)
return list(
set([item for sublist in all_hypernyms for item in sublist])
) # flattens the list and removes duplicate categories