How to define semantic hierarchies / relationships when using NLTK?

I want to use NLTK and wordnet to understand the semantic connection between two words. For example, if I find β€œemployee” and β€œwaiter”, he returns something showing that this employee is more general than the waiter. Or for "employee" and "worker" it returns equal. Does anyone know how to do this?

0
source share
1 answer

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" 
+6
source

All Articles