First, you need to solve the problem of getting words in lemmas, and then in Synsets, i.e. how can you identify sync from word?
word => lemma => lemma.pos.sense => synset Waiters => waiter => 'waiter.n.01' => wn.Synset('waiter.n.01')
So, let's say you are already dealing with the above problem and have reached the highest waiter , then you can continue to compare synsets. Note that a word can have many synsets
from nltk.corpus import wordnet as wn waiter = wn.Synset('waiter.n.01') employee = wn.Synset('employee.n.01') all_hyponyms_of_waiter = list(set([w.replace("_"," ") for s in waiter.closure(lambda s:s.hyponyms()) for w in s.lemma_names])) all_hyponyms_of_employee = list(set([w.replace("_"," ") for s in employee.closure(lambda s:s.hyponyms()) for w in s.lemma_names])) if 'waiter' in all_hyponyms_of_employee: print 'employee more general than waiter' elif 'employee' in all_hyponyms_of_waiter: print 'waiter more general than employee' else: print "The SUMO ontology used in wordnet just doesn't have employee or waiter under the same tree"
source share