I tried to analyze personal facebook messenger data with python like in the youtube video Livestream: Analyzing Facebook Messenger Data with Python.
I downloaded the messenger data as json files and chose a chat history that I wanted to analyze. After creating a dataframe with the relevant information, I wanted to create a new column 'sentiment' using the SentimentIntensityAnalyzer.
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']
when I execute the following code:
messages['sentiment'] = messages['content'].apply(get_polarity)
I get a complex 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, text, punc_list, 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'