diff --git a/market_analysis/sentiment_analysis.py b/market_analysis/sentiment_analysis.py new file mode 100644 index 000000000..3b9bea8a3 --- /dev/null +++ b/market_analysis/sentiment_analysis.py @@ -0,0 +1,26 @@ +import tweepy +import nltk +from nltk.sentiment import SentimentIntensityAnalyzer +from textblob import TextBlob + +class SentimentAnalysis: + def __init__(self, api_key, api_secret, access_token, access_token_secret): + self.api_key = api_key + self.api_secret = api_secret + self.access_token = access_token + self.access_token_secret = access_token_secret + self.auth = tweepy.OAuthHandler(self.api_key, self.api_secret) + self.auth.set_access_token(self.access_token, self.access_token_secret) + self.api = tweepy.API(self.auth) + self.sia = SentimentIntensityAnalyzer() + + def get_tweets(self, query, count): + tweets = tweepy.Cursor(self.api.search, q=query, lang='en', tweet_mode='extended').items(count) + return [tweet.full_text for tweet in tweets] + + def analyze_sentiment(self, tweets): + sentiments = [] + for tweet in tweets: + blob = TextBlob(tweet) + sentiments.append(self.sia.polarity_scores(tweet)['compound']) + return sentiments