【问题标题】:How to generate a list of antonyms for adjectives in WordNet using Python如何使用 Python 在 WordNet 中生成形容词的反义词列表
【发布时间】:2014-08-03 06:50:01
【问题描述】:

我想在 Python 中执行以下操作(我有 NLTK 库,但我对 Python 不是很好,所以我用一个奇怪的伪代码编写了以下内容):

from nltk.corpus import wordnet as wn  #Import the WordNet library
for each adjective as adj in wn        #Get all adjectives from the wordnet dictionary
    print adj & antonym                #List all antonyms for each adjective 
once list is complete then export to txt file

这样我就可以生成一个完整的形容词反义词词典。我认为它应该是可行的,但我不知道如何创建 Python 脚本。我想用 Python 来做,因为那是 NLTK 的母语。

【问题讨论】:

  • 1. from nltk.corpus import wordnet as wn '导入 wordnet 库 2. for each adjective as adj in wn '从 wordnet 字典中获取所有形容词 3.print adj & antonym '列出每个形容词的所有反义词 4.once list is complete then导出为txt文件
  • 在 wordnet 中列出反义词并不是那么简单,因为选择了超下位词通过同义词集链接,反义词通过引理链接。

标签: python nlp nltk wordnet


【解决方案1】:

以下函数使用 WordNet 返回给定单词的一组仅形容词反义词:

from nltk.corpus import wordnet as wn

def antonyms_for(word):
    antonyms = set()
    for ss in wn.synsets(word):
        for lemma in ss.lemmas():
            any_pos_antonyms = [ antonym.name() for antonym in lemma.antonyms() ]
            for antonym in any_pos_antonyms:
                antonym_synsets = wn.synsets(antonym)
                if wn.ADJ not in [ ss.pos() for ss in antonym_synsets ]:
                    continue
                antonyms.add(antonym)
    return antonyms

用法:

print(antonyms_for("good"))

【讨论】:

    【解决方案2】:
    from nltk.corpus import wordnet as wn
    
    for i in wn.all_synsets():
        if i.pos() in ['a', 's']: # If synset is adj or satelite-adj.
            for j in i.lemmas(): # Iterating through lemmas for each synset.
                if j.antonyms(): # If adj has antonym.
                    # Prints the adj-antonym pair.
                    print j.name(), j.antonyms()[0].name()
    

    注意会有反向重复。

    [出]:

    able unable
    unable able
    abaxial adaxial
    adaxial abaxial
    acroscopic basiscopic
    basiscopic acroscopic
    abducent adducent
    adducent abducent
    nascent dying
    dying nascent
    abridged unabridged
    unabridged abridged
    absolute relative
    relative absolute
    absorbent nonabsorbent
    nonabsorbent absorbent
    adsorbent nonadsorbent
    nonadsorbent adsorbent
    absorbable adsorbable
    adsorbable absorbable
    abstemious gluttonous
    gluttonous abstemious
    abstract concrete
    ...
    

    【讨论】:

    • 代码似乎不起作用。我将其更改为在 pos 和导致错误的引理之后摆脱 () 。现在我得到这个错误: Traceback (last recent call last): File "", line 5, in TypeError: 'str' object is not callable when I use the following code: from nltk.corpus import wordnet as wn for i in wn.all_synsets(): if i.pos in ['a', 's']: for j in i.lemmas: if j.antonyms(): print j.name(), j.antonyms ()[0].name()
    • 好的,我已经对其进行了排序。代码应为:from nltk.corpus import wordnet as wn for i in wn.all_synsets(): if i.pos in ['a', 's']: for j in i.lemmas: if j.antonyms() : 打印 j.name, j.antonyms()[0].name
    • 更新你的 NLTK,新的 nltk 使用 get 函数而不是访问 synset 的属性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多