NLP 情感分析:'list' 对象没有属性 'sentiment'

NLP sentiment analysis: 'list' object has no attribute 'sentiment'

对于当前的项目,我计划使用 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)

您似乎在尝试对列表而不是 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)