Perform Sentiment Analysis and Classification on Text in Python
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
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 requesttry:
# Perform Sentiment Analysis and Classification on Text
api_response = api_instance.analytics_sentiment(input)
except ApiException as e:
print("Exception when calling AnalyticsApi->analytics_sentiment: %s\n" % e)