Use the AI Guardrails SDK for Java to manage custom text libraries for text anti-spam.
Library types
Text libraries are classified along two dimensions:
By content type
| Type | API value |
|---|---|
| Keyword library | textKeyword |
| Text pattern library | similarText |
By purpose
| Purpose | API value |
|---|---|
| Blocklist | BLACK |
| Allowlist | WHITE |
| Review list | suspect |
Prerequisites
Before you begin, ensure that you have:
Java dependencies installed — see Installation
Use the Java version specified in the Installation topic. Using a different version causes operation calls to fail.
Query text libraries
DescribeKeywordLibRequest returns all text libraries in your account, including libraries for text anti-spam, image ad detection, and audio anti-spam. Filter the results by LibType, resourceType, and source to get the libraries you need.
Query keyword libraries
DescribeKeywordLibRequest describeKeywordLibRequest = new DescribeKeywordLibRequest();
describeKeywordLibRequest.setServiceModule("open_api");
try {
DescribeKeywordLibResponse describeKeywordLibResponse = client.getAcsResponse(describeKeywordLibRequest);
List<DescribeKeywordLibResponse.KeywordLib> allLibs = describeKeywordLibResponse.getKeywordLibList();
List<DescribeKeywordLibResponse.KeywordLib> keywordLibs = new ArrayList<>();
for (DescribeKeywordLibResponse.KeywordLib lib : allLibs) {
String libType = lib.getLibType();
String resourceType = lib.getResourceType();
String source = lib.getSource();
// Custom keyword libraries for text anti-spam
if ("textKeyword".equals(libType) && "TEXT".equals(resourceType) && "MANUAL".equals(source)) {
keywordLibs.add(lib);
}
// Feedback-based keyword libraries for text anti-spam
if ("textKeyword".equals(libType) && "TEXT".equals(resourceType) && "FEEDBACK".equals(source)) {
keywordLibs.add(lib);
}
}
System.out.println(JSON.toJSONString(keywordLibs));
} catch (ClientException e) {
e.printStackTrace();
}Query text pattern libraries
DescribeKeywordLibRequest describeKeywordLibRequest = new DescribeKeywordLibRequest();
describeKeywordLibRequest.setServiceModule("open_api");
try {
DescribeKeywordLibResponse describeKeywordLibResponse = client.getAcsResponse(describeKeywordLibRequest);
List<DescribeKeywordLibResponse.KeywordLib> allLibs = describeKeywordLibResponse.getKeywordLibList();
List<DescribeKeywordLibResponse.KeywordLib> patternLibs = new ArrayList<>();
for (DescribeKeywordLibResponse.KeywordLib lib : allLibs) {
String libType = lib.getLibType();
String resourceType = lib.getResourceType();
String source = lib.getSource();
// Custom text pattern libraries for text anti-spam
if ("similarText".equals(libType) && "TEXT".equals(resourceType) && "MANUAL".equals(source)) {
patternLibs.add(lib);
}
// Feedback-based text pattern libraries for text anti-spam
if ("similarText".equals(libType) && "TEXT".equals(resourceType) && "FEEDBACK".equals(source)) {
patternLibs.add(lib);
}
}
System.out.println(JSON.toJSONString(patternLibs));
} catch (ClientException e) {
e.printStackTrace();
}For the full list of API parameters, see Get a list of text libraries.
Create a text library
Create a keyword library
CreateKeywordLibRequest createKeywordLibRequest = new CreateKeywordLibRequest();
createKeywordLibRequest.setServiceModule("open_api");
createKeywordLibRequest.setName("Term library for testing");
createKeywordLibRequest.setResourceType("TEXT"); // Text anti-spam
createKeywordLibRequest.setLibType("textKeyword"); // Keyword library
createKeywordLibRequest.setCategory("BLACK"); // Blocklist
try {
CreateKeywordLibResponse createKeywordLibResponse = client.getAcsResponse(createKeywordLibRequest);
// Library created successfully; record the ID for subsequent operations
String libId = createKeywordLibResponse.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");
createKeywordLibRequest.setResourceType("TEXT"); // Text anti-spam
createKeywordLibRequest.setLibType("similarText"); // Text pattern library
createKeywordLibRequest.setCategory("BLACK"); // Blocklist; also accepts WHITE or suspect
try {
CreateKeywordLibResponse createKeywordLibResponse = client.getAcsResponse(createKeywordLibRequest);
// Library created successfully; record the ID for subsequent operations
String libId = createKeywordLibResponse.getId();
} catch (ClientException e) {
e.printStackTrace();
}Update a text library
Update the name or the moderation scenarios (BizTypes) associated with a text library.
UpdateKeywordLibRequest updateKeywordLibRequest = new UpdateKeywordLibRequest();
updateKeywordLibRequest.setId(0); // ID of the library to update
updateKeywordLibRequest.setName("New library name");
// Associate the library with one or more moderation scenarios
updateKeywordLibRequest.setBizTypes(JSON.toJSONString(Arrays.asList("New BizType_1", "New BizType_2")));
try {
UpdateKeywordLibResponse updateKeywordLibResponse = client.getAcsResponse(updateKeywordLibRequest);
// A non-empty requestId indicates success
String requestId = updateKeywordLibResponse.getRequestId();
} catch (ClientException e) {
e.printStackTrace();
}Delete a text library
DeleteKeywordLibRequest deleteKeywordLibRequest = new DeleteKeywordLibRequest();
deleteKeywordLibRequest.setId(3353); // ID of the library to delete
try {
DeleteKeywordLibResponse deleteKeywordLibResponse = client.getAcsResponse(deleteKeywordLibRequest);
// A non-empty requestId indicates success
String requestId = deleteKeywordLibResponse.getRequestId();
} catch (ClientException e) {
e.printStackTrace();
}Deleting a text library permanently deletes all text entries it contains. Feedback-based text libraries cannot be deleted.
Search for text entries
By default, the API returns all text entries in a library with pagination. Set the keyword parameter to search in fuzzy mode.
DescribeKeywordRequest describeKeywordRequest = new DescribeKeywordRequest();
describeKeywordRequest.setKeywordLibId(0); // Library ID
describeKeywordRequest.setPageSize(10); // Entries per page
describeKeywordRequest.setCurrentPage(1); // Page number
describeKeywordRequest.setKeyword("Text entry to search for"); // Optional: fuzzy search term
try {
DescribeKeywordResponse describeKeywordResponse = client.getAcsResponse(describeKeywordRequest);
Integer totalCount = describeKeywordResponse.getTotalCount(); // Total number of matching entries
Integer currentPage = describeKeywordResponse.getCurrentPage(); // Current page number
Integer pageSize = describeKeywordResponse.getPageSize(); // Entries per page
for (DescribeKeywordResponse.Keyword item : describeKeywordResponse.getKeywordList()) {
Integer id = item.getId(); // Entry ID
String createTime = item.getCreateTime(); // Creation time
Integer hitCount = item.getHitCount(); // Number of times this entry was matched
String keyword = item.getKeyword(); // The keyword text
}
} catch (ClientException e) {
e.printStackTrace();
}Add text entries
CreateKeywordRequest createKeywordRequest = new CreateKeywordRequest();
createKeywordRequest.setKeywordLibId(0L); // Library ID
createKeywordRequest.setKeywords(JSON.toJSONString(Arrays.asList("Term_1", "Term_2")));
try {
CreateKeywordResponse createKeywordResponse = client.getAcsResponse(createKeywordRequest);
Integer successCount = createKeywordResponse.getSuccessCount(); // Number of entries added
List<String> invalidKeywordList = createKeywordResponse.getInvalidKeywordList(); // Entries that failed validation
} catch (ClientException e) {
e.printStackTrace();
}Remove text entries
DeleteKeywordRequest deleteKeywordRequest = new DeleteKeywordRequest();
deleteKeywordRequest.setKeywordLibId(String.valueOf("ID of the text library")); // Library ID
deleteKeywordRequest.setIds(JSON.toJSONString(Arrays.asList(1, 2))); // Entry IDs to remove
try {
DeleteKeywordResponse deleteKeywordResponse = client.getAcsResponse(deleteKeywordRequest);
// A non-empty requestId indicates success
String requestId = deleteKeywordResponse.getRequestId();
} catch (ClientException e) {
e.printStackTrace();
}What's next
Get a list of text libraries — Full API parameter reference for
DescribeKeywordLibInstallation — Install AI Guardrails SDK for Java