How to get all hyponyms words / synset in python nltk and wordnet?

I have a list of all nouns in wordnet, now I want to leave only the words that are vehicles and delete the rest. How can I do it? Below is the pseudo code I want to make, but I don't know how to make it work.

for word in wordlist: if not "vehicle" in wn.synsets(word): wordlist.remove(word) 
+8
source share
2 answers
 from nltk.corpus import wordnet as wn vehicle = wn.synset('vehicle.n.01') typesOfVehicles = list(set([w for s in vehicle.closure(lambda s:s.hyponyms()) for w in s.lemma_names()])) 

This will give you all the unique words from each set, which is the hyponym of the noun "vehicle" (1st sense).

+10
source
 def get_hyponyms(synset): hyponyms = set() for hyponym in synset.hyponyms(): hyponyms |= set(get_hyponyms(hyponym)) return hyponyms | set(synset.hyponyms()) 
+7
source

All Articles