DEV Community

Pulsebit News Sentiment API
Pulsebit News Sentiment API

Posted on

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

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

We recently discovered a remarkable anomaly: a 24-hour momentum spike of +1.450 in the defence sentiment data. This spike stands out not just for its magnitude but also because it suggests an unusual shift in sentiment that could have far-reaching implications. As developers focused on extracting actionable insights from sentiment analysis, identifying and reacting to such anomalies is crucial.

The problem we face is that many existing pipelines are not equipped to handle multilingual origins or entity dominance effectively. Imagine your model missed this significant spike by 24 hours, simply because it was unable to parse sentiment data from a key language or entity dominating the discussion. In this case, the language of origin may have skewed the sentiment analysis, leading to delayed insights in a rapidly evolving context, such as defence.

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

Here’s a Python snippet that can help you catch this type of anomaly. We’ll start by filtering by geographic origin if the data were available, followed by scoring the narrative framing itself.

import requests

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


# Define the sentiment analysis parameters
topic = 'defence'
score = +0.000
confidence = 0.87
momentum = +1.450

# Geographic origin filter — here we would filter by language/country if data were available
geo_filter_url = "https://api.pulsebit.com/dataset/daily_dataset"
geo_params = {
    'topic': topic,
    'language': 'en',  # Assuming English for this example
}

# Fetch the filtered data
geo_response = requests.get(geo_filter_url, params=geo_params)
geo_data = geo_response.json()  # This is where you'd handle the data

# Check if geo data is available
if not geo_data:
    print("DATA UNAVAILABLE: no geo filter data returned.")
else:
    print("Geo-filtered data received.")

# Meta-sentiment moment: score the narrative framing itself
meta_sentiment_url = "https://api.pulsebit.com/sentiment"
narrative_input = "Defence narrative sentiment cluster analysis"
meta_response = requests.post(meta_sentiment_url, json={'input': narrative_input})
meta_sentiment_score = meta_response.json()

print(f"Meta sentiment score for narrative: {meta_sentiment_score}")
Enter fullscreen mode Exit fullscreen mode

In this code, the first part checks for geographic filtering based on the available language data. If there's no data, it alerts you, and you can adjust your pipeline accordingly. The second part scores the narrative framing, which provides a deeper insight into the sentiment surrounding the defence topic — something we believe is essential to understanding the anomalies fully.

![DATA UNAVAILABLE: countries — verify /news_recent is return
[DATA UNAVAILABLE: countries — verify /news_recent is returning country/region values for topic: defence]

Now that we’ve addressed the anomaly detection, let’s explore three specific builds you can implement based on this pattern:

  1. Geographic Sentiment Tracker: Set a threshold for sentiment scores (e.g., sentiment score < 0.1) and apply a geo filter to track anomalies across different countries. This would help you identify where sentiments are diverging significantly.

  2. Meta-Sentiment Analysis Loop: Create a scheduled job that runs the cluster reason string back through the sentiment endpoint every hour. If the sentiment score deviates by more than 0.5 from the baseline, alert your team for further investigation.

  3. Dynamic Alert System: Implement an alert system that triggers when both the momentum exceeds a certain threshold (e.g., +1.0) and the confidence level is above 0.85. This dual-check will help ensure that alerts are only generated for significant and reliable sentiment shifts.

With these builds, we can ensure that our systems remain responsive to sentiment anomalies, particularly in areas as critical as defence.

To get started with this, check out our documentation at pulsebit.lojenterprise.com/docs. You can copy, paste, and run this code in under 10 minutes to start detecting anomalies effectively.

Top comments (0)