Hey,
I’m trying to perform sentimental analysis on tweets from Twitter.
Here is my Project Code:
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
Dash app initialization
app = dash.Dash(name)
Layout of the dashboard
app.layout = html.Div([
dcc.Input(id=‘keyword-input’, type=‘text’, value=‘data science’),
dcc.Graph(id=‘live-update-graph’),
dcc.Interval(
id=‘interval-component’,
interval=30 * 1000, # in milliseconds
n_intervals=0
)
])
Callback to update the graph
@app.callback(Output(‘live-update-graph’, ‘figure’),
[Input(‘interval-component’, ‘n_intervals’),
Input(‘keyword-input’, ‘value’)])
def update_graph(n, keyword):
# Fetch tweets based on the provided keyword
tweets = api.search_tweets(q=keyword, count=100, lang=“en”, tweet_mode=‘extended’)
# Analyze sentiments
sentiments = {'Positive': 0, 'Negative': 0, 'Neutral': 0}
for tweet in tweets:
analysis = TextBlob(tweet.full_text)
if analysis.sentiment.polarity > 0:
sentiments['Positive'] += 1
elif analysis.sentiment.polarity < 0:
sentiments['Negative'] += 1
else:
sentiments['Neutral'] += 1
# Create a pie chart
labels = list(sentiments.keys())
values = list(sentiments.values())
return {
'data': [
{'x': labels, 'y': values, 'type': 'pie', 'name': 'Sentiments'},
],
'layout': {
'title': f'Sentiment Analysis for "{keyword}" Tweets',
}
}
if name == ‘main’:
app.run_server(debug=True)
And I am encountering the following Errors when running the app.