This topic describes how to use the SDK for Python to call text anti-spam operations and moderate text for spam such as pornographic and terrorist content.

Background information

Only synchronous moderation is supported. 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 Billing methods.

Preparations

Before calling operations, make the following preparations:
  1. Create an AccessKey ID and AccessKey secret of your Alibaba Cloud account. For more information, see Create an AccessKey.
  2. Install Python dependencies. For more information, see Installation.

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 call the TextFeedbackRequest operation 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 results.

You can add terms in the Alibaba Cloud Content Moderation console or by calling the corresponding operation. The terms are UTF-8-encoded. For more information about the operation, see Text moderation.

Sample code
# coding=utf-8
# Use the following code 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("yourAccessKeyId", "yourAccessKeySecret",'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 moderation result does not meet your expectations, you can call the TextFeedbackRequest operation to provide feedback on the machine-assisted moderation result.

The system corrects the moderation result based on your feedback and adds the feedback to the corresponding text library. When you submit a text pattern next time, the system returns the moderation result that is corrected based on your feedback. For more information about the operation, see Give feedback on moderation results.

Sample code
# coding=utf-8
# Use the following code to call the TextFeedbackRequest operation.
import json

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

# Use the AccessKey pair of your Alibaba Cloud account.
clt = client.AcsClient("yourAccessKeyId", "yourAccessKeySecret", "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 = 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 expected text moderation result.
request.set_content(bytearray(json.dumps({"taskId": "txt6z3Na17XbrD4P7QdJzYbk1-1q4seJ", "label": "normal"}), "utf-8"))
response = clt.do_action_with_exception(request)
print(response)