如何获取Python同义词集列表的第一个内容?

问题描述 投票:0回答:1

enter image description here我有一个废弃的文本存储在变量“message”下。 我已经删除了 StopWords 并将结果存储在变量“without_stop_words”中。 我想循环遍历“without_stop_words”中的每个单词并获取它们的含义和代词。

目前我正在尝试获取含义,但收到错误:“IndexError:列表索引超出范围”

enter image description here

   
 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)

我想获取“without_stop_words”中每个单词的含义。

python nlp nltk sentiment-analysis synset
1个回答
0
投票

错误来自这一行

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
为空。

© www.soinside.com 2019 - 2024. All rights reserved.