How to get synonyms from nltk WordNet Python

孤者浪人 提交于 2019-11-27 04:22:59
Francis Bond

If you want the synonyms in the synset (aka the lemmas that make up the set), you can get them with lemma_names():

>>> for ss in wn.synsets('small'):
>>>     print(ss.name(), ss.lemma_names())

small.n.01 ['small']
small.n.02 ['small']
small.a.01 ['small', 'little']
minor.s.10 ['minor', 'modest', 'small', 'small-scale', 'pocket-size',  'pocket-sized']
little.s.03 ['little', 'small']
small.s.04 ['small']
humble.s.01 ['humble', 'low', 'lowly', 'modest', 'small']    
...

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'])

You've already got the synonyms. That's what a Synset is.

>>> wn.synsets('small')
[Synset('small.n.01'),
 Synset('small.n.02'),
 Synset('small.a.01'),
 Synset('minor.s.10'),
 Synset('little.s.03'),
 Synset('small.s.04'),
 Synset('humble.s.01'),
 Synset('little.s.07'),
 Synset('little.s.05'),
 Synset('small.s.08'),
 Synset('modest.s.02'),
 Synset('belittled.s.01'),
 Synset('small.r.01')]

That's the same list of top-level entries that the web interface gave you.

If you also want the "similar to" list, that's not the same thing as the synonyms. For that, you call similar_tos() on each Synset.

So, to show the same information as the website, start with something like this:

for ss in wn.synsets('small'):
    print(ss)
    for sim in ss.similar_tos():
        print('    {}'.format(sim))

Of course the website is also printing the part of speech (sim.pos), list of lemmas (sim.lemma_names), definition (sim.definition), and examples (sim.examples) for each synset at both levels. and it's grouping them by parts of speech, and it's added in links to other things that you can follow, and so forth. But that should be enough to get you started.

Simplest program to print the synonyms of a given word

from nltk.corpus import wordnet

for syn in wordnet.synsets("good"):
    for name in syn.lemma_names():
        print(name)

This worked for me

wordnet.synsets('change')[0].hypernyms()[0].lemma_names()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!