NLP情感分析:“列表”对象没有属性“情感”

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

对于当前项目,我计划使用TextBlob对许多单词组合进行情感分析。

运行情感分析行polarity = common_words.sentiment.polarity并使用print(i,word,freq,polarity)调用结果时,我收到以下错误消息:

polarity = common_words.sentiment.polarity
AttributeError: 'list' object has no attribute 'sentiment'

是否有任何巧妙的调整来使它运行?相应的代码部分如下所示:

for i in ['Text_Pro','Text_Con','Text_Main']:
    common_words = get_top_n_trigram(df[i], 150)
    polarity = common_words.sentiment.polarity
    for word, freq in common_words:
        print(i, word, freq, polarity)

编辑:请在下面找到完整的解决方案(根据与leopardxpreload用户的讨论):

for i in ['Text_Pro','Text_Con','Text_Main']:
    common_words = str(get_top_n_trigram(df[i], 150))
    polarity_list = str([TextBlob(i).sentiment.polarity for i in common_words])
    for element in polarity_list:
        print(i, element)
    for word, freq in common_words:
        print(i, word, freq)
python nlp sentiment-analysis textblob
1个回答
1
投票

似乎您正在尝试使用列表中的TextBlob调用而不是TextBlob对象。

for i in ['Text_Pro','Text_Con','Text_Main']:
    common_words = get_top_n_trigram(df[i], 150)
    # Not sure what is in common_words, but it needs to be a string
    polarity = TextBlob(common_words).sentiment.polarity
    for word, freq in common_words:
        print(i, word, freq, polarity)

如果common_words是列表,则可能需要添加:

polarity_list = [TextBlob(i).sentiment.polarity for i in common_words]

可能的复制粘贴解决方案:

for i in ['Text_Pro','Text_Con','Text_Main']:
    common_words = get_top_n_trigram(df[i], 150)
    polarity_list = [TextBlob(i).sentiment.polarity for i in common_words]
    for element in polarity_list:
        print(i, element)
© www.soinside.com 2019 - 2024. All rights reserved.