Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
from credentials import API_KEY, API_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET
from nltk.corpus import stopwords
import tweepy
import json
import nltk
from textblob import TextBlob
auth = tweepy.OAuthHandler(API_KEY, API_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
class listener(tweepy.StreamListener):
def on_data(self, data):
collected_tweet = json.loads(data)
tweetID = collected_tweet["id"]
if "extended_tweet" in collected_tweet:
tweet = collected_tweet['extended_tweet']['full_text']
else:
tweet = collected_tweet['text']
sentiment_analysis(tweet, tweetID)
def sentiment_analysis(tweet, tweetID):
tokens = nltk.word_tokenize(tweet)
cleaned = [word for word in tokens if word.isalpha()]
str = ""
str.join(cleaned)
print(str)
polarity = TextBlob(str).sentiment.polarity
if polarity <=0.25:
clean_tweets(tweet,tweetID)
else:
return
#Step 1 Parse tweet decode what is said
def clean_tweets(tweet, tweetID):
tokens = nltk.word_tokenize(tweet)
tokens = [w.lower() for w in tokens]
cleaned = [word for word in tokens if word.isalpha()]
stop_words = set(stopwords.words('english'))
cleaned = [w for w in cleaned if not w in stop_words]
porter = nltk.PorterStemmer()
stemmed = [porter.stem(token) for token in cleaned]
respondToTweet(stemmed, tweetID)
#Step 2 Get what reply
def respondToTweet(stemmed, tweetID):
if 'symptom' in stemmed:
responsetweet = "The main symptoms of coronavirus are: high temperature, continuous cough and loss of smell or taste. Find further information at: https://www.nhs.uk/conditions/coronavirus-covid-19/symptoms/#symptoms"
elif 'conspiracy' in stemmed:
responsetweet = "There are a number of different conspiracy theories surrounding the vaccines affects on individuals. Find further information disporving these at: https://www.factcheck.org/search/#gsc.tab=0&gsc.q=covid%20vaccine%20effects&gsc.sort="
elif 'pregnant' in stemmed:
responsetweet ="There has been no specific reason to believe that the vaccine is not safe for pregnant women. Find further information at: https://www.who.int/news-room/q-a-detail/coronavirus-disease-(covid-19)-vaccines-safety"
elif 'bill' or 'gates' in stemmed:
responsetweet = "A number of different conspiracies have been created surrounding Bill gates which are inccorect. Find further information on the goals of the Gates foundation: https://www.factcheck.org/2020/04/conspiracy-theory-misinterprets-goals-of-gates-fou "
elif 'more' or 'deaths'
else:
return
response(responsetweet, tweetID)
def streamtweets():
streaming = listener()
stream = tweepy.Stream(auth=api.auth, listener=streaming, tweet_mode='extended')
stream.filter(track=['#corna19vaccine'])
#Step 3 respond
def response(responsetweet, tweetID):
api.update_status(responsetweet, in_reply_to_status_id=tweetID, auto_populate_reply_metadata=True)
if __name__ == "__main__":
streamtweets()