Haleel/Cryptweets
0
1import tweepy2import json3!pip -q install transformers4!pip3 install emoji5import emoji6import transformers7from transformers import pipeline8 9sentimentmodel=pipeline(model="finiteautomata/bertweet-base-sentiment-analysis")10 11 12 13# authenticate the api key and token with twitter14 15from tweepy.auth import OAuthHandler16 17auth = OAuthHandler('t6d8uj4w9P6DjrNSmfSd42gEI', 'ZLcVXunCbN6NO4rUup6vTR33rO32epm0LFHLkzFLZnhjbQmQzZ')18auth.set_access_token('1405483945379074054-ktOMmQ6HUcZwzOxiHfjcFmP74hkTn5', 'hV792qfEAuJcvZmkzmoMf61qaaSnoV8D2YiIFYjzAgr06')19 20 21api = tweepy.API(auth,wait_on_rate_limit=True)22 23 24# extract the tweets with the given keyword25 26def tweextractor(keyword):27 tweet=[] #list to store tweets extracted28 29 # search_tweets method extract atmost 100 tweets with given keyword30 # use it with cursor to extract more than that in one go31 32 # take 500 popular tweets and 500 recent tweets to keep consistency in the result33 34 for i in tweepy.Cursor(api.search_tweets, keyword,tweet_mode="extended",lang='en',result_type='recent',count=100).items(500):35 i = json.dumps(i._json)36 i = json.loads(i)37 if i['full_text'] not in tweet:38 tweet.append(i['full_text'])39 for i in tweepy.Cursor(api.search_tweets, keyword,tweet_mode="extended",lang='en',result_type='popular',count=100).items(500):40 i = json.dumps(i._json)41 i = json.loads(i)42 if i['full_text'] not in tweet and len(i['full_text'])<500:43 tweet.append(i['full_text'])44 print(len(tweet),'tweets total tweets found')45 return tweet46 47 48 49# method to take keyword and find overall sentiment50 51def sentimentanalyser(keyword):52 tweets=tweextractor(keyword)53 score=[]54 mood=sentimentmodel(tweet)55 for i in mood:56 if i['label']=='POS':57 score.append(i['score'])58 elif i['label']=='NEG':59 score.append(-i['score'])60 sentiment=sum(score)/len(score)61 if sentiment>0:62 return 'Sentiment of public is positive with probability '+sentiment63 else: return 'Sentiment of public is negative with probability'+(-sentiment)64 65 66 67 