本文介绍了如何使用TextBlob和Python进行标题的情感分析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to calculate the polarity and subjectivity for some headlines that I have.My code works fine, it does not gives any error but for some rows it gives result 0.00000 for polarity and subjectivity. Do you know why?

您可以在此处下载数据表格:

You can download the data form here:

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

我做错什么了吗?这是代码:

Am I doing something wrong?This is the code:

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,这对我来说很奇怪.

I mean, I know that 0 is possible result, but Im getting 0.0000 in 40%-50% of rows, thats a lot, not even 0.00001, that seams strange to me.

你能帮我吗?

推荐答案

有时会发生.尝试从多语种使用极性方法. https://polyglot.readthedocs.io/en/latest/Installation.html

its sometimes happen.Try to use polarity method from polyglot.https://polyglot.readthedocs.io/en/latest/Installation.html

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

and compare results. Firstly you should make some preprocessing like:

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

这篇关于如何使用TextBlob和Python进行标题的情感分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 16:09