Change language

Analyzing Twitter Sentiment Using Python

| |

What is analysis moods?
Sentiment Analysis — it is the process of "computer" determining whether a piece of email is positive, negative, or neutral. This is also known as opinion analysis , getting the opinion or position of the speaker.

Why sentiment analysis?

  • Business: In marketing, companies use it to develop their strategies, to understand how consumers feel about a product or brand, how people react to their campaigns or product launches, and why consumers aren’t buying some of them.
    goods.
  • Politics. In the political sphere, it is used to track political views, identify consistency and inconsistencies between statements and actions at the government level. It can also be used to predict election results!
  • Public Action: Sentiment analysis is also used to monitor and analyze social phenomena, identify potentially dangerous situations and determine the overall sentiment of the blogosphere.

Installation :

  • Tweepy: tweepy — Python client for the official Twitter API .
    Install it using the following pip command:
     pip install tweepy 
  • TextBlob: textblob — it is a python library for processing text data.
    Install it using the following pip command:
     pip install textblob 

    We also need to install multiple NLTK packages using the following command:

     python -m textblob.download_corpora 

    (The corporation — is nothing else, as a large and structured set of texts.)

Authentication:
In order to receive tweets through the Twitter API, you need to register for the app with your Twitter account. Follow these steps for the same:

  • Open this link and click the "Create New App" button
  • Fill in the application details. You can leave the Callback URL blank.
  • Once the application is created, you will be redirected to the application page.
  • Click the Access Keys and Tokens tab.
  • Copy the "Consumer Key", "Consumer Secret", "Access Token" and "Access Token Secret".

Implementation:

import re

import tweepy

from tweepy import OAuthHandler

from textblob import TextBlob

  

class TwitterClient ( object ):

  " ""

General Twitter class for sentiment analysis.

"" "

def __ init __ ( self ):

"" "

  Class constructor or initialization method.

"" "

  # keys and tokens from Twitter developer console

consumer_key = ’XXXXXXXXXXXXXXXXXXXXXXXX’

consumer_secret = ’XXXXXXXXXXXXXXXXXXXXXXXXXXXX’

access_token = ’XXXXXXXXXXXXXXXXXXXXXXXXXXXX’

  access_token_secret = ’ XXXXXXXXXXXXXXXXXXXXXXXXX’

 

# trying to authenticate

try :

# create OAuthHa object ndler

self . auth = OAuthHandler (consumer_key, consumer_secret)

# set access token and secret

self . auth.set_access_token (access_token, access_token_secret)

# create a tweepy API object to receive tweets

  self . api = tweepy.API ( self . auth)

except :

  print ( " Error: Authentication Failed " )

 

  def clean_tweet ( self , tweet):

  " ""

Utility to clean up twitter text by removing links, special characters

using simple regular expressions.

"" "

  return ’’ . join (re.sub ("(@ [A - Za - z0 - 9 ] + ) | ([^ 0 - 9A - Za - z])

| (w + : / / S + ) "," ", tweet) .split ())

 

def get_tweet_sentiment ( self , tweet):

"" "

  A utility function to classify the sentiment of a past tweet

using the textblob sentiment method

"" "

  # create a TextBlob of the submitted tweet text

  analysis = TextBlob ( self . clean_tweet (tweet))

# set mood

  if analysis.sentiment.polarity" 0 :

return ’positive’

  elif analysis.sentiment.polarity = = 0 :

return ’neutral’

  else :

return ’ negative’

 

def get_tweets ( self , query, count = 10 ):

“” ”

  Main function for receiving tweets and analyzing them.

" ""

# empty list to store parsed tweets

tweets = []

  

  try :

# invoke twitter api to receive tweets

  fetched_tweets = self . api.search (q = query, count = count)

 

# parses tweets one by one

for tweet in fetched_tweets:

  # empty dictionary for storing required tweet parameters

  parsed_tweet = {}

  

  # save the tweet text

  parsed_tweet [ ’ text’ ] = tweet.text

# save the mood of the tweet

parsed_tweet [ ’ sentiment’ ] = self . get_tweet_sentiment (tweet.text)

 

# add parsed tweet to tweet list

if tweet.retweet_count" 0 :

# if the tweet is retweeted, make sure to add it only once

if parsed_tweet not in tweets:

tweets.append (parsed_tweet)

else :

tweets.append (parsed_tweet)

 

# return parsed tweets

return twee ts

 

except tweepy.TweepError as e:

  # print error (if any)

print ( "Error:" + str (e))

 

def main ():

# create a TwitterClient class object

  api = TwitterClient ()

  #call in the function for receiving tweets

tweets = api.get_tweets (query = ’Donald Trump’ , count = 200 )

 

# select positive tweets from tweets

  ptweets = = = ’positive’ ]

  # percentage of positive tweets

  p rint ( "Positive tweets percentage: {}%" . format ( 100 * len (ptweets) / len (tweets)))

  # select negative tweets from tweets

  ntweets = = = ’negative’ ]

  # percentage of negative tweets

  print ( "Negative tweets percentage: {}%" . format ( 100 * len (ntweets) / len (tweets)))

# percentage of neutral tweets

print ("Neutral tweets percentage: {} %

". format ( 100 * len (tweets - ntweets - ptweets) / len (tweets)))

 

# print the first 5 positive tweets

  print ( "Positive tweets:" )

for tweet in ptweets [: 10 ]:

print (tweet [ ’text’ ])

 

# print the first 5 negative tweets

  print ( " Negative tweets: " )

for tweet in ntweets [: 10 ]:

print (tweet [ ’text’ ])

  

if __ name__ = = "__ main__" :

# main function call

  main ()

This is what the example output looks like when running the above programs:

 Positive tweets percenta ge: 22% Negative tweets percentage: 15% Positive tweets: RT @JohnGGalt: Amazing — after years of attacking Donald Trump the media managed to turn #InaugurationDay into all about themselves. # MakeAme… RT @ vooda1: CNN Declines to Air White House Press Conference Live YES! THANK YOU @CNN FOR NOT LEGITIMI ... RT @Muheeb_Shawwa: Donald J. Trump’s speech sounded eerily familiar ... POTUS plans new deal for UK as Theresa May to be first foreign leader to meet new president since inauguration. @ Realdonaldtrump #Syria #Mexico #Russia & amp; now #Afghanistan. Another #DearDonaldTrump Letter worth a read @AJEnglish Negative tweets: RT @Slate: Donald Trump’s administration: “Government by the worst men.” RT @RVAwonk: Trump, Sean Spicer, et al. lie for a reason. Their lies are not just lies. Their lies are authoritarian propaganda. RT @KomptonMusic: Me: I hate corn Donald Trump: I hate corn too Me: https://t.co/GPgy8R8HB5 It’s ridiculous that people are more annoyed at this than Donald Trump’s sexism. RT @tony_broach: Chris Wallace on Fox news right now talking crap about Donald Trump news conference it seems he can’t face the truth eithe ... RT @fravel: With False Claims, Donald Trump Attacks Media on Crowd Turnout Aziz Ansari Just Hit Donald Trump Hard In An Epic Saturday NIght Live Monologue 

We follow these 3 basic steps in our program:

  • Authorize Twitter API client.
  • Make a GET request to the Twitter API to get tweets for a specific request.
  • Parse tweets. Classify each tweet as positive, negative or neutral.

Now let’s try to understand the above code snippet:

  • First of all, we create a class TwitterClient . This class contains all the methods for interacting with the Twitter API and parsing tweets. We use the __init__ function to handle API client authentication.
  • In the get_tweets function, we use:
     fetched_tweets = self.api.search (q = query, count = count) 

    call the Twitter API to get tweets.

  • In get_tweet_sentiment we use the textblob module.
     analysis = TextBlob (self.clean_tweet (tweet)) 

    TextBlob — it is a high-level library built on top of the NLTK library. First, we call the clean_tweet, method to remove links, special characters, etc. from the tweet using some simple regex.
    Then, when we submit a Tweet to create a TextBlob object, the following processing is done on the text by the textblob library:

    • Tokenize a tweet, that is, separate words by text.
    • Remove stop words from tokens (stop words — are commonly used words that are not relevant to text analysis, such as me, me, you, etc.)
    • Perform POS (part of speech) token labeling and select only important tokens / tokens like adjectives, adverbs, etc.
    • Pass tokens to a sentiment classifier which classifies the sentiment in a tweet as positive, negative or neutral, assigning it a polarity of -1.0 to 1.0.

    Here’s how a sentiment classifier is created:

    • TextBlob uses the Movie Reviews dataset, where reviews are already flagged as positive or negative.
    • Positive and negative traits are extracted from each positive and negative feedback, respectively.
    • Training data now consists of flagged positive and negative traits. This data is trained naive Bayes classifier .

    Then we use sentiment.polarity class method TextBlob get the polarity of the tweet from -1 to 1.
    Then we classify the polarity as:

     if analysis.sentiment.polarity" 0: return ’positive’ elif analysis.sentiment.polarity == 0: return’ neutral’ else: return ’negative’ 
  • Finally, parsed tweets are returned. We can then do various kinds of statistical analysis of the tweets. For example, in the above program, we tried to find the percentage of positive, negative, and neutral tweets about a request.

Links :

This article courtesy of Nikhil Kumar . If you like Python.Engineering and would like to contribute, you can also write an article using contrib.python.engineering, or email your article to [email protected] See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically