This Python script demonstrates how to use the Sentiment Analyser API to analyze the sentiment of a specific text. The script utilizes the requests
library to make an HTTP GET request to the API, which then returns a sentiment analysis as a JSON response.
import requests
# Set the API endpoint URL
url = "https://sentiment-analyzer3.p.rapidapi.com/Sentiment"
# Define the query parameters with the text to be analyzed
querystring = {"text": "Good Morning"}
# Include the necessary headers, including your RapidAPI key and host information
headers = {
"X-RapidAPI-Key": "{YOUR-API-KEY}",
"X-RapidAPI-Host": "sentiment-analyzer3.p.rapidapi.com"
}
# Send the GET request and capture the response
response = requests.get(url, headers=headers, params=querystring)
# Print the JSON response from the API
print(response.json())
The Sentiment Analyser API provides a detailed JSON object that quantifies the sentiment of the provided text. Below are descriptions of each component in the JSON response:
neg
: Float value representing the percentage of the analyzed text that was deemed negative. A value of 0.0
indicates an absence of negative sentiment.
neu
: Float value indicating the percentage of the text considered neutral. For instance, 25.6%
of the text in the example is neutral.
pos
: Float value showing the percentage of the text recognized as positive. In this example, 74.4%
of the text is positive.
compound
: A normalized compound score that sums all lexicon ratings which have been standardized to fall between -1
(most negative) and +1
(most positive). The score of 0.4404
suggests a predominantly positive sentiment.
sentiment
: A string that categorizes the overall sentiment of the text based on the analysis. Possible values include “Positive,” “Negative,” or “Neutral.” In this case, the sentiment is “Positive”.
{
"neg": 0.0,
"neu": 0.256,
"pos": 0.744,
"compound": 0.4404,
"sentiment": "Positive"
}