Twitter Feeds


Step 0: Business Understanding

  • This project consists of performing a Sentiment Analysis using Twitter API's
  • The business objectives are the following:

    1) Gather tweets using both search and streaming Twitter APIs.

    2) Analyze them and classify them as positive or negative.

  • Application of Sentiment Analysis in Business and Marketing:

    Companies use Sentiment Analysis to develop their strategies and to understand customers’ feelings towards products or brands, how people respond to new campaigns or product launches, and why consumers do not buy some products.


  Key Learnings:


  Here are the key learnings from this project:

  • Twython is a Python library providing an easy way to access Twitter data.
  • Twython, as a Twitter client, provides methods that directly map to Twitter's public API's.
  • You can add a question mark after a Twython method name to see its signature.
  • Twitter's Search API searches against a sampling of recent Tweets published in the past 7 days

   => Also:

  • We could check for tweets BEFORE and AFTER a big event that changes how people think about a topic, OR:
  • We could check for variations in sentiment when looking at tweets from different locations/geographies

Step 1: Install and Import Libraries

In [ ]:
# Load Libraries
!pip install twython
!pip install tweepy 
!pip install textblob 
In [ ]:
# Import TextBlob for Sentiment Analysis and Twython as a Twitter API client
from textblob import TextBlob
from twython import Twython
# Import regular expressions
import re 
In [ ]:
# Instantiate a Twitter API client (a Twython object for accessing Twitter's API's). This requires
# authenticating a Twitter user

CONSUMER_KEY = "hiVDcJ8Pb9xLBsT6ml7udaD2O"
CONSUMER_SECRET = "ZLCkw8hi6AQYhonotZ2AJTjaWPs5TD1qybGkcT5ZPhQdmuEZxe"

twitter_client = Twython (CONSUMER_KEY, CONSUMER_SECRET)

Observations

Twython's methods map directly to the public API's provided by Twitter.

Methods in Twitter's API's have the following characteristics of most Twython API methods:

  • They return a dictionary
  • They directly map to a documented Twitter API endpoint

Like most modern APIs, the Twitter API uses the JSON format to serialize and transmit data. Twython's methods, i.e. the show_user method, therefore need to perform the following:

  • Contact the Twitter API, receive the data as JSON, and automatically, for our convenience, deserialize it as a Python dictionary.

  • It can be a pretty big dictionary, because show_user returns not just data about the user, but data about the user’s latest tweet.

Step 2: Search for Tweets on "turmeric"

In [4]:
# Return 100 tweets containing the word "turmeric", excluding retweets and replies
search_results = twitter_client.search(q="turmeric -filter:retweets AND -filter:replies", count = 60, language = 'Eng') # results come back as a dictionary 
In [5]:
all_tweets = search_results['statuses']

Step 3: Perform Analysis

In [6]:
# Parse tweets one by one, and create, for each Tweet, a list of 
# value pairs: Tweet Text & Tweet Sentiment

# Create empty list
tweets = []

# Create a dictionary of Tweet Text and Tweet Sentiment for each Tweet
for tweet in all_tweets:
    print(tweet['text'])
    
    # Empty dictionary to store 'text' and 'sentiment' for each Tweet
    parsed_tweet = {}
    
    # Save text of a Tweet
    parsed_tweet['text'] = tweet['text']
    
    # Print and Save sentiment of each Tweet
    blob_to_analyze = TextBlob(tweet['text'])
    print (blob_to_analyze.sentiment)
    print(" ")
    parsed_tweet['sentiment'] = blob_to_analyze.sentiment
    
    # Append parsed_tweet to the list of Tweets
    tweets.append(parsed_tweet)
Research suggests that tetrahydrocurcumin may be more effective for addressing gastrointestinal symptoms, particula… https://t.co/BTKCR2PwVc
Sentiment(polarity=0.55, subjectivity=0.65)
 
Have you Check out my latest video on https://t.co/mRWGfX13tQ @byshondaland I have a Turmeric Coconut Mask Recipe y… https://t.co/r0H36NYFZx
Sentiment(polarity=0.5, subjectivity=0.9)
 
A great gift for mothers day - pack of 4 turmeric latte blends https://t.co/ox1TiJYP0k
Sentiment(polarity=0.8, subjectivity=0.75)
 
Tropical Turmeric Overnight Oats - https://t.co/YRBcCjh3h9
Sentiment(polarity=0.0, subjectivity=0.0)
 
We don’t like to play favorites, but turmeric is at the top of our list when it comes to superfoods.  It’s the anti… https://t.co/j5jhtp9uJd
Sentiment(polarity=0.5, subjectivity=0.5)
 
There are so many good reasons for supplementing with turmeric, including sports injuries #herbs #remedies https://t.co/YxGq2qW7d1
Sentiment(polarity=0.6, subjectivity=0.55)
 
Turmeric Curcumin with Bioperine Joint Pain Relief - Anti-Inflammatory, Antioxidant & Anti-Aging Supplement https://t.co/TtQffBBvyv
Sentiment(polarity=0.0, subjectivity=0.0)
 
https://t.co/zfv4pbOWy7 ☜Amazon
#Turmeric #Curcumin with #Ginger & #Bioperine - Best Vegan Joint Pain #Relief,… https://t.co/rN2sBKhnlD
Sentiment(polarity=1.0, subjectivity=0.3)
 
New health benefits of turmeric #SmartNews  https://t.co/E7HCdpJwhi
Sentiment(polarity=0.13636363636363635, subjectivity=0.45454545454545453)
 
Discon Turmeric sapphire Blouse Wanit... https://t.co/rRDsITSFKa
Sentiment(polarity=0.0, subjectivity=0.0)
 
Good information... Turmeric or Curcumin: Plants vs. Pills #care2 https://t.co/uN4fujYSHm
Sentiment(polarity=0.7, subjectivity=0.6000000000000001)
 
Our Gr 12 Culinary Arts students served up a lunch of butter chicken, basmati rice, naan bread, sautéed broccoli, a… https://t.co/2U9Dob3Zlw
Sentiment(polarity=-0.3, subjectivity=0.475)
 
シュッ、フワッ、バフッ(チェンジアップ)
Sentiment(polarity=0.0, subjectivity=0.0)
 
Crisp Panko Chicken, Fresh Shiitakes Stir-fried with Sweet Onion and Lot's Wyfe Garlic Turmeric… https://t.co/WJWaHxOTYR
Sentiment(polarity=0.075, subjectivity=0.6291666666666667)
 
Honey with Turmeric: The Most Potent Antibiotic That not even Doctors Can Explain https://t.co/b2vYG3NyZ8
Sentiment(polarity=0.5, subjectivity=0.5)
 
Markets across #India really are the perfect place to find the most authentic spices and discover how they can be u… https://t.co/5oS2V6T3Kg
Sentiment(polarity=0.55, subjectivity=0.6125)
 
Nutrition from Pinterest- Turmeric Recipes for an Immune Boost - An Unblurred Lady 3/8/2018 - Turmeric Recipes for… https://t.co/853gsO6qlK
Sentiment(polarity=0.0, subjectivity=0.0)
 
.@TheGingerPeople Releases 3 Turmeric-Infused Drinks https://t.co/LZ314wnE02 https://t.co/cJGn3R8RjG
Sentiment(polarity=0.0, subjectivity=0.0)
 
Lemon Coconut Chia Muffins - Vegan Richa https://t.co/uYqhDM8yxU #vegan #dairyfree #eggfree #turmeric #soyfree… https://t.co/KPIcVe5jDS
Sentiment(polarity=0.0, subjectivity=0.0)
 
#ProjectTransistor I felt an immediate difference when I started taking turmeric daily... like a pill skin it... https://t.co/G3a6Fa6nph
Sentiment(polarity=0.0, subjectivity=0.0)
 
Turmeric tea headass
Sentiment(polarity=0.0, subjectivity=0.0)
 
Need a little savory & sweet? Try this recipe for Honey-Turmeric Pork with Beet and Carrot Salad via @bonappetit https://t.co/wGE6moJqji
Sentiment(polarity=0.08124999999999999, subjectivity=0.575)
 
My sister put this “cool,” “new,” “hip” turmeric face mask on and now she looks like a fucking dirty hen. Christ.
Sentiment(polarity=-0.03787878787878788, subjectivity=0.6348484848484849)
 
DINNER SPECIAL: PMK jackfruit curry and lemon turmeric couscous with soycream and lime!

#PlantMatterKitchen #Vegan… https://t.co/c6ZgdHGAIA
Sentiment(polarity=0.44642857142857145, subjectivity=0.5714285714285714)
 
Did you know... Turmeric aids in healthy digestion, reduces swelling and morning stiffness, and helps your metaboli… https://t.co/mBdfXNH2ce
Sentiment(polarity=0.5, subjectivity=0.5)
 
#StayWell with one of our Citrus Fire juices! 🔥🔥🔥🔥 It's grapefruit, orange, ginger, turmeric, cayenne & oil of oreg… https://t.co/uPktK6Pvih
Sentiment(polarity=0.0, subjectivity=0.0)
 
Once more my food heroes meet in one dish...@BootstrapCook ‘s unusual but very tasty Chickpea and Peach curry (albe… https://t.co/95nhJ1VCCY
Sentiment(polarity=0.3, subjectivity=0.6)
 
Sip me Baby!
One more time.
Turmeric Latte will be available later this week. It’s delicious and… https://t.co/Vm8ZELV7j3
Sentiment(polarity=0.475, subjectivity=0.475)
 
Lol same with Indian things. Turmeric latte my ass
Sentiment(polarity=0.4, subjectivity=0.4125)
 
lunch@#gzlloyd
coconut glazed chicken, basmati rice, turmeric , green curry, harissa brussels , roasted veggies, cilantro coconut tofu
Sentiment(polarity=-0.4, subjectivity=0.625)
 
🚨 New Pasta Alert🚨 
turmeric fettuccine, littleneck clams, bottarga, leek, dill, parmigiano… https://t.co/S64NVklhRh
Sentiment(polarity=0.13636363636363635, subjectivity=0.45454545454545453)
 
Just Pinned to BUY ME EVERYTHING NOW!: The BEST chicken soup youll ever eat is the best homemade nourishing healthy… https://t.co/RlXgMHFzsV
Sentiment(polarity=0.4666666666666666, subjectivity=0.5166666666666667)
 
Turmeric Farro With Roasted Vegetables https://t.co/IuEuEFY5xe
Sentiment(polarity=0.0, subjectivity=0.0)
 
Honey with Turmeric: The Most Potent Antibiotic That not even Doctors Can Explain https://t.co/PWnQYMSyYQ
Sentiment(polarity=0.5, subjectivity=0.5)
 
Today I made aubergine & pak choi stirfry with sticky black rice, turmeric latte, seeded bread and banana & walnut… https://t.co/DNrVIfVQ0H
Sentiment(polarity=-0.16666666666666666, subjectivity=0.43333333333333335)
 
There may be no specific cure for arthritis, but Turmeric may help!  https://t.co/jwj1uHJtUn https://t.co/tvNQmXXWM2
Sentiment(polarity=0.0, subjectivity=0.125)
 
Check out organic Turmeric Curcumin from IshaPur. Use SAVE20 for 20% off. https://t.co/hIchq1U8P1 #IshaPurTurmeric https://t.co/JoBoUKmzFv
Sentiment(polarity=0.0, subjectivity=0.0)
 
#turmeric #shots… https://t.co/E9WuCjImwM
Sentiment(polarity=0.0, subjectivity=0.0)
 
Vegan Turmeric, Oat, Shea Butter & Green Tea Soap-Eczema, Anti-inflammatory, Acne-Order @ https://t.co/H6j4dhgf7x https://t.co/hJlcJtVoB6
Sentiment(polarity=-0.2, subjectivity=0.3)
 
“Celebrate International Women’s Day with Kiehls Turmeric & Cranberry Seed Energizing Radiance Masque” https://t.co/XDbXaeqeoC
Sentiment(polarity=0.0, subjectivity=0.0)
 
Turmeric, Almond, and Sesame Crispbread https://t.co/I9xWxhWARg
Sentiment(polarity=0.0, subjectivity=0.0)
 
Always organic turmeric, broccoli, tomato and mini peppers. #dirtydozen #organic #produce #peppers #tomato… https://t.co/6cpYji6G1R
Sentiment(polarity=0.0, subjectivity=0.0)
 
It's #SpokenWord #Poetry #Comedy #OpenMic tonight! #Cauliflower #Potato #Cumin #Turmeric #Soup #MadeInUtica… https://t.co/G2F4sZveQW
Sentiment(polarity=0.0, subjectivity=0.0)
 
Curry ingredient #turmeric is MORE effective than paracetamol or ibuprofen at easing painful injuries, study finds!… https://t.co/OWihoioQY3
Sentiment(polarity=0.1333333333333334, subjectivity=0.7333333333333334)
 
Here's a delicious (and super fast) winter soup, chock-full of anti-inflammatory ingredients. Try this Healing Carr… https://t.co/ZrPmOKxRSZ
Sentiment(polarity=0.5111111111111111, subjectivity=0.7555555555555555)
 
#Raw #Vegan #Smoothie coconut milk fresh turmeric, sweet potato & apple w/ frozen... https://t.co/T1v5JeDVFi https://t.co/JaWvaFGsRK
Sentiment(polarity=0.13974358974358972, subjectivity=0.5371794871794872)
 
Resident gives mini TED talks @AzCIM IMR annual faculty meeting. Turmeric milk shifts from her mother’s home remedy… https://t.co/Kkv9h638Fs
Sentiment(polarity=0.0, subjectivity=0.0)
 
I try to incorporate a lot of #turmeric in my diet for its #antiinflammatory benefits, usually in the form of… https://t.co/3DZW0NrPGM
Sentiment(polarity=-0.25, subjectivity=0.25)
 
#ThursdayThoughts RE: Weekend. Auntie visiting. She's big on HEALTH so Mama's serving DELICIOUS #HimalyaFresh dips… https://t.co/MPJKHcQnbr
Sentiment(polarity=0.5, subjectivity=0.55)
 
Put this Sweet and Tangy Turmeric Quinoa Salad on your meal plan this week!
https://t.co/t2o79hzQ1R https://t.co/u4WcIoIT67
Sentiment(polarity=0.4375, subjectivity=0.65)
 
Turmeric Curcumin with Ginger & Bioperine for $13.97!

https://t.co/IGG7sMsXWb https://t.co/PWNgw3jVMN
Sentiment(polarity=0.0, subjectivity=0.0)
 
Turmeric Curcumin with Ginger & Bioperine for $13.97!

#FKDLD

https://t.co/O8VqKpKSFx https://t.co/HAOcITtGbw
Sentiment(polarity=0.0, subjectivity=0.0)
 
Learn the secret to Moroccan cooking… and how to yellow a carpet?

Step inside to learn more:… https://t.co/sJXaby5jKj
Sentiment(polarity=-0.2, subjectivity=0.35)
 
the turmeric ginger tea was mmmmmm
Sentiment(polarity=0.0, subjectivity=0.0)
 
CLICK Here for a Discount –>: Glucosamine Chondroitin Turmeric MSM Boswellia – Joint Support Supplement for Relief… https://t.co/uIBwqzlgjR
Sentiment(polarity=0.0, subjectivity=0.0)
 
I added a video to a @YouTube playlist https://t.co/jc9imKeVl4 Your Brain Physically Changing by Consuming Turmeric, Do You Know How?
Sentiment(polarity=0.0, subjectivity=0.14285714285714285)
 
CLICK Here for a Discount –>: Turmeric Curcumin Max Potency 95% Curcuminoids 1950mg with Bioperine Black Pepper for… https://t.co/LAAgIYOjiE
Sentiment(polarity=-0.16666666666666666, subjectivity=0.43333333333333335)
 
I would love to say I didn't become a Simpson the exact moment I took off my turmeric face mask and now I can't take the yellow off.
Sentiment(polarity=0.25, subjectivity=0.2833333333333333)
 
Check out organic Turmeric Curcumin from IshaPur. Use SAVE20 for 20% off. https://t.co/cFj6wUSsVH #IshaPurTurmeric https://t.co/zFIuHx2Pwr
Sentiment(polarity=0.0, subjectivity=0.0)
 
Yes I paid $4 for turmeric and milk that I could’ve paid $0 at my home
Sentiment(polarity=0.0, subjectivity=0.0)
 
In [7]:
# Pick positive tweets from tweets
ptweets = [t for t in tweets if (t['sentiment'][0] > 0)]
# percentage of positive tweets
print("Positive tweets percentage: {} %".format(100*len(ptweets)/len(tweets)))
# picking negative tweets from tweets
ntweets = [t for t in tweets if t['sentiment'][0] < 0]
# 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) - len(ntweets) - len(ptweets))/len(tweets)))
Positive tweets percentage: 43.333333333333336 %
Negative tweets percentage: 13.333333333333334 %
Neutral tweets percentage: 43.333333333333336 %
In [8]:
# printing first 5 positive tweets
print("\n\nPositive tweets:")
for tweet in ptweets[:10]:
    print(tweet['text'])

# print first 5 negative tweets
print("\n\nNegative tweets:")
for tweet in ntweets[:10]:
    print(tweet['text'])
Positive tweets:
Research suggests that tetrahydrocurcumin may be more effective for addressing gastrointestinal symptoms, particula… https://t.co/BTKCR2PwVc
Have you Check out my latest video on https://t.co/mRWGfX13tQ @byshondaland I have a Turmeric Coconut Mask Recipe y… https://t.co/r0H36NYFZx
A great gift for mothers day - pack of 4 turmeric latte blends https://t.co/ox1TiJYP0k
We don’t like to play favorites, but turmeric is at the top of our list when it comes to superfoods.  It’s the anti… https://t.co/j5jhtp9uJd
There are so many good reasons for supplementing with turmeric, including sports injuries #herbs #remedies https://t.co/YxGq2qW7d1
https://t.co/zfv4pbOWy7 ☜Amazon
#Turmeric #Curcumin with #Ginger &amp; #Bioperine - Best Vegan Joint Pain #Relief,… https://t.co/rN2sBKhnlD
New health benefits of turmeric #SmartNews  https://t.co/E7HCdpJwhi
Good information... Turmeric or Curcumin: Plants vs. Pills #care2 https://t.co/uN4fujYSHm
Crisp Panko Chicken, Fresh Shiitakes Stir-fried with Sweet Onion and Lot's Wyfe Garlic Turmeric… https://t.co/WJWaHxOTYR
Honey with Turmeric: The Most Potent Antibiotic That not even Doctors Can Explain https://t.co/b2vYG3NyZ8


Negative tweets:
Our Gr 12 Culinary Arts students served up a lunch of butter chicken, basmati rice, naan bread, sautéed broccoli, a… https://t.co/2U9Dob3Zlw
My sister put this “cool,” “new,” “hip” turmeric face mask on and now she looks like a fucking dirty hen. Christ.
lunch@#gzlloyd
coconut glazed chicken, basmati rice, turmeric , green curry, harissa brussels , roasted veggies, cilantro coconut tofu
Today I made aubergine &amp; pak choi stirfry with sticky black rice, turmeric latte, seeded bread and banana &amp; walnut… https://t.co/DNrVIfVQ0H
Vegan Turmeric, Oat, Shea Butter &amp; Green Tea Soap-Eczema, Anti-inflammatory, Acne-Order @ https://t.co/H6j4dhgf7x https://t.co/hJlcJtVoB6
I try to incorporate a lot of #turmeric in my diet for its #antiinflammatory benefits, usually in the form of… https://t.co/3DZW0NrPGM
Learn the secret to Moroccan cooking… and how to yellow a carpet?

Step inside to learn more:… https://t.co/sJXaby5jKj
CLICK Here for a Discount –&gt;: Turmeric Curcumin Max Potency 95% Curcuminoids 1950mg with Bioperine Black Pepper for… https://t.co/LAAgIYOjiE

Comments:

  • I conducted searches on Whole Foods, Le Bron, Red Sparrow, and Ritalin
  • Sentiment results are often inaccurate
  • Even though we specified Language as English we get tweets in different languages