如何对 Pandas 中的名词短语进行情感分析?

问题描述

我需要您的帮助,因为我尝试了所有方法,但无法使用 TextBlob 对从数据帧中的推文中提取的名词短语执行情感分析。另外我认为 TextBlob.noun_phrases 函数没有产生正确的结果。请在下图中亲自查看。我真的是 Python 新手,请帮忙!!

所以我从数据框中提取名词短语的代码是:

from textblob import TextBlob
nltk.download('wordnet')
nltk.download('brown')
nltk.download('punkt')

def blob(text):
  return TextBlob(text).noun_phrases

df['Noun_Phrases'] = df['Tweets'].apply(blob)

df

enter image description here

接下来,我的情感分析代码如下,我得到如下图所示的错误

def getsubjectivity(text):
return TextBlob(text).sentiment.subjectivity 

df['Subjectivity'] = df['Noun_Phrases'].apply(getsubjectivity)

错误:类型错误:传递给 text__init__(text) 参数必须是字符串,而不是

enter image description here

解决方法

不确定您的目标。在您的 getsubjectivity 函数中,输入必须是字符串,似乎您正在为它提供一个列表。

如果您进行以下更改,您将克服错误。

def getsubjectivity(text):
    text=''.join(text)
    return TextBlob(text).sentiment.subjectivity