This topic describes how to use Content Moderation SDK for Python to manage custom text libraries. This meets your personalized requirements for text anti-spam.

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

  • Python dependencies are installed. For more information, see Installation.
    Note You must use the required Python 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 want to submit a local image or a binary image stream for image moderation.

Query text libraries

  • Query term libraries.
    # coding=utf-8
    from aliyunsdkcore import client
    from aliyunsdkcore.profile import region_provider
    from aliyunsdkgreen.request.v20170823 import DescribeKeywordLibRequest
    import json
    
    # Enter your AccessKey pair. 
    clt = client.AcsClient("Your AccessKey ID", "Your AccessKey secret", 'cn-shanghai')
    region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
    # The request object cannot be reused. You must create a request object for each request. 
    request = DescribeKeywordLibRequest.DescribeKeywordLibRequest()
    request.set_ServiceModule("open_api")
    response = clt.do_action_with_exception(request)
    print(response)
    result = json.loads(response)
    allLibs = result["KeywordLibList"]
    textAntispamKeywordLibs = []
    for keywordLib in allLibs:
        libType = keywordLib["LibType"]
        resourceType = keywordLib["ResourceType"]
        source = keywordLib["Source"]
        # List the custom term libraries for text anti-spam. 
        if "textKeyword" == libType and "TEXT" == resourceType and "MANUAL" == source:
            textAntispamKeywordLibs.append(keywordLib)
    
        # List the feedback-based term libraries for text anti-spam. 
        if "textKeyword" == libType and "TEXT" == resourceType and "FEEDBACK" == source:
            textAntispamKeywordLibs.append(keywordLib)
    print (json.dumps(textAntispamKeywordLibs))
  • Query text pattern libraries, including custom text pattern libraries and feedback-based text pattern libraries.
    # coding=utf-8
    from aliyunsdkcore import client
    from aliyunsdkcore.profile import region_provider
    from aliyunsdkgreen.request.v20170823 import DescribeKeywordLibRequest
    import json
    
    # Enter your AccessKey pair. 
    clt = client.AcsClient("Your AccessKey ID", "Your AccessKey secret", 'cn-shanghai')
    region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
    # The request object cannot be reused. You must create a request object for each request. 
    request = DescribeKeywordLibRequest.DescribeKeywordLibRequest()
    request.set_ServiceModule("open_api")
    response = clt.do_action_with_exception(request)
    print(response)
    result = json.loads(response)
    allLibs = result["KeywordLibList"]
    similarTextLibs = []
    for keywordLib in allLibs:
        libType = keywordLib["LibType"]
        resourceType = keywordLib["ResourceType"]
        source = keywordLib["Source"]
        # List the custom text pattern libraries for text anti-spam. 
        if "similarText" == libType and "TEXT" == resourceType and "MANUAL" == source:
            similarTextLibs.append(keywordLib)
    
        # List the feedback-based text pattern libraries for text anti-spam. 
        if "similarText" == libType and "TEXT" == resourceType and "FEEDBACK" == source:
            similarTextLibs.append(keywordLib)
    print (json.dumps(similarTextLibs))

Create a text library

  • Create a term library.
    # coding=utf-8
    from aliyunsdkcore import client
    from aliyunsdkcore.profile import region_provider
    from aliyunsdkgreen.request.v20170823 import CreateKeywordLibRequest
    
    # Enter your AccessKey pair. 
    clt = client.AcsClient("Your AccessKey ID", "Your AccessKey secret", 'cn-shanghai')
    region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
    # The request object cannot be reused. You must create a request object for each request. 
    request = CreateKeywordLibRequest.CreateKeywordLibRequest()
    request.set_ServiceModule("open_api")
    request.set_Name("Term library for testing")
    # Set the resource type of the text library. In this example, a text library is created for text anti-spam. 
    request.set_ResourceType("TEXT")
    # Set the type of the text library. In this example, a term library is created. 
    request.set_LibType("textKeyword")
    # Set the category of the text library. In this example, a blacklist is created. 
    request.set_Category("BLACK")
    response = clt.do_action_with_exception(request)
    print(response)
  • Create a text pattern library.
    # coding=utf-8
    from aliyunsdkcore import client
    from aliyunsdkcore.profile import region_provider
    from aliyunsdkgreen.request.v20170823 import CreateKeywordLibRequest
    
    # Enter your AccessKey pair. 
    clt = client.AcsClient("Your AccessKey ID", "Your AccessKey secret", 'cn-shanghai')
    region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
    # The request object cannot be reused. You must create a request object for each request. 
    request = CreateKeywordLibRequest.CreateKeywordLibRequest()
    request.set_ServiceModule("open_api")
    request.set_Name("Text pattern library for testing")
    # Set the resource type of the text library. In this example, a text library is created for text anti-spam. 
    request.set_ResourceType("TEXT")
    # Set the type of the text library. In this example, a text pattern library is created. 
    request.set_LibType("similarText")
    # Set the category of the text library. In this example, a blacklist is created. 
    request.set_Category("BLACK")
    response = clt.do_action_with_exception(request)
    print(response)

Modify a text library

Change the name of a text library and the business scenario to which the text library applies.

# coding=utf-8

from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import UpdateKeywordLibRequest

# Enter your AccessKey pair. 
clt = client.AcsClient("Your AccessKey ID", "Your AccessKey secret", 'cn-shanghai')
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# The request object cannot be reused. You must create a request object for each request. 
request = UpdateKeywordLibRequest.UpdateKeywordLibRequest()
request.set_Id(7534001)
request.set_Name('New name of the text library')
response = clt.do_action_with_exception(request)
print(response)

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.
# coding=utf-8
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import DeleteKeywordLibRequest

# Enter your AccessKey pair. 
clt = client.AcsClient("Your AccessKey ID", "Your AccessKey secret", 'cn-shanghai')
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# The request object cannot be reused. You must create a request object for each request. 
request = DeleteKeywordLibRequest.DeleteKeywordLibRequest()
request.set_Id(3353)
response = clt.do_action_with_exception(request)
print(response)

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.

# coding=utf-8
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import DescribeKeywordRequest

# Enter your AccessKey pair. 
clt = client.AcsClient("Your AccessKey ID", "Your AccessKey secret", 'cn-shanghai')
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# The request object cannot be reused. You must create a request object for each request. 
request = DescribeKeywordRequest.DescribeKeywordRequest()
request.set_KeywordLibId("2693")
request.set_PageSize(10)
request.set_CurrentPage(1)
response = clt.do_action_with_exception(request)
print(response)

Add text

# coding=utf-8
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import CreateKeywordRequest

# Enter your AccessKey pair. 
clt = client.AcsClient("Your AccessKey ID", "Your AccessKey secret", 'cn-shanghai')
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# The request object cannot be reused. You must create a request object for each request. 
request = CreateKeywordRequest.CreateKeywordRequest()
request.set_KeywordLibId(2693)
request.set_Keywords("['How do you do', 'Perfect']")
response = clt.do_action_with_exception(request)
print(response)

Delete text

# coding=utf-8
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import DeleteKeywordRequest

# Enter your AccessKey pair. 
clt = client.AcsClient("Your AccessKey ID", "Your AccessKey secret", 'cn-shanghai')
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# The request object cannot be reused. You must create a request object for each request. 
request = DeleteKeywordRequest.DeleteKeywordRequest()
request.set_KeywordLibId(2693)
request.set_Ids("[1,2]")
response = clt.do_action_with_exception(request)
print(response)