### Install TextBlob Library Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_sentiment-analysis.ipynb This command installs the TextBlob library, which is a prerequisite for performing sentiment analysis using TextBlob. Ensure you have pip installed. ```bash #pip install textblob ``` -------------------------------- ### Install Azure Text Analytics Libraries Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_visualization_sentiment.ipynb Installs the necessary Azure libraries for text analytics and core credentials. These libraries are required to interact with the Azure Text Analytics service. ```python #pip install azure.core.credentials #pip install azure.ai.textanalytics ``` -------------------------------- ### Setup Data Directory Structure Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_sentiment-analysis.ipynb Creates a nested directory structure for storing data, specifically for 'gold' standard news data related to S&P 500. It ensures that directories are created if they do not already exist. ```python data_folder = os.path.join(os.getcwd(), 'data') #Create the data directory os.makedirs(data_folder, exist_ok=True) gold_data_folder = data_folder +"/gold" os.makedirs(gold_data_folder, exist_ok=True) #Create sub folder for stock news data in gold news_data_gold = gold_data_folder +"/snp500_news" os.makedirs(news_data_gold, exist_ok=True) ``` -------------------------------- ### Setup Directory Structure for Data Layers Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb Creates a hierarchical directory structure for storing data in different layers: bronze (raw), silver (processed), and gold (serving). It specifically sets up subdirectories for S&P 500 news data within each layer. ```python data_folder = os.path.join(os.getcwd(), 'data') #Create the data directory os.makedirs(data_folder, exist_ok=True) #Create the bronze, silver and gold folders bronze_data_folder = data_folder +"/bronze" os.makedirs(bronze_data_folder, exist_ok=True) silver_data_folder = data_folder +"/silver" os.makedirs(silver_data_folder, exist_ok=True) gold_data_folder = data_folder +"/gold" os.makedirs(gold_data_folder, exist_ok=True) #Create sub folder for stock news data in bronze news_data_bronze = bronze_data_folder +"/snp500_news" os.makedirs(news_data_bronze, exist_ok=True) #Create sub folder for stock news data in silver news_data_silver = silver_data_folder +"/snp500_news" os.makedirs(news_data_silver, exist_ok=True) #Create sub folder for stock news data in gold news_data_gold = gold_data_folder +"/snp500_news" os.makedirs(news_data_gold, exist_ok=True) ``` -------------------------------- ### VADER Sentiment Analysis Example Source: https://context7.com/rajdeepbiswas/stock_sentiment_analysis/llms.txt Demonstrates basic sentiment analysis using the VADER (Valence Aware Dictionary and sEntiment Reasoner) lexicon. It takes a text string as input and returns a sentiment label ('Positive' or 'Negative'). ```python text = "stock market rallied strongly today with impressive gains" print(sentiment_analyzer_vader(text)) # Output: 'Positive' text = "company reported massive losses and layoffs expected" print(sentiment_analyzer_vader(text)) # Output: 'Negative' ``` -------------------------------- ### Download NLTK Opinion Lexicon for Sentiment Analysis Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_sentiment-analysis.ipynb This code snippet downloads the 'opinion_lexicon' corpus from NLTK, which contains positive and negative words used for dictionary-based sentiment analysis. It requires the NLTK library to be installed. ```python import nltk nltk.download('opinion_lexicon') #Uncomment if using for the first time from nltk.corpus import opinion_lexicon ``` -------------------------------- ### Generate Word Cloud for Apple News Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_visualization_sentiment.ipynb Filters the DataFrame to get news specifically for 'AAPL' (Apple) and generates a word cloud from the 'CleanedText' column. The word cloud visualizes the most frequent words in the news articles, with specified dimensions, word count, and background color. ```python # Reviews for Apple rev_apple = df_snp500_news_gold[df_snp500_news_gold['Ticker']=='AAPL']['CleanedText'] wordcloud = WordCloud(width=1600, height=800, random_state=1, max_words=500, background_color='white',) wordcloud.generate(str(set(rev_apple))) # declare our figure plt.figure(figsize=(20,10)) plt.title("Apple Word Cloud", fontsize=40,color='Green') plt.imshow(wordcloud, interpolation='bilinear') plt.axis('off') plt.tight_layout(pad=10) plt.show() ``` -------------------------------- ### Build Punctuation Dictionary in Python Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb Constructs a dictionary mapping Unicode punctuation characters to None. It iterates through all possible Unicode characters, checks if their category starts with 'P' (Punctuation), and adds them to the dictionary. This dictionary can be used for efficient removal of punctuation from text. It requires the 'unicodedata' and 'sys' modules. ```python #Build punctuation dictionary import unicodedata import sys # Create a dictionary of punctuation characters punctuation = dict.fromkeys(i for i in range(sys.maxunicode) if unicodedata.category(chr(i)).startswith('P')) ``` -------------------------------- ### Count Total Stock News Items (Python) Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb Calculates the total number of news items retrieved for a stock by getting the length of the list containing the news. This helps in understanding the volume of news available. ```python len(all_news) ``` -------------------------------- ### TextBlob Sentiment Analysis Source: https://context7.com/rajdeepbiswas/stock_sentiment_analysis/llms.txt Performs sentiment analysis using TextBlob, which provides polarity scores ranging from -1 (negative) to 1 (positive). It includes functions to get the raw polarity score and to classify sentiment into 'Positive', 'Neutral', or 'Negative' based on defined thresholds. Punctuation can affect the scoring intensity. ```python from textblob import TextBlob def get_textBlob_score(text): """Get polarity score from TextBlob (-1 to 1).""" return TextBlob(text).sentiment.polarity def sentiment_analyzer_textBlob(text): """ Analyze sentiment using TextBlob. Scoring rules: - Positive: polarity > 0.05 - Neutral: -0.05 <= polarity <= 0.05 - Negative: polarity < -0.05 """ polarity = TextBlob(text).sentiment.polarity if polarity > 0.05: return 'Positive' elif polarity < -0.05: return 'Negative' else: return 'Neutral' # Apply to dataframe df_gold['sentiment_textBlob'] = df_gold['CleanedText'].apply(sentiment_analyzer_textBlob) # Note: Punctuation affects TextBlob's scoring intensity print(get_textBlob_score("The phone is super cool!")) # 0.385 print(get_textBlob_score("The phone is super cool!!")) # 0.440 print(get_textBlob_score("The phone is super cool!!!")) # 0.508 ``` -------------------------------- ### Get TextBlob Polarity Score Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_sentiment-analysis.ipynb This Python function calculates the sentiment polarity of a given text using TextBlob. The polarity score ranges from -1 (most negative) to 1 (most positive). ```python from textblob import TextBlob # Get the polarity score using below function def get_textBlob_score(sent): # This polarity score is between -1 to 1 polarity = TextBlob(sent).sentiment.polarity return polarity ``` -------------------------------- ### Get Unique Tickers from DataFrame in Python Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb Extracts an array of unique 'Ticker' values from the 'combined_snp500_news' DataFrame. This is useful for identifying all the distinct stock symbols present in the dataset. It requires the Pandas library. ```python current_array = combined_snp500_news['Ticker'].unique() ``` -------------------------------- ### Create Directory for Data Chunks Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb This code creates a new directory named 'small_chunks' within the `news_data_gold` path. The `os.makedirs` function creates the directory, and `exist_ok=True` prevents an error if the directory already exists. ```python import os import numpy as np # Assuming news_data_gold is defined elsewhere # news_data_gold = "/path/to/your/news_data_gold" news_data_gold_chunks = news_data_gold +"/small_chunks" os.makedirs(news_data_gold_chunks, exist_ok=True) ``` -------------------------------- ### Download VADER Lexicon (Python) Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_sentiment-analysis.ipynb This code snippet downloads the 'vader_lexicon' from NLTK, which is required for the VADER sentiment analysis tool. It should be uncommented and run if this is the first time using VADER. ```python nltk.download('vader_lexicon') #Uncomment if running for the first time ``` -------------------------------- ### Authenticate Azure ML Workspace Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb Authenticates and loads an Azure Machine Learning workspace using a configuration file. It prints the Azure ML SDK version and the workspace name upon successful connection. ```python import azureml.core from azureml.core import Workspace # Load the workspace from the saved config file ws = Workspace.from_config() print('Ready to use Azure ML {} to work with {}'.format(azureml.core.VERSION, ws.name)) ``` -------------------------------- ### Initialize VADER Sentiment Analyzer (Python) Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_sentiment-analysis.ipynb This code imports the SentimentIntensityAnalyzer from NLTK's sentiment.vader module and creates an instance named 'vds'. This instance is then used to perform sentiment analysis on text data. ```python import nltk #import the VADER Sentiment Analysis from nltk. #Then create an instance for the imported library. from nltk.sentiment.vader import SentimentIntensityAnalyzer vds = SentimentIntensityAnalyzer() ``` -------------------------------- ### Create Directory for Stock News Data (Python) Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_visualization_sentiment.ipynb This Python code creates a subdirectory for storing news data with sentiment analysis results. It uses the `os` module to create the directory if it doesn't already exist, ensuring data organization. ```python #Create sub folder for stock news data in gold news_data_gold_sentiment = news_data_gold +"/news_with_sentiment" os.makedirs(news_data_gold_sentiment, exist_ok=True) ``` -------------------------------- ### Import Libraries for Data Analysis and Azure ML Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb Imports essential Python libraries for data manipulation (pandas, numpy), visualization (seaborn, matplotlib), web data fetching (pandas_datareader), and Azure Machine Learning core functionalities. It also includes warnings suppression. ```python #Import required Libraries import os import numpy as np # linear algebra import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv) import seaborn as sns import matplotlib.pyplot as plt from matplotlib.image import imread import cv2 %matplotlib inline import warnings warnings.filterwarnings("ignore") #pip install pandas_datareader import pandas_datareader.data as web import pandas as pd import datetime as dt import azureml.core import azureml.automl from azureml.core import Workspace, Dataset, Datastore ``` -------------------------------- ### Initialize Positive and Negative Word Lists Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_sentiment-analysis.ipynb This code initializes sets of positive and negative words by loading them from the NLTK 'opinion_lexicon'. These sets are used to quickly check the sentiment polarity of individual words during analysis. ```python #Generate a list of positive words and a list of negative words from #the dictionary downloaded above: pos_list=set(opinion_lexicon.positive()) neg_list=set(opinion_lexicon.negative()) ``` -------------------------------- ### Authenticate Azure Text Analytics Client Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_visualization_sentiment.ipynb Creates a function to instantiate the TextAnalyticsClient using provided key and endpoint. This client is then used to authenticate and interact with the Azure Text Analytics service. ```python from azure.ai.textanalytics import TextAnalyticsClient from azure.core.credentials import AzureKeyCredential def authenticate_client(): ta_credential = AzureKeyCredential(key) text_analytics_client = TextAnalyticsClient( endpoint=endpoint, credential=ta_credential) return text_analytics_client client = authenticate_client() ``` -------------------------------- ### Download NLTK Stopwords and Punkt Tokenizer Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_sentiment-analysis.ipynb This snippet downloads the 'stopwords' corpus and the 'punkt' tokenizer models from NLTK. These are essential for text preprocessing, specifically for removing common words that do not carry significant meaning and for tokenizing sentences into words. ```python nltk.download('stopwords') #Uncomment if using for the first time nltk.download('punkt') #Uncomment if using for the first time from nltk.corpus import stopwords from nltk.tokenize import word_tokenize ``` -------------------------------- ### Create Directory for Sentiment Data (Python) Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_sentiment-analysis.ipynb This Python code snippet creates a subdirectory named 'news_with_sentiment' within a specified 'gold' directory. It uses the 'os' module to create the directory, ensuring it exists and handling cases where it might already be present. ```python import os news_data_gold_sentiment = news_data_gold +"/news_with_sentiment" os.makedirs(news_data_gold_sentiment, exist_ok=True) ``` -------------------------------- ### Create Directory and Read CSV with Sentiment Data (Python) Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_sentiment-analysis.ipynb This snippet demonstrates creating a directory for storing sentiment analysis results and then reading a CSV file containing news data and sentiment scores into a pandas DataFrame. It assumes `news_data_gold` is a predefined path. ```python #Create sub folder for stock news data in gold news_data_gold_sentiment = news_data_gold +"/news_with_sentiment" os.makedirs(news_data_gold_sentiment, exist_ok=True) output_file_name = news_data_gold_sentiment + '/apple_sentiment.csv' apple_sent_gold = pd.read_csv(output_file_name, index_col=None, header=0) apple_sent_gold ``` -------------------------------- ### Generate Word Cloud Visualization for Stock News Source: https://context7.com/rajdeepbiswas/stock_sentiment_analysis/llms.txt Generates word cloud visualizations for stock news analysis using pandas, matplotlib, and wordcloud. It filters news data for a specific ticker, creates a word cloud from the cleaned text, and displays it with a title. This function requires 'pandas', 'matplotlib', and 'wordcloud' libraries. ```python import pandas as pd import matplotlib.pyplot as plt from wordcloud import WordCloud # Load gold data df_gold = pd.read_csv('data/gold/snp500_news_gold.csv') def generate_wordcloud(df, ticker, title_color='Green'): """Generate word cloud for a specific stock ticker.""" # Filter reviews for ticker reviews = df[df['Ticker'] == ticker]['CleanedText'] # Create word cloud wordcloud = WordCloud( width=1600, height=800, random_state=1, max_words=500, background_color='white' ) wordcloud.generate(str(set(reviews))) # Display plt.figure(figsize=(20, 10)) plt.title(f"{ticker} Word Cloud", fontsize=40, color=title_color) plt.imshow(wordcloud, interpolation='bilinear') plt.axis('off') plt.tight_layout(pad=10) plt.show() # Generate word clouds for specific stocks generate_wordcloud(df_gold, 'AAPL', 'Green') # Apple generate_wordcloud(df_gold, 'MSFT', 'Blue') # Microsoft ``` -------------------------------- ### Download Current Stock News using RSS Feed (Python) Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb Fetches current stock news for a given ticker symbol using the `news.get_rss` function. It then accesses the first news item and its summary. Dependencies include the `news` library. ```python all_news = news.get_rss("A") all_news[0] ``` -------------------------------- ### Display First Rows of a Data Chunk DataFrame Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb This code displays the first few rows of the `test_df` DataFrame, which was loaded from a specific CSV chunk file. This allows for a visual inspection of the data within that chunk. ```python test_df.head() ``` -------------------------------- ### Create DataFrame Backup in Python Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb Creates a backup copy of the 'combined_snp500_news' DataFrame and assigns it to 'combined_snp500_news_backup'. This is a good practice to preserve the original state of the data before performing potentially destructive operations or further transformations. It requires the Pandas library. ```python combined_snp500_news_backup = combined_snp500_news ``` -------------------------------- ### Process and Structure Stock News into DataFrame (Python) Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb Iterates through fetched news, stores summaries in a dictionary, and then populates a pandas DataFrame with ticker, news number, and summary. Requires pandas and assumes `all_news`, `ticker`, and `all_news_list` are defined. ```python snp500_news = {} combined_snp500_news = pd.DataFrame(columns = ["Ticker", "NewsNum", "Value"]) #combined_snp500_news.columns = ["Ticker", "NewsNum", "Value"] for i in range (len(all_news)): snp500_news[ticker+'_'+str(i)] = all_news_list[i]['summary'] for key,value in snp500_news.items(): news_num = key.split('_')[1] new_row = {'Ticker':ticker, 'NewsNum':news_num, 'Value':value} #append row to the dataframe combined_snp500_news = combined_snp500_news.append(new_row, ignore_index=True) combined_snp500_news ``` -------------------------------- ### Display First Rows of DataFrame in Python Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb Displays the first 5 rows of the 'combined_snp500_news' DataFrame. This is a standard method for quickly previewing the structure and content of a DataFrame, especially after data loading or transformation. It requires the Pandas library. ```python combined_snp500_news.head() ``` -------------------------------- ### Select and Prepare DataFrame for Output Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb This code selects specific columns ('Ticker', 'NewsNum', 'CleanedText') from the `df_snp500_news_silver` DataFrame to create a new DataFrame `df_snp500_news_gold`. This is often done to isolate relevant data before saving or further processing. ```python df_snp500_news_gold = df_snp500_news_silver[['Ticker','NewsNum','CleanedText']] ``` -------------------------------- ### Load DataFrame from CSV (Python) Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_visualization_sentiment.ipynb This Python code demonstrates how to load data from a CSV file into a Pandas DataFrame. It reads the specified CSV file, sets the 'Sentiment' column as the index, and then displays the loaded DataFrame. ```python test_df = pd.read_csv(output_file_name, index_col=None, header=0) test_df ``` -------------------------------- ### Display First Rows of DataFrame with Pandas Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb This code snippet displays the first 100 rows of the `df_snp500_news_silver` DataFrame using the `head()` method. This is useful for a quick inspection of the data's beginning. ```python df_snp500_news_silver.head(100) ``` -------------------------------- ### POST /analyze-text Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_visualization_sentiment.ipynb This endpoint allows you to send documents to Azure Text Analytics for sentiment analysis. It requires an endpoint URL, subscription key, and a JSON payload containing the documents. ```APIDOC ## POST /analyze-text ### Description This endpoint performs sentiment analysis on the provided documents using Azure Text Analytics. ### Method POST ### Endpoint ### Parameters #### Headers - **Content-Type** (string) - Required - `application/json` - **Ocp-Apim-Subscription-Key** (string) - Required - Your Azure Text Analytics subscription key #### Request Body - **documents** (string) - Required - A JSON string containing the documents to analyze. Example: `{"documents": [{"id": "1", "language": "en", "text": "This is a great product!"}]}` ### Request Example ```bash curl -X POST \ -H "Content-Type: application/json" \ -H "Ocp-Apim-Subscription-Key:" \ -d '{"documents": [{"id": "1", "language": "en", "text": "This is a great product!"}]}' ``` ### Response #### Success Response (200) - **documents** (array) - An array of sentiment analysis results for each document. - **id** (string) - The ID of the document. - **sentiment** (string) - The overall sentiment of the document (e.g., 'positive', 'negative', 'neutral'). - **confidenceScores** (object) - Confidence scores for each sentiment. - **positive** (number) - Confidence score for positive sentiment. - **neutral** (number) - Confidence score for neutral sentiment. - **negative** (number) - Confidence score for negative sentiment. #### Response Example ```json { "documents": [ { "id": "1", "sentiment": "positive", "confidenceScores": { "positive": 0.99, "neutral": 0.01, "negative": 0.00 } } ] } ``` ``` -------------------------------- ### Fetch and Structure S&P 500 Stock News into DataFrame (Python) Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb Fetches news for each S&P 500 ticker, processes it into a dictionary, and appends it to a combined DataFrame. Includes error handling and a delay mechanism for rate limiting. Dependencies: pandas, time, and a `news` library with `get_yf_rss` and `si` for tickers. ```python #snp500_list = si.tickers_sp500() snp500_news = {} combined_snp500_news = pd.DataFrame(data=None, columns = ["Ticker", "NewsNum", "Value"]) import time from time import sleep import importlib for ticker in snp500_list: try: all_news_list = news.get_yf_rss(ticker) for i in range (len(all_news_list)): snp500_news[ticker+'_'+str(i)] = all_news_list[i]['summary'] for key,value in snp500_news.items(): news_num = key.split('_')[1] new_row = {'Ticker':ticker, 'NewsNum':news_num, 'Value':value} #append row to the dataframe combined_snp500_news = combined_snp500_news.append(new_row, ignore_index=True) #break except Exception as e: print (ticker, e) sleep(300) all_news_list = news.get_yf_rss(ticker) for i in range (len(all_news_list)): snp500_news[ticker+'_'+str(i)] = all_news_list[i]['summary'] for key,value in snp500_news.items(): news_num = key.split('_')[1] ``` -------------------------------- ### Dictionary-Based Sentiment Analysis with NLTK Source: https://context7.com/rajdeepbiswas/stock_sentiment_analysis/llms.txt Analyzes sentiment using NLTK's opinion lexicon. It tokenizes text, removes stopwords, and counts positive and negative words to determine an overall sentiment. Requires downloading NLTK data ('opinion_lexicon', 'stopwords', 'punkt'). ```python import nltk from nltk.corpus import opinion_lexicon, stopwords from nltk.tokenize import word_tokenize # Download required NLTK data nltk.download('opinion_lexicon') nltk.download('stopwords') nltk.download('punkt') # Build positive and negative word sets pos_words = set(opinion_lexicon.positive()) neg_words = set(opinion_lexicon.negative()) stop_words = set(stopwords.words('english')) def sentiment_analyzer_dictionary(text): """ Analyze sentiment using dictionary-based word counting. Algorithm: 1. Tokenize text 2. Remove stopwords 3. Count positive and negative words 4. Calculate net sentiment score """ sentiment_score = 0 # Tokenize and remove stopwords tokens = word_tokenize(text) filtered_words = [word for word in tokens if word not in stop_words] # Score each word for word in filtered_words: if word in pos_words: sentiment_score += 1 elif word in neg_words: sentiment_score -= 1 # Determine sentiment label if sentiment_score > 0: return 'Positive' elif sentiment_score < 0: return 'Negative' else: return 'Neutral' # Apply to dataframe df_gold['sentiment_dictionary'] = df_gold['CleanedText'].apply(sentiment_analyzer_dictionary) ``` -------------------------------- ### Display DataFrame Info in Python Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb Prints a concise summary of a Pandas DataFrame, including the index dtype and columns, non-null values and their types, and memory usage. This is crucial for understanding the structure, data types, and potential missing values in the DataFrame. It requires the Pandas library. ```python combined_snp500_news.info() ``` -------------------------------- ### Perform Sentiment Analysis and Opinion Mining in Python Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_visualization_sentiment.ipynb This Python function iterates through sentences, extracts confidence scores for neutral, positive, and negative sentiments, and then processes mined opinions. For each opinion, it prints the target's sentiment and confidence scores, followed by the assessment's sentiment and confidence scores. This requires a pre-initialized sentiment analysis client. ```python def sentiment_analysis_with_opinion_mining(client): """ Performs sentiment analysis and opinion mining on sentences using the provided client. Extracts and prints sentiment scores for sentences, targets, and assessments. """ for sentence in client.get_sentences(): print("Sentence: {}".format(sentence.text)) print("......Neutral={0:.2f} ......Positive={1:.2f} ......Negative={2:.2f}".format( sentence.confidence_scores.neutral, sentence.confidence_scores.positive, sentence.confidence_scores.negative, )) for mined_opinion in sentence.mined_opinions: #print("Sentence: {}".format(sentence.text)) target = mined_opinion.target print("......'{}' target '{}'".format(target.sentiment, target.text)) print("......Target score:\n......Positive={0:.2f}\n......Negative={1:.2f}\n".format( target.confidence_scores.positive, target.confidence_scores.negative, )) for assessment in mined_opinion.assessments: print("......'{}' assessment '{}'".format(assessment.sentiment, assessment.text)) print("......Assessment score:\n......Positive={0:.2f}\n......Negative={1:.2f}\n".format( assessment.confidence_scores.positive, assessment.confidence_scores.negative, )) #print("\n") #print("\n") sentiment_analysis_with_opinion_mining(client) ``` -------------------------------- ### Generate Word Cloud for Microsoft News Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_visualization_sentiment.ipynb Filters the DataFrame to retrieve news articles for 'MSFT' (Microsoft) and generates a word cloud based on the 'CleanedText' column. This visualization highlights the most common words in Microsoft-related news. ```python # Reviews for Microsoft rev_msft = df_snp500_news_gold[df_snp500_news_gold['Ticker']=='MSFT']['CleanedText'] wordcloud = WordCloud(width=1600, height=800, random_state=1, max_words=500, background_color='white',) wordcloud.generate(str(set(rev_msft))) # declare our figure plt.figure(figsize=(20,10)) plt.title("Microsoft Word Cloud", fontsize=40,color='Blue') plt.imshow(wordcloud, interpolation='bilinear') plt.axis('off') plt.tight_layout(pad=10) plt.show() ``` -------------------------------- ### Configure S&P 500 Ticker List Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb Retrieves a list of S&P 500 company tickers. This is a prerequisite for fetching news related to these specific stocks. ```python snp500_list = si.tickers_sp500() ``` -------------------------------- ### Perform VADER Sentiment Analysis on Text (Python) Source: https://context7.com/rajdeepbiswas/stock_sentiment_analysis/llms.txt Analyzes the sentiment of cleaned text data using the VADER (Valence Aware Dictionary and sEntiment Reasoner) lexicon. This method is particularly effective for social media text. It assigns a sentiment label (Positive, Negative, or Neutral) based on the compound score calculated by VADER and adds this as a new column to the DataFrame. ```python import nltk from nltk.sentiment.vader import SentimentIntensityAnalyzer # Download VADER lexicon (first time only) nltk.download('vader_lexicon') # Initialize VADER analyzer vds = SentimentIntensityAnalyzer() def sentiment_analyzer_vader(text): """ Analyze sentiment using VADER. Scoring rules: - Positive: compound score >= 0.05 - Neutral: -0.05 < compound score < 0.05 - Negative: compound score <= -0.05 """ vader_scores = vds.polarity_scores(text) compound_score = vader_scores['compound'] if compound_score > 0.05: return 'Positive' elif compound_score < -0.05: return 'Negative' else: return 'Neutral' # Apply to dataframe df_gold['sentiment_vader'] = df_gold['CleanedText'].apply(sentiment_analyzer_vader) ``` -------------------------------- ### Display DataFrame Information Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_visualization_sentiment.ipynb Prints information about the pandas DataFrame `df_snp500_news_gold`, including the data types of each column, the number of non-null values, and memory usage. This is useful for understanding the structure and content of the loaded data. ```python df_snp500_news_gold.info() ``` -------------------------------- ### Display First Rows of Gold DataFrame Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb This snippet displays the first few rows of the `df_snp500_news_gold` DataFrame, which contains the selected and potentially cleaned data, before further operations. ```python df_snp500_news_gold.head() ``` -------------------------------- ### Perform Sentiment Analysis on Text Documents Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_visualization_sentiment.ipynb Defines a function to perform sentiment analysis on a list of text documents using the authenticated TextAnalyticsClient. It prints the overall document sentiment, confidence scores, and sentiment for each sentence within the document. ```python def sentiment_analysis(client): #documents = ["I had the best day of my life. I wish you were there with me."] response = client.analyze_sentiment(documents) for resp in response: print("Document Sentiment: {}".format(resp.sentiment)) print("Overall scores: positive={0:.2f}; neutral={1:.2f}; negative={2:.2f} \n".format( resp.confidence_scores.positive, resp.confidence_scores.neutral, resp.confidence_scores.negative, )) for idx, sentence in enumerate(resp.sentences): print("Sentence: {}".format(sentence.text)) print("Sentence {} sentiment: {}".format(idx+1, sentence.sentiment)) print("Sentence score:\nPositive={0:.2f}\nNeutral={1:.2f}\nNegative={2:.2f}\n".format( sentence.confidence_scores.positive, sentence.confidence_scores.neutral, sentence.confidence_scores.negative, )) documents = rev_apple[-3:].values.tolist() sentiment_analysis(client) ``` -------------------------------- ### Fetch Stock News from Yahoo Finance RSS (Python) Source: https://context7.com/rajdeepbiswas/stock_sentiment_analysis/llms.txt Fetches news headlines and summaries for S&P 500 tickers from Yahoo Finance RSS feeds. It stores the raw news data, handling potential errors and implementing rate limiting. The output is a Pandas DataFrame containing ticker, news number, and the news summary. ```python import yahoo_fin.stock_info as si from yahoo_fin import news import pandas as pd from time import sleep # Get list of all S&P 500 tickers snp500_list = si.tickers_sp500() # Initialize data structures snp500_news = {} combined_snp500_news = pd.DataFrame(columns=["Ticker", "NewsNum", "Value"]) # Fetch news for each ticker for ticker in snp500_list: try: # Get RSS feed for ticker all_news_list = news.get_yf_rss(ticker) # Process each news item for i in range(len(all_news_list)): snp500_news[ticker + '_' + str(i)] = all_news_list[i]['summary'] # Build dataframe for key, value in snp500_news.items(): news_num = key.split('_')[1] new_row = {'Ticker': ticker, 'NewsNum': news_num, 'Value': value} combined_snp500_news = combined_snp500_news.append(new_row, ignore_index=True) except Exception as e: print(ticker, e) sleep(300) # Rate limiting - wait 5 minutes on error # Output: DataFrame with columns [Ticker, NewsNum, Value] # Example row: {'Ticker': 'AAPL', 'NewsNum': '0', 'Value': 'Apple announces new iPhone...'} ``` -------------------------------- ### Visualize Sentiment Distribution (Python) Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_visualization_sentiment.ipynb This Python code visualizes the distribution of sentiment labels (positive, neutral, negative) from a Pandas DataFrame. It uses `seaborn` and `matplotlib` to create a bar plot, displaying the counts of each sentiment category. ```python #Get the distribution of the ratings x=test_df['Sentiment'].value_counts() x=x.sort_index() #plot plt.figure(figsize=(8,4)) ax= sns.barplot(x=x.index, y=x.values, alpha=0.8) plt.title("Apple Sentiment Distribution") plt.ylabel('# of reviews', fontsize=12) plt.xlabel('Sentiment ', fontsize=12) #adding the text labels rects = ax.patches labels = x.values for rect, label in zip(rects, labels): height = rect.get_height() ax.text(rect.get_x() + rect.get_width()/2, height, label, ha='center', va='bottom') plt.show() ``` -------------------------------- ### Read and Concatenate CSV Files with Pandas Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb This code snippet reads all CSV files from a specified directory into a list and then concatenates them into a single Pandas DataFrame. It uses the `glob` module to find files and `pandas` for data manipulation. Ensure the `news_data_silver` path is correctly set. ```python import glob import pandas as pd # Assuming news_data_silver is defined elsewhere # news_data_silver = "/path/to/your/news_data_silver" all_files = glob.glob(news_data_silver + "/*.csv") li = [] for filename in all_files: df = pd.read_csv(filename, index_col=None, header=0) li.append(df) df_snp500_news_silver = pd.concat(li, axis=0, ignore_index=True) ``` -------------------------------- ### Display First Rows of DataFrame with Cleaned Text Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb This snippet displays the first few rows of the DataFrame after a text cleaning function has been applied. It shows both the original 'Value' column and the new 'CleanedText' column, allowing for verification of the cleaning process. ```python df_snp500_news_silver.head() ``` -------------------------------- ### Perform Sentiment Analysis with Opinion Mining (Python) Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_visualization_sentiment.ipynb This Python function utilizes a client library to perform sentiment analysis on a list of documents. It includes an option to show opinion mining, which extracts specific opinions about targets within sentences. The results are then categorized into positive, negative, and mixed opinions. ```python def sentiment_analysis_with_opinion_mining(client): documents = [ "A major crypto hedge-fund manager expects bitcoin to tumble once the SEC greenlights a bitcoin-backed ETF—Here’s why" ] #print(documents) result = client.analyze_sentiment(documents, show_opinion_mining=True) doc_result = [doc for doc in result if not doc.is_error] #print(doc_result) positive_reviews = [doc for doc in doc_result if doc.sentiment == "positive"] negative_reviews = [doc for doc in doc_result if doc.sentiment == "negative"] positive_mined_opinions = [] mixed_mined_opinions = [] negative_mined_opinions = [] for document in doc_result: """ print("Document Sentiment: {}".format(document.sentiment)) print("Overall scores: positive={0:.2f}; neutral={1:.2f}; negative={2:.2f} \n".format( document.confidence_scores.positive, document.confidence_scores.neutral, document.confidence_scores.negative, )) """ for sentence in document.sentences: print("Sentence: {}".format(sentence.text)) print(sentence.mined_opinions) """ print("Sentence: {}".format(sentence.text)) print("Sentence sentiment: {}".format(sentence.sentiment)) print("Sentence score:\nPositive={0:.2f}\nNeutral={1:.2f}\nNegative={2:.2f}\n".format( sentence.confidence_scores.positive, ``` -------------------------------- ### Display DataFrame Information and Shape with Pandas Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb These snippets display the structure and dimensions of a Pandas DataFrame. `df.info()` provides a concise summary of the DataFrame, including the index dtype and columns, non-null values and memory usage. `df.shape` returns a tuple representing the dimensionality of the DataFrame. ```python df_snp500_news_silver.info() ``` ```python df_snp500_news_silver.shape ``` -------------------------------- ### Extract Target-Opinion Pairs with Opinion Mining Source: https://context7.com/rajdeepbiswas/stock_sentiment_analysis/llms.txt Extracts target-opinion pairs from text using Azure AI's sentiment analysis with opinion mining. It iterates through documents, sentences, and mined opinions to identify targets and their associated assessments and sentiments. This function requires the 'azure-ai-textanalytics' library. ```python def analyze_with_opinion_mining(documents): """Extract target-opinion pairs from text.""" result = client.analyze_sentiment(documents, show_opinion_mining=True) for doc in result: for sentence in doc.sentences: for opinion in sentence.mined_opinions: target = opinion.target print(f"Target: '{target.text}' - Sentiment: {target.sentiment}") for assessment in opinion.assessments: print(f" Assessment: '{assessment.text}' - {assessment.sentiment}") ``` -------------------------------- ### Plot Sentiment Distribution for Stocks Source: https://context7.com/rajdeepbiswas/stock_sentiment_analysis/llms.txt Creates bar charts showing sentiment distribution for analyzed stocks using pandas, matplotlib, and seaborn. It filters data for a specific ticker, counts sentiment occurrences, and plots the distribution with value labels on the bars. This function requires 'pandas', 'matplotlib', and 'seaborn' libraries. ```python import pandas as pd import matplotlib.pyplot as plt import seaborn as sns # Configure visualization style sns.set_style("dark") def plot_sentiment_distribution(df, ticker): """Plot sentiment distribution for a stock.""" # Filter data stock_data = df[df['Ticker'] == ticker] # Get sentiment counts sentiment_counts = stock_data['Sentiment'].value_counts().sort_index() # Create plot plt.figure(figsize=(8, 4)) ax = sns.barplot(x=sentiment_counts.index, y=sentiment_counts.values, alpha=0.8) plt.title(f"{ticker} Sentiment Distribution") plt.ylabel('# of reviews', fontsize=12) plt.xlabel('Sentiment', fontsize=12) # Add value labels on bars for rect, label in zip(ax.patches, sentiment_counts.values): height = rect.get_height() ax.text(rect.get_x() + rect.get_width()/2, height, label, ha='center', va='bottom') plt.show() # Example usage plot_sentiment_distribution(df_analyzed, 'AAPL') ``` -------------------------------- ### Display DataFrame Contents in Python Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb Displays the current contents of the 'combined_snp500_news' DataFrame. This is useful for quick inspection of the data after operations like appending rows. It relies on the Pandas library. ```python combined_snp500_news ``` -------------------------------- ### Load English Stop Words Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_sentiment-analysis.ipynb This snippet loads the list of common English stop words from the NLTK corpus. Stop words are frequently occurring words that are often removed from text during natural language processing to reduce noise and focus on more meaningful terms. ```python # Load stop words stop_words = stopwords.words('english') ``` -------------------------------- ### Read a Specific Chunk CSV File and Count Values Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb This code snippet reads a specific CSV file (representing a data chunk) back into a Pandas DataFrame and then counts the non-null values in each column. This is a verification step to ensure the chunking and saving process was successful. ```python filename = news_data_gold_chunks + '/snp500_news_2.csv' test_df = pd.read_csv(filename, index_col=None, header=0) test_df.count() ``` -------------------------------- ### Clean Text Data by Removing Punctuation and Stopwords (Python) Source: https://context7.com/rajdeepbiswas/stock_sentiment_analysis/llms.txt Cleans raw text data by removing punctuation and common English stopwords. It tokenizes the text, converts it to lowercase, and then filters out stopwords. This function is crucial for preparing text for sentiment analysis and is applied to data stored in the Silver layer, with the cleaned output saved to the Gold layer. ```python import unicodedata import sys import glob import pandas as pd from nltk.corpus import stopwords from nltk.tokenize import word_tokenize # Build punctuation dictionary punctuation = dict.fromkeys(i for i in range(sys.maxunicode) if unicodedata.category(chr(i)).startswith('P')) punctuation.update({96: None}) # Add backtick/grave accent # Load stopwords stop_words = set(stopwords.words('english')) def function_clean_stop(text): """Remove punctuation and stopwords from text.""" # Remove punctuation cleaned = text.translate(punctuation) # Tokenize and remove stopwords tokens = word_tokenize(cleaned.lower()) filtered = [word for word in tokens if word not in stop_words] return ' '.join(filtered) # Read all Silver layer CSV files all_files = glob.glob("data/silver/*.csv") li = [] for filename in all_files: df = pd.read_csv(filename, index_col=None, header=0) li.append(df) df_silver = pd.concat(li, axis=0, ignore_index=True) # Apply cleaning function df_silver['CleanedText'] = df_silver['Value'].apply(function_clean_stop) # Save to Gold layer df_gold = df_silver[['Ticker', 'NewsNum', 'CleanedText']] df_gold.to_csv('data/gold/snp500_news_gold.csv', index=False) # Input: "SANTA CLARA, Calif., September 23, 2021--Pfizer executive..." # Output: "santa clara calif september pfizer executive..." ``` -------------------------------- ### Read Gold File into Pandas DataFrame Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_sentiment-analysis.ipynb Reads a CSV file containing S&P 500 news data into a pandas DataFrame. It sets the index column and header row as specified. This is a crucial step for loading the dataset for further analysis. ```python output_file_name = news_data_gold + '/snp500_all_news.csv' df_snp500_news_gold = pd.read_csv(output_file_name, index_col=None, header=0) ``` -------------------------------- ### Write DataFrame to CSV in Python Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb Saves the 'combined_snp500_news' DataFrame to a CSV file named 'snp500_all_news_1.csv' in the specified 'news_data_silver' directory. The 'index=False' argument prevents writing the DataFrame index to the CSV. This function requires the Pandas library and assumes 'news_data_silver' is a defined path. ```python output_file_name = news_data_silver + '/snp500_all_news_1.csv' combined_snp500_news.to_csv(output_file_name, index=False) ``` -------------------------------- ### Sentiment Analysis with Opinion Mining Function (Python) Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_visualization_sentiment.ipynb This Python function performs sentiment analysis on a list of documents, including opinion mining. It utilizes a client object to analyze sentiment and extracts opinions, targets, and assessments from the results, printing them to the console. ```python def sentiment_analysis_with_opinion_mining(client,documents): #documents = [ # "global shopper face possible shortage smartphones good ahead christmas power cut meet government energy use target forced chinese factory shut left household dark" #] #print(documents) result = client.analyze_sentiment(documents, show_opinion_mining=True) doc_result = [doc for doc in result if not doc.is_error] #print(doc_result) positive_reviews = [doc for doc in doc_result if doc.sentiment == "positive"] negative_reviews = [doc for doc in doc_result if doc.sentiment == "negative"] positive_mined_opinions = [] mixed_mined_opinions = [] negative_mined_opinions = [] for document in doc_result: """ print("Document Sentiment: {}".format(document.sentiment)) print("Overall scores: positive={0:.2f}; neutral={1:.2f}; negative={2:.2f} \n".format( document.confidence_scores.positive, document.confidence_scores.neutral, document.confidence_scores.negative, )) """ for sentence in document.sentences: print("Sentence: {}".format(sentence.text)) print(sentence.mined_opinions) """ print("Sentence: {}".format(sentence.text)) print("Sentence sentiment: {}".format(sentence.sentiment)) print("Sentence score:\nPositive={0:.2f}\nNeutral={1:.2f}\nNegative={2:.2f}\n".format( sentence.confidence_scores.positive, sentence.confidence_scores.neutral, sentence.confidence_scores.negative, )) """ for mined_opinion in sentence.mined_opinions: #print("Sentence: {}".format(sentence.text)) target = mined_opinion.target print("......'{}' target '{}'".format(target.sentiment, target.text)) print("......Target score:\n......Positive={0:.2f}\n......Negative={1:.2f}\n".format( target.confidence_scores.positive, target.confidence_scores.negative, )) for assessment in mined_opinion.assessments: print("......'{}' assessment '{}'".format(assessment.sentiment, assessment.text)) print("......Assessment score:\n......Positive={0:.2f}\n......Negative={1:.2f}\n".format( assessment.confidence_scores.positive, assessment.confidence_scores.negative, )) #print("\n") #print("\n") sentiment_analysis_with_opinion_mining(client,documents) ``` -------------------------------- ### Read Sentiment Analysis Results from CSV (Python) Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_sentiment-analysis.ipynb This Python code snippet reads a CSV file containing sentiment analysis results back into a pandas DataFrame. It specifies the output file name and sets the first column as the index. The resulting DataFrame 'test_df' is then displayed. ```python import pandas as pd test_df = pd.read_csv(output_file_name, index_col=None, header=0) test_df ``` -------------------------------- ### Save Sentiment Analysis Results to CSV (Python) Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_sentiment-analysis.ipynb This Python code snippet saves the sentiment analysis results for Apple stock (stored in 'apple_sent_gold' DataFrame) to a CSV file. The file is named 'apple_sentiment_all_analyze.csv' and is saved in the previously created sentiment data directory. The index is not included in the CSV. ```python import pandas as pd output_file_name = news_data_gold_sentiment + '/apple_sentiment_all_analyze.csv' apple_sent_gold.to_csv(output_file_name, index=False) ``` -------------------------------- ### Apply TextBlob Sentiment Analysis to DataFrame Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_sentiment-analysis.ipynb This Python code applies the `sentiment_analyzer_textBlob` function to the 'CleanedText' column of the `apple_sent_gold` DataFrame, creating a new column 'sentiment_textBlob' to store the results. ```python apple_sent_gold['sentiment_textBlob']=apple_sent_gold['CleanedText'].apply(sentiment_analyzer_textBlob) apple_sent_gold ``` -------------------------------- ### Extract Summary from Stock News Item (Python) Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/get_snp500_news.ipynb Extracts the 'summary' field from a single stock news item, which is typically a dictionary. This is useful for isolating the main text of the news article. No external dependencies beyond the news item structure. ```python all_news[0]['summary'] ``` -------------------------------- ### Save DataFrame to CSV (Python) Source: https://github.com/rajdeepbiswas/stock_sentiment_analysis/blob/main/notebook/stock_news_visualization_sentiment.ipynb This Python code snippet saves a Pandas DataFrame containing news sentiment data to a CSV file. It specifies the output file path and ensures that the DataFrame index is not written to the file. ```python output_file_name = news_data_gold_sentiment + '/apple_sentiment.csv' df_news_aapl_2.to_csv(output_file_name, index=False) ```