All Products
Search
Document Center

Content Moderation:Text anti-spam

Last Updated:Jul 31, 2023

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

Description

Only synchronous moderation is supported for text anti-spam. For more information about the related parameters, see /green/text/scan.

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 Content Moderation 1.0 activation and billing.

Prerequisites

Go dependencies are installed. For more information, see Installation.

Note

You must use the required Go version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.

Moderate text for spam

Text anti-spam allows you to add custom terms, such as the brand terms of competitors. If the moderated text contains the terms that you add, the value of the suggestion parameter is block in the returned machine-assisted moderation result.

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

Operation

Description

Supported region

TextScanRequest

Submits text moderation tasks with the scenes parameter set to antispam.

  • cn-shanghai: China (Shanghai)

  • cn-beijing: China (Beijing)

  • cn-shenzhen: China (Shenzhen)

  • ap-southeast-1: Singapore

Sample code

package main

import (
   "encoding/json"
   "fmt"
   "github.com/aliyun/alibaba-cloud-sdk-go/services/green"
   "strconv"
)

func main() {
   /**
    * 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.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
    *     Obtain the AccessKey secret of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
    */
   client, _err := green.NewClientWithAccessKey(
        "cn-shanghai", 
        "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")
   if _err != nil {
      fmt.Println(_err.Error())
      return
   }
   task := map[string]interface{}{"content": "Content of the text to be moderated"}
  // scenes: the moderation scenario. Set the value to antispam. 
   content, _ := json.Marshal(
      map[string]interface{}{
         "scenes": [...]string{"antispam"},
         "tasks":  [...]map[string]interface{}{task},
      },
   )

   textScanRequest := green.CreateTextScanRequest()
   textScanRequest.SetContent(content)
   textScanResponse, err := client.TextScan(textScanRequest)
   if err != nil {
      fmt.Println(err.Error())
      return
   }
   if textScanResponse.GetHttpStatus() != 200 {
      fmt.Println("response not success. status:" + strconv.Itoa(textScanResponse.GetHttpStatus()))
   }
   fmt.Println(textScanResponse.GetHttpContentString())
}

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 matches 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: China (Shanghai)

  • cn-beijing: China (Beijing)

  • cn-shenzhen: China (Shenzhen)

  • ap-southeast-1: Singapore

Sample code

package main

import (
   "encoding/json"
   "fmt"
   "github.com/aliyun/alibaba-cloud-sdk-go/services/green"
   "strconv"
)

func main() {
   /**
    * 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.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
    *     Obtain the AccessKey secret of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
    */
   client, _err := green.NewClientWithAccessKey(
        "cn-shanghai", 
        "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")
   if _err != nil {
      fmt.Println(_err.Error())
      return
   }
  // label: The expected category of moderation results for the moderated text in the specified moderation scenario. 
   content, _ := json.Marshal(
      map[string]interface{}{
         "taskId": "ID of the text moderation task", "content": "Text content", "label": "spam",
      },
   )

   request := green.CreateTextFeedbackRequest()
   request.SetContent(content)
   response, err := client.TextFeedback(request)
   if err != nil {
      fmt.Println(err.Error())
      return
   }
   if response.GetHttpStatus() != 200 {
      fmt.Println("response not success. status:" + strconv.Itoa(response.GetHttpStatus()))
   }
   fmt.Println(response.GetHttpContentString())
}