全部产品
Search
文档中心

内容安全:文本反垃圾检测

更新时间:Jul 31, 2023

本文介绍了如何使用.NET SDK文本反垃圾接口,对文本内容进行色情、暴恐、涉政等风险进行识别。

功能描述

文本反垃圾接口目前仅支持同步检测。关于参数的详细说明,请参见文本同步检测API文档

一次请求可以检测多条文本,也可以检测单条文本。按实际检测的文本条数进行计费,请参见计费概述

前提条件

已安装.NET依赖。关于安装.NET依赖的具体操作,请参见安装.NET依赖

说明

请一定按照安装.NET依赖页面中的版本安装,否则会导致调用失败。

文本内容检测

文本垃圾检测支持自定义关键词,例如添加一些竞品关键词等。如果被检测的文本中包含您添加的关键词,算法会返回您block。

您可以前往内容安全控制台添加关键词,也可以通过API接口添加关键词。

接口

描述

支持的Region

TextScanRequest

提交文本反垃圾检测任务,检测场景参数请传递antispam(scenes=antispam)。

  • cn-shanghai:华东2(上海)

  • cn-beijing:华北2(北京)

  • cn-shenzhen:华南1(深圳)

  • ap-southeast-1:新加坡

示例代码

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)
        {
            /**
             * 常见获取环境变量方式:
             *     获取RAM用户AccessKey ID:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "建议从环境变量中获取RAM用户AccessKey ID",
                    "建议从环境变量中获取RAM用户AccessKey Secret");
            // 注意:此处实例化的client尽可能重复使用,提升检测性能。避免重复建立连接。
            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", "待检测文本内容");

            Dictionary<string, object> httpBody = new Dictionary<string, object>();
            // 检测场景。文本垃圾检测请传递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);
            }

        }
    }
}

文本反垃圾结果反馈

如果您认为文本检测的结果与您的期望不符,您可以通过文本反垃圾结果反馈接口纠正算法的检测结果。

系统会将您反馈的结果添加到对应的检测文本库,在您下次提交相似的内容进行检测时,以您通过反馈接口校正后的结果作为检测结果。

接口

描述

支持的地域

TextFeedbackRequest

提交文本反垃圾检测结果的反馈,以人工反馈的检测结果纠正算法检测结果。

  • cn-shanghai:华东2(上海)

  • cn-beijing:华北2(北京)

  • cn-shenzhen:华南1(深圳)

  • ap-southeast-1:新加坡

示例代码

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)
        {
            /**
             * 常见获取环境变量方式:
             *     获取RAM用户AccessKey ID:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "建议从环境变量中获取RAM用户AccessKey ID",
                    "建议从环境变量中获取RAM用户AccessKey Secret");
            // 注意:此处实例化的client尽可能重复使用,提升检测性能。避免重复建立连接。
            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:反馈的分类,与具体的scene对应。
            Dictionary<string, object> httpBody = new Dictionary<string, object>();
            httpBody.Add("taskId", "文本审核任务ID");
            httpBody.Add("label", "spam");
            httpBody.Add("content", "文本内容");

            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);
            }s
        }
    }
}