直接上官网链接:文本反垃圾检测 - 内容安全 - 阿里云
前期准备工作:
1、在阿里云官网注册账号
2、在管理控制台,管理AccessKeyID和AccessKeySecret
在访问控制中给账户授予内容安全的权限
?
?
?
?
3、 文本内容垃圾检测
文本垃圾内容检测:文本同步检测 - 内容安全 - 阿里云
文本垃圾内容Java SDK: 文本反垃圾检测 - 内容安全 - 阿里云
一、引入依赖包
二、根据官网提供的实例代码进行改写
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.ClientException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.TextScanRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.util.*;
/**
* @Author admin
* @Date 2022/1/28 17:56
* 文本内容检测
**/
@Component
public class VerifyText {
@Value("${aliyun.accesskey}")
private String accesskey;
@Value("${aliyun.secretkey}")
private String secretkey;
public List<Map<String,String>> verify(String text) throws Exception {
IClientProfile profile = DefaultProfile
.getProfile("cn-shanghai", accesskey, secretkey);
DefaultProfile
.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
IAcsClient client = new DefaultAcsClient(profile);
TextScanRequest textScanRequest = new TextScanRequest();
textScanRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
textScanRequest.setHttpContentType(FormatType.JSON);
textScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
textScanRequest.setEncoding("UTF-8");
textScanRequest.setRegionId("cn-shanghai");
List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
Map<String, Object> task1 = new LinkedHashMap<String, Object>();
task1.put("dataId", UUID.randomUUID().toString());
/**
* 待检测的文本,长度不超过10000个字符。
*/
task1.put("content", text);
tasks.add(task1);
JSONObject data = new JSONObject();
/**
* 检测场景。文本垃圾检测请传递antispam。
**/
data.put("scenes", Arrays.asList("antispam"));
data.put("tasks", tasks);
System.out.println(JSON.toJSONString(data, true));
textScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
// 请务必设置超时时间。
textScanRequest.setConnectTimeout(3000);
textScanRequest.setReadTimeout(6000);
try {
HttpResponse httpResponse = client.doAction(textScanRequest);
if(httpResponse.isSuccess()){
JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
System.out.println(JSON.toJSONString(scrResponse, true));
if (200 == scrResponse.getInteger("code")) {
List<Map<String,String>> resultMaps = new ArrayList<>();
JSONArray taskResults = scrResponse.getJSONArray("data");
for (Object taskResult : taskResults) {
if(200 == ((JSONObject)taskResult).getInteger("code")){
JSONArray sceneResults = ((JSONObject)taskResult).getJSONArray("results");
for (Object sceneResult : sceneResults) {
String scene = ((JSONObject)sceneResult).getString("scene");
String suggestion = ((JSONObject)sceneResult).getString("suggestion");
// 根据scene和suggetion做相关处理。
// suggestion为pass表示未命中垃圾。suggestion为block表示命中了垃圾,可以通过label字段查看命中的垃圾分类。
System.out.println("args = [" + scene + "]");
System.out.println("args = [" + suggestion + "]");
Map<String, String> resultMap = new HashMap<>();
resultMap.put("scene",scene);
resultMap.put("suggestion",suggestion);
resultMaps.add(resultMap);
}
}else{
System.out.println("task process fail:" + ((JSONObject)taskResult).getInteger("code"));
}
}
return resultMaps;
} else {
System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
}
}else{
System.out.println("response not success. status:" + httpResponse.getStatus());
}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
?三、测试
import com.qing.aliyun.verify.VerifyText;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Map;
/**
* @Author admin
* @Date 2022/1/28 18:13
**/
@SpringBootTest
@RunWith(SpringRunner.class)
public class test {
@Autowired
private VerifyText verifyText;
@Test
public void test() throws Exception {
String text = "你他妈真是个傻缺";
List<Map<String, String>> resultMaps = verifyText.verify(text);
for (Map<String, String> resultMap : resultMaps) {
System.out.println("resultMap = " + resultMap);
}
}
}
检测结果为:?
?
?下面是审核成功的:
@Test
public void test() throws Exception {
String text = "该配合你演出的我视而不见~";
List<Map<String, String>> resultMaps = verifyText.verify(text);
for (Map<String, String> resultMap : resultMaps) {
System.out.println("resultMap = " + resultMap);
}
}
?语音审核和图片审核都是差不多的,看一下阿里云的产品文档就行了
?
?
|