DEV Community

Pulsebit News Sentiment API
Pulsebit News Sentiment API

Posted on

How to Detect Agriculture Sentiment Shifts with the Pulsebit API (Python)

How to Detect Agriculture Sentiment Shifts with the Pulsebit API (Python)

The Problem

If you've ever dug into agricultural data, you know the pain of scraping sentiment data from multiple sources. You might find yourself juggling APIs, RSS feeds, and even social media data, trying to stitch together a coherent picture of sentiment trends. The issue is not just about gathering data; it’s about ensuring it’s timely and relevant. What you need is a streamlined solution that gives you actionable insights without the clutter.

The Solution

Enter the Pulsebit API. With just one endpoint, you can get the sentiment analysis you need without the hassle of DIY scraping. The /news_semantic endpoint provides a detailed view of sentiment shifts in agriculture, and it’s particularly useful right now. Why? Because the current sentiment score for agriculture is at 0.00 with momentum at +0.25 and a high confidence level of 0.87. This signals that something is brewing; the sentiment is stagnant but gaining momentum.

The Code

Let’s get started with a simple Python script to fetch this data. You'll need the requests library, so make sure to install it if you haven’t already:

pip install requests
Enter fullscreen mode Exit fullscreen mode

Now, here’s how you can make a GET request to the Pulsebit API:

import requests

![Left: Python GET /news_semantic call for 'agriculture'. Righ](https://pub-c3309ec893c24fb9ae292f229e1688a6.r2.dev/figures/g3_code_output_split_1772853033936.png)
*Left: Python GET /news_semantic call for 'agriculture'. Right: live JSON response structure. Three lines of Python. Clean JSON. No infrastructure required. Source: Pulsebit /news_semantic.*


def get_agriculture_sentiment():
    url = "https://pulsebit.lojenterprise.com/api/news_semantic"
    params = {
        'topic': 'agriculture',
        'region': 'india'
    }

    response = requests.get(url, params=params)

    if response.status_code == 200:
        return response.json()
    else:
        print("Error fetching data:", response.status_code)
        return None

data = get_agriculture_sentiment()
print(data)
Enter fullscreen mode Exit fullscreen mode

Make sure to handle any errors gracefully and check for the status code to ensure the request was successful.

Reading the Response

When you receive a response, it will look something like this:

{
    "TOPIC": "agriculture",
    "momentum_24h": +0.250,
    "sentiment_score": +0.000,
    "confidence": 0.870,
    "sentiment_index_0_100": 70.0,
    "direction": "rising",
    "semantic_clusters": 19,
    "region": "india",
    "semantic_similarity_avg": 0.234
}
Enter fullscreen mode Exit fullscreen mode

Here’s a breakdown of key fields:

  • TOPIC: The subject of interest, which is agriculture.
  • momentum_24h: The change in sentiment momentum in the last 24 hours, currently at +0.250.
  • sentiment_score: The overall sentiment score, which is at +0.000. This indicates neutral sentiment, but the momentum is rising.
  • confidence: This is a solid 0.870, meaning you can trust this data.
  • sentiment_index_0_100: This index is at 70.0, indicating a generally positive sentiment.
  • direction: Indicates that the sentiment is rising.
  • semantic_clusters: There are 19 clusters, meaning diverse sources contributing to the sentiment.
  • region: The data is specific to india.
  • semantic_similarity_avg: An average similarity of 0.234, giving you a sense of how closely related the sentiment sources are.

Three Use Cases

  1. Algo Alert: Set up an algorithmic alert when the sentiment changes significantly. Given the current momentum, you might want to trigger alerts when momentum exceeds +0.5.

  2. Slack Bot: Build a Slack bot that posts daily updates. When sentiment shifts, you can notify your team about potential opportunities or risks in agriculture.

  3. Dashboard: Create a dashboard to visualize sentiment trends over time. Use the momentum and clusters to guide your decision-making process.

Get Started

Ready to dive in? Check out the Pulsebit API Documentation to get all the details you need to start leveraging sentiment analysis in your projects.

With the data we have, it's clear that while the sentiment score is flat, the momentum is on the rise. This could be the perfect time for you to make strategic decisions in agriculture. Don’t let the opportunity slip by!

Arabic coverage led by 4.2 hours. English at T+4.2h. Confide
Arabic coverage led by 4.2 hours. English at T+4.2h. Confidence scores: Arabic 0.82, Mandarin 0.68, English 0.41 Source: Pulsebit /sentiment_by_lang.

Top comments (0)