DEV Community

Pulsebit News Sentiment API
Pulsebit News Sentiment API

Posted on

How to Detect Artificial Intelligence Sentiment Shifts with the Pulsebit API (Python)

How to Detect Artificial Intelligence Sentiment Shifts with the Pulsebit API (Python)

The Problem

If you've ever tried scraping sentiment data for AI topics, you know it can be a tedious process. Websites often block scrapers, and the data you collect may not even be reliable. On top of that, tracking sentiment shifts over time is complex. You’re left wondering if the sentiment you’re analyzing is even relevant or timely.

The Solution

Enter the Pulsebit API. This API provides a single endpoint to pull up-to-date sentiment data for specific topics, like "artificial intelligence." Just yesterday, I noticed something curious when I queried the API: the sentiment score for AI was a flat +0.00 with a momentum of +0.15. While that might seem unremarkable at first glance, it’s the momentum that caught my eye. A momentum of +0.15 indicates a rising trend in sentiment, despite the static score. This intriguing combination means sentiment is shifting, and it’s worth investigating further.

The Code

Let’s see how to make a simple GET request to the Pulsebit API using Python. You’ll need the requests library, so make sure to install it if you haven’t already:

pip install requests
Enter fullscreen mode Exit fullscreen mode

Here’s a straightforward script to fetch sentiment data for "artificial intelligence":

import requests

def fetch_ai_sentiment():
    url = "https://pulsebit.lojenterprise.com/api/news_semantic"
    params = {
        'topic': 'artificial intelligence',
        'region': 'kenya',  # You can change this based on your needs
    }

    response = requests.get(url, params=params)

    if response.status_code == 200:
        return response.json()
    else:
        print("Error:", response.status_code)
        return None

data = fetch_ai_sentiment()
print(data)
Enter fullscreen mode Exit fullscreen mode

Reading the Response

When you call the API, you’ll receive a JSON response containing several fields. Here’s what to look for:

  • sentiment_score: The overall sentiment (in this case, +0.00). This indicates a neutral sentiment.
  • momentum_24h: The change in sentiment momentum (here, +0.15). This is the key metric that suggests a rising trend.
  • confidence: A value between 0 and 1 that indicates the reliability of the sentiment score (0.87 is quite high).
  • sentiment_index_0_100: A normalized score reflecting sentiment on a scale from 0 to 100 (62.25 indicates a moderately positive sentiment).
  • direction: Indicates whether the sentiment is rising or falling (in our case, it’s rising).
  • semantic_clusters: The number of distinct themes or topics within the sentiment data (0 means no clusters detected).
  • region: The geographical context of the sentiment data (we queried it for Kenya).
  • semantic_similarity_avg: A measure of how closely related the news articles are to each other (0.231 is relatively low, suggesting diverse topics).

Three Use Cases

  1. Algo Alert: You can create an algorithm that triggers alerts when momentum rises above a certain threshold. For example, if momentum goes above 0.1, it could send you a notification to look deeper into AI-related news.

  2. Slack Bot: Integrate this API into a Slack bot that updates your team on sentiment shifts in real-time. A simple bot could summarize sentiment changes daily, allowing your team to stay informed without digging through data.

  3. Dashboard: Build a dashboard that visualizes sentiment trends over time. This can help you spot patterns and correlations with external events. Use libraries like Dash or Streamlit to create an interactive UI.

Get Started

Ready to dive in? Check out the Pulsebit API documentation for more details on endpoints and usage. With just a few lines of code, you can start tracking sentiment shifts in real time. It’s a game changer for anyone wanting to stay ahead of the curve in AI discussions.

Top comments (0)