핫워드 '환자'가 근접한 위치에 있는 경우 PERSON_NAME 일치의 가능성을 높임 의료 데이터베이스에서 환자 이름을 스캔하기 위한 InspectConfig 속성 사용을 보여줍니다. Cloud DLP의 기본 제공 PERSON_NAME infoType 감지기를 사용할 수 있지만, 이 경우 Cloud DLP가 환자 이름뿐만 아니라 모든 사람의 이름에서 일치하는 항목을 찾습니다. 이 문제를 해결하려면 잠재적인 일치 항목의 첫 글자로부터 특정 문자 수 만큼의 근접도 내에서 '환자'라는 단어를 찾는 핫워드 규칙을 포함할 수 있습니다. 이는 특수한 기준에 해당하므로 이 패턴과 일치하는 결과에 'very likely' 가능성을 할당할 수 있습니다. InspectConfig 내에서 최소 가능성을 VERY_LIKELY로 설정하면 이 구성과 일치하는 항목만 검색 결과에 반환되도록 보장할 수 있습니다.
더 살펴보기
이 코드 샘플이 포함된 자세한 문서는 다음을 참조하세요.
코드 샘플
C#
Cloud DLP용 클라이언트 라이브러리를 설치하고 사용하는 방법은 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;
}
}
Go
Cloud DLP용 클라이언트 라이브러리를 설치하고 사용하는 방법은 Cloud DLP 클라이언트 라이브러리를 참조하세요.
Cloud DLP에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.
import (
"context"
"fmt"
"io"
dlp "cloud.google.com/go/dlp/apiv2"
"cloud.google.com/go/dlp/apiv2/dlppb"
)
// inspectStringCustomHotWord inspects a string from sensitive data by using a custom hot word
func inspectStringCustomHotWord(w io.Writer, projectID, textToInspect, customHotWord, infoTypeName string) error {
// projectID := "my-project-id"
// textToInspect := "patient name: John Doe"
// customHotWord := "patient"
// infoTypeName := "PERSON_NAME"
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()
// Specify the type and content to be inspected.
contentItem := &dlppb.ContentItem{
DataItem: &dlppb.ContentItem_ByteItem{
ByteItem: &dlppb.ByteContentItem{
Type: dlppb.ByteContentItem_TEXT_UTF8,
Data: []byte(textToInspect),
},
},
}
// Increase likelihood of matches that have customHotword nearby
hotwordRule := &dlppb.InspectionRule_HotwordRule{
HotwordRule: &dlppb.CustomInfoType_DetectionRule_HotwordRule{
HotwordRegex: &dlppb.CustomInfoType_Regex{
Pattern: customHotWord,
},
Proximity: &dlppb.CustomInfoType_DetectionRule_Proximity{
WindowBefore: 50,
},
LikelihoodAdjustment: &dlppb.CustomInfoType_DetectionRule_LikelihoodAdjustment{
Adjustment: &dlppb.CustomInfoType_DetectionRule_LikelihoodAdjustment_FixedLikelihood{
FixedLikelihood: dlppb.Likelihood_VERY_LIKELY,
},
},
},
}
// Construct a ruleset that applies the hotword rule to the PERSON_NAME infotype.
ruleSet := &dlppb.InspectionRuleSet{
InfoTypes: []*dlppb.InfoType{
{Name: infoTypeName}, //"PERSON_NAME"
},
Rules: []*dlppb.InspectionRule{
{
Type: &dlppb.InspectionRule_HotwordRule{
HotwordRule: hotwordRule.HotwordRule,
},
},
},
}
// Construct the Inspect request to be sent by the client.
req := &dlppb.InspectContentRequest{
Parent: fmt.Sprintf("projects/%s/locations/global", projectID),
Item: contentItem,
InspectConfig: &dlppb.InspectConfig{
InfoTypes: []*dlppb.InfoType{
{Name: infoTypeName}, //"PERSON_NAME"
},
IncludeQuote: true,
MinLikelihood: dlppb.Likelihood_VERY_LIKELY,
RuleSet: []*dlppb.InspectionRuleSet{
ruleSet,
},
},
}
// Send the request.
resp, err := client.InspectContent(ctx, req)
if err != nil {
return err
}
// Parse the response and process results
fmt.Fprintf(w, "Findings: %v\n", len(resp.Result.Findings))
for _, v := range resp.GetResult().Findings {
fmt.Fprintf(w, "Quote: %v\n", v.GetQuote())
fmt.Fprintf(w, "Infotype Name: %v\n", v.GetInfoType().GetName())
fmt.Fprintf(w, "Likelihood: %v\n", v.GetLikelihood())
}
return nil
}
Java
Cloud DLP용 클라이언트 라이브러리를 설치하고 사용하는 방법은 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 샘플 브라우저를 참조하세요.