This topic describes how to use the PHP SDK to moderate text for spam such as pornography and terrorist content.
Moderate text for spam
Text anti-spam allows you to add custom terms, such as brand terms of competitors. If the text being moderated contains the terms you add, the value of the suggestion parameter returned by the text anti-spam algorithm is block.
You can log on to the Alibaba Cloud Content Moderation console or call the corresponding operation to add terms. The terms are UTF-8-encoded. For more information about the text anti-spam operation, see Moderate text for spam.
<? php
include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
use Green\Request\V20180509 as Green;
$iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
$client = new DefaultAcsClient($iClientProfile);
$request = new Green\TextScanRequest();
$request->setMethod("POST");
$request->setAcceptFormat("JSON");
$task1 = array('dataId' => uniqid(),
'content' => 'Text to be moderated'
);
/**
* Set the scenes parameter to antispam.
**/
$request->setContent(json_encode(array("tasks" => array($task1),
"scenes" => array("antispam"))));
try {
$response = $client->getAcsResponse($request);
print_r($response);
if(200 == $response->code){
$taskResults = $response->data;
foreach ($taskResults as $taskResult) {
if(200 == $taskResult->code){
$sceneResults = $taskResult->results;
foreach ($sceneResults as $sceneResult) {
$scene = $sceneResult->scene;
$suggestion = $sceneResult->suggestion;
// Take a further action on the text based on the values of the scene and suggestion parameters.
// do something
}
}else{
print_r("task process fail:" + $response->code);
}
}
}else{
print_r("detect not success. code:" + $response->code);
}
} catch (Exception $e) {
print_r($e);
}
Provide feedback on the text anti-spam result
If the text anti-spam result does not meet your expectations, you can call the TextFeedbackRequest operation to provide feedback on the text anti-spam result. In the feedback, you can pass a correct label for the text moderated.
The server corrects the text anti-spam result based on your feedback and adds the feedback to a text pattern blacklist or whitelist. When you submit a text pattern next time, the server returns the text anti-spam result based on the label that you passed by calling the TextFeedbackRequest operation. For more information about the feedback operation, see Give feedback on moderation results.
<? php
include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
use Green\Request\V20180509 as Green;
$iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
$client = new DefaultAcsClient($iClientProfile);
$request = new Green\TextFeedbackRequest();
$request->setMethod("POST");
$request->setAcceptFormat("JSON");
$task1 = array('dataId' => uniqid(),
'content' => 'Text to be moderated'
);
/**
* Set the scenes parameter to antispam.
**/
$request->setContent(json_encode(array("taskId" => 'test taskId', "content" => "Text to be moderated",
"label" => 'spam')));
try {
$response = $client->getAcsResponse($request);
print_r($response);
} catch (Exception $e) {
print_r($e);
}