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 came across an intriguing anomaly: a 24-hour momentum spike of -0.116 in the economy topic. This shift is noteworthy, especially when considering the sparse cluster story titled "U.S. Foreign Policy and Israel's Influence," which revealed no articles. The absence of coverage around such a significant shift suggests a critical gap in how sentiment analysis is performed, particularly in multilingual contexts.

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

When you rely solely on English-language data, your model missed this by several hours, potentially leaving you blind to important nuances arising from non-English sources. In this case, the dominant language of the region is European languages, which may have contributed to the lack of coverage. This oversight could lead to reactive strategies based on incomplete information, hampering your decision-making process.

To catch this anomaly with our API, we can leverage Python. Here's how you can get started:

import requests

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


# Define the parameters for the API request
topic = 'economy'
score = +0.000
confidence = 0.00
momentum = -0.116

# Geographic origin filter (placeholder: no geo filter data returned)
# In a real scenario, you would filter by language/country here
geo_filter = {'language': 'en', 'country': 'EU'}  # Example filter

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


# Call the API endpoint for sentiment analysis
response = requests.post('https://api.pulsebit.io/sentiment', json={
    'topic': topic,
    'score': score,
    'confidence': confidence,
    'momentum': momentum,
    'geo_filter': geo_filter
})

# Check the response
if response.status_code == 200:
    print("Sentiment Analysis Result:", response.json())
else:
    print("Error:", response.status_code, response.text)
Enter fullscreen mode Exit fullscreen mode

This code snippet sets up a basic structure to analyze sentiment around the economy, though currently, no geo filter data is returned. In scenarios where language and country data are available, you can filter the results to hone in on specific regions, enhancing the accuracy of your analysis.

Next, we can dive deeper into the cluster's narrative framing. The cluster reason string was "Clustered by shared themes: must, war, not, watch:, pull.". This can be scored for meta-sentiment using the following code:

# Input the cluster reason for meta-sentiment analysis
cluster_reason = "Clustered by shared themes: must, war, not, watch:, pull."

# Call the sentiment API for the cluster reason
meta_sentiment_response = requests.post('https://api.pulsebit.io/sentiment', json={
    'text': cluster_reason
})

# Check the response for meta-sentiment
if meta_sentiment_response.status_code == 200:
    print("Meta-Sentiment Analysis Result:", meta_sentiment_response.json())
else:
    print("Error:", meta_sentiment_response.status_code, meta_sentiment_response.text)
Enter fullscreen mode Exit fullscreen mode

This allows you to quantify the narrative itself, giving you a clearer picture of how these themes relate to the economic sentiment anomaly.

Now, let’s build three specific applications based on this analysis:

  1. Geographic Sentiment Alert: Set up a threshold for momentum spikes, say below -0.1 in the economy topic. Use the geo filter to trigger alerts when sentiment in non-English regions shows significant drops. This can provide early warnings about potential economic shifts.

  2. Narrative Impact Tracker: Establish a routine to run the meta-sentiment loop on clustered themes. For example, if the sentiment score of the cluster reason string is below a threshold (e.g., 40), flag it for further investigation. This could reveal how narrative framing shifts public sentiment.

  3. Comparative Analysis Dashboard: Create a dashboard that compares current sentiment metrics against historical baselines. Implement a visualization that highlights spikes like the recent -0.116 momentum for the economy topic, particularly focusing on how these relate to articles from different languages.

By addressing the themes forming around the economy, such as those highlighted in the analysis, we can stay ahead of the curve. More insights are available at pulsebit.lojenterprise.com/docs, where you can copy, paste, and run this in under 10 minutes.

Top comments (0)