All Products
Search
Document Center

Content Moderation:Manage custom text libraries

Last Updated:Aug 03, 2023

This topic describes how to use Content Moderation SDK for .NET to manage custom text libraries. This helps meet the personalized requirements in text anti-spam scenarios.

Description

Based on the text type, text libraries are classified into term libraries and text pattern libraries. Based on the management purposes, text libraries are classified into whitelists, blacklists, and review lists. For more information about parameters, see DescribeKeywordLib.

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 .NET version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.

Query text libraries

  • Query term libraries

    using System;
    using Newtonsoft.Json;
    using Aliyun.Acs.Core;
    using Aliyun.Acs.Core.Profile;
    using Aliyun.Acs.Green.Model.V20170823;
    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");
                DefaultAcsClient client = new DefaultAcsClient(profile);
    
                DescribeKeywordLibRequest request = new DescribeKeywordLibRequest();
                request.ServiceModule = "open_api";
                try
                {
                    DescribeKeywordLibResponse response = client.GetAcsResponse(request);
                    Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
                    // List the text libraries configured for text anti-spam. 
                    List<DescribeKeywordLibResponse.DescribeKeywordLib_KeywordLib> allLibs = response.KeywordLibList;
                    List<DescribeKeywordLibResponse.DescribeKeywordLib_KeywordLib> textAntispamKeywordLibs = new List<DescribeKeywordLibResponse.DescribeKeywordLib_KeywordLib>();
                    foreach (DescribeKeywordLibResponse.DescribeKeywordLib_KeywordLib keywordLib in allLibs)
                    {
                        string LibType = keywordLib.LibType;
                        string resourceType = keywordLib.ResourceType;
                        string source = keywordLib.Source;
                        // List the custom term libraries for text anti-spam. 
                        if ("textKeyword".Equals(LibType) && "TEXT".Equals(resourceType) && "MANUAL".Equals(source))
                        {
                            textAntispamKeywordLibs.Add(keywordLib);
                        }
                        // List the feedback-based term libraries for text anti-spam. 
                        if ("textKeyword".Equals(LibType) && "TEXT".Equals(resourceType) && "FEEDBACK".Equals(source))
                        {
                            textAntispamKeywordLibs.Add(keywordLib);
                        }
                    }
                    Console.WriteLine(JsonConvert.SerializeObject(textAntispamKeywordLibs));
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed with error info: {0}", ex.Message);
                }
            }
        }
    }
  • Query text pattern libraries, including custom text pattern libraries and feedback-based text pattern libraries

    using System;
    using Newtonsoft.Json;
    using Aliyun.Acs.Core;
    using Aliyun.Acs.Core.Profile;
    using Aliyun.Acs.Green.Model.V20170823;
    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");
                DefaultAcsClient client = new DefaultAcsClient(profile);
    
                DescribeKeywordLibRequest request = new DescribeKeywordLibRequest();
                request.ServiceModule = "open_api";
                try
                {
                    // This method returns all text libraries, including term libraries and text pattern libraries for text anti-spam, term libraries for moderating ads in images, and term libraries for audio anti-spam. 
                    DescribeKeywordLibResponse response = client.GetAcsResponse(request);
                    Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
    
                    // List text pattern libraries. 
                    List<DescribeKeywordLibResponse.DescribeKeywordLib_KeywordLib> allLibs = response.KeywordLibList;
                    List<DescribeKeywordLibResponse.DescribeKeywordLib_KeywordLib> similarTextLibs = new List<DescribeKeywordLibResponse.DescribeKeywordLib_KeywordLib>();
                    foreach (DescribeKeywordLibResponse.DescribeKeywordLib_KeywordLib keywordLib in allLibs)
                    {
                        String LibType = keywordLib.LibType;
                        String resourceType = keywordLib.ResourceType;
                        String source = keywordLib.Source;
                        // List the custom text pattern libraries for text anti-spam. 
                        if ("similarText".Equals(LibType) && "TEXT".Equals(resourceType) && "MANUAL".Equals(source))
                        {
                            similarTextLibs.Add(keywordLib);
                        }
    
                        // List the feedback-based text pattern libraries for text anti-spam. 
                        if ("similarText".Equals(LibType) && "TEXT".Equals(resourceType) && "FEEDBACK".Equals(source))
                        {
                            similarTextLibs.Add(keywordLib);
                        }
                    }
    
                    Console.WriteLine(JsonConvert.SerializeObject(similarTextLibs));
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed with error info: {0}", ex.Message);
                }
            }
        }
    }

Create text libraries

  • Create a term library

    using System;
    using Aliyun.Acs.Core;
    using Aliyun.Acs.Core.Profile;
    using Aliyun.Acs.Green.Model.V20170823;
    
    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");
                DefaultAcsClient client = new DefaultAcsClient(profile);
    
                CreateKeywordLibRequest createKeywordLibRequest = new CreateKeywordLibRequest();
                createKeywordLibRequest.ServiceModule = "open_api";
                createKeywordLibRequest.Name = "Term library for testing";
                // Specify that the created library is used for text anti-spam. 
                createKeywordLibRequest.ResourceType = "TEXT";
                // Specify that the created library is a term library. 
                createKeywordLibRequest.LibType = "textKeyword";
                // Specify that the created library is a blacklist. 
                createKeywordLibRequest.Category = "BLACK";
                try
                {
                    CreateKeywordLibResponse describeKeywordLibResponse = client.GetAcsResponse(createKeywordLibRequest);
                    Console.WriteLine(System.Text.Encoding.Default.GetString(describeKeywordLibResponse.HttpResponse.Content));
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed with error info: {0}", ex.Message);
                }
            }
        }
    }
  • Create a text pattern library

    using System;
    using Aliyun.Acs.Core;
    using Aliyun.Acs.Core.Profile;
    using Aliyun.Acs.Green.Model.V20170823;
    
    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");
                DefaultAcsClient client = new DefaultAcsClient(profile);
    
                CreateKeywordLibRequest createKeywordLibRequest = new CreateKeywordLibRequest();
                createKeywordLibRequest.ServiceModule = "open_api";
                createKeywordLibRequest.Name = "Text pattern library for testing";
                // Specify that the created library is used for text anti-spam. 
                createKeywordLibRequest.ResourceType = "TEXT";
                // Specify that the created library is a text pattern library. 
                createKeywordLibRequest.LibType = "similarText";
                // Specify that the created library is a blacklist. You can also specify that the created library is a whitelist or review list. 
                createKeywordLibRequest.Category = "BLACK";
                try
                {
                    CreateKeywordLibResponse describeKeywordLibResponse = client.GetAcsResponse(createKeywordLibRequest);
                    Console.WriteLine(System.Text.Encoding.Default.GetString(describeKeywordLibResponse.HttpResponse.Content));
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed with error info: {0}", ex.Message);
                }
            }
        }
    }

Modify a text library

Change the name of a text library and the business scenario to which the text library applies.

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20170823;
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");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            UpdateKeywordLibRequest updateKeywordLibRequest = new UpdateKeywordLibRequest();
            // Specify the ID of the text library to be modified. 
            updateKeywordLibRequest.Id = ID of the text library;
            // Specify the new name of the text library. 
            updateKeywordLibRequest.Name = "New name of the text library";
            // Specify the new business scenario to which the text library applies. 
            updateKeywordLibRequest.BizTypes = JsonConvert.SerializeObject(new List<string> { "comment", "title" });
            try
            {
                UpdateKeywordLibResponse updateKeywordLibResponse = client.GetAcsResponse(updateKeywordLibRequest);
                Console.WriteLine(System.Text.Encoding.Default.GetString(updateKeywordLibResponse.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}

Delete a text library

Important

If you delete a text library, the text entries in the text library are also deleted. You cannot delete a feedback-based text library.

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20170823;
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");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            DeleteKeywordLibRequest deleteKeywordLibRequest = new DeleteKeywordLibRequest();
            // Specify the ID of the text library to be deleted. 
            deleteKeywordLibRequest.Id = ID of the text library;

            try
            {
                DeleteKeywordLibResponse deleteKeywordLibResponse = client.GetAcsResponse(deleteKeywordLibRequest);
                Console.WriteLine(System.Text.Encoding.Default.GetString(deleteKeywordLibResponse.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}

Search for text entries

By default, the system returns all text entries in a text library by page. If you set the Keyword parameter, the system searches for all text entries that match the specified term in fuzzy mode.

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20170823;
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");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            DescribeKeywordRequest describeKeywordRequest = new DescribeKeywordRequest();
            // Specify the ID of the text library in which you want to search for text entries. 
            describeKeywordRequest.KeywordLibId = ID of the text library;
            describeKeywordRequest.PageSize = 10;
            describeKeywordRequest.CurrentPage = 1;
            // (Optional) Specify the term that is used for fuzzy search. 
            describeKeywordRequest.Keyword = "Text entry to be searched for";

            try
            {
                DescribeKeywordResponse describeKeywordResponse = client.GetAcsResponse(describeKeywordRequest);
                Console.WriteLine(System.Text.Encoding.Default.GetString(describeKeywordResponse.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}

Add text entries

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20170823;
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");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            CreateKeywordRequest createKeywordRequest = new CreateKeywordRequest();
            // Specify the ID of the text library to which you want to add text entries. 
            createKeywordRequest.KeywordLibId = ID of the text library;
            // Specify the text entries to be added. 
            createKeywordRequest.Keywords=JsonConvert.SerializeObject (new List<string> { "Text_1", "Text_2" });
            try
            {
                CreateKeywordResponse createKeywordResponse = client.GetAcsResponse(createKeywordRequest);
                Console.WriteLine(System.Text.Encoding.Default.GetString(createKeywordResponse.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}

Remove text entries

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20170823;
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");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            DeleteKeywordRequest deleteKeywordRequest = new DeleteKeywordRequest();
            // Specify the ID of the text library from which you want to remove text entries. 
            deleteKeywordRequest.KeywordLibId = "ID of the text library";
            // Specify the IDs of the text entries to be removed. 
            deleteKeywordRequest.Ids = JsonConvert.SerializeObject(new List<int> { Text ID_1, Text ID_2 });

            try
            {
                DeleteKeywordResponse deleteKeywordResponse = client.GetAcsResponse(deleteKeywordRequest);
                Console.WriteLine(System.Text.Encoding.Default.GetString(deleteKeywordResponse.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}