Sma Exp 09 Code Print
Sma Exp 09 Code Print
CODE :
import tweepy
import pandas as pd
import matplotlib.pyplot as plt
from textblob import TextBlob
from wordcloud import WordCloud, STOPWORDS
import datetime
def authenticate_twitter():
auth = tweepy.OAuthHandler('cust_token', 'consumer_secret')
auth.set_access_token('access_token', 'access_token_secret')
return tweepy.API(auth, wait_on_rate_limit=True)
def process_tweets(results):
data = pd.DataFrame({
'id': [t.id for t in results],
'text': [t.text.split('https:')[0] for t in results],
'created_at': [t.created_at for t in results],
'retweet_count': [t.retweet_count for t in results],
'user_followers_count': [t.author.followers_count for t in results],
'user_location': [t.author.location for t in results],
'hashtags': [t.entities.get('hashtags') for t in results]
})
data.drop_duplicates('text', inplace=True)
data['Sentiment'] = data['text'].apply(lambda x: TextBlob(x).sentiment.polarity)
data['SentimentClass'] = pd.cut(data['Sentiment'], [-float('inf'), 0, 0.01, float('inf')],
labels=['Negative', 'Neutral', 'Positive'])
return data
plt.subplot(221)
plt.title(f"{title}{company} Hashtags")
ht_text = " ".join(h['text'] for hashtag in hashtags for h in hashtag if h['text'].lower() !=
'fuck')
wc.generate(ht_text)
plt.imshow(wc)
plt.axis("off")
plt.subplot(222)
plt.title(f"{title}{company} Tweets")
tweet_text = " ".join(data['text'].str.replace('RT', ''))
wc.generate(tweet_text)
plt.imshow(wc)
plt.axis("off")
plt.show()
plt.figure(figsize=[15,6])
sentiment_pct = pd.DataFrame({
cmp1_id: cmp1_data['SentimentClass'].value_counts(normalize=True),
cmp2_id: cmp2_data['SentimentClass'].value_counts(normalize=True)
}).reindex(['Negative', 'Neutral', 'Positive']).fillna(0)
sentiment_pct.plot.bar()
plt.show()
metrics = [
('user_followers_count', 'max', "Number of Followers"),
('Sentiment', 'mean', "Average Sentiment"),
('retweet_count', 'mean', "Average Retweets")
]
plt.subplot(224)
for data in [cmp1_data, cmp2_data]:
data['created_at'] = pd.to_datetime(data['created_at']).dt.normalize()
plt.bar([cmp1_id, cmp2_id],
[d.groupby('created_at').size().mean() for d in [cmp1_data, cmp2_data]])
plt.title("Comparison of Number of Tweets")
plt.show()
# Generate visualizations
generate_wordclouds(cmp_data, cmp_data['hashtags'], company, "About Company Tweets
- ")
generate_wordclouds(rival_data, rival_data['hashtags'], rival, "About Company Tweets - ")
plot_sentiment_analysis(cmp_data, rival_data, company, rival)
plot_company_metrics(cmp_own_data, rival_own_data, company, rival)
OUTPUT