如何使用Python为WordNet中的形容词生成反义词列表

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

最佳答案
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()

请注意,将存在反向重复.

[OUT]:

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中的形容词生成反义词列表 - 代码日志