DEV Community

Pulsebit News Sentiment API
Pulsebit News Sentiment API

Posted on

How to Detect Business Sentiment Anomalies with the Pulsebit API (Python)

How to Detect Business Sentiment Anomalies with the Pulsebit API (Python)

We recently noticed something intriguing: a 24-hour momentum spike of +0.136 in business sentiment. This anomaly caught our attention because it indicates a significant shift in how businesses are perceived over a short period. It’s not just a number; it reflects changing dynamics that we all need to pay attention to.

The Problem

This spike highlights a structural gap in sentiment analysis pipelines that don’t account for multilingual origins or entity dominance. If your model doesn’t handle these nuances, you might have missed this momentum shift by several hours, leading to delayed insights. The dominant language in business narratives often shifts, and if your entity dominance isn’t well-calibrated, you could miss critical signals. For example, an English-dominated model might overlook emerging sentiments in Spanish or Mandarin, skewing your interpretation of the data.

![DATA UNAVAILABLE: lag_hours — verify /dataset/daily_dataset
[DATA UNAVAILABLE: lag_hours — verify /dataset/daily_dataset is returning sentiment_by_lang data for topic: business]

The Code

To catch anomalies like this one, we can implement a simple Python script using our API. Below is the code snippet that allows us to filter by geographic origin and analyze sentiment narratives.

import requests

![Left: Python GET /news_semantic call for 'business'. Right: ](https://pub-c3309ec893c24fb9ae292f229e1688a6.r2.dev/figures/g3_code_output_split_1773150300770.png)
*Left: Python GET /news_semantic call for 'business'. Right: returned JSON response structure (clusters: 0). Source: Pulsebit /news_semantic.*


# Step 1: Geographic origin filter
# Assuming we have access to geo filtering data
language = "en"  # Specify the language filter
country = "US"   # Specify the country filter
response = requests.get(f"https://api.pulsebit.com/data?topic=business&lang={language}&country={country}")
data = response.json()

![[DATA UNAVAILABLE: countries  verify /news_recent is return](https://pub-c3309ec893c24fb9ae292f229e1688a6.r2.dev/figures/g3_geo_output_1773150300861.png)
*[DATA UNAVAILABLE: countries  verify /news_recent is returning country/region values for topic: business]*


# Check for sentiment spike
momentum = data['momentum_24h']
if momentum > 0.1:  # Example threshold
    print(f"Anomaly detected: {momentum}")

# Step 2: Meta-sentiment moment
narrative_text = "Business narrative sentiment cluster analysis"
meta_sentiment_response = requests.post(
    "https://api.pulsebit.com/sentiment",
    json={"text": narrative_text}
)
meta_sentiment_data = meta_sentiment_response.json()

print(f"Meta-sentiment score: {meta_sentiment_data['score']}, Confidence: {meta_sentiment_data['confidence']}")
Enter fullscreen mode Exit fullscreen mode

This code does two crucial things: First, it filters sentiment data by language and country, allowing you to focus on specific geographic influences. While we have not retrieved geo filter data yet, it is a powerful feature when available. Second, it runs a meta-sentiment analysis to score the narrative itself, which provides a deeper understanding of the context behind the data.

Three Builds Tonight

  1. Geo-Filtered Anomaly Detection: Create a system that triggers alerts when sentiment spikes exceed a certain threshold, specifically for regions like North America or Europe. For instance, if momentum exceeds +0.1 in the US, notify your team.

  2. Meta-Sentiment Dashboard: Develop a dashboard that visualizes meta-sentiment scores alongside business sentiment data. Use the response from the POST /sentiment endpoint to frame discussions around business narratives, giving your team immediate context.

  3. Multilingual Sentiment Aggregator: Build a service that aggregates sentiment data across multiple languages and compares them. For example, if the momentum in Spanish is rising while English remains flat, this could indicate emerging trends that your current model isn’t capturing.

Get Started

Dive into our documentation at pulsebit.lojenterprise.com/docs. With just a few lines of code, you can copy-paste and run this in under 10 minutes to catch anomalies like the one we discovered. Start leveraging these insights to enhance your business sentiment analysis today.

Top comments (0)