问题描述
我试图像youtube视频Livestream: Analyzing Facebook Messenger Data with Python中一样,使用python分析个人的Messenger数据。
我将Messenger数据下载为json文件,然后选择了要分析的聊天记录。创建具有相关信息的数据框后,我想使用SentimentIntensityAnalyzer创建一个新列“ sentiment”。
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
nltk.download('vader_lexicon')
sentiment_analyzer = SentimentIntensityAnalyzer()
def get_polarity(text):
return sentiment_analyzer.polarity_scores(text)['compound']
当我执行以下代码时:
messages['sentiment'] = messages['content'].apply(get_polarity)
我收到一个复杂的AttributeError:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-39-fd7b9fe4e58e> in <module>
----> 1 messages['sentiment'] = messages['content'].apply(get_polarity)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py in apply(self,func,convert_dtype,args,**kwds)
3846 else:
3847 values = self.astype(object).values
-> 3848 mapped = lib.map_infer(values,f,convert=convert_dtype)
3849
3850 if len(mapped) and isinstance(mapped[0],Series):
pandas\_libs\lib.pyx in pandas._libs.lib.map_infer()
<ipython-input-33-59bf7708c7e1> in get_polarity(text)
1 def get_polarity(text):
----> 2 return sentiment_analyzer.polarity_scores(text)['compound']
C:\ProgramData\Anaconda3\lib\site-packages\nltk\sentiment\vader.py in polarity_scores(self,text)
359 # text,words_and_emoticons,is_cap_diff = self.preprocess(text)
360 sentitext = SentiText(text,self.constants.PUNC_LIST,--> 361 self.constants.REGEX_REMOVE_PUNCTUATION)
362 sentiments = []
363 words_and_emoticons = sentitext.words_and_emoticons
C:\ProgramData\Anaconda3\lib\site-packages\nltk\sentiment\vader.py in __init__(self,text,punc_list,regex_remove_punctuation)
268 def __init__(self,regex_remove_punctuation):
269 if not isinstance(text,str):
--> 270 text = str(text.encode("utf-8"))
271 self.text = text
272 self.PUNC_LIST = punc_list
AttributeError: 'float' object has no attribute 'encode'
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)