创建常规自定义字典检测器

自定义字典提供了匹配字词或短语列表的简单而强大的功能。您可以将自定义字典用作检测器,也可以将其用作内置检测器的例外列表。您还可使用自定义字典来强化内置的 infoType 检测器,以匹配其他结果。

本部分介绍了如何根据字词列表创建常规自定义字典检测器。

字典自定义 infoType 检测器详解

API 概览中所述,要创建字典自定义 infoType 检测器,需要定义一个包含下列内容的 CustomInfoType 对象:

  • 您希望在 InfoType 对象中为自定义 infoType 检测器指定的名称。
  • (可选)Likelihood 值。如果省略此字段,则字典项的匹配项将返回默认可能性 VERY_LIKELY
  • (可选)DetectionRule 对象或热词规则。这些规则可对在指定热词一定邻近范围内的结果调整可能性。详细了解自定义匹配可能性中的热词规则。
  • (可选)SensitivityScore 值。如果省略此字段,则字典项的匹配项将返回默认敏感度级别 HIGH

    敏感度得分用于数据分析结果。在分析数据时,Sensitive Data Protection 会使用 infoType 的敏感度得分来计算敏感度级别

  • Dictionary - 可以是包含要扫描的字词列表的 WordList,也可以是要扫描的字词列表(以换行符分隔)所在单个文本文件的 CloudStoragePath

作为一个 JSON 对象,包含所有可选组件的字典自定义 infoType 检测器如下所示:此 JSON 包含存储在 Cloud Storage 中的字典文本文件的路径。如需查看内嵌字词列表,请参阅本主题后面的示例部分。

{
  "customInfoTypes":[
    {
      "infoType":{
        "name":"CUSTOM_INFOTYPE_NAME"
      },
      "likelihood":"LIKELIHOOD_LEVEL",
      "detectionRules":[
        {
          "hotwordRule":{
            HOTWORD_RULE
          }
        },
        ...
      ],
      "sensitivityScore":{
          "score": "SENSITIVITY_SCORE"
        },
      "dictionary":
      {
        "cloudStoragePath":
        {
          "path": "gs://PATH_TO_TXT_FILE"
        }
      }
    }
  ],
  ...
}

字典匹配详情

以下是关于 Sensitive Data Protection 如何与字典字词和短语进行匹配的指南。这些要点同时适用于常规自定义字典和大型自定义字典:

  • 字典字词不区分大小写。如果您的字典包含 Abby,将匹配 abbyABBYAbby 等。
  • 扫描匹配项时,字典或要扫描的内容中,除了 Unicode 基本多语言平面中包含的字母、数字和其他字母字符外,其他所有字符一律被视为空格。如果您的字典扫描 Abby Abernathy,将匹配 abby abernathyAbby, AbernathyAbby (ABERNATHY) 等。
  • 任何匹配项两侧的字符均必须与字词中相邻字符的类型(字母或数字)不同。如果您的字典扫描 Abi,将匹配 Abi904 的前三个字符,但不匹配 Abigail 的前三个字符。
  • 如果字典字词包含 Unicode 标准的辅助多语言平面中的字符,则可能会产生意外结果。此类字符的示例包括表情符号、科学符号和历史文字。

字母、数字和其他字母字符的定义如下:

  • 字母:Unicode 规范中具有常规类别 LuLlLtLmLo 的字符
  • 数字:Unicode 规范中具有一般类别 Nd 的字符
  • 其他字母字符:Unicode 规范中具有一般类别 Nl 或具有贡献属性 Other_Alphabetic(如 Unicode 标准所定义)的字符

示例

简单字词列表

假设您的数据中包括患者在就诊期间接受治疗的病房。在特定数据集中,这些位置可能被视为敏感数据,但是 Sensitive Data Protection 的内置检测器不会捕捉它们。

病房列为:

  • "RM-Orange"
  • "RM-Yellow"
  • "RM-Green"

C#

如需了解如何安装和使用 Sensitive Data Protection 客户端库,请参阅 Sensitive Data Protection 客户端库

如需向 Sensitive Data Protection 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证


using System;
using Google.Api.Gax.ResourceNames;
using Google.Cloud.Dlp.V2;

public class DeidentifyWithSimpleWordList
{
    public static DeidentifyContentResponse Deidentify(string projectId, string text)
    {
        // Instantiate a client.
        var dlp = DlpServiceClient.Create();

        var contentItem = new ContentItem { Value = text };

        var wordList = new CustomInfoType.Types.Dictionary.Types.WordList
        {
            Words = { new string[] { "RM-GREEN", "RM-YELLOW", "RM-ORANGE" } }
        };

        var infoType = new InfoType
        {
            Name = "CUSTOM_ROOM_ID"
        };

        var customInfoType = new CustomInfoType
        {
            InfoType = infoType,
            Dictionary = new CustomInfoType.Types.Dictionary
            {
                WordList = wordList
            }
        };

        var inspectConfig = new InspectConfig
        {
            CustomInfoTypes =
            {
                customInfoType,
            }
        };
        var primitiveTransformation = new PrimitiveTransformation
        {
            ReplaceWithInfoTypeConfig = new ReplaceWithInfoTypeConfig { }
        };

        var transformation = new InfoTypeTransformations.Types.InfoTypeTransformation
        {
            InfoTypes = { infoType },
            PrimitiveTransformation = primitiveTransformation
        };

        var deidentifyConfig = new DeidentifyConfig
        {
            InfoTypeTransformations = new InfoTypeTransformations
            {
                Transformations = { transformation }
            }
        };

        var request = new DeidentifyContentRequest
        {
            Parent = new LocationName(projectId, "global").ToString(),
            InspectConfig = inspectConfig,
            DeidentifyConfig = deidentifyConfig,
            Item = contentItem
        };

        // Call the API.
        var response = dlp.DeidentifyContent(request);

        // Inspect the results.
        Console.WriteLine($"Deidentified content: {response.Item.Value}");
        return response;
    }
}

Go

如需了解如何安装和使用 Sensitive Data Protection 客户端库,请参阅 Sensitive Data Protection 客户端库

如需向 Sensitive Data Protection 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证

import (
	"context"
	"fmt"
	"io"

	dlp "cloud.google.com/go/dlp/apiv2"
	"cloud.google.com/go/dlp/apiv2/dlppb"
)

// deidentifyWithWordList matches against a custom simple word list to de-identify sensitive
// data based on the input
func deidentifyWithWordList(w io.Writer, projectID, input string, infoTypeName string, wordList []string) error {
	// projectID := "my-project-id"
	// input := "Patient was seen in RM-YELLOW then transferred to rm green."
	// wordList := []string{"RM-GREEN", "RM-YELLOW", "RM-ORANGE"}

	ctx := context.Background()

	// Initialize a client once and reuse it to send multiple requests. Clients
	// are safe to use across goroutines. When the client is no longer needed,
	// call the Close method to cleanup its resources.
	client, err := dlp.NewClient(ctx)
	if err != nil {
		return err
	}
	// Closing the client safely cleans up background resources.
	defer client.Close()