How to get synonyms from nltk WordNet Python

后端 未结 6 1371
礼貌的吻别
礼貌的吻别 2020-11-30 04:10

WordNet is great, but I\'m having a hard time getting synonyms in nltk. If you search similar to for the word \'small\' like here, it shows all of the synonyms.

Ba

6条回答
  •  不知归路
    2020-11-30 04:36

    You can use wordnet.synset and lemmas in order to get all the synonyms:

    example :

    from itertools import chain
    from nltk.corpus import wordnet
    
    synonyms = wordnet.synsets(text)
    lemmas = set(chain.from_iterable([word.lemma_names() for word in synonyms]))
    

    Demo:

    >>> synonyms = wordnet.synsets('change')
    >>> set(chain.from_iterable([word.lemma_names() for word in synonyms]))
    set([u'interchange', u'convert', u'variety', u'vary', u'exchange', u'modify', u'alteration', u'switch', u'commute', u'shift', u'modification', u'deepen', u'transfer', u'alter', u'change'])
    

提交回复
热议问题