提高旁边有热词“患者”的 PERSON_NAME 匹配项的可能性。说明如何使用 InspectConfig 属性来扫描医疗数据库中的患者姓名。您可以使用 Cloud DLP 的内置 PERSON_NAME infoType 检测器,但这会导致 Cloud DLP 匹配所有人名,而不仅仅是患者姓名。为解决此问题,您可以添加一条热词规则,用于在潜在匹配项第一个字符的一定邻近字符范围内查找“患者”一词。然后,您可以向匹配该模式的发现结果分配“可能性极大”(very likely) 这一可能性,因为这些结果符合您的特殊条件。通过在 InspectConfig 中将 Likelihood 的最小值设置为 VERY_LIKELY,可确保发现结果中仅返回符合此配置的匹配项。
包含此代码示例的文档页面
如需查看上下文中使用的代码示例,请参阅以下文档:
代码示例
C#
如需了解如何安装和使用 Cloud DLP 客户端库,请参阅 Cloud DLP 客户端库。
using Google.Api.Gax.ResourceNames;
using Google.Cloud.Dlp.V2;
using System;
using static Google.Cloud.Dlp.V2.CustomInfoType.Types;
public class InspectStringCustomHotword
{
public static InspectContentResponse Inspect(string projectId, string textToInspect, string customHotword)
{
var dlp = DlpServiceClient.Create();
var byteContentItem = new ByteContentItem
{
Type = ByteContentItem.Types.BytesType.TextUtf8,
Data = Google.Protobuf.ByteString.CopyFromUtf8(textToInspect)
};
var contentItem = new ContentItem
{
ByteItem = byteContentItem
};
var hotwordRule = new DetectionRule.Types.HotwordRule
{
HotwordRegex = new Regex { Pattern = customHotword },
Proximity = new DetectionRule.Types.Proximity { WindowBefore = 50 },
LikelihoodAdjustment = new DetectionRule.Types.LikelihoodAdjustment { FixedLikelihood = Likelihood.VeryLikely }
};
var infoType = new InfoType { Name = "PERSON_NAME" };
var inspectionRuleSet = new InspectionRuleSet
{
InfoTypes = { infoType },
Rules = { new InspectionRule { HotwordRule = hotwordRule } }
};
var inspectConfig = new InspectConfig
{
InfoTypes = { infoType },
IncludeQuote = true,
RuleSet = { inspectionRuleSet },
MinLikelihood = Likelihood.VeryLikely
};
var request = new InspectContentRequest
{
Parent = new LocationName(projectId, "global").ToString(),
Item = contentItem,
InspectConfig = inspectConfig
};
var response = dlp.InspectContent(request);
Console.WriteLine($"Findings: {response.Result.Findings.Count}");
foreach (var f in response.Result.Findings)
{
Console.WriteLine("\tQuote: " + f.Quote);
Console.WriteLine("\tInfo type: " + f.InfoType.Name);
Console.WriteLine("\tLikelihood: " + f.Likelihood);
}
return response;
}
}
Java
如需了解如何安装和使用 Cloud DLP 客户端库,请参阅 Cloud DLP 客户端库。
import com.google.cloud.dlp.v2.DlpServiceClient;
import com.google.privacy.dlp.v2.ByteContentItem;
import com.google.privacy.dlp.v2.ByteContentItem.BytesType;
import com.google.privacy.dlp.v2.ContentItem;
import com.google.privacy.dlp.v2.CustomInfoType.DetectionRule.HotwordRule;
import com.google.privacy.dlp.v2.CustomInfoType.DetectionRule.LikelihoodAdjustment;
import com.google.privacy.dlp.v2.CustomInfoType.DetectionRule.Proximity;
import com.google.privacy.dlp.v2.CustomInfoType.Regex;
import com.google.privacy.dlp.v2.Finding;
import com.google.privacy.dlp.v2.InfoType;
import com.google.privacy.dlp.v2.InspectConfig;
import com.google.privacy.dlp.v2.InspectContentRequest;
import com.google.privacy.dlp.v2.InspectContentResponse;
import com.google.privacy.dlp.v2.InspectionRule;
import com.google.privacy.dlp.v2.InspectionRuleSet;
import com.google.privacy.dlp.v2.Likelihood;
import com.google.privacy.dlp.v2.LocationName;
import com.google.protobuf.ByteString;
import java.io.IOException;
public class InspectStringCustomHotword {
public static void main(String[] args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
String textToInspect = "patient name: John Doe";
String customHotword = "patient";
inspectStringCustomHotword(projectId, textToInspect, customHotword);
}
// Inspects the provided text.
public static void inspectStringCustomHotword(
String projectId, String textToInspect, String customHotword) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (DlpServiceClient dlp = DlpServiceClient.create()) {
// Specify the type and content to be inspected.
ByteContentItem byteItem =
ByteContentItem.newBuilder()
.setType(BytesType.TEXT_UTF8)
.setData(ByteString.copyFromUtf8(textToInspect))
.build();
ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();
// Increase likelihood of matches that have customHotword nearby
HotwordRule hotwordRule =
HotwordRule.newBuilder()
.setHotwordRegex(Regex.newBuilder().setPattern(customHotword))
.setProximity(Proximity.newBuilder().setWindowBefore(50))
.setLikelihoodAdjustment(
LikelihoodAdjustment.newBuilder().setFixedLikelihood(Likelihood.VERY_LIKELY))
.build();
// Construct a ruleset that applies the hotword rule to the PERSON_NAME infotype.
InspectionRuleSet ruleSet =
InspectionRuleSet.newBuilder()
.addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME").build())
.addRules(InspectionRule.newBuilder().setHotwordRule(hotwordRule))
.build();
// Construct the configuration for the Inspect request.
InspectConfig config =
InspectConfig.newBuilder()
.addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME").build())
.setIncludeQuote(true)
.addRuleSet(ruleSet)
.setMinLikelihood(Likelihood.VERY_LIKELY)
.build();
// Construct the Inspect request to be sent by the client.
InspectContentRequest request =
InspectContentRequest.newBuilder()
.setParent(LocationName.of(projectId, "global").toString())
.setItem(item)
.setInspectConfig(config)
.build();
// Use the client to send the API request.
InspectContentResponse response = dlp.inspectContent(request);
// Parse the response and process results
System.out.println("Findings: " + response.getResult().getFindingsCount());
for (Finding f : response.getResult().getFindingsList()) {
System.out.println("\tQuote: " + f.getQuote());
System.out.println("\tInfo type: " + f.getInfoType().getName());
System.out.println("\tLikelihood: " + f.getLikelihood());
}
}
}
}
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。