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 Python 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

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

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
    
    # Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
    # Common ways to obtain environment variables:
    # Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
    # Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
    clt = client.AcsClient("We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables", "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
    
    # Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
    # Common ways to obtain environment variables:
    # Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
    # Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
    clt = client.AcsClient("We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables", "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 text libraries

  • Create a term library

    # coding=utf-8
    from aliyunsdkcore import client
    from aliyunsdkcore.profile import region_provider
    from aliyunsdkgreen.request.v20170823 import CreateKeywordLibRequest
    
    # Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
    # Common ways to obtain environment variables:
    # Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
    # Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
    clt = client.AcsClient("We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables", "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")
    # Specify that the created library is used for text anti-spam. 
    request.set_ResourceType("TEXT")
    # Specify that the created library is a term library. 
    request.set_LibType("textKeyword")
    # Specify that the created library is a blacklist. 
    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
    
    # Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
    # Common ways to obtain environment variables:
    # Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
    # Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
    clt = client.AcsClient("We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables", "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")
    # Specify that the created library is used for text anti-spam. 
    request.set_ResourceType("TEXT")
    # Specify that the created library is a text pattern library. 
    request.set_LibType("similarText")
    # Specify that the created library is a blacklist. 
    request.set_Category("BLACK")
    response = clt.do_action_with_exception(request)
    print(response)

Modify a text library

Modify 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

# Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
# Common ways to obtain environment variables:
# Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables", "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

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.

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

# Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
# Common ways to obtain environment variables:
# Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables", "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 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.

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

# Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
# Common ways to obtain environment variables:
# Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables", "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 entries

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

# Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
# Common ways to obtain environment variables:
# Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables", "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)

Remove text entries

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

# Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
# Common ways to obtain environment variables:
# Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables", "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)