Skip to content

Instantly share code, notes, and snippets.

@under0tech
Created May 6, 2023 07:49
Show Gist options
  • Save under0tech/23ab4fffed21425a5fe8dc28974990b3 to your computer and use it in GitHub Desktop.
Save under0tech/23ab4fffed21425a5fe8dc28974990b3 to your computer and use it in GitHub Desktop.
# ANALYSIS
def twitter_analysis(twitter_api, stock):
sentiment = {}
tweets = tweepy.Cursor(twitter_api.search, q=stock, lang='en').items(TWITTER_TWEETS_COUNT)
negative = []
positive = []
neutral = []
for tweet in tweets:
score = SentimentIntensityAnalyzer().polarity_scores(tweet.text)
compound = score['compound']
if compound >= 0.05:
positive.append(tweet)
elif compound <= -0.05:
negative.append(tweet)
elif compound > -0.05 and compound < 0.05:
neutral.append(tweet)
if len(positive) > len(negative):
sentiment_str = 'POSITIVE'
elif len(negative) > len(positive):
sentiment_str = 'NEGATIVE'
else:
sentiment_str = 'NA'
sentiment = {'sentiment': sentiment_str, 'stock' : stock, \
'positive': len(positive), \
'negative': len(negative), \
'neutral': len(neutral)}
return sentiment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment