Skip to content

Commit

Permalink
Create sentiment_analysis.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 27, 2024
1 parent 9520a5e commit a1afdee
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions market_analysis/sentiment_analysis.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a1afdee

Please sign in to comment.