How to Perform Hate Speech Analysis & Detection on Text using Java

Cloudmersive
2 min readApr 6, 2022

--

Most of us know right from wrong. So, when it comes to detecting Hate Speech, people should be the best filters, right?

Unfortunately, our bandwidth for detecting hateful written content is severely limited, and the volume of content our websites and applications receive is completely insurmountable.

Thankfully, our Hate Speech detection API can effectively accomplish what a team of human beings cannot: it can automatically classify strings of text for hate speech content, and provide a classification score indicating the degree to which content did, or did not, contain hate speech. Below, we’ll walk through how to use this API in Java with ready-to-run code from our API Console page.

To get started installing the Java SDK with Maven, first add a reference to the repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Following that, add a reference to the dependency:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Lastly, add the import classes and call 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();
HateSpeechAnalysisRequest input = new HateSpeechAnalysisRequest(); // HateSpeechAnalysisRequest | Input hate speech analysis request
try {
HateSpeechAnalysisResponse result = apiInstance.analyticsHateSpeech(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling AnalyticsApi#analyticsHateSpeech");
e.printStackTrace();
}

Easy as pie. Don’t forget to add your inputs where indicated in the documentation and supply your Cloudmersive API key (obtainable on our website — www.cloudmersive.com). For further reference, review the sample response model below:

{
"Successful": true,
"HateSpeechScoreResult": 0,
"SentenceCount": 0
}

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet