使用Java SDK自定义文本库并管理文本库中的文本内容,可以满足文本反垃圾检测场景的个性化需求。根据文本类型的不同,文本库分为关键词文本库和相似文本文本库;根据管控目的不同,文本库分为白名单、黑名单、疑似名单。本示例为您介绍如何使用Alibaba Cloud SDK for Java创建自定义文本库。

前提条件

在使用本教程之前,请确保已完成以下操作:
  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见 阿里云开发工具包(SDK)
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-green -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-green</artifactId>
            <version>3.5.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.61</version>
        </dependency>
    </dependencies>

代码示例

  • 创建公共方法
    public static IAcsClient create() {
        DefaultProfile profile = DefaultProfile.getProfile(
                "<your-region-id>", // 您的可用区ID
                "<your-access-key-id>", // 您的AccessKey ID
                "<your-access-key-secret>"); // 您的AccessKey Secret
        return new DefaultAcsClient(profile);
    }
  • 获取文本库列表
    private static void describeKeywordLibRequest(IAcsClient client) {
    
        DescribeKeywordLibRequest request = new DescribeKeywordLibRequest();
        // open_api:用于内容检测API功能的文本库。
        request.setServiceModule("open_api");
    
        try {
            DescribeKeywordLibResponse response = client.getAcsResponse(request);
            System.out.println(JSON.toJSONString(response, true));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
  • 创建文本库
    private static void createKeywordLib(IAcsClient client) {
    
        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);
            System.out.println(JSON.toJSONString(describeKeywordLibResponse, true));
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
  • 修改文本库
    private static void updateKeywordLib(IAcsClient client) {
        UpdateKeywordLibRequest updateKeywordLibRequest = new UpdateKeywordLibRequest();
        //设置要操作的文本库id
        updateKeywordLibRequest.setId(6743);
        //设置新的文本库名称
        updateKeywordLibRequest.setName("测试修改名称");
        //设置新的bizType
        updateKeywordLibRequest.setBizTypes(JSON.toJSONString(Arrays.asList("comment", "title")));
    
        try {
            UpdateKeywordLibResponse updateKeywordLibResponse = client.getAcsResponse(updateKeywordLibRequest);
            System.out.println(JSON.toJSONString(updateKeywordLibResponse, true));
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
  • 删除文本库
    private static void deleteKeywordLib(IAcsClient client) {
        DeleteKeywordLibRequest deleteKeywordLibRequest = new DeleteKeywordLibRequest();
        //设置要删除的文本库id
        deleteKeywordLibRequest.setId(6743);
        try {
            DeleteKeywordLibResponse deleteKeywordLibResponse = client.getAcsResponse(deleteKeywordLibRequest);
            System.out.println(JSON.toJSONString(deleteKeywordLibResponse, true));
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
  • 查找文本
    private static void describeKeyword(IAcsClient client) {
        DescribeKeywordRequest describeKeywordRequest = new DescribeKeywordRequest();
        //要查询的文本库id
        describeKeywordRequest.setKeywordLibId(6741);
        describeKeywordRequest.setPageSize(10);
        describeKeywordRequest.setCurrentPage(1);
        //可选,用于模糊查
        describeKeywordRequest.setKeyword("你");
        try {
            DescribeKeywordResponse describeKeywordResponse = client.getAcsResponse(describeKeywordRequest);
            System.out.println(JSON.toJSONString(describeKeywordResponse, true));
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
  • 添加文本
    private static void createKeyword(IAcsClient client) {
        CreateKeywordRequest createKeywordRequest = new CreateKeywordRequest();
        //设置文本库id
        createKeywordRequest.setKeywordLibId(6741);
        //要添加的文本
        createKeywordRequest.setKeywords(JSON.toJSONString(Arrays.asList("法克儿")));
    
        try {
            CreateKeywordResponse createKeywordResponse = client.getAcsResponse(createKeywordRequest);
            System.out.println(JSON.toJSONString(createKeywordResponse,true));
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
  • 删除文本
    private static void deleteKeyword(IAcsClient client) {
        DeleteKeywordRequest deleteKeywordRequest = new DeleteKeywordRequest();
        //设置文本id
        deleteKeywordRequest.setKeywordLibId(String.valueOf(6741));
        //要删除的文本id
        deleteKeywordRequest.setIds(JSON.toJSONString(Arrays.asList(1, 2)));
    
        try {
            DeleteKeywordResponse deleteKeywordResponse = client.getAcsResponse(deleteKeywordRequest);
            System.out.println(JSON.toJSONString(deleteKeywordResponse));
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }