Understanding Sentiment Analysis with Python and Transformers - A yahoo stock finance use case

Sentiment analysis is a crucial tool in natural language processing (NLP), enabling businesses and researchers to gauge public opinion and emotions from text data. In this article, we’ll explore sentiment analysis by leveraging Python’s transformers library and its high-level pipeline utility. We'll apply this to analyze news headlines from Yahoo Finance for a specific stock.

Setting Up the Environment

To begin, ensure you have the required libraries installed:

pip install transformers feedparser

Here, transformers provides access to state-of-the-art NLP models, while feedparser handles parsing RSS feeds for news headlines.

Loading and Parsing RSS Feeds

RSS feeds are a great way to collect data for sentiment analysis. In this example, we use Yahoo Finance’s RSS feed to extract news articles for a specific stock.

import feedparser

# Define the stock ticker and keyword
ticker = 'META'  # Meta Platforms (formerly Facebook)
keyword = 'meta'

# Construct the RSS feed URL
rss_url = f'https://finance.yahoo.com/rss/headline?s={ticker}'

# Parse the feed
feed = feedparser.parse(rss_url)

This code retrieves articles related to the stock identified by its ticker symbol.


Sentiment Analysis Using Transformers

The transformers library makes sentiment analysis simple by using pre-trained models. Here, we use ProsusAI/finbert, a model fine-tuned for financial sentiment classification.

from transformers import pipeline

# Initialize the sentiment analysis pipeline
pipe = pipeline("text-classification", model="ProsusAI/finbert")

The text-classification pipeline simplifies the process by automatically handling tokenization and prediction.


Analyzing News Headlines

The main task is to iterate through the feed entries, extract relevant headlines, and classify their sentiment. The sentiment scores will help determine the overall market sentiment for the stock.

total_scores = 0
num_articles = 0

for i, entry in enumerate(feed.entries):
    # Skip articles not containing the keyword
    if keyword.lower() not in entry.summary.lower():
        continue

    print(f'Title: {entry.title}')
    print(f'Link: {entry.link}')
    print(f'Published: {entry.published}')
    print(f'Summary: {entry.summary}')

    # Perform sentiment analysis
    sentiment = pipe(entry.summary)[0]

    print(f"Sentiment Label: {sentiment['label']}, Score: {sentiment['score']}")

    # Update scores based on sentiment label
    if sentiment['label'] == 'positive':
        total_scores += sentiment['score']
        num_articles += 1
    elif sentiment['label'] == 'negative':
        total_scores -= sentiment['score']
        num_articles += 1

This loop performs the following steps:

  1. Filters articles containing the specified keyword.

  2. Prints the article’s metadata (title, link, published date, and summary).

  3. Analyzes the sentiment of the summary using pipe.

  4. Updates the sentiment score based on whether the sentiment is positive or negative.


Calculating and Interpreting the Results

After processing all articles, we calculate the average sentiment score and classify the overall sentiment as positive, neutral, or negative:

average_score = total_scores / num_articles

print(f'Overall Sentiment: {"Positive" if average_score >= 0.15 else "Negative" if average_score <= -0.15 else "Neutral"} ({average_score})')

Here, thresholds (e.g., ±0.15) determine whether the sentiment is neutral or leans positive/negative.


Sample Output

For a given stock, you might see output like this:

Title: Meta Platforms launches new VR headset
Link: https://finance.yahoo.com/article/meta-vr-headset
Published: 2024-12-25
Summary: Meta announced the release of its new virtual reality headset today, boosting investor confidence.
Sentiment Label: positive, Score: 0.98

Overall Sentiment: Positive (0.85)

Conclusion

This example demonstrates how to combine Python libraries to build a powerful sentiment analysis pipeline. The transformers library simplifies NLP tasks with pre-trained models, while feedparser provides a convenient way to gather data. You can adapt and extend this code to analyze different stocks, news sources, or custom datasets.

Would you like to explore any additional sentiment analysis techniques or related use cases? Let us know! See my github link - https://github.com/opadotun-taiwo/finance_-sentiment-analysis

0
Subscribe to my newsletter

Read articles from Opadotun Taiwo Oluwaseun directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Opadotun Taiwo Oluwaseun
Opadotun Taiwo Oluwaseun