【问题标题】:How to do sentiment analysis of headlines with TextBlob and Python如何使用 TextBlob 和 Python 对标题进行情感分析
【发布时间】:2019-11-18 17:33:09
【问题描述】:

我想计算我拥有的一些标题的极性和主观性。 我的代码工作正常,它没有给出任何错误,但对于某些行,它给出了极性和主观性的结果 0.00000。你知道为什么吗?

您可以在这里下载数据表格:

https://www.sendspace.com/file/e8w4tw

我做错了吗? 这是代码:

import pandas as pd
from textblob import TextBlob

pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)

df = pd.read_excel('coca cola news.xlsx', encoding='utf8')

df = df.dropna().reset_index(drop = True)
df = df.drop_duplicates().reset_index(drop = True)
print(df)

head_sentiment = []
head_subj = []

par_sentiment = []
par_subj = []


df['Headline Sentiment'] =  df['Headline'].apply(lambda text: TextBlob(text).sentiment.polarity).round(4)
df['Headline Subjectivity'] =  df['Headline'].apply(lambda text: TextBlob(text).sentiment.subjectivity).round(4)

df['Paragraph Sentiment'] =  df['Paragraph'].apply(lambda text: TextBlob(text).sentiment.polarity).round(4)
df['Paragraph Subjectivity'] =  df['Paragraph'].apply(lambda text: TextBlob(text).sentiment.subjectivity).round(4)

print(df)

print(df[df.columns[-4:]])

我的意思是,我知道 0 是可能的结果,但我在 40%-50% 的行中得到 0.0000,这很多,甚至没有 0.00001,这对我来说很奇怪。

你能帮帮我吗?

【问题讨论】:

    标签: python pandas sentiment-analysis textblob


    【解决方案1】:

    它有时会发生。 尝试使用 polyglot 中的极性方法。 https://polyglot.readthedocs.io/en/latest/Installation.html

    并比较结果。首先,您应该进行一些预处理,例如:

    • 删除停用词
    • 删除数字、html 链接、数字、特殊字符等

    【讨论】:

    • 在使用 textblob 或 polyglot 时是否需要进行预处理?
    • 检查预处理前后的结果并比较结果。
    猜你喜欢
    • 1970-01-01
    • 2020-03-26
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多