This topic provides the sample code to help you use the Java SDK to manage custom text libraries. You can use the Java SDK to customize a text library and manage the text in the library to meet the personalized requirements in the text anti-spam scenario. Based on the text type, text libraries are classified into term libraries and text pattern libraries. Based on the management purposes, text libraries are classified into whitelists, blacklists, and review lists.

Query text libraries

  • Query term libraries
    DescribeKeywordLibRequest describeKeywordLibRequest = new DescribeKeywordLibRequest();
            describeKeywordLibRequest.setServiceModule("open_api");
    
            try {
                // This method lists all text libraries, including term libraries and text pattern libraries for text anti-spam, term libraries for detecting ads in images, and term libraries for audio anti-spam.
                DescribeKeywordLibResponse describeKeywordLibResponse = client.getAcsResponse(describeKeywordLibRequest);
                System.out.println(JSON.toJSONString(describeKeywordLibResponse));
    
                // List text libraries configured for text anti-spam.
                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();
                    // List the term libraries that are customized for text anti-spam.
                    if ("textKeyword".equals(LibType) && "TEXT".equals(resourceType)  && "MANUAL".equals(source)) {
                        textAntispamKeywordLibs.add(keywordLib);
                    }
                    // List the term libraries that are automatically created for text anti-spam based on your feedback.
                    if ("textKeyword".equals(LibType) && "TEXT".equals(resourceType)  && "FEEDBACK".equals(source)) {
                        textAntispamKeywordLibs.add(keywordLib);
                    }
                }
    
                System.out.println(JSON.toJSONString(textAntispamKeywordLibs));
            } catch (ClientException e) {
                e.printStackTrace();
            }
  • Query text pattern libraries, including those that are customized or automatically created based on your feedback
    DescribeKeywordLibRequest describeKeywordLibRequest = new DescribeKeywordLibRequest();
            describeKeywordLibRequest.setServiceModule("open_api");
    
            try {
                // This method lists all text libraries, including term libraries and text pattern libraries for text anti-spam, term libraries for detecting ads in images, and term libraries for audio anti-spam.
                DescribeKeywordLibResponse describeKeywordLibResponse = client.getAcsResponse(describeKeywordLibRequest);
                System.out.println(JSON.toJSONString(describeKeywordLibResponse));
    
                // List text pattern libraries.
                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();
                    // List the text pattern libraries that are customized for text anti-spam.
                    if("similarText".equals(LibType) && "TEXT".equals(resourceType)  && "MANUAL".equals(source)) {
                        similarTextLibs.add(keywordLib);
                    }
    
                    // List the text pattern libraries that are automatically created for text anti-spam based on your feedback.
                    if("similarText".equals(LibType) && "TEXT".equals(resourceType)  && "FEEDBACK".equals(source)) {
                        similarTextLibs.add(keywordLib);
                    }
                }
    
                System.out.println(JSON.toJSONString(similarTextLibs));
            } catch (ClientException e) {
                e.printStackTrace();
            }

Create a text library

  • Create a term library
    CreateKeywordLibRequest createKeywordLibRequest = new CreateKeywordLibRequest();
            createKeywordLibRequest.setServiceModule("open_api");
            createKeywordLibRequest.setName("Term library for testing");
            // Specify that the new library be used for text anti-spam.
            createKeywordLibRequest.setResourceType("TEXT");
            // Specify that the new library be a term library.
            createKeywordLibRequest.setLibType("textKeyword");
            // You can only create a blacklist as the new term library.
            createKeywordLibRequest.setCategory("BLACK");
    
            try {
                CreateKeywordLibResponse describeKeywordLibResponse = client.getAcsResponse(createKeywordLibRequest);
                System.out.println(JSON.toJSONString(describeKeywordLibResponse));
            } catch (ClientException e) {
                e.printStackTrace();
            }
  • Create a text pattern library
    CreateKeywordLibRequest createKeywordLibRequest = new CreateKeywordLibRequest();
            createKeywordLibRequest.setServiceModule("open_api");
            createKeywordLibRequest.setName("Text pattern library for testing");
            // Specify that the new library be used for text anti-spam.
            createKeywordLibRequest.setResourceType("TEXT");
            // Specify that the new library be a text pattern library.
            createKeywordLibRequest.setLibType("similarText");
            // You can create a blacklist, whitelist, or review list as the new text pattern library.
            createKeywordLibRequest.setCategory("BLACK");
    
            try {
                CreateKeywordLibResponse describeKeywordLibResponse = client.getAcsResponse(createKeywordLibRequest);
                System.out.println(JSON.toJSONString(describeKeywordLibResponse));
            } catch (ClientException e) {
                e.printStackTrace();
            }

Modify a text library

Use the following code to modify the Name and BizTypes parameters of a text library:

UpdateKeywordLibRequest updateKeywordLibRequest = new UpdateKeywordLibRequest();
        // Enter the ID of the text library to modify.
        updateKeywordLibRequest.setId(2693);
        // Enter the new name of the text library.
        updateKeywordLibRequest.setName("New name of the text library");
        // Set a new value for the BizTypes parameter.
        updateKeywordLibRequest.setBizTypes(JSON.toJSONString(Arrays.asList("comment", "title")));

        try {
            UpdateKeywordLibResponse updateKeywordLibResponse = client.getAcsResponse(updateKeywordLibRequest);
            System.out.println(JSON.toJSONString(updateKeywordLibResponse));
        } catch (ClientException e) {
            e.printStackTrace();
        }

Delete a text library

Notice After you delete a text library, text in the text library is also deleted. You cannot delete a feedback-based text library.
DeleteKeywordLibRequest deleteKeywordLibRequest = new DeleteKeywordLibRequest();
        // Enter the ID of the text library to delete.
        deleteKeywordLibRequest.setId(3353);

        try {
            DeleteKeywordLibResponse deleteKeywordLibResponse = client.getAcsResponse(deleteKeywordLibRequest);
            System.out.println(JSON.toJSONString(deleteKeywordLibResponse));
        } catch (ClientException e) {
            e.printStackTrace();
        }

Search for text in a text library

By default, all text in the text library is returned by page. If you set the Keyword parameter, all text that contains the specified term is returned in fuzzy mode.

DescribeKeywordRequest describeKeywordRequest = new DescribeKeywordRequest();
        // Enter the ID of the text library in which you want to search for text.
        describeKeywordRequest.setKeywordLibId(2693);
        describeKeywordRequest.setPageSize(10);
        describeKeywordRequest.setCurrentPage(1);
        // The Keyword parameter is optional and is used for fuzzy search.
        describeKeywordRequest.setKeyword("You");
        try {
            DescribeKeywordResponse describeKeywordResponse = client.getAcsResponse(describeKeywordRequest);
            System.out.println(JSON.toJSONString(describeKeywordResponse));
        } catch (ClientException e) {
            e.printStackTrace();
        }

Add text

CreateKeywordRequest createKeywordRequest = new CreateKeywordRequest();
        // Enter the ID of the text library to which you want to add text.
        createKeywordRequest.setKeywordLibId(2693);
        // Enter the text to be added.
        createKeywordRequest.setKeywords(JSON.toJSONString(Arrays.asList("How do you do", "Perfect")));

        try {
            CreateKeywordResponse createKeywordResponse = client.getAcsResponse(createKeywordRequest);
            System.out.println(JSON.toJSONString(createKeywordResponse));
        } catch (ClientException e) {
            e.printStackTrace();
        }

Delete text

DeleteKeywordRequest deleteKeywordRequest = new DeleteKeywordRequest();
        // Enter the ID of the text library from which you want to delete text.
        deleteKeywordRequest.setKeywordLibId(String.valueOf(2693));
        // Enter the IDs of the text to delete.
        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();
        }