首頁  >  文章  >  後端開發  >  TP5 封裝阿里雲敏感字詞偵測介面

TP5 封裝阿里雲敏感字詞偵測介面

*文
*文原創
2017-12-21 11:14:113880瀏覽

在實際專案中,難免會需要屏蔽敏感詞彙。本文以TP5為實例封裝一個敏感字檢測介面供大家參考。

阿里雲內容偵測API SDK開發套件:https://help.aliyun.com/knowledge_detail/50180.html

<?php
namespace Potting;
include_once &#39;Aliyun/aliyun-php-sdk-core/Config.php&#39;;
use Green\Request\V20170112 as Green;
class TextScan{
	private static $accessKeyId=&#39;&#39;;
	private static $accessKeySecret=&#39;&#39;;
	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(&#39;dataId&#39; => uniqid(),
		&#39;content&#39; => $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 == &#39;normal&#39;){
				$data[&#39;code&#39;]=true;
				$data[&#39;label&#39;]=$result->label;
			}else{
				$data[&#39;code&#39;]=false;
				$data[&#39;label&#39;]=self::getlabel($result->label);
			}
			return $data;
		} catch (Exception $e) {
			print_r($e);
		}

	}
	static private function getlabel($label){
		switch ($label){
			case  &#39;normal&#39;:
				return &#39;正常文本&#39;;
				break;
			case  &#39;spam&#39;:
				return &#39;输入的内容含垃圾信息&#39;;
				break;
			case  &#39;ad&#39;:
				return &#39;输入的内容含广告&#39;;
				break;
			case  &#39;politics&#39;:
				return &#39;输入的内容含渉政&#39;;
				break;
			case  &#39;terrorism&#39;:
				return &#39;输入的内容含暴恐&#39;;
				break;
			case  &#39;abuse&#39;:
				return &#39;输入的内容含辱骂&#39;;
				break;
			case  &#39;porn&#39;:
				return &#39;输入的内容含色情&#39;;
				break;
			case  &#39;flood&#39;:
				return &#39;输入的内容含灌水&#39;;
				break;
			case  &#39;contraband&#39;:
				return &#39;输入的内容含垃违禁&#39;;
				break;
			case  &#39;customized&#39;:
				return &#39;输入的内容包含敏感词&#39;;
				break;
			default:
				return &#39;&#39;;
				break;
		}
	}
}

相關閱讀:

TP5實作郵件傳送服務封裝以及可傳送附件的實例

tp5如何使用bootstrapvalidator進行非同步驗證信箱的範例

#關於TP5.0 MVC入門影片的資源分享

以上就是這篇文章的全部內容,同學們如果有疑問,可以在下方留言區探討喔~

以上是TP5 封裝阿里雲敏感字詞偵測介面的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn