All Products
Search
Document Center

Content Moderation:Manage custom text libraries

Last Updated:Jul 31, 2023

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

Description

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 DescribeKeywordLib.

For more information, see Endpoints.

Prerequisites

  • Java dependencies are installed. For more information, see Installation.

    Note

    You must use the 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 {
        // Return 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 text pattern libraries and feedback-based text pattern libraries

    DescribeKeywordLibRequest describeKeywordLibRequest = new DescribeKeywordLibRequest();
    describeKeywordLibRequest.setServiceModule("open_api");
    
    try {
        // Return 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 text libraries

  • Create a term library

    CreateKeywordLibRequest createKeywordLibRequest = new CreateKeywordLibRequest();
    createKeywordLibRequest.setServiceModule("open_api");
    createKeywordLibRequest.setName("Term library for testing");
    // Specify that the created library is used for text anti-spam. 
    createKeywordLibRequest.setResourceType("TEXT");
    // Specify that the created library is a term library. 
    createKeywordLibRequest.setLibType("textKeyword");
    // Specify that the created library is a blacklist. 
    createKeywordLibRequest.setCategory("BLACK");
    
    try {
        CreateKeywordLibResponse describeKeywordLibResponse = client.getAcsResponse(createKeywordLibRequest);
        // The ID of the term library. If no error is reported, the term library is created. 
        String libId = describeKeywordLibResponse.getId();
    } 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 created library is used for text anti-spam. 
    createKeywordLibRequest.setResourceType("TEXT");
    // Specify that the created library is a text pattern library. 
    createKeywordLibRequest.setLibType("similarText");
    // Specify that the created library is a blacklist. You can also specify that the created library is a whitelist or review list. 
    createKeywordLibRequest.setCategory("BLACK");
    
    try {
        CreateKeywordLibResponse describeKeywordLibResponse = client.getAcsResponse(createKeywordLibRequest);
        // The ID of the text pattern library. If no error is reported, the text pattern library is created. 
        String libId = describeKeywordLibResponse.getId();
    } catch (ClientException e) {
        e.printStackTrace();
    }

Modify a text library

Modify the name of a text library and the moderation scenario for which the text library is used.

UpdateKeywordLibRequest updateKeywordLibRequest = new UpdateKeywordLibRequest();
// Specify the ID of the text library to be modified. 
updateKeywordLibRequest.setId(0);
// Specify the new name of the text library. 
updateKeywordLibRequest.setName("New name of the text library");
// Specify the new moderation scenario to which the text library applies. 
updateKeywordLibRequest.setBizTypes(JSON.toJSONString(Arrays.asList("New BizType_1", "New BizType_2")));

try {
    UpdateKeywordLibResponse updateKeywordLibResponse = client.getAcsResponse(updateKeywordLibRequest);
    // The ID of the request. If no error is reported, the modification is successful. 
    String requestId = updateKeywordLibResponse.getRequestId();
} catch (ClientException e) {
    e.printStackTrace();
}

Delete a text library

Important

If you delete a text library, the text entries in the text library are also deleted. You cannot delete a feedback-based text library.

DeleteKeywordLibRequest deleteKeywordLibRequest = new DeleteKeywordLibRequest();
// Specify the ID of the text library to be deleted. 
deleteKeywordLibRequest.setId(3353);

try {
    DeleteKeywordLibResponse deleteKeywordLibResponse = client.getAcsResponse(deleteKeywordLibRequest);
    // The ID of the request. If no error is reported, the text library is deleted. 
    String requestId = deleteKeywordLibResponse.getRequestId();
} catch (ClientException e) {
    e.printStackTrace();
}

Search for text entries

By default, the system returns all text entries in a text library by page. If you set the Keyword parameter, the system searches for all text entries that match 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 entries. 
describeKeywordRequest.setKeywordLibId(0);
describeKeywordRequest.setPageSize(10);
describeKeywordRequest.setCurrentPage(1);
// (Optional) Specify the term that is used for fuzzy search. 
describeKeywordRequest.setKeyword("Text entry to be searched for");
try {
    DescribeKeywordResponse describeKeywordResponse = client.getAcsResponse(describeKeywordRequest);
    // The total number of entries returned. 
    Integer totalCount = describeKeywordResponse.getTotalCount();
    // The number of the returned page. 
    Integer currentPage = describeKeywordResponse.getCurrentPage();
    // The number of entries returned per page. 
    Integer pageSize = describeKeywordResponse.getPageSize();
    for (DescribeKeywordResponse.Keyword keywordItem : describeKeywordResponse.getKeywordList()) {
        // The primary key ID of the term. 
        Integer id = keywordItem.getId();
        // The time when the term was created. 
        String createTime = keywordItem.getCreateTime();
        // The number of times that the term was hit. 
        Integer hitCount = keywordItem.getHitCount();
        // The term. 
        String keyword = keywordItem.getKeyword();
    }
} catch (ClientException e) {
    e.printStackTrace();
}

Add text entries

CreateKeywordRequest createKeywordRequest = new CreateKeywordRequest();
// Specify the ID of the text library to which you want to add text entries. 
createKeywordRequest.setKeywordLibId(0L);
// The text entries to be added. 
createKeywordRequest.setKeywords(JSON.toJSONString(Arrays.asList("Term_1", "Term_2")));

try {
    CreateKeywordResponse createKeywordResponse = client.getAcsResponse(createKeywordRequest);
    // The number of terms added. 
    Integer successCount = createKeywordResponse.getSuccessCount();
    // The list of invalid terms, which are the terms that failed to be added. 
    List<String> invalidKeywordList = createKeywordResponse.getInvalidKeywordList();
} catch (ClientException e) {
    e.printStackTrace();
}

Remove text entries

DeleteKeywordRequest deleteKeywordRequest = new DeleteKeywordRequest();
// Specify the ID of the text library from which you want to remove text entries. 
deleteKeywordRequest.setKeywordLibId(String.valueOf("ID of the text library"));
// The text entries to be removed. 
deleteKeywordRequest.setIds(JSON.toJSONString(Arrays.asList(1, 2)));

try {
    DeleteKeywordResponse deleteKeywordResponse = client.getAcsResponse(deleteKeywordRequest);
    // The ID of the request. If no error is reported, the text entries are removed. 
    String requestId = deleteKeywordResponse.getRequestId();
} catch (ClientException e) {
    e.printStackTrace();
}