This topic describes how to use Content Moderation SDK for .NET 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 the 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

The dependencies for Content Moderation SDK for .NET are installed. For more information about how to install the dependencies, see Installation.
Note You must use the required versions of the dependencies described in the Installation topic. 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 you add, block is returned for the suggestion parameter.

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
using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;

namespace csharp_sdk_sample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
            IClientProfile profile = DefaultProfile.GetProfile(
                "cn-shanghai",
                "Your AccessKey ID",
                "Your AccessKey secret");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            TextScanRequest request = new TextScanRequest();
            request.AcceptFormat = FormatType.JSON;
            request.ContentType = FormatType.JSON;
            request.Method = MethodType.POST;
            request.Encoding = "UTF-8";

            Dictionary<string, object> task1 = new Dictionary<string, object>();
            task1.Add("content", "Content of the text to be moderated");

            Dictionary<string, object> httpBody = new Dictionary<string, object>();
            // Specify the moderation scenario. Set the scenes parameter to antispam. 
            httpBody.Add("scenes", new List<string> { "antispam" });
            httpBody.Add("bizType", "default");
            httpBody.Add("tasks", new List<Dictionary<string, object>> { task1 });

            request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON);
            try
            {
                TextScanResponse response = client.GetAcsResponse(request);
                if (response.HttpResponse.Status != 200)
                {
                    Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status);
                }
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }

        }
    }
}

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
using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;

namespace csharp_sdk_sample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
            IClientProfile profile = DefaultProfile.GetProfile(
                "cn-shanghai",
                "Your AccessKey ID",
                "Your AccessKey secret");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            TextFeedbackRequest request = new TextFeedbackRequest();
            request.AcceptFormat = FormatType.JSON;
            request.ContentType = FormatType.JSON;
            request.Method = MethodType.POST;
            request.Encoding = "UTF-8";

            // label: The expected category of moderation results for the moderated text in the specified moderation scenario. 
            Dictionary<string, object> httpBody = new Dictionary<string, object>();
            httpBody.Add("taskId", "ID of the text moderation task");
            httpBody.Add("label", "spam");
            httpBody.Add("content", "Content of the moderated text");

            request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON);
            try
            {
                TextFeedbackResponse response = client.GetAcsResponse(request);
                if (response.HttpResponse.Status != 200)
                {
                    Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status);
                }
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}