文档

自定义文本库

更新时间:

本文介绍了如何使用Java SDK管理自定义文本库,以满足文本反垃圾检测场景的个性化需求。

功能描述

根据文本类型的不同,文本库分为关键词文本库和相似文本库;根据管控目的不同,文本库分为白名单、黑名单、疑似名单。关于参数的详细信息,请参见自定义文本库API文档

您需要使用内容安全的API接入地址,调用本SDK接口。关于API接入地址的信息,请参见接入地址(Endpoint)

前提条件

  • 安装Java依赖。关于安装Java依赖的具体操作,请参见安装Java依赖

    说明

    请一定按照安装Java依赖页面中的版本安装,否则会导致调用失败。

  • 如果使用本地文件或者二进制文件检测,请下载并在项目工程中引入Extension.Uploader工具类

获取文本库列表

  • 获取关键词文本库列表

    DescribeKeywordLibRequest describeKeywordLibRequest = new DescribeKeywordLibRequest();
    describeKeywordLibRequest.setServiceModule("open_api");
    
    try {
        // 将返回所有文本库,包括文本反垃圾的关键词文本库、文本反垃圾的相似文本库、图片广告的关键词文本库、语音反垃圾的关键词文本库。
        DescribeKeywordLibResponse describeKeywordLibResponse = client.getAcsResponse(describeKeywordLibRequest);
        System.out.println(JSON.toJSONString(describeKeywordLibResponse));
    
        // 过滤出文本反垃圾场景配置的文本库。
        List<DescribeKeywordLibResponse.KeywordLib> allLibs = describeKeywordLibResponse.getKeywordLibList();
        List<DescribeKeywordLibResponse.KeywordLib> textAntispamKeywordLibs = new ArrayList<DescribeKeywordLibResponse.KeywordLib>();
        for (DescribeKeywordLibResponse.KeywordLib keywordLib : allLibs) {
            String LibType = keywordLib.getLibType();
            String resourceType = keywordLib.getResourceType();
            String source =  keywordLib.getSource();
            // 获取文本反垃圾自定义的关键词文本库。
            if ("textKeyword".equals(LibType) && "TEXT".equals(resourceType)  && "MANUAL".equals(source)) {
                textAntispamKeywordLibs.add(keywordLib);
            }
            // 获取文本反垃圾回流的关键词文本库。
            if ("textKeyword".equals(LibType) && "TEXT".equals(resourceType)  && "FEEDBACK".equals(source)) {
                textAntispamKeywordLibs.add(keywordLib);
            }
        }
    
         System.out.println(JSON.toJSONString(textAntispamKeywordLibs));
     } catch (ClientException e) {
        e.printStackTrace();
    }
  • 获取相似文本库列表(包含自定义和系统回流的相似文本库)

    DescribeKeywordLibRequest describeKeywordLibRequest = new DescribeKeywordLibRequest();
    describeKeywordLibRequest.setServiceModule("open_api");
    
    try {
        // 将返回所有文本库,包括文本反垃圾的关键词文本库、文本反垃圾的相似文本库、图片广告的关键词文本库、语音反垃圾的关键词文本库。
        DescribeKeywordLibResponse describeKeywordLibResponse = client.getAcsResponse(describeKeywordLibRequest);
        System.out.println(JSON.toJSONString(describeKeywordLibResponse));
    
        // 过滤出相似文本库。
        List<DescribeKeywordLibResponse.KeywordLib> allLibs = describeKeywordLibResponse.getKeywordLibList();
        List<DescribeKeywordLibResponse.KeywordLib> similarTextLibs = new ArrayList<DescribeKeywordLibResponse.KeywordLib>();
        for (DescribeKeywordLibResponse.KeywordLib keywordLib : allLibs) {
            String LibType =  keywordLib.getLibType();
            String resourceType = keywordLib.getResourceType();
            String source =  keywordLib.getSource();
            // 获取文本反垃圾自定义的相似文本库。
            if("similarText".equals(LibType) && "TEXT".equals(resourceType)  && "MANUAL".equals(source)) {
                similarTextLibs.add(keywordLib);
            }
    
            // 获取文本反垃圾回流的相似文本库。
            if("similarText".equals(LibType) && "TEXT".equals(resourceType)  && "FEEDBACK".equals(source)) {
                similarTextLibs.add(keywordLib);
            }
        }
        System.out.println(JSON.toJSONString(similarTextLibs));
    } catch (ClientException e) {
        e.printStackTrace();
    }

创建文本库

  • 创建关键词文本库

    CreateKeywordLibRequest createKeywordLibRequest = new CreateKeywordLibRequest();
    createKeywordLibRequest.setServiceModule("open_api");
    createKeywordLibRequest.setName("测试关键词文本库");
    // 设置为文本反垃圾场景使用。
    createKeywordLibRequest.setResourceType("TEXT");
    // 设置类型为关键词。
    createKeywordLibRequest.setLibType("textKeyword");
    // 设置创建黑名单库。
    createKeywordLibRequest.setCategory("BLACK");
    
    try {
        CreateKeywordLibResponse describeKeywordLibResponse = client.getAcsResponse(createKeywordLibRequest);
        // 关键词文本库ID。无异常则表示创建成功。
        String libId = describeKeywordLibResponse.getId();
    } catch (ClientException e) {
        e.printStackTrace();
    }
  • 创建相似文本库

    CreateKeywordLibRequest createKeywordLibRequest = new CreateKeywordLibRequest();
    createKeywordLibRequest.setServiceModule("open_api");
    createKeywordLibRequest.setName("测试相似文本库");
    // 设置为文本反垃圾场景使用。
    createKeywordLibRequest.setResourceType("TEXT");
    // 设置类型为相似文本。
    createKeywordLibRequest.setLibType("similarText");
    // 相似文本库支持创建黑名单库、白名单、疑似名单。
    createKeywordLibRequest.setCategory("BLACK");
    
    try {
        CreateKeywordLibResponse describeKeywordLibResponse = client.getAcsResponse(createKeywordLibRequest);
        // 相似文本库ID。无异常则表示创建成功。
        String libId = describeKeywordLibResponse.getId();
    } catch (ClientException e) {
        e.printStackTrace();
    }

修改文本库

更新文本库库名以及修改文本库所使用的检测场景。

UpdateKeywordLibRequest updateKeywordLibRequest = new UpdateKeywordLibRequest();
// 设置待操作的文本库ID。
updateKeywordLibRequest.setId(0);
// 设置新的文本库名称。
updateKeywordLibRequest.setName("修改后的名称");
// 设置新的BizType。
updateKeywordLibRequest.setBizTypes(JSON.toJSONString(Arrays.asList("新的BizType_1", "新的BizType_2")));

try {
    UpdateKeywordLibResponse updateKeywordLibResponse = client.getAcsResponse(updateKeywordLibRequest);
    // 请求ID。无异常则表示修改成功。
    String requestId = updateKeywordLibResponse.getRequestId();
} catch (ClientException e) {
    e.printStackTrace();
}

删除文本库

重要

删除文本库也将删除文本库下的文本。系统回流的文本库不允许删除。

DeleteKeywordLibRequest deleteKeywordLibRequest = new DeleteKeywordLibRequest();
// 设置待删除的文本库ID。
deleteKeywordLibRequest.setId(3353);

try {
    DeleteKeywordLibResponse deleteKeywordLibResponse = client.getAcsResponse(deleteKeywordLibRequest);
    // 请求ID。无异常则表示删除成功。
    String requestId = deleteKeywordLibResponse.getRequestId();
} catch (ClientException e) {
    e.printStackTrace();
}

查找文本

默认分页获取所有文本。如果设置了Keyword字段,将模糊查找包含该字段值的文本。

DescribeKeywordRequest describeKeywordRequest = new DescribeKeywordRequest();
// 设置待查询的文本库ID。
describeKeywordRequest.setKeywordLibId(0);
describeKeywordRequest.setPageSize(10);
describeKeywordRequest.setCurrentPage(1);
// 可选,用于模糊查。
describeKeywordRequest.setKeyword("查询文本");
try {
    DescribeKeywordResponse describeKeywordResponse = client.getAcsResponse(describeKeywordRequest);
    // 总数。
    Integer totalCount = describeKeywordResponse.getTotalCount();
    // 当前页。
    Integer currentPage = describeKeywordResponse.getCurrentPage();
    // 分页大小。
    Integer pageSize = describeKeywordResponse.getPageSize();
    for (DescribeKeywordResponse.Keyword keywordItem : describeKeywordResponse.getKeywordList()) {
        // 关键词主键ID。
        Integer id = keywordItem.getId();
        // 创建时间。
        String createTime = keywordItem.getCreateTime();
        // 关键词的匹配命中次数。
        Integer hitCount = keywordItem.getHitCount();
        // 关键词。
        String keyword = keywordItem.getKeyword();
    }
} catch (ClientException e) {
    e.printStackTrace();
}

添加文本

CreateKeywordRequest createKeywordRequest = new CreateKeywordRequest();
// 设置待操作的文本库ID。
createKeywordRequest.setKeywordLibId(0L);
// 待添加的文本。
createKeywordRequest.setKeywords(JSON.toJSONString(Arrays.asList("关键词_1", "关键词_2")));

try {
    CreateKeywordResponse createKeywordResponse = client.getAcsResponse(createKeywordRequest);
    // 已成功添加的关键词数量。
    Integer successCount = createKeywordResponse.getSuccessCount();
    // 无效的关键词列表,即未添加成功的关键词。
    List<String> invalidKeywordList = createKeywordResponse.getInvalidKeywordList();
} catch (ClientException e) {
    e.printStackTrace();
}

删除文本

DeleteKeywordRequest deleteKeywordRequest = new DeleteKeywordRequest();
// 设置待操作的文本库ID。
deleteKeywordRequest.setKeywordLibId(String.valueOf("文本库ID"));
// 待删除的文本。
deleteKeywordRequest.setIds(JSON.toJSONString(Arrays.asList(1, 2)));

try {
    DeleteKeywordResponse deleteKeywordResponse = client.getAcsResponse(deleteKeywordRequest);
    // 请求ID。无异常则表示删除成功。
    String requestId = deleteKeywordResponse.getRequestId();
} catch (ClientException e) {
    e.printStackTrace();
}
  • 本页导读 (0)
文档反馈