DEV Community

Pulsebit News Sentiment API
Pulsebit News Sentiment API

Posted on

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

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

We recently stumbled upon a striking anomaly: a 24-hour momentum spike of -0.116 in the economy topic. This drop suggests that sentiment around economic themes is taking a significant nosedive. The implications of such a shift can ripple through various sectors, especially when it comes to understanding economic narratives and their influences.

The problem here is clear. If your model doesn't account for multilingual origins or dominant entities, you might have missed this critical shift by several hours. The leading language in this context is likely English, given the prevalence of English-language news sources. Without a robust mechanism to filter and prioritize sentiment from different languages or dominant entities, your insights can become skewed or even completely missed.

es coverage led by 26.0 hours. id at T+26.0h. Confidence sco
es coverage led by 26.0 hours. id at T+26.0h. Confidence scores: en 0.87, fr 0.85, es 0.85 Source: Pulsebit /sentiment_by_lang.

Let’s get into the code that can help identify these anomalies. Here’s how you can leverage our API to catch this drop in sentiment:

import requests

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


# Setting up the parameters for the query
topic = 'economy'
momentum = -0.116
score = +0.000
confidence = 0.00

# Geographic origin filter (Note: Current data lacks geo filter)
# Example: Use language/country filters when available
geo_filter = {
    'language': 'en',  # English
    'country': 'US'    # United States
}

# Check for available data in the dataset
response = requests.get(f"https://api.pulsebit.com/dataset/daily_dataset?topic={topic}")
data = response.json()

# If geo filter data becomes available, you can filter sentiment
if 'geo_data' in data:
    print("Geo filter data available. Proceed with analysis.")
else:
    print("DATA UNAVAILABLE: no geo filter data returned.")

# Meta-sentiment moment: Analyze cluster reason
cluster_reason = "Clustered by shared themes: must, war, not, watch:, pull."
sentiment_response = requests.post("https://api.pulsebit.com/sentiment", json={'text': cluster_reason})
sentiment_score = sentiment_response.json().get('score')

print(f"Sentiment score for cluster reason: {sentiment_score}")
Enter fullscreen mode Exit fullscreen mode

In this code, we set up a basic structure to query our topic of interest—economy—and capture the momentum spike. A geographic filter would allow you to refine your analysis to specific regions, but note that our current data lacks this capability. However, it’s important to keep this in mind for future improvements.

Geographic detection output for economy. in leads with 2 art
Geographic detection output for economy. in leads with 2 articles and sentiment +0.08. Source: Pulsebit /news_recent geographic fields.

Additionally, we run the cluster reason string through a meta-sentiment analysis to evaluate how the narrative is framed. This is crucial as it adds an extra layer of understanding to the sentiment shifts. The output gives us insights into how themes are clustered, which can be pivotal in gauging public sentiment.

Now that we’ve seen how to detect these anomalies, here are three specific builds you can create using this pattern:

  1. Geo-filtered Sentiment Tracker: Build a sentiment analysis tool that uses a geographic filter to identify significant shifts in economic sentiment across specific regions. Set a threshold of momentum less than -0.1 to trigger alerts and notify you of critical changes.

  2. Meta-Sentiment Analysis Dashboard: Create a dashboard that visualizes the sentiment scores of cluster reasons over time. Use the meta-sentiment loop to generate insights from the framing of narratives, particularly focusing on themes like "must," "war," and "not." This can help in predicting market responses.

  3. Anomaly Detection Pipeline: Develop a pipeline that detects anomalies in sentiment by comparing current sentiment scores against a historical baseline. Use the 24-hour momentum spike as a signal. Set up a routine that triggers when a momentum drop exceeds -0.1, allowing you to quickly analyze the context behind the shift.

Getting started with our API is straightforward. You can find everything you need at pulsebit.lojenterprise.com/docs. With the code provided, you can copy, paste, and run it in under 10 minutes, paving the way for deeper insights into economic sentiment anomalies.

Top comments (0)