Perform Sentiment Analysis & Classification on Text using Java
Take advantage of our powerful Sentiment Analysis API to instantly classify text as positive, negative or neutral using Natural Language Processing. Below, code snippets are available to easily connect using Java — all you need to do is copy & paste, add your input, and supply your Cloudmersive API key where indicated in the documentation to call the function.
First things first: to install the Java SDK with Maven, copy the below reference & add it to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add the next reference to the dependency in pom.xml:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
To wrap things up, add the import classes and copy in the NLP function:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.AnalyticsApi;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");AnalyticsApi apiInstance = new AnalyticsApi();
SentimentAnalysisRequest input = new SentimentAnalysisRequest(); // SentimentAnalysisRequest | Input sentiment analysis request
try {
SentimentAnalysisResponse result = apiInstance.analyticsSentiment(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling AnalyticsApi#analyticsSentiment");
e.printStackTrace();
}
Now you’re good to go. A successful call should yield a response in the below format:
{
"Successful": true,
"SentimentClassificationResult": "string",
"SentimentScoreResult": 0,
"SentenceCount": 0
}