This topic describes how to use the Python SDK to moderate text for spam such as pornography and terrorist content.

Note Currently, the Python SDK only supports synchronous text moderation. You can send a request to moderate one or more pieces of text. The system charges you based on the number of pieces of text moderated. For more information, see Billing methods.

Moderate text for spam

Text anti-spam allows you to add custom terms, such as brand terms of competitors. If the text being moderated contains the terms you add, the value of the suggestion parameter returned by the text anti-spam algorithm is block.

You can log on to the Alibaba Cloud Content Moderation console or call the corresponding operation to add terms. The terms are UTF-8-encoded. For more information about the text anti-spam operation, see Moderate text for spam.

Sample code
#coding=utf-8
# Call the TextScanRequest operation.
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import TextScanRequest
from aliyunsdkgreen.request.extension import HttpContentHelper
import json
import uuid
import datetime

# Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. You can add the AccessKey ID and AccessKey secret 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')
request = TextScanRequest.TextScanRequest()
request.set_accept_format('JSON')
task1 = {"dataId": str(uuid.uuid1()),
         "content": "Text to be moderated",
         "time":datetime.datetime.now().microsecond
        }
# Set the scenes parameter to antispam.
request.set_content(HttpContentHelper.toValue({"tasks": [task1], "scenes": ["antispam"]}))
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 on the text based on the values of the scene and suggestion parameters.
                 # do something

Provide feedback on the text anti-spam result

If the text anti-spam result does not meet your expectations, you can call the TextFeedbackRequest operation to provide feedback on the text anti-spam result. In the feedback, you can pass a correct label for the text moderated.

The server corrects the text anti-spam result based on your feedback and adds the feedback to a text pattern blacklist or whitelist. When you submit a text pattern next time, the server returns the text anti-spam result based on the label that you passed by calling the TextFeedbackRequest operation. For more information about the feedback operation, see Give feedback on moderation results.

Sample code
#coding=utf-8
# Call the TextFeedbackRequest operation.
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import TextFeedbackRequest
from aliyunsdkgreen.request.extension import HttpContentHelper

# Use the AccessKey ID and AccessKey secret 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')

# The taskId parameter indicates the ID of the text moderation task for which you want to provide feedback. The label parameter indicates the actual text anti-spam result.
request.set_content(HttpContentHelper.toValue({"taskId": "txt6z3Na17XbrD4P7QdJzYbk1-1q4seJ", "label": "normal"}))
response = clt.do_action_with_exception(request)
print(response)