How to get synonyms from nltk WordNet Python

后端 未结 6 1191
礼貌的吻别
礼貌的吻别 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:19

    This worked for me

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

    0 讨论(0)
  • 2020-11-30 04:24

    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)
    
    0 讨论(0)
  • 2020-11-30 04:27

    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']    
    ...
    
    0 讨论(0)
  • 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'])
    
    0 讨论(0)
  • 2020-11-30 04:37

    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.

    0 讨论(0)
  • 2020-11-30 04:38

    I've code Thesaurus Lookup for Synonym recently, I used this function :

    def find_synonyms(keyword) :
    
        synonyms = []
        for synset in wordnet.synsets(keyword):
            for lemma in synset.lemmas():
                synonyms.append(lemma.name())
    
        return str(synonyms)
    

    But if you prefer to host your own Dictionary, you might interested with my project on offline synonym dictionary lookup on my github page :

    https://github.com/syauqiex/offline_english_synonym_dictionary

    0 讨论(0)
提交回复
热议问题