Reputation: 1
I ma doing research on tweets sentiment analysis.
I have number of different keywords that i want to track (track=['Trump', 'ocean', 'Democrat', 'Republican', 'gardening','BMCSoftware', 'school', 'CT', '#metoo', 'governor', 'Iran']).
Problem is that how I can identify that which tweet corresponds to which keyword.
I tried my best to solve this problem but I can’t.
Please help me to identify tweets related to keywords.
here is my code..
import tweepy
from tweepy import Stream
from tweepy import StreamListener
import json
from textblob import TextBlob
import re #Regulor Expretion
import csv
import nltk
nltk.download('punkt')
class MyListner(StreamListener):
def on_data(self, data):
raw_twitts=json.loads(data)
try:
created_at=raw_twitts['created_at']
tweets=raw_twitts['text']
tweet_id=raw_twitts['id']
screen_name=raw_twitts['user']['screen_name']
hasgtages=raw_twitts['entities']['hashtags']
description = raw_twitts['user']['description']
retweet=raw_twitts['retweet_count']
with open('Stream_data.csv', 'a') as myFile:
writer = csv.writer(myFile)
writer.writerow([created_at,tweet_id,screen_name,hasgtages,description,tweets,retweet])
print("created_at: ",created_at)
print("id: ",tweet_id)
print("screen_name: ",screen_name)
print("hasgtage: ",hasgtages)
print("description: ",description)
print("text: ",tweets)
print("retweet: ",retweet)
except:
print("Errors got")
def on_error(self,status):
print(status)
auth = tweepy.OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token, access_token_secret)
twitter_stream=Stream(auth,MyListner())
twitter_stream.filter(track=['Trump', 'ocean', 'Democrat', 'Republican', 'gardening','BMCSoftware', 'school', 'CT', '#metoo', 'governor', 'Iran'])
Upvotes: 0
Views: 68