Created
May 6, 2023 07:49
-
-
Save under0tech/23ab4fffed21425a5fe8dc28974990b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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