Text anti-spam

更新时间:
复制 MD 格式

Use AI Guardrails SDK for .NET to scan text for spam, pornographic content, and terrorist content. Only synchronous moderation is supported. Billing is based on the number of text entries moderated. See Billing overview for details.

Prerequisites

Before you begin, make sure you have:

Important

Use the .NET version specified in the installation guide. Using a different version causes subsequent API calls to fail.

Scan text for spam

TextScanRequest submits text moderation tasks with the scenes parameter set to antispam.

Supported regions: cn-shanghai (China (Shanghai)), cn-beijing (China (Beijing)), cn-shenzhen (China (Shenzhen)), ap-southeast-1 (Singapore)

Constraints:

  • Only synchronous moderation is supported.

  • Text anti-spam supports custom terms — for example, brand terms of competitors. When moderated text matches a custom term, the suggestion field returns block in the machine-assisted moderation result. Add custom terms in the AI Guardrails console or by calling an API operation.

For the full list of request parameters, see /green/text/scan.

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)
        {
            /**
             * Common ways to obtain environment variables:
             *     Obtain the AccessKey ID of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     Obtain the AccessKey secret of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "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");
            // Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections.
            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 moderation results

If a moderation result is incorrect, use TextFeedbackRequest to submit feedback. The service updates the machine-assisted moderation result based on your feedback and adds it to the text library. Future text that matches the same pattern returns the corrected result.

Supported regions: cn-shanghai (China (Shanghai)), cn-beijing (China (Beijing)), cn-shenzhen (China (Shenzhen)), ap-southeast-1 (Singapore)

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)
        {
            /**
             * Common ways to obtain environment variables:
             *     Obtain the AccessKey ID of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     Obtain the AccessKey secret of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "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");
            // Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections.
            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);
            }
        }
    }
}

What's next