A practical example – Twitter real-time sentiment analysis

Twitter is said to have almost 7,000 tweets every second on a wide variety of topics. Let's try to build a sentiment analyzer that can capture the emotions of the news from different news sources in real time. We will start by importing the required packages:

  1. Import the needed packages:
import tweepy,json,time
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
analyzer = SentimentIntensityAnalyzer()

Note that we are using the following two packages:

  1. VADER sentiment analysis, which stands for Valence Aware Dictionary and Sentiment Reasoner. It is one of the popular rule-based sentiment analysis tools that is developed for social media. If you have never used it before, then you will first have to run the following:
pip install vaderSentiment
  1. Tweepy, which is a Python-based API to access Twitter. Again, if you have never used it before, you need to first run this:
pip install Tweepy
  1. The next step is a bit tricky. You need to make a request to create a developer account with Twitter to get access to the live stream of tweets. Once you have the API keys, you can represent them with the following variables:
twitter_access_token = <your_twitter_access_token>
twitter_access_token_secret = <your_twitter_access_token_secret>
twitter_consumer_key = <your_consumer_key>
twitter_consumer_secret = <your_twitter_consumer_secret>
  1. Let's then configure the Tweepy API authentication. For that, we need to provide the previously created variables:
auth = tweepy.OAuthHandler(twitter_consumer_key, twitter_consumer_secret)
auth.set_access_token(twitter_access_token, twitter_access_token_secret)
api = tweepy.API(auth, parser=tweepy.parsers.JSONParser())
  1. Now comes the interesting part. We will choose the Twitter handles of the news sources that we want to monitor for sentiment analysis. For this example, we have chosen the following news sources:
news_sources = ("@BBC", "@ctvnews", "@CNN","@FoxNews", "@dawn_com")
  1. Now, let's create the main loop. This loop will start with an empty array called array_sentiments to hold the sentiments. Then, we will loop through all five news sources and collect 100 tweets each. Then, for each tweet, we will calculate its polarity:

  1. Now, let's create a graph that shows the polarity of the news from these individual news sources:

Note that each of the news sources is represented by a different color.

  1. Now, let's look at the summary statistics:

The preceding numbers summarize the trends of the sentiments. For example, the sentiments of BBC are found to be the most positive, and the Canadian news channel, CTVnews, seems to be carrying the most negative emotions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset