DEV Community

Pulsebit News Sentiment API
Pulsebit News Sentiment API

Posted on

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

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

The Problem

As developers who work with sentiment data, you know the pain of scraping information from various sources to gauge public sentiment on topics like sustainability. The process is not only tedious but often leads to incomplete or skewed results. When you finally scrape enough data, you're left parsing through noise, trying to make sense of what the public really thinks.

Imagine instead having a single source of truth that provides real-time insights into sentiment shifts without the hassle of DIY scraping. You can focus on building your applications instead of getting lost in the weeds of data collection.

The Solution

Enter the Pulsebit API. With just one endpoint, you can retrieve comprehensive sentiment analysis on various topics, including sustainability. This API gives you access to structured sentiment data that’s actionable and ready to use in your applications.

For our needs, we will leverage the /news_semantic endpoint. It provides a wealth of information in a single call, allowing you to quickly assess sentiment changes and utilize that data effectively.

The Code

Here’s how you can use Python to make a GET request to the Pulsebit API to fetch sustainability sentiment data:

import requests

![Left: Python GET /news_semantic call for 'sustainability'. R](https://aace88ba921016d861eaeb8858550e91.r2.cloudflarestorage.com/pulsebit-marketing/figures/g3_code_output_split_1772636721595.png)
*Left: Python GET /news_semantic call for 'sustainability'. Right: live JSON response structure. Three lines of Python. Clean JSON. No infrastructure required. Source: Pulsebit /news_semantic.*


# Define the endpoint and your API key
url = "https://pulsebit.lojenterprise.com/api/news_semantic"
api_key = "YOUR_API_KEY"

# Set the parameters for the request
params = {
    "topic": "sustainability",
    "region": "us"
}

# Make the GET request
response = requests.get(url, headers={"Authorization": f"Bearer {api_key}"}, params=params)

# Check if the request was successful
if response.status_code == 200:
    data = response.json()
    print(data)
else:
    print(f"Error fetching data: {response.status_code}")
Enter fullscreen mode Exit fullscreen mode

Reading the Response

When you successfully fetch data from the Pulsebit API, you’ll receive a JSON response containing several fields. Here's a breakdown of what each of these fields means:

  • topic: The subject of the sentiment analysis, in this case, "sustainability".
  • momentum_24h: Indicates the change in sentiment momentum over the last 24 hours. Here it’s +0.400, suggesting a rising interest.
  • sentiment_score: The overall sentiment score, which is currently at +0.000. This indicates a neutral sentiment right now.
  • confidence: A confidence level of 0.870 shows a strong reliability in the sentiment score provided.
  • sentiment_index_0_100: A measure of sentiment on a scale from 0 to 100, currently at 61.25, indicating a positive tilt.
  • direction: "rising" confirms that sentiment is trending upwards.
  • semantic_clusters: Indicates the number of semantic clusters identified; here it’s 0, which means no distinct groups were found.
  • region: The geographical focus of the sentiment analysis, which is the US in this case.
  • semantic_similarity_avg: The average semantic similarity score, currently at 0.457, indicating a moderate level of relatedness in the discussions.

Geographic detection output for sustainability filter. No ge
Geographic detection output for sustainability filter. No geo data leads by article count. Bar colour: sentiment direction. Source: Pulsebit articles[].country.

Three Use Cases

  1. Algo Alert: Set up an alert system that triggers when the momentum exceeds a certain threshold, allowing you to react promptly to shifting sentiments.

  2. Slack Bot: Create a Slack bot that posts updates on sustainability sentiment at regular intervals or when significant changes occur, keeping your team informed.

  3. Dashboard: Build a dashboard to visualize sentiment trends over time. Use the momentum_24h to display real-time shifts and the sentiment_index_0_100 to show overall public perception.

Get Started

To dive deeper into the capabilities of the Pulsebit API, check out the official documentation here. With this tool at your disposal, you can leave the scraping behind and focus on building smarter applications that leverage real-time sentiment data.

Arabic coverage led by 4.2 hours. English at T+4.2h. Confide
Arabic coverage led by 4.2 hours. English at T+4.2h. Confidence scores: Arabic 0.82, Mandarin 0.68, English 0.41 Source: Pulsebit /sentiment_by_lang.

Top comments (0)