DEV Community

Pulsebit News Sentiment API
Pulsebit News Sentiment API

Posted on

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

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

The Problem

You’re likely familiar with the pain of scraping sentiment data. The process can be cumbersome, requiring constant maintenance to ensure your scraper is compliant with the latest website changes. Plus, parsing and analyzing the data can feel like a never-ending chore. You want actionable insights quickly, not busy work.

Take the current environment sentiment, for example. Right now, it’s at +0.38 with a momentum of +1.40. That’s a significant shift from historical numbers. In fact, this rise signifies an unusual spike that could indicate a change in public perception or awareness regarding environmental issues. Ignoring these shifts means missing out on valuable insights.

The Solution

Enter the Pulsebit API. With just one endpoint, you can retrieve sentiment data without the headache of scraping. The /news_semantic endpoint delivers a rich payload that provides insights into sentiment shifts. You’ll get a clear picture of the current sentiment landscape, including confidence levels, momentum, and more.

The Code

Here’s how you can pull data from the Pulsebit API using Python. You’ll need to have requests installed, so make sure to run pip install requests first.

import requests

# Replace 'YOUR_API_KEY' with your actual Pulsebit API key
api_key = 'YOUR_API_KEY'
url = "https://pulsebit.lojenterprise.com/api/news_semantic"

params = {
    'topic': 'environment',
    'api_key': api_key
}

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

if response.status_code == 200:
    data = response.json()
    print(data)
else:
    print(f"Error: {response.status_code}")
Enter fullscreen mode Exit fullscreen mode

Reading the Response

Once you call the API, you’ll receive a JSON response that contains the following fields:

  • sentiment_score: The overall sentiment score, currently at +0.375. This score indicates a positive sentiment around environmental topics.
  • momentum_24h: Describes the change in sentiment over the last 24 hours, which is at +1.400. A high momentum indicates increasing interest or concern.
  • confidence: At 0.870, this suggests high reliability in the sentiment score—this is not just a random fluctuation.
  • sentiment_index_0_100: Currently at 68.75, indicating a strong positive sentiment relative to the baseline.
  • direction: It’s labeled as rising, confirming that the sentiment is trending upwards.
  • semantic_clusters: Currently 0, indicating no distinct clusters of sentiment are forming.
  • region: This is set to global, meaning the data reflects worldwide sentiment.
  • semantic_similarity_avg: At 0.249, this metric tells you about the average semantic similarity among articles.

This payload gives you everything you need to make informed decisions.

Three Use Cases

  1. Algo Alert: Set up an automated alert system that triggers when sentiment exceeds a certain threshold. For example, if the sentiment score rises above +0.4, you could send notifications to your team for immediate action.

  2. Slack Bot: Create a Slack bot that fetches and shares the latest sentiment data in your channel. This keeps your team informed in real-time, allowing for quick responses to shifts in public sentiment.

  3. Dashboard: Build a dashboard that visualizes sentiment over time. By plotting the sentiment score and momentum, you can easily spot trends and anomalies, helping you make data-driven decisions.

Get Started

Ready to dive in? Head over to the Pulsebit API documentation to get your API key and explore more functionalities. With just a few lines of code, you can leverage real-time sentiment data and stay ahead of the curve without the hassle of DIY scraping.

This is the future of sentiment analysis—use it wisely!

Top comments (0)