<jupyter_start><jupyter_text>News Mood Observations:1. From the observed news outlets, they all have a range of positive and negative tweet. No one news outlet only has positive or only has negative tweets2. From the observed news outlets, it appears majority aim at having a neutral tweet3. When looking at average compound, some news outlets may appear to be more positiv then others but it isn't always true since the polarity of a tweet ranges<jupyter_code>#I chose the first challenge: News Mood

%matplotlib inline
# Dependencies
import tweepy
import numpy as np
from pprint import pprint
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import datetime


#import vader sentiment
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
analyzer = SentimentIntensityAnalyzer()

#get api_key from config file (USE YOUR OWN)
from config import (consumer_key,
                    consumer_secret,
                    access_token,
                    access_token_secret)

#establishes api_keys
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, parser=tweepy.parsers.JSONParser())
#gets today's date for the charts
now = datetime.datetime.now()

final_date=now.strftime("%m/%d/%Y")
# Target User Account
target_news = ["@BBC", "@CBS", "@CNN", "@FoxNews", "@nytimes"]

#dictionary that will hold all data
vader_dict = {"News": [],
              "Tweet":[],
              "Compound": [],
              "Positive": [],
              "Negative": [],
              "Neutral": [],
              "Tweet Ago": []}



#going through each news outlet
for news in target_news:
    counter = 1 #counter for tweets ago
    for x in range(1, 6): #5 pages for 100 tweets overall
        public_tweets = api.user_timeline(news, page=x) #grabs timeline tweets
        
        for tweet in public_tweets: #goes through ever tweet 
            #grabs name and text of tweet
            name = tweet["user"]["name"]
            text = tweet["text"]
            #polorizes tweet text
            results = analyzer.polarity_scores(tweet["text"])
            compound = results["compound"]
            positive = results["pos"]
            neutral = results["neu"]
            negative = results["neg"]
            
            #adds to dictionary
            vader_dict["News"].append(name)
            vader_dict["Tweet"].append(text)
            vader_dict["Compound"].append(compound)
            vader_dict["Positive"].append(positive)
            vader_dict["Negative"].append(negative)
            vader_dict["Neutral"].append(neutral)
            vader_dict["Tweet Ago"].append(counter)
            counter+=1 #tweet counter increases
      
   
#creates dataframe and prints it as csv
summary_df= pd.DataFrame(vader_dict)

summary_df.to_csv("./final_news_df.csv")
summary_df
#segments data into each news outlet to prepare for plotting
bbc_data = summary_df.loc[summary_df['News'] == "BBC"]
cbs_data = summary_df.loc[summary_df['News'] == "CBS"]
cnn_data = summary_df.loc[summary_df['News'] == "CNN"]
fox_data = summary_df.loc[summary_df['News'] == "Fox News"]
nyt_data = summary_df.loc[summary_df['News'] == "The New York Times"]
#gets x_axis for each news
bbc_x_axis = bbc_data["Tweet Ago"]
cbs_x_axis = cbs_data["Tweet Ago"]
cnn_x_axis = cnn_data["Tweet Ago"]
fox_x_axis = fox_data["Tweet Ago"]
nyt_x_axis = nyt_data["Tweet Ago"]

#gets y_axis for each news
bbc_y_axis = bbc_data["Compound"]
cbs_y_axis = cbs_data["Compound"]
cnn_y_axis = cnn_data["Compound"]
fox_y_axis = fox_data["Compound"]
nyt_y_axis = nyt_data["Compound"]
#plots each scatter and adds the properties
plt.scatter(x = bbc_x_axis, y = bbc_y_axis, marker='o', facecolors ="lightskyblue", edgecolors="black", s=100, alpha=.60, label="BBC")
plt.scatter(x =cbs_x_axis, y = cbs_y_axis, marker='o', facecolors ="green", edgecolors="black", s=100, alpha=.60, label="CBS")
plt.scatter(x = cnn_x_axis, y= cnn_y_axis, marker='o', facecolors ="red", edgecolors="black", s=100, alpha=.60, label= "CNN")
plt.scatter(x = fox_x_axis, y= fox_y_axis, marker='o', facecolors ="blue", edgecolors="black", s=100, alpha=.60, label= "Fox News")
plt.scatter(x = nyt_x_axis, y= nyt_y_axis, marker='o', facecolors ="yellow", edgecolors="black", s=100, alpha=.60, label= "The New York Times")
plt.xlim(105, -5)
plt.grid()
legend = plt.legend(loc = "best", bbox_to_anchor=(1, 1))
plt.title(f"Sentiment Analysis of Media Tweets ({final_date})")
plt.xlabel("Tweets Ago")
plt.ylabel("Tweet Polarity")
plt.ylim(-1,1)
sns.set_style("darkgrid")
plt.savefig("./first_plot_scatter.png", bbox_inches="tight")
#gets the axis for each news for bar graph

bbc_axis = bbc_data.mean()["Compound"]
cbs_axis = cbs_data.mean()["Compound"]
cnn_axis = cnn_data.mean()["Compound"]
fox_axis = fox_data.mean()["Compound"]
nyt_axis = nyt_data.mean()["Compound"]

#prepares ticks and graphs into bar
bar_y_axis = [bbc_axis, cbs_axis, cnn_axis, fox_axis, nyt_axis]

bar_x_axis = np.arange(len(bar_y_axis))

colors = ['lightblue','green','red', 'blue', 'yellow']

tick_locations = [value for value in bar_x_axis]
bar_fig = plt.bar(bar_x_axis, bar_y_axis, color=colors, edgecolor=colors , alpha=1, align="center", width=1.0)
plt.xticks(tick_locations, ["BBC", "CBS", "CNN", "Fox", "NYT"])
plt.title(f"Overall Media Sentiment Based On Twitter ({final_date})")
plt.ylabel("Tweet Polarity")
plt.ylim(-.09, .16)
sns.set_style("dark")

#adds the labels at the end of each bar
values = bar_fig.patches


for value in values:
    
    y_bars = value.get_height()
    x_bars = value.get_x() + value.get_width() / 2
    space = 5
    va = 'bottom'
    if y_bars < 0:
        space *= -1
        va = 'top'
    label = "{:.3f}".format(y_bars)
    plt.annotate(label, (x_bars, y_bars), xytext=(0, space),textcoords="offset points", ha='center',va=va)                      
                                    
plt.savefig("./second_plot_bar.png", bbox_inches="tight")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13172757475083058, 'fraction_numerical': 0.00946843853820598, 'mean_word_length': 3.8753036437246964, 'pattern_counts': {'":': 7, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '303848', 'license': 'MIT', 'n_tokens_mistral': 2156, 'n_tokens_neox': 2020, 'n_words': 528, 'path': 'social_analytics_hw.ipynb', 'repo_name': 'alexaceves7/hw7_social_analytics', 'size': '6020'}