Open In App

Facebook Sentiment Analysis using python

This article is a Facebook sentiment analysis using Vader, nowadays many government institutions and companies need to know their customers’ feedback and comment on social media such as Facebook.



What is sentiment analysis?

Sentiment analysis is one of the best modern branches of machine learning, which is mainly used to analyze the data in order to know one’s own idea, nowadays it is used by many companies to their own feedback from customers.
 

Why should we use sentiment analysis?

Installations in Anaconda

 
conda install -c anaconda nltk
pip install nltk
conda install -c conda-forge numpy
pip install numpy
conda install -c anaconda pandas
pip install pandas
conda install -c conda-forge matplotlib
pip install matplotlib 

Authentication

There are many ways to fetch Facebook comments those are: 



Among the above methods, we used downloading the Facebook comment dataset from the Kaggle website which is the best dataset provider. For the code we already used kindle.txt for analysis of kindle amazon facebook comment, you can use your own Facebook comment using this code to analyze your own comments or create a file in text format and try it for simplification.
Below is the implementation. 




import time
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import nltk
import io
import unicodedata
import numpy as np
import re
import string
from numpy import linalg
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.tokenize import PunktSentenceTokenizer
from nltk.tokenize import PunktSentenceTokenizer
from nltk.corpus import webtext
from nltk.stem.porter import PorterStemmer
from nltk.stem.wordnet import WordNetLemmatizer
 
 
with open('kindle.txt', encoding ='ISO-8859-2') as f:
    text = f.read()
     
sent_tokenizer = PunktSentenceTokenizer(text)
sents = sent_tokenizer.tokenize(text)
 
print(word_tokenize(text))
print(sent_tokenize(text))
 
porter_stemmer = PorterStemmer()
 
nltk_tokens = nltk.word_tokenize(text)
 
for w in nltk_tokens:
    print ("Actual: % s Stem: % s" % (w, porter_stemmer.stem(w)))
     
 
wordnet_lemmatizer = WordNetLemmatizer()
nltk_tokens = nltk.word_tokenize(text)
 
for w in nltk_tokens:
    print ("Actual: % s Lemma: % s" % (w, wordnet_lemmatizer.lemmatize(w)))
     
text = nltk.word_tokenize(text)
print(nltk.pos_tag(text))
 
sid = SentimentIntensityAnalyzer()
tokenizer = nltk.data.load('tokenizers / punkt / english.pickle')
 
with open('kindle.txt', encoding ='ISO-8859-2') as f:
    for text in f.read().split('\n'):
        print(text)
        scores = sid.polarity_scores(text)
        for key in sorted(scores):
            print('{0}: {1}, '.format(key, scores[key]), end ='')
              
    print()

Output: 

here is the  sample output of the code:
['i', 'love', 'my', 'kindle']
['i love my kindle']
Actual: i Stem: i
Actual: love Stem: love
Actual: my Stem: my
Actual: kindle Stem: kindl
Actual: i Lemma: i
Actual: love Lemma: love
Actual: my Lemma: my
Actual: kindle Lemma: kindle
[('i', 'NN'), ('love', 'VBP'), ('my', 'PRP$'), ('kindle', 'NN')]
i love my kindle
compound: 0.6369, neg: 0.0, neu: 0.323, pos: 0.677,

We follow these major steps in our program: 

Now, let us try to understand the above piece of code:

with open(‘kindle.txt’, encoding=’ISO-8859-2′) as f:

sent_tokenizer = PunktSentenceTokenizer(text) 
sents = sent_tokenizer.tokenize(text) 
print(word_tokenize(text)) 
print(sent_tokenize(text)) 

from nltk.stem.porter import PorterStemmer 
porter_stemmer = PorterStemmer() 
nltk_tokens = nltk.word_tokenize(text) 
for w in nltk_tokens: 
     print (“Actual: %s Stem: %s” % (w, porter_stemmer.stem(w)))

from nltk.stem.wordnet import WordNetLemmatizer 
wordnet_lemmatizer = WordNetLemmatizer() 
nltk_tokens = nltk.word_tokenize(text) 
for w in nltk_tokens: 
     print (“Actual: %s Lemma: %s” % (w,           wordnet_lemmatizer.lemmatize(w))) 

text = nltk.word_tokenize(text)
print(nltk.pos_tag(text)) 

Here is how vader sentiment analyzer works:

sid = SentimentIntensityAnalyzer() 
tokenizer = nltk.data.load(‘tokenizers/punkt/english.pickle’) 
with open(‘kindle.txt’, encoding=’ISO-8859-2′) as f: 
     for text in f.read().split(‘\n’): 
          print(text) 
          scores = sid.polarity_scores(text) 
          for key in sorted(scores): 
               print(‘{0}: {1}, ‘.format(key, scores[key]), end=”) 
     print()

 i love my kindle
compound: 0.6369, neg: 0.0, neu: 0.323, pos: 0.677 

Article Tags :