Perform Sentiment Analysis and Classification on Text in Python

Cloudmersive
2 min readJan 13, 2022

--

Performing Sentiment Analysis & Classification on text is an excellent way to determine how a given ‘crowd’ of users is reacting to content — based on the language they use, you can measure if a body of text felt positive, negative, or neutral/indifferent towards a given subject. Using the /nlp-v2/analytics/sentiment iteration of the Cloudmersive NLP API v2, you empower your application with the ability to perform Sentiment Analysis in just a few simple steps.

To get started using this API, you’ll first need to use the below command to install the Python SDK:

pip install cloudmersive-nlp-api-clientfrom __future__ import print_function
import time
import cloudmersive_nlp_api_client
from cloudmersive_nlp_api_client.rest import ApiException
from pprint import pprint

The next step is to configure your API authorization key from your free Cloudmersive account, which can be accomplished with the following snippet:

# Configure API key authorization: Apikey
configuration = cloudmersive_nlp_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'

After that, you can just copy in the rest of the API callback function below. The returned result will let you know if the action was successful as well as leave you with a sentiment score & sentence count.

# create an instance of the API class
api_instance = cloudmersive_nlp_api_client.AnalyticsApi(cloudmersive_nlp_api_client.ApiClient(configuration))
input = cloudmersive_nlp_api_client.SentimentAnalysisRequest() # SentimentAnalysisRequest | Input sentiment analysis request
try:
# Perform Sentiment Analysis and Classification on Text
api_response = api_instance.analytics_sentiment(input)
pprint(api_response)
except ApiException as e:
print("Exception when calling AnalyticsApi->analytics_sentiment: %s\n" % e)

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet