在实际项目中,难免会需要屏蔽敏感词汇。本文以tp5为实例封装一个敏感词检测接口供大家参考。
阿里云内容检测api sdk开发包:https://help.aliyun.com/knowledge_detail/50180.html
<?php
namespace potting;
include_once 'aliyun/aliyun-php-sdk-core/config.php';
use green\request\v20170112 as green;
class textscan{
private static $accesskeyid='';
private static $accesskeysecret='';
static public function text($content){
date_default_timezone_set("prc");
$iclientprofile = \defaultprofile::getprofile("cn-shanghai", self::$accesskeyid, self::$accesskeysecret);
\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' => $content
);
/**
* 文本垃圾检测: antispam
* 关键词检测: keyword
**/
$request->setcontent(json_encode(array("tasks" => array($task1),
"scenes" => array("antispam"))));
try {
$response = $client->getacsresponse($request);
$result=array();
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;
$result=$taskresult;
}
}else{
print_r("task process fail:" + $response->code);
}
}
}else{
print_r("detect not success. code:" + $response->code);
}
$result=$result->results[0];
$data=array();
if($result->label == 'normal'){
$data['code']=true;
$data['label']=$result->label;
}else{
$data['code']=false;
$data['label']=self::getlabel($result->label);
}
return $data;
} catch (exception $e) {
print_r($e);
}
}
static private function getlabel($label){
switch ($label){
case 'normal':
return '正常文本';
break;
case 'spam':
return '输入的内容含垃圾信息';
break;
case 'ad':
return '输入的内容含广告';
break;
case 'politics':
return '输入的内容含渉政';
break;
case 'terrorism':
return '输入的内容含暴恐';
break;
case 'abuse':
return '输入的内容含辱骂';
break;
case 'porn':
return '输入的内容含色情';
break;
case 'flood':
return '输入的内容含灌水';
break;
case 'contraband':
return '输入的内容含垃违禁';
break;
case 'customized':
return '输入的内容包含敏感词';
break;
default:
return '';
break;
}
}
}
相关阅读:
tp5实现邮件发送服务封装以及可发附件的实例
tp5如何使用bootstrapvalidator进行异步验证邮箱的示例
关于tp5.0 mvc入门视频的资源分享
以上就是本篇文章的全部内容,同学们如果有疑问,可以在下方评论区探讨哦~
以上就是tp5 封装阿里云敏感词检测接口的详细内容。