How to Detect Profane, Obscene Language in Text using PHP
Incorporating a profanity detection service into your applications can play an important role in automatic content moderation, preventing foul language from littering public reviews and other elements of user-generated content.
Using the below PHP code examples, you can easily implement a Profanity and Obscene Language Analysis & Detection API for free. This service will provide a profanity score result ranging from 0.0–1.0, where lower values indicate clean language was used, and higher values indicate an increasing amount of profanity or obscene language usage (values above 0.7 contain a large amount of profanity).
Let’s run the following command to install the client SDK:
composer require cloudmersive/cloudmersive_nlp_api_client
After that, let’s copy & paste the ready-to-run PHP code examples below to structure our API call:
<?php
require_once(__DIR__ . '/vendor/autoload.php');
// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');
$apiInstance = new Swagger\Client\Api\AnalyticsApi(
new GuzzleHttp\Client(),
$config
);
$input = new \Swagger\Client\Model\ProfanityAnalysisRequest(); // \Swagger\Client\Model\ProfanityAnalysisRequest | Input profanity analysis request
try {
$result = $apiInstance->analyticsProfanity($input);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling AnalyticsApi->analyticsProfanity: ', $e->getMessage(), PHP_EOL;
}
?>
To authenticate access, you’ll need a free-tier API key (which you can copy into the $config line where indicated above). To get one, register a free account on our website; this will provide you with a limit of 800 free API calls per month with zero commitments upon hitting that limit.