如何获取Python同义词列表的第一个内容?
我有一段抓取下来的文本,存放在一个叫“message”的变量里。我已经去掉了一些常用词(也就是那些对理解内容帮助不大的词),把结果放在了“without_stop_words”这个变量里。
现在我想逐个查看“without_stop_words”里的每个单词,获取它们的意思和发音。
目前我在尝试获取单词的意思时,遇到了一个错误:“IndexError: list index out of range”,意思是我试图访问一个不存在的列表索引。
我想要获取“without_stop_words”中每个单词的意思。
for writeup in writeups:
message = writeup.text
#Stop Words
stop_words = set(stopwords.words('english'))
#print(stop_words)
tokenized_words = word_tokenize(message)
#Filtering Stop Words
without_stop_words = []
for word in tokenized_words:
if word not in stop_words:
without_stop_words.append(word)
#Word Meanings
word_meanings = []
for each_word in without_stop_words:
sync_words = wordnet.synsets(each_word)
meaning = sync_words[0].definition()
print(meaning)
1 个回答
1
这个错误是由这一行代码引起的
meaning = sync_words[0].definition()
它表示 sync_words
是空的
for each_word in without_stop_words:
sync_words = wordnet.synsets(each_word)
if sync_words:
meaning = sync_words[0].definition()
word_meanings.append(meaning)
else:
# Whatever you want to do if it's empty
这样做可以解决错误,但你应该去查一下为什么 sync_words
一开始是空的。