10

I have a list of all the nouns in wordnet now i want to leave only words which are vehicles and remove the rest. How do i do it? Below is the pseudo-code i want to make but i do not know how to make it work

for word in wordlist:
  if not "vehicle" in wn.synsets(word):
    wordlist.remove(word)
alvas
  • 115,346
  • 109
  • 446
  • 738
watisit
  • 193
  • 1
  • 2
  • 10

2 Answers2

14
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 every synset that is a hyponym of the noun "vehicle" (1st sense).

Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
Jared
  • 25,627
  • 7
  • 56
  • 61
  • But when i try to narrow it further, i get this error Traceback (most recent call last): File "D:...\test.py", line 10, in if "car" in word: TypeError: argument of type 'Synset' is not iterable – watisit Mar 11 '13 at 05:46
  • @Jared, very elegant answer but there's a `gotcha` when the `Synset.closure(lambda s:s.hyponyms()` goes to an infinite loop. Try `wn.synset('restrain.v.01').closure(lambda s:s.hyponyms()` – alvas Apr 07 '13 at 22:00
  • 1
    I get `TypeError: 'method' object is not iterable` with this method. – Stefan D Nov 09 '15 at 16:06
  • 1
    @StefanD This is a relatively old answer and probably does not work with the the newer versions of NLTK. If you found an alternative solution that works in newer versions, post an answer or feel free to update this answer! – Jared Nov 11 '15 at 00:30
  • User Easton commented in an answer that `lemma_names` is a method and thus parentheses should be added. Don't know if that's correct but I'm leaving it here for review by those familiar with this module. – Tom Zych Jan 22 '16 at 11:20
9
def get_hyponyms(synset):
    hyponyms = set()
    for hyponym in synset.hyponyms():
        hyponyms |= set(get_hyponyms(hyponym))
    return hyponyms | set(synset.hyponyms())
Stefan D
  • 1,229
  • 2
  • 15
  • 29