Click here to Skip to main content
15,867,832 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
THE QUESTION IS AS FOLLOWS:

Find out the views of different people on the demonetization by analysing the tweets from twitter. Use the Twitter API to extract all tweets related to demonetisation in India by using appropriate filters. Divide each tweet text into words to calculate the sentiment of the whole tweet. Rate the word as per its meaning from +5 to -5 using the dictionary AFINN. The AFINN is a dictionary which consists of 2500 words which are rated from +5 to -5 depending on their meaning. You can download the dictionary from the following link: **AFINN dictionary ( https://drive.google.com/file/d/1AjcJCNH2Oc9j-bJgjdELaUMlWZ3pXyfZ/view )** Now, you have to generate a sentiment rating for each tweet. After that, perform the sentiment analysis by filtering out positive sentiment tweets from negative sentiment tweets.

What I have tried:

I have extracted the twitter data which is in JSON format. But I do not know how to proceed since I don't know about JSON. Can anybody please give me the code about what to do next?

import tweepy
import json
import time

#Twitter API credentials
consumer_key = "tUnfouXORfnaVNEAyTrLmW2ZU"
consumer_secret = "Yxmd1sLKqp2YwXzJ5IJjaVO6PtrOeq1lKyl5AS2Zu2zktjYZKQ"
access_key = "1215780002-2fC55jHbZ4X7NDHgKFJMO1g63Aw0jn1zdmhJjs8"
access_secret = "MJfwXrZ9hKvfb8EUba7eoKlu5BIPDwRDKAXHZOBPdPc2p"

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
#refer http://docs.tweepy.org/en/v3.2.0/api.html#API
#tells tweepy.API to automatically wait for rate limits to replenish

#Put your search term
searchquery = "#Demonetisation"

users =tweepy.Cursor(api.search,q=searchquery).items()
count = 0
errorCount=0

file = open('search.json', 'w') 

while True:
    try:
        user = next(users)
        count += 1
        #use count-break during dev to avoid twitter restrictions
        #if (count>10):
        #    break
    except tweepy.TweepError:
        #catches TweepError when rate limiting occurs, sleeps, then restarts.
        #nominally 15 minnutes, make a bit longer to avoid attention.
        print ("sleeping....")
        user = next(users)
    except StopIteration:
        break
    try:
        print ("Writing to JSON tweet number:"+str(count))
        json.dump(user._json,file,sort_keys = True,indent = 4)
        
    except UnicodeEncodeError:
        errorCount += 1
        print ("UnicodeEncodeError,errorCount ="+str(errorCount))

print ("completed, errorCount ="+str(errorCount)+" total tweets="+str(count))
Posted
Updated 5-Jun-18 9:41am

Python
#Twitter API credentials
consumer_key = "tUnfouXORfnaVNEAyTrLmW2ZU"
consumer_secret = "Yxmd1sLKqp2YwXzJ5IJjaVO6PtrOeq1lKyl5AS2Zu2zktjYZKQ"
access_key = "1215780002-2fC55jHbZ4X7NDHgKFJMO1g63Aw0jn1zdmhJjs8"
access_secret = "MJfwXrZ9hKvfb8EUba7eoKlu5BIPDwRDKAXHZOBPdPc2p"

Looks like you just created a big problem.
You have just published your twitter credentials to public internet.
Advice: have those credentials invalidated as fast as possible.
 
Share this answer
 
Comments
Member 13225387 6-Jun-18 9:08am    
I have already done that. Do you have any solution to my question?
Patrice T 6-Jun-18 9:30am    
Sorry, can't help you further.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900