gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:56:12 27 4
gpt4 key购买 nike

我想在 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()

注意会有反向重复。

[输出]:

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 - 如何使用 Python 在 WordNet 中生成形容词的反义词列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24192979/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com