AttributeError-使用nltk.sentiment.vader / SentimentIntensityAnalyzer分析Facebook Messenger数据

我试图像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'
shenhaizhe 回答:AttributeError-使用nltk.sentiment.vader / SentimentIntensityAnalyzer分析Facebook Messenger数据

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/1376061.html

大家都在问