【问题标题】:How to get synonyms from nltk WordNet Python如何从 nltk WordNet Python 中获取同义词
【发布时间】:2013-10-16 00:32:13
【问题描述】:

WordNet 很棒,但我很难在 nltk 中找到同义词。如果您搜索类似 here 之类的“小”一词,它会显示所有同义词。

基本上我只需要知道以下内容: wn.synsets('word')[i].option()where option 可以是上位词和反义词,但是获取同义词的选项是什么?

【问题讨论】:

  • 同义词集已经是同义词列表。如果您查看wn.synsets('small'),它的顶级成员与网页完全相同。
  • 另外,wn.synsets('word')[i].hypernyms 只会返回一个绑定方法;我想你最后想要一个()...
  • 对不起,让我更具体一些,我想为第一个相似形容词获得相似选项。一些词包括:原子,亚原子,矮脚鸡。
  • 好的,Wordnet(和 NLTK)对其术语非常谨慎。如果您想要的不是同义词,搜索同义词将无济于事。

标签: python nltk wordnet


【解决方案1】:

您可能对Synset 感兴趣:

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

这与 Web 界面为您提供的顶级条目列表相同。

如果您还想要“相似于”列表,这与同义词不同。为此,您在每个 Synset 上调用 similar_tos()。

因此,要显示与网站相同的信息,请从以下内容开始:

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

当然,该网站还打印了两个级别的每个同义词集的词性 (sim.pos)、引理列表 (sim.lemma_names)、定义 (sim.definition) 和示例 (sim.examples)。它按词性对它们进行分组,并添加到您可以关注的其他内容的链接中,等等。但这应该足以让您入门。

【讨论】:

  • 这篇文章的建议,wn.synsets('word') 返回“word”的同义词是完全错误的。相反,该函数返回“单词”的不同语义概念的列表。概念的同义词。可以通过wn.synsets('word')[i].lemmas()接收synset。
  • @charbugs,我同意:这个答案是错误的。例如,“wiz”是“whiz”的一种含义的同义词,即它是一个拼写不同但含义相同的词。如果我们评论的答案是正确的,那么wn.synsets('whiz') 的输出将包括“wiz”,但它没有。但是,for synset in wn.synsets('whiz'): print synset.lemma_names() 的输出确实包含“wiz”。
  • 这个答案似乎比公认的答案更好。包含similar_tos 可以获得原始问题中要求的额外输出。
【解决方案2】:

这里有一些帮助函数使 NLTK 更易于使用,以及如何使用这些函数的两个示例。

def download_nltk_dependencies_if_needed():
    try:
        nltk.word_tokenize('foobar')
    except LookupError:
        nltk.download('punkt')
    try:
        nltk.pos_tag(nltk.word_tokenize('foobar'))
    except LookupError:
        nltk.download('averaged_perceptron_tagger')

def get_some_word_synonyms(word):
    word = word.lower()
    synonyms = []
    synsets = wordnet.synsets(word)
    if (len(synsets) == 0):
        return []
    synset = synsets[0]
    lemma_names = synset.lemma_names()
    for lemma_name in lemma_names:
        lemma_name = lemma_name.lower().replace('_', ' ')
        if (lemma_name != word and lemma_name not in synonyms):
            synonyms.append(lemma_name)
    return synonyms

def get_all_word_synonyms(word):
    word = word.lower()
    synonyms = []
    synsets = wordnet.synsets(word)
    if (len(synsets) == 0):
        return []
    for synset in synsets:
        lemma_names = synset.lemma_names()
        for lemma_name in lemma_names:
            lemma_name = lemma_name.lower().replace('_', ' ')
            if (lemma_name != word and lemma_name not in synonyms):
                synonyms.append(lemma_name)
    return synonyms

示例 1:get_some_word_synonyms

这种方法往往会返回最相关的同义词,但像“愤怒”这样的词不会返回任何同义词。

download_nltk_dependencies_if_needed()

words = ['dog', 'fire', 'erupted', 'throw', 'sweet', 'center', 'said', 'angry', 'iPhone', 'ThisIsNotARealWorddd', 'awesome', 'amazing', 'jim dandy', 'change']

for word in words:
    print('Synonyms for {}:'.format(word))
    synonyms = get_some_word_synonyms(word)
    for synonym in synonyms:
        print("    {}".format(synonym))

示例 1 输出:

Synonyms for dog:
    domestic dog
    canis familiaris
Synonyms for fire:
Synonyms for erupted:
    erupt
    break out
Synonyms for throw:
Synonyms for sweet:
    henry sweet
Synonyms for center:
    centre
    middle
    heart
    eye
Synonyms for said:
    state
    say
    tell
Synonyms for angry:
Synonyms for iPhone:
Synonyms for ThisIsNotARealWorddd:
Synonyms for awesome:
    amazing
    awe-inspiring
    awful
    awing
Synonyms for amazing:
    amaze
    astonish
    astound
Synonyms for jim dandy:
Synonyms for change:
    alteration
    modification

示例 2:get_all_word_synonyms

这种方法将返回所有可能的同义词,但有些可能不是很相关。

download_nltk_dependencies_if_needed()

words = ['dog', 'fire', 'erupted', 'throw', 'sweet', 'center', 'said', 'angry', 'iPhone', 'ThisIsNotARealWorddd', 'awesome', 'amazing', 'jim dandy', 'change']

for word in words:
    print('Synonyms for {}:'.format(word))
    synonyms = get_some_word_synonyms(word)
    for synonym in synonyms:
        print("    {}".format(synonym))

示例 2 输出:

Synonyms for dog:
    domestic dog
    canis familiaris
    frump
    cad
    bounder
    blackguard
    hound
    heel
    frank
    frankfurter
    hotdog
    hot dog
    wiener
    wienerwurst
    weenie
    pawl
    detent
    click
    andiron
    firedog
    dog-iron
    chase
    chase after
    trail
    tail
    tag
    give chase
    go after
    track
Synonyms for fire:
    firing
    flame
    flaming
    ardor
    ardour
    fervor
    fervour
    fervency
    fervidness
    attack
    flak
    flack
    blast
    open fire
    discharge
    displace
    give notice
    can
    dismiss
    give the axe
    send away
    sack
    force out
    give the sack
    terminate
    go off
    arouse
    elicit
    enkindle
    kindle
    evoke
    raise
    provoke
    burn
    burn down
    fuel
Synonyms for erupted:
    erupt
    break out
    irrupt
    flare up
    flare
    break open
    burst out
    ignite
    catch fire
    take fire
    combust
    conflagrate
    come out
    break through
    push through
    belch
    extravasate
    break
    burst
    recrudesce
Synonyms for throw:
    stroke
    cam stroke
    shed
    cast
    cast off
    shake off
    throw off
    throw away
    drop
    thrust
    give
    flip
    switch
    project
    contrive
    bewilder
    bemuse
    discombobulate
    hurl
    hold
    have
    make
    confuse
    fox
    befuddle
    fuddle
    bedevil
    confound
Synonyms for sweet:
    henry sweet
    dessert
    afters
    confection
    sweetness
    sugariness
    angelic
    angelical
    cherubic
    seraphic
    dulcet
    honeyed
    mellifluous
    mellisonant
    gratifying
    odoriferous
    odorous
    perfumed
    scented
    sweet-scented
    sweet-smelling
    fresh
    unfermented
    sugared
    sweetened
    sweet-flavored
    sweetly
Synonyms for center:
    centre
    middle
    heart
    eye
    center field
    centerfield
    midpoint
    kernel
    substance
    core
    essence
    gist
    heart and soul
    inwardness
    marrow
    meat
    nub
    pith
    sum
    nitty-gritty
    center of attention
    centre of attention
    nerve center
    nerve centre
    snapper
    plaza
    mall
    shopping mall
    shopping center
    shopping centre
    focus on
    center on
    revolve around
    revolve about
    concentrate on
    concentrate
    focus
    pore
    rivet
    halfway
    midway
Synonyms for said:
    state
    say
    tell
    allege
    aver
    suppose
    read
    order
    enjoin
    pronounce
    articulate
    enounce
    sound out
    enunciate
    aforesaid
    aforementioned
Synonyms for angry:
    furious
    raging
    tempestuous
    wild
Synonyms for iPhone:
Synonyms for ThisIsNotARealWorddd:
Synonyms for awesome:
    amazing
    awe-inspiring
    awful
    awing
Synonyms for amazing:
    amaze
    astonish
    astound
    perplex
    vex
    stick
    get
    puzzle
    mystify
    baffle
    beat
    pose
    bewilder
    flummox
    stupefy
    nonplus
    gravel
    dumbfound
    astonishing
    awe-inspiring
    awesome
    awful
    awing
Synonyms for jim dandy:
Synonyms for change:
    alteration
    modification
    variety
    alter
    modify
    vary
    switch
    shift
    exchange
    commute
    convert
    interchange
    transfer
    deepen

【讨论】:

    【解决方案3】:

    我最近为同义词编写了同义词库查找代码,我使用了这个功能:

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

    但如果您更喜欢托管自己的词典,您可能会对我的 github 页面上的离线同义词词典查找项目感兴趣:

    https://github.com/syauqiex/offline_english_synonym_dictionary

    【讨论】:

      【解决方案4】:

      打印给定单词同义词的最简单程序

      from nltk.corpus import wordnet
      
      for syn in wordnet.synsets("good"):
          for name in syn.lemma_names():
              print(name)
      

      【讨论】:

        【解决方案5】:

        这对我有用

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

        【讨论】:

          【解决方案6】:

          您可以使用wordnet.synset 和lemmas 来获取所有同义词:

          示例:

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

          演示:

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

          【讨论】:

          • 第一次导入应该是'from itertools import chain'。
          • 别忘了:from nltk.corpus import wordnet
          • 当 lemma_names() 返回嵌套列表时不起作用。例如。 synonyms = wordnet.synsets('test') 失败
          • @Johan 这是另一个你可以解决的问题,正如这里所解释的stackoverflow.com/a/29244327/2867928
          【解决方案7】:

          如果您想要同义词集中的同义词(也就是组成该集合的引理),您可以通过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']    
          ...
          

          【讨论】:

          • OP 真的应该将此答案标记为正确。
          猜你喜欢
          • 2013-02-26
          • 1970-01-01
          • 2013-03-11
          • 1970-01-01
          • 1970-01-01
          • 2015-09-22
          • 2014-08-31
          • 2017-04-18
          • 1970-01-01
          相关资源
          最近更新 更多