Search code examples
pythonnltkwordnet

How compare wordnet Synsets with another word?


I need to check if some word its sysnset of another words.. for example : cat and dog .. first i need to find synsets of cat by this code:

list= wn.synsets('cat')

then the list of synsets are returned:

[Synset('cat.n.01'), Synset('guy.n.01'), Synset('cat.n.03'), Synset('kat.n.01'), Synset('cat-o'-nine-tails.n.01'), Synset('caterpillar.n.02'), Synset('big_cat.n.01'), Synset('computerized_tomography.n.01'), Synset('cat.v.01'), Synset('vomit.v.01')

So, now I need to check if dog in this list ???

How can I do it by nltk Python code?


Solution

  • from nltk.corpus import wordnet as wn
    
    for s in wn.synsets('cat'):
        lemmas = s.lemmas()
        for l in lemmas:
            if l.name() == 'dog':
                print l.synset()
    

    Notice that this code searches a joint synset of two words which are considered to be synonyms (so nothing will be found with your 'cat' and 'dog' example). However, there are also other relations in wordnet. For instance, you can search for a 'cat' synset that contains 'dog' as antonym.