pythonnltkwordnet

How can I find the distance from a noun to its root node in word net?


I want to find how specific a word in my sentence is. So for doing that the assumption that I have taken is the lower a word is in wordnet's hypernym hierarchy it is going to be more specific. But I am not able to write a code that is going to give the number of nodes between a noun word and its root hypernym which is 'entity.n.01' and I am trying to do that using wordnet of nltk. Please help me in this regard.

Any help would be appreciated.


Solution

  • Try this...

    from nltk.corpus import wordnet as wn
    ss = wn.synset('car.n.01')
    print min([len(path) for path in ss.hypernym_paths()])
    

    hypernym_paths() gives you a list of the possible paths back to the root, which for a noun is entity.n.01.