Search code examples
pythonwordnet

Retrieve all words related to one WordNet


I want to get all the words related to 'food' with WordNet

import nltk
from nltk.corpus import wordnet as wn
food = wn.synsets('food')

for synset in food:
   for lemma in synset.lemmas():
       print lemma.name()

With this code I got this :

food
nutrient
food
solid_food
food
food_for_thought
intellectual_nourishment

What I'm trying to achieve is something like this

food
-> solid_food
-> liquid_food
-> powder_food

And something that goes on and on recursively like
solid_food
-> vegetables
-> meat ...

In short I'm trying to access the hierarchy of Wordnet from one word : 'food'.
How can I do this, any idea ?


Solution

  • You can explore the hierarchy by following the relation Hyponymy that take you from the general term to the specific term:

    Hyponymy shows the relationship between the more general terms (hypernyms) and the more specific instances of it (hyponyms).

    ref

    >>>  food[1].hyponyms()
    [Synset('fish.n.02'), Synset('slop.n.04'), Synset('coconut.n.01'), 
    Synset('baked_goods.n.01'), Synset('yogurt.n.01'), Synset('breakfast_food.n.01'), Synset('seafood.n.01'), Synset('cheese.n.01'), Synset('pasta.n.02'), Synset('meat.n.01'), Synset('leftovers.n.01'), Synset('butter.n.01'), Synset('produce.n.01'), Synset('convenience_food.n.01'), Synset('fresh_food.n.01'), Synset('junk_food.n.01'), Synset('dika_bread.n.01'), Synset('loaf.n.02'), Synset('chocolate.n.02'), Synset('health_food.n.01')]
    

    Keep using synonyms too.