Perform Hate Speech Analysis and Detection on Text in Python
The first step to protecting your applications & websites from hate speech is detecting it quickly in your content. Thankfully, there’s an API for that: the Cloudmersive NLP API v2 offers developers a simple iteration capable of performing hate speech analysis and detection on text (please note this currently only supports English language input). If any content contains hate speech, you’ll receive a hate speech score greater than zero in the API response. Let’s walk through how you can use this API in Python to keep your content clean in a few simple steps.
To use this API, you’ll need to start by running the Python SDK installation command — you can copy the below code for that:
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
After this step & before you copy in the callback function, you’ll need to use the below snippet for API key authorization (you can get your API key by registering a free account on Cloudmersive.com):
# Configure API key authorization: Apikey
configuration = cloudmersive_nlp_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
Now you can finish up the process by copying the rest of the code below:
# 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.HateSpeechAnalysisRequest() # HateSpeechAnalysisRequest | Input hate speech analysis requesttry:
# Perform Hate Speech Analysis and Detection on Text
api_response = api_instance.analytics_hate_speech(input)
pprint(api_response)
except ApiException as e:
print("Exception when calling AnalyticsApi->analytics_hate_speech: %s\n" % e)