DEV Community

Pulsebit News Sentiment API
Pulsebit News Sentiment API

Posted on

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

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

We just discovered a notable anomaly: a 24-hour momentum spike of +0.417 related to immigration sentiment. This spike is particularly intriguing given the current political climate and public discourse surrounding immigration policies. It suggests that something significant is happening beneath the surface, and we need to dig deeper to understand the implications.

When our model processed this data, it missed the opportunity to catch this anomaly by several hours—specifically, it failed to consider the dominant language and entity themes at play. In this case, the leading language was English, with the dominant entity being Trump’s immigration policies. If your pipeline isn’t set up to handle multilingual origins or the prominence of certain entities, you might miss critical sentiment shifts like this one.

en coverage led by 18.6 hours. it at T+18.6h. Confidence sco
en coverage led by 18.6 hours. it at T+18.6h. Confidence scores: en 0.86, fr 0.85, es 0.86 Source: Pulsebit /sentiment_by_lang.

Here’s how we can catch this anomaly using our API. First, we’ll focus on the immigration topic with a specific score and momentum value:

import requests

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


# Define the parameters
topic = 'immigration'
score = +0.000
confidence = 0.00
momentum = +0.417

# Geographic origin filter (hypothetical since we don't have geo data)
geo_filter = {
    "language": "en",
    "country": "US"
}

![Geographic detection output for immigration. in leads with 1](https://pub-c3309ec893c24fb9ae292f229e1688a6.r2.dev/figures/g3_geo_output_1773377374162.png)
*Geographic detection output for immigration. in leads with 11 articles and sentiment -0.03. Source: Pulsebit /news_recent geographic fields.*


# Send a request to our API
response = requests.get(f'https://api.pulsebit.com/topic/{topic}', params=geo_filter)
data = response.json()

print(data)
Enter fullscreen mode Exit fullscreen mode

In the above code, we're hypothetically filtering by language and country, but note that we currently don’t have geo filter data returned. You need to verify the /dataset/daily_dataset and /news_recent endpoints for the immigration topic to ensure that such data is available for your specific needs. When geo-filtering is possible, it allows you to refine your analysis to a specific region, enhancing the relevance and accuracy of your insights.

Next, we’ll score the narrative framing itself using the cluster reason string we received:

# Cluster reason string
cluster_reason = "Clustered by shared themes: qui, économique, sur, trump, claqué."

# Send a request to score the narrative
sentiment_response = requests.post('https://api.pulsebit.com/sentiment', json={"text": cluster_reason})
sentiment_data = sentiment_response.json()

print(sentiment_data)
Enter fullscreen mode Exit fullscreen mode

This code snippet sends the cluster reason through our sentiment scoring endpoint, providing a deeper understanding of how the narrative is framed. This is crucial because it not only reflects the sentiment around immigration but also the underlying political themes that could influence public opinion and behavior.

Now, let’s talk about three specific builds we can create with this pattern:

  1. Geo-Filtered Alert System: Set a threshold of +0.300 momentum for immigration sentiment spikes specifically from the US. When this threshold is crossed, trigger an alert to notify your team. This can be set up using the aforementioned geo filter.

  2. Meta-Sentiment Dashboard: Build a dashboard that visualizes the sentiment scores derived from cluster reason strings. Use a threshold of +0.500 sentiment score to indicate significant narrative shifts. This will help you track how narratives evolve over time.

  3. Comparative Analysis Tool: Create a tool that compares current immigration sentiment to historical baselines. Set a threshold of 10% deviation from average sentiment scores over the past month to identify noteworthy spikes.

These builds allow you to harness the power of sentiment analysis in a way that’s responsive to current events, particularly in the context of immigration discourse.

To get started, check our detailed documentation at pulsebit.lojenterprise.com/docs. You can copy, paste, and run the provided code in under 10 minutes to start detecting these sentiment anomalies for yourself.

Top comments (0)