首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NLP情感分析:“列表”对象没有属性“情感”

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

Stack Overflow用户
提问于 2020-06-10 09:15:44
回答 1查看 1.2K关注 0票数 2

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

当运行情感分析线polarity = common_words.sentiment.polarity并使用打印(I、word、freq、极性)调用结果时,我将收到以下错误消息:

代码语言:javascript
复制
polarity = common_words.sentiment.polarity
AttributeError: 'list' object has no attribute 'sentiment'

有什么聪明的调整来让它运行吗?相应的代码部分如下所示:

代码语言:javascript
复制
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)

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

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-10 09:21:50

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

代码语言:javascript
复制
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是一个列表,您可能需要添加:

代码语言:javascript
复制
polarity_list = [TextBlob(i).sentiment.polarity for i in common_words]

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

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62299922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档