使用Java SDK文本反垃圾接口对文本内容进行色情、暴恐、涉政等风险识别。

说明 目前仅支持同步检测。一次请求可以检测多条文本,也可以检测单条文本;按实际检测的文本条数进行计费,具体请参见计费说明

准备工作

在进行具体的服务调用之前,请参见以下步骤,完成准备工作:
  1. 创建阿里云AccessKeyId和AccessKeySecret。具体请参见创建AccessKey
  2. 安装Java依赖。具体请参见安装Java依赖
  3. 下载并在项目工程中引入Extension.Uploader工具类

文本内容检测

文本垃圾检测支持自定义关键词,例如添加一些竞品关键词等。如果被检测的文本中包含您添加的关键词,算法会返回您suggestion=block。

您可以前往云盾内容安全控制台添加关键词,也可以通过API接口添加关键词。关键词编码请使用UTF-8,接口说明请参见文本反垃圾API文档

接口描述
接口 功能 支持的Region 描述
TextScanRequest 文本扫描接口。
  • cn-shanghai
  • cn-beijing
  • cn-shenzhen
  • ap-southeast-1
  • us-west-1
检测场景传递antispam(scene=antispam)。
示例代码
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.*;

public class Main {

    public static void main(String[] args) throws Exception {
        IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "您的AccessKeyId", "您的AccessKeySecret");
        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", "test content");
        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")) {
                    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 + "]");
                            }
                        }else{
                            System.out.println("task process fail:" + ((JSONObject)taskResult).getInteger("code"));
                        }
                    }
                } 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();
        }
    }

}

文本反垃圾结果反馈

如果您认为我们的文本检测结果与您的期望不符,您可以通过文本垃圾结果反馈接口纠正算法检测结果。

系统会将您反馈的结果添加进相似文本黑库或相似文本白库,在您下次提交相似的内容进行检测时,以您通过反馈接口传递的label作为结果返回。具体接口说明请参见文本反垃圾结果反馈API文档

接口描述
接口 功能 支持的Region 描述
TextFeedbackRequest 文本反垃圾结果反馈。
  • cn-shanghai
  • cn-beijing
  • cn-shenzhen
  • ap-southeast-1
  • us-west-1
以人工反馈的检测结果纠正算法检测结果。
示例代码
import com.alibaba.fastjson.JSON;
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.TextFeedbackRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class Main {

    public static void main(String[] args) throws Exception {
        IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "您的AccessKeyId", "您的AccessKeySecret");
        IAcsClient client = new DefaultAcsClient(profile);
        TextFeedbackRequest textFeedbackRequest = new TextFeedbackRequest();
        textFeedbackRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
        textFeedbackRequest.setHttpContentType(FormatType.JSON);
        textFeedbackRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
        textFeedbackRequest.setEncoding("UTF-8");
        textFeedbackRequest.setRegionId("cn-shanghai");

        JSONObject data = new JSONObject();
        data.put("taskId", "txt6z3Na17XbrD4P7QdJzYbk1-1q4seJ");
        data.put("label", "spam");
        System.out.println(JSON.toJSONString(data, true));
        textFeedbackRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);

        // 请务必设置超时时间
        textFeedbackRequest.setConnectTimeout(3000);
        textFeedbackRequest.setReadTimeout(6000);
        try {
            HttpResponse httpResponse = client.doAction(textFeedbackRequest);
            if(httpResponse.isSuccess()){
                JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                System.out.println(JSON.toJSONString(scrResponse, true));
            }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();
        }
    }

}