Share via


在 C 中針對自訂字詞清單檢查文字#

Azure Content Moderator 中預設的全域字詞清單足以滿足大部分的 con帳篷模式ration 需求。 不過,您可能需要篩選組織特定的字詞。

您可以使用 Content Moderator SDK for .NET 來建立自訂字詞清單,以搭配文字仲裁 API 使用。

本文提供資訊和程式碼範例,協助您開始使用適用于 .NET 的 Content Moderator SDK 來:

  • 建立清單。
  • 將字詞新增至清單。
  • 根據清單中的字詞篩選字詞。
  • 從清單中刪除字詞。
  • 刪除清單。
  • 編輯清單資訊。
  • 重新整理索引,讓清單的變更包含在新的掃描中。

如尚未擁有 Azure 訂用帳戶,請在開始之前先建立免費帳戶

註冊 Content Moderator 服務

您必須先有訂用帳戶金鑰,才能透過 REST API 或 SDK 使用 Content Moderator 服務。 訂閱Azure 入口網站 中的 Content Moderator 服務以取得一個。

建立 Visual Studio 專案

  1. 將新的 主控台應用程式 (.NET Framework) 專案新增至您的解決方案。

  2. 將專案 命名為 TermLists 。 選取此專案作為方案的單一啟動專案。

安裝必要的套件

安裝 TermLists 專案的下列 NuGet 套件:

  • Microsoft.Azure.CognitiveServices.ContentModerator
  • Microsoft.Rest.ClientRuntime
  • Microsoft.Rest.ClientRuntime.Azure
  • Newtonsoft.Json

更新程式的 using 語句

加入下列 using 陳述式。

using Microsoft.Azure.CognitiveServices.ContentModerator;
using Microsoft.Azure.CognitiveServices.ContentModerator.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;

建立 Content Moderator 用戶端

新增下列程式碼,為您的訂用帳戶建立 Content Moderator 用戶端。 使用 AzureEndpoint 端點 URL 和訂用帳戶金鑰的值來更新 和 CMSubscriptionKey 欄位。 您可以在資源Azure 入口網站的 [快速入門] 索引標籤中找到這些專案

/// <summary>
/// Wraps the creation and configuration of a Content Moderator client.
/// </summary>
/// <remarks>This class library contains insecure code. If you adapt this 
/// code for use in production, use a secure method of storing and using
/// your Content Moderator subscription key.</remarks>
public static class Clients
{
    /// <summary>
    /// The base URL fragment for Content Moderator calls.
    /// </summary>
    private static readonly string AzureEndpoint = "YOUR ENDPOINT URL";

    /// <summary>
    /// Your Content Moderator subscription key.
    /// </summary>
    private static readonly string CMSubscriptionKey = "YOUR API KEY";

    /// <summary>
    /// Returns a new Content Moderator client for your subscription.
    /// </summary>
    /// <returns>The new client.</returns>
    /// <remarks>The <see cref="ContentModeratorClient"/> is disposable.
    /// When you have finished using the client,
    /// you should dispose of it either directly or indirectly. </remarks>
    public static ContentModeratorClient NewClient()
    {
        // Create and initialize an instance of the Content Moderator API wrapper.
        ContentModeratorClient client = new ContentModeratorClient(new ApiKeyServiceClientCredentials(CMSubscriptionKey));

        client.Endpoint = AzureEndpoint;
        return client;
    }
}

重要

當您完成時,請記得從程式碼中移除金鑰,且絕不會公開發布。 針對生產環境,請使用安全的方式來儲存和存取您的認證,例如 Azure 金鑰保存庫 。 如需詳細資訊,請參閱 Azure AI 服務安全性一文。

新增私人屬性

將下列私人屬性新增至命名空間 TermLists 類別 Program。

/// <summary>
/// The language of the terms in the term lists.
/// </summary>
private const string lang = "eng";

/// <summary>
/// The minimum amount of time, in milliseconds, to wait between calls
/// to the Content Moderator APIs.
/// </summary>
private const int throttleRate = 3000;

/// <summary>
/// The number of minutes to delay after updating the search index before
/// performing image match operations against the list.
/// </summary>
private const double latencyDelay = 0.5;

建立字詞清單

您可以使用 ContentModeratorClient.ListManagementTermLists.Create 建立字詞清單 。 Create 的第一個參數 是包含 MIME 類型的字串,其應該是 「application/json」。 如需詳細資訊,請參閱 API 參考 。 第二個 參數是 Body 物件,其中包含新字詞清單的名稱和描述。

注意

每個清單的字詞清單上限 為 5 個,不能 超過 10,000 個字詞

將下列方法定義新增至命名空間 TermLists 類別 Program。

注意

您的 Content Moderator 服務金鑰具有每秒要求數 (RPS) 速率限制,如果您超過限制,SDK 會擲回例外狀況,並出現 429 錯誤碼。 免費層金鑰有一個 RPS 速率限制。

/// <summary>
/// Creates a new term list.
/// </summary>
/// <param name="client">The Content Moderator client.</param>
/// <returns>The term list ID.</returns>
static string CreateTermList (ContentModeratorClient client)
{
    Console.WriteLine("Creating term list.");

    Body body = new Body("Term list name", "Term list description");
    TermList list = client.ListManagementTermLists.Create("application/json", body);
    if (false == list.Id.HasValue)
    {
        throw new Exception("TermList.Id value missing.");
    }
    else
    {
        string list_id = list.Id.Value.ToString();
        Console.WriteLine("Term list created. ID: {0}.", list_id);
        Thread.Sleep(throttleRate);
        return list_id;
    }
}

更新字詞清單名稱和描述

您可以使用 ContentModeratorClient.ListManagementTermLists.Update 來更新字詞清單資訊 。 Update 的第一個參數 是字詞清單識別碼。 第二個參數是 MIME 類型,應該是 「application/json」。 如需詳細資訊,請參閱 API 參考 。 第三個 參數是 Body 物件,其中包含新的名稱和描述。

將下列方法定義新增至命名空間 TermLists 類別 Program。

/// <summary>
/// Update the information for the indicated term list.
/// </summary>
/// <param name="client">The Content Moderator client.</param>
/// <param name="list_id">The ID of the term list to update.</param>
/// <param name="name">The new name for the term list.</param>
/// <param name="description">The new description for the term list.</param>
static void UpdateTermList (ContentModeratorClient client, string list_id, string name = null, string description = null)
{
    Console.WriteLine("Updating information for term list with ID {0}.", list_id);
    Body body = new Body(name, description);
    client.ListManagementTermLists.Update(list_id, "application/json", body);
    Thread.Sleep(throttleRate);
}

將字詞新增至字詞清單

將下列方法定義新增至命名空間 TermLists 類別 Program。

/// <summary>
/// Add a term to the indicated term list.
/// </summary>
/// <param name="client">The Content Moderator client.</param>
/// <param name="list_id">The ID of the term list to update.</param>
/// <param name="term">The term to add to the term list.</param>
static void AddTerm (ContentModeratorClient client, string list_id, string term)
{
    Console.WriteLine("Adding term \"{0}\" to term list with ID {1}.", term, list_id);
    client.ListManagementTerm.AddTerm(list_id, term, lang);
    Thread.Sleep(throttleRate);
}

取得字詞清單中的所有字詞

將下列方法定義新增至命名空間 TermLists 類別 Program。

/// <summary>
/// Get all terms in the indicated term list.
/// </summary>
/// <param name="client">The Content Moderator client.</param>
/// <param name="list_id">The ID of the term list from which to get all terms.</param>
static void GetAllTerms(ContentModeratorClient client, string list_id)
{
    Console.WriteLine("Getting terms in term list with ID {0}.", list_id);
    Terms terms = client.ListManagementTerm.GetAllTerms(list_id, lang);
    TermsData data = terms.Data;
    foreach (TermsInList term in data.Terms)
    {
        Console.WriteLine(term.Term);
    }
    Thread.Sleep(throttleRate);
}

新增程式碼以重新整理搜尋索引

對字詞清單進行變更之後,您會在下次使用字詞清單來篩選文字時,重新整理其搜尋索引,以包含變更。 這類似于桌面上的搜尋引擎(如果已啟用)或 Web 搜尋引擎會持續重新整理其索引,以包含新的檔案或頁面。

您可以使用 ContentModeratorClient.ListManagementTermLists.RefreshIndexMethod 重新整理字詞清單搜尋索引

將下列方法定義新增至命名空間 TermLists 類別 Program。

/// <summary>
/// Refresh the search index for the indicated term list.
/// </summary>
/// <param name="client">The Content Moderator client.</param>
/// <param name="list_id">The ID of the term list to refresh.</param>
static void RefreshSearchIndex (ContentModeratorClient client, string list_id)
{
    Console.WriteLine("Refreshing search index for term list with ID {0}.", list_id);
    client.ListManagementTermLists.RefreshIndexMethod(list_id, lang);
    Thread.Sleep((int)(latencyDelay * 60 * 1000));
}

使用字詞清單的螢幕文字

搭配 ContentModeratorClient.TextModeration.ScreenText 使用字詞清單 來篩選文字,其採用下列參數。

  • 字詞清單中的字詞語言。
  • MIME 類型,可以是 「text/html」、「text/xml」、「text/markdown」 或 「text/plain」。
  • 要畫面的文字。
  • boolean 值。 將此欄位設定為 true ,以在篩選前自動校正文字。
  • boolean 值。 將此欄位設定為 true ,以偵測文字中的個人資料。
  • 字詞清單識別碼。

如需詳細資訊,請參閱 API 參考

ScreenText 會傳 回 Screen 物件,其具有 Terms 屬性,其中會列出 Content Moderator 在篩選中偵測到的任何字詞。 請注意,如果 Content Moderator 在篩選期間未偵測到任何字詞, 則 Terms 屬性的值 為 null

將下列方法定義新增至命名空間 TermLists 類別 Program。

/// <summary>
/// Screen the indicated text for terms in the indicated term list.
/// </summary>
/// <param name="client">The Content Moderator client.</param>
/// <param name="list_id">The ID of the term list to use to screen the text.</param>
/// <param name="text">The text to screen.</param>
static void ScreenText (ContentModeratorClient client, string list_id, string text)
{
    Console.WriteLine("Screening text: \"{0}\" using term list with ID {1}.", text, list_id);
    Screen screen = client.TextModeration.ScreenText(lang, "text/plain", text, false, false, list_id);
    if (null == screen.Terms)
    {
        Console.WriteLine("No terms from the term list were detected in the text.");
    }
    else
    {
        foreach (DetectedTerms term in screen.Terms)
        {
            Console.WriteLine(String.Format("Found term: \"{0}\" from list ID {1} at index {2}.", term.Term, term.ListId, term.Index));
        }
    }
    Thread.Sleep(throttleRate);
}

刪除字詞和清單

刪除字詞或清單很簡單。 您可以使用 SDK 來執行下列工作:

  • 刪除字詞。 ( ContentModeratorClient.ListManagementTerm.DeleteTerm
  • 刪除清單中的所有字詞,而不刪除清單。 ( ContentModeratorClient.ListManagementTerm.DeleteAllTerms
  • 刪除清單及其所有內容。 ( ContentModeratorClient.ListManagementTermLists.Delete

刪除字詞

將下列方法定義新增至命名空間 TermLists 類別 Program。

/// <summary>
/// Delete a term from the indicated term list.
/// </summary>
/// <param name="client">The Content Moderator client.</param>
/// <param name="list_id">The ID of the term list from which to delete the term.</param>
/// <param name="term">The term to delete.</param>
static void DeleteTerm (ContentModeratorClient client, string list_id, string term)
{
    Console.WriteLine("Removed term \"{0}\" from term list with ID {1}.", term, list_id);
    client.ListManagementTerm.DeleteTerm(list_id, term, lang);
    Thread.Sleep(throttleRate);
}

刪除字詞清單中的所有字詞

將下列方法定義新增至命名空間 TermLists 類別 Program。

/// <summary>
/// Delete all terms from the indicated term list.
/// </summary>
/// <param name="client">The Content Moderator client.</param>
/// <param name="list_id">The ID of the term list from which to delete all terms.</param>
static void DeleteAllTerms (ContentModeratorClient client, string list_id)
{
    Console.WriteLine("Removing all terms from term list with ID {0}.", list_id);
    client.ListManagementTerm.DeleteAllTerms(list_id, lang);
    Thread.Sleep(throttleRate);
}

刪除字詞清單

將下列方法定義新增至命名空間 TermLists 類別 Program。

/// <summary>
/// Delete the indicated term list.
/// </summary>
/// <param name="client">The Content Moderator client.</param>
/// <param name="list_id">The ID of the term list to delete.</param>
static void DeleteTermList (ContentModeratorClient client, string list_id)
{
    Console.WriteLine("Deleting term list with ID {0}.", list_id);
    client.ListManagementTermLists.Delete(list_id);
    Thread.Sleep(throttleRate);
}

撰寫 Main 方法

Main 方法定義新增至命名空間 TermLists 類別 Program 。 最後,關閉 Program 類別和 TermLists 命名空間。

static void Main(string[] args)
{
    using (var client = Clients.NewClient())
    {
        string list_id = CreateTermList(client);

        UpdateTermList(client, list_id, "name", "description");
        AddTerm(client, list_id, "term1");
        AddTerm(client, list_id, "term2");

        GetAllTerms(client, list_id);

        // Always remember to refresh the search index of your list
        RefreshSearchIndex(client, list_id);

        string text = "This text contains the terms \"term1\" and \"term2\".";
        ScreenText(client, list_id, text);

        DeleteTerm(client, list_id, "term1");

        // Always remember to refresh the search index of your list
        RefreshSearchIndex(client, list_id);

        text = "This text contains the terms \"term1\" and \"term2\".";
        ScreenText(client, list_id, text);

        DeleteAllTerms(client, list_id);
        DeleteTermList(client, list_id);

        Console.WriteLine("Press ENTER to close the application.");
        Console.ReadLine();
    }
}

執行應用程式以查看輸出

您的主控台輸出如下所示:

Creating term list.
Term list created. ID: 252.
Updating information for term list with ID 252.

Adding term "term1" to term list with ID 252.
Adding term "term2" to term list with ID 252.

Getting terms in term list with ID 252.
term1
term2

Refreshing search index for term list with ID 252.

Screening text: "This text contains the terms "term1" and "term2"." using term list with ID 252.
Found term: "term1" from list ID 252 at index 32.
Found term: "term2" from list ID 252 at index 46.

Removed term "term1" from term list with ID 252.

Refreshing search index for term list with ID 252.

Screening text: "This text contains the terms "term1" and "term2"." using term list with ID 252.
Found term: "term2" from list ID 252 at index 46.

Removing all terms from term list with ID 252.
Deleting term list with ID 252.
Press ENTER to close the application.

下一步

取得適用于 .NET 的 Content Moderator .NET SDK Visual Studio 解決方案 ,以及其他適用于 .NET 的 Content Moderator 快速入門,並開始整合。