This topic describes how to use Content Moderation SDK for Python to moderate text for spam such as pornographic and terrorist content.

Background information

Only synchronous moderation is supported for the text anti-spam operation. For more information about parameters, see Synchronous text moderation.

You can send a request to moderate one or more text entries. You are charged based on the number of text entries that are moderated. For more information, see Overview.

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.

Submit text moderation tasks

To moderate text for spam, you must submit a text moderation task. If a text moderation result does not meet your expectations, you can use TextFeedbackRequest to provide feedback on the machine-assisted moderation result. Text anti-spam allows you to add custom terms, such as the brand terms of competitors. If the moderated text contains the terms you add, the value of the suggestion parameter is block in the returned machine-assisted moderation result.

You can add terms in the Alibaba Cloud Content Moderation console or by calling an API operation.

Operation Description Supported region
TextScanRequest Submits text moderation tasks. Set the scenes parameter to antispam.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore (Singapore)
Sample code
# coding=utf-8
# The following code provides an example on how to call the TextScanRequest operation. 
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import TextScanRequest
import json
import uuid
import datetime

# Use the AccessKey pair of your Alibaba Cloud account. You can add the AccessKey pair to the profile or directly enter them in plaintext in the code. 
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 = TextScanRequest.TextScanRequest()
request.set_accept_format('JSON')
task1 = {"dataId": str(uuid.uuid1()),
         "content":"textContentToBeModerated",
         "time":datetime.datetime.now().microsecond
        }
# Set the scenes parameter to antispam. 
request.set_content(bytearray(json.dumps({"tasks": [task1], "scenes": ["antispam"]}), "utf-8"))
response = clt.do_action_with_exception(request)
print(response)
result = json.loads(response)
if 200 == result["code"]:
    taskResults = result["data"]
    for taskResult in taskResults:
        if (200 == taskResult["code"]):
             sceneResults = taskResult["results"]
             for sceneResult in sceneResults:
                 scene = sceneResult["scene"]
                 suggestion = sceneResult["suggestion"]
                 # Take a further action based on the values of the scene and suggestion parameters. 

Provide feedback on text anti-spam results

If a text anti-spam result does not meet your expectations, you can use TextFeedbackRequest to provide feedback on the machine-assisted moderation result.

The server corrects the text anti-spam result based on your feedback and adds the feedback to a text library. When you submit text that meets the text pattern next time, the system returns the text anti-spam result that is corrected based on your feedback.

Operation Description Supported region
TextFeedbackRequest Provides feedback on a text anti-spam result to correct the machine-assisted moderation result that does not meet your expectations.
  • cn-shanghai
  • cn-beijing
  • cn-shenzhen
  • ap-southeast-1
Sample code
# coding=utf-8

from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import TextFeedbackRequest
import json

# Use the AccessKey pair of your Alibaba Cloud account. 
clt = client.AcsClient("Your AccessKey ID", "Your AccessKey secret","cn-shanghai")
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
request = TextFeedbackRequest.TextFeedbackRequest()
request.set_accept_format('JSON')

request.set_content(
    json.dumps({"dataId": "ID of the moderated text", "taskId": "ID of the text moderation task",
                "content": "Text content", "label": "spam", "note": "Remarks"}))

try:
    response = clt.do_action_with_exception(request)
    print response
except Exception, err:
    print err.__str__()