如何使用Python在WordNet中生成形容词的反义词列表 [英] How to generate a list of antonyms for adjectives in WordNet using Python

查看:128
本文介绍了如何使用Python在WordNet中生成形容词的反义词列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Python中执行以下操作(我有NLTK库,但是我对Python不太满意,所以我用怪异的伪代码编写了以下内容):

I want to do the following in Python (I have the NLTK library, but I'm not great with Python, so I've written the following in a weird pseudocode):

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的母语.

This is so I can generate a complete dictionary of antonyms for adjectives. I think it should be doable, but I don't know how to create the Python script. I'd like to do it in Python as that's the NLTK's native language.

推荐答案

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

请注意,将有反向重复项.

Note that there will be reverse duplicates.

[输出]:

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
...

这篇关于如何使用Python在WordNet中生成形容词的反义词列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆