This topic describes how to use Content Moderation SDK for Java to manage custom text libraries. This allows you to meet the personalized requirements in the text anti-spam scenario.

Background information

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. For more information about parameters, see Query text libraries.

Prerequisites

  • Java dependencies are installed. For more information, see Installation.
    Note You must use the required Java version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.
  • The Extension.Uploader utility class is downloaded and imported into your project if you submit a local image or a binary image stream for image moderation.

Query text libraries

  • Query term libraries
    DescribeKeywordLibRequest describeKeywordLibRequest = new DescribeKeywordLibRequest();
            describeKeywordLibRequest.setServiceModule("open_api");
    
            try {
                // This method returns 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 the 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 custom term libraries for text anti-spam. 
                    if ("textKeyword".equals(LibType) && "TEXT".equals(resourceType)  && "MANUAL".equals(source)) {
                        textAntispamKeywordLibs.add(keywordLib);
                    }
                    // List the feedback-based term libraries for text anti-spam. 
                    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 custom and feedback-based text pattern libraries
    DescribeKeywordLibRequest describeKeywordLibRequest = new DescribeKeywordLibRequest();
            describeKeywordLibRequest.setServiceModule("open_api");
    
            try {
                // This method returns 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 custom text pattern libraries for text anti-spam. 
                    if("similarText".equals(LibType) && "TEXT".equals(resourceType)  && "MANUAL".equals(source)) {
                        similarTextLibs.add(keywordLib);
                    }
    
                    // List the feedback-based text pattern libraries for text anti-spam. 
                    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 is used for text anti-spam. 
            createKeywordLibRequest.setResourceType("TEXT");
            // Specify that the new library is a term library. 
            createKeywordLibRequest.setLibType("textKeyword");
            // Specify that the new library is a blacklist. 
            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 is used for text anti-spam. 
            createKeywordLibRequest.setResourceType("TEXT");
            // Specify that the new library is a text pattern library. 
            createKeywordLibRequest.setLibType("similarText");
            // Specify that the new library is a blacklist. You can also specify that the new library is a whitelist or review list. 
            createKeywordLibRequest.setCategory("BLACK");
    
            try {
                CreateKeywordLibResponse describeKeywordLibResponse = client.getAcsResponse(createKeywordLibRequest);
                System.out.println(JSON.toJSONString(describeKeywordLibResponse));
            } catch (ClientException e) {
                e.printStackTrace();
            }

Modify a text library

Change the name and bizType of a text library.

UpdateKeywordLibRequest updateKeywordLibRequest = new UpdateKeywordLibRequest();
        // Specify the ID of the text library to modify. 
        updateKeywordLibRequest.setId(2693);
        // Specify the new name of the text library. 
        updateKeywordLibRequest.setName("New name of the text library");
        // Specify the new bizType. 
        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 If you delete a text library, the text in the text library is also deleted. You cannot delete a feedback-based text library.
DeleteKeywordLibRequest deleteKeywordLibRequest = new DeleteKeywordLibRequest();
        // Specify 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

By default, the system returns all text in a text library on multiple pages. If you set the Keyword parameter, the system searches for all text that matches the specified term in fuzzy mode.

DescribeKeywordRequest describeKeywordRequest = new DescribeKeywordRequest();
        // Specify the ID of the text library in which you want to search for text. 
        describeKeywordRequest.setKeywordLibId(2693);
        describeKeywordRequest.setPageSize(10);
        describeKeywordRequest.setCurrentPage(1);
        // Optional. Specify the keyword that 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();
        // Specify the ID of the text library to which you want to add text. 
        createKeywordRequest.setKeywordLibId(2693);
        // Specify 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();
        // Specify the ID of the text library from which you want to delete text. 
        deleteKeywordRequest.setKeywordLibId(String.valueOf(2693));
        // Specify the IDs of the text to be deleted. 
        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();
        }