Reputation: 1423
I am using nltk here to post process classification labels(from imagenet) of my model. For instance, my model might put label 'black bear' on an image. How should I determine if 'black bear' is a kind of 'animal'(animal's hyponym) using wordnet?
I tried this method But the tricking part is that when I use the code below to get synset of 'black bear', I get an empty list! Thus I can't decide whether 'black bear' is a hyponym of 'animal'
from nltk.corpus import wordnet as wn
blackbear = wn.synsets('black bear')
Is there any solution for this problem? Thanks!
Upvotes: 3
Views: 638
Reputation: 122270
For multiword expressions, use underscores instead of spaces, i.e.
>>> from nltk.corpus import wordnet as wn
>>> wn.synsets('black bear')
[]
>>> wn.synsets('black_bear')
[Synset('asiatic_black_bear.n.01'), Synset('american_black_bear.n.01')]
And to get the hyper/hyponym from Determining Hypernym or Hyponym using wordnet nltk:
>>> bear = wn.synsets('bear', pos='n')[0]
>>> bear.definition()
u'massive plantigrade carnivorous or omnivorous mammals with long shaggy coats and strong claws'
>>> black_bear = wn.synsets('black_bear', pos='n')[0]
>>> black_bear.definition()
u'bear with a black coat living in central and eastern Asia'
>>> hypobear = set([i for i in bear.closure(lambda s:s.hyponyms())])
>>> hyperblackbear = set([i for i in black_bear.closure(lambda s:s.hypernyms())])
>>> black_bear in hypobear
True
>>> animal = wn.synsets('animal')[0]
>>> animal.definition()
u'a living organism characterized by voluntary movement'
>>> hypoanimal = set([i for i in animal.closure(lambda s:s.hyponyms())])
>>> black_bear in hypoanimal
True
>>> bear in hypoanimal
True
But do note that WordNet has limited coverage especially on multi-word expressions (MWEs).
Upvotes: 3