Skip to content

Commit 7b58d23

Browse files
[DLP] Implemented sample for DeIdentify Data Replace With Dictionary (GoogleCloudPlatform#7853)
* implemented DeIdentifyDataReplaceWithDictionary.java * resolved review comments * modify license version push * resolved the test error --------- Co-authored-by: Averi Kitsch <akitsch@google.com>
1 parent e4617d9 commit 7b58d23

2 files changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dlp.snippets;
18+
19+
// [START dlp_deidentify_dictionary_replacement]
20+
21+
22+
import com.google.cloud.dlp.v2.DlpServiceClient;
23+
import com.google.privacy.dlp.v2.ContentItem;
24+
import com.google.privacy.dlp.v2.CustomInfoType.Dictionary.WordList;
25+
import com.google.privacy.dlp.v2.DeidentifyConfig;
26+
import com.google.privacy.dlp.v2.DeidentifyContentRequest;
27+
import com.google.privacy.dlp.v2.DeidentifyContentResponse;
28+
import com.google.privacy.dlp.v2.InfoType;
29+
import com.google.privacy.dlp.v2.InfoTypeTransformations;
30+
import com.google.privacy.dlp.v2.InspectConfig;
31+
import com.google.privacy.dlp.v2.LocationName;
32+
import com.google.privacy.dlp.v2.PrimitiveTransformation;
33+
import com.google.privacy.dlp.v2.ReplaceDictionaryConfig;
34+
import java.io.IOException;
35+
36+
public class DeIdentifyDataReplaceWithDictionary {
37+
public static void main(String[] args) throws Exception {
38+
// TODO(developer): Replace these variables before running the sample.
39+
// The Google Cloud project id to use as a parent resource.
40+
String projectId = "your-project-id";
41+
// The string to de-identify
42+
String textToDeIdentify =
43+
"My name is Alicia Abernathy, and my email address is aabernathy@example.com.";
44+
deidentifyDataReplaceWithDictionary(projectId, textToDeIdentify);
45+
}
46+
47+
public static void deidentifyDataReplaceWithDictionary(String projectId, String textToDeIdentify)
48+
throws IOException {
49+
// Initialize client that will be used to send requests. This client only needs to be created
50+
// once, and can be reused for multiple requests. After completing all of your requests, call
51+
// the "close" method on the client to safely clean up any remaining background resources.
52+
try (DlpServiceClient dlp = DlpServiceClient.create()) {
53+
// Specify the content to be inspected.
54+
ContentItem item = ContentItem.newBuilder().setValue(textToDeIdentify).build();
55+
56+
// Specify the type of info the inspection will look for.
57+
// See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types
58+
InfoType infoType = InfoType.newBuilder().setName("EMAIL_ADDRESS").build();
59+
InspectConfig inspectConfig = InspectConfig.newBuilder().addInfoTypes(infoType).build();
60+
61+
// Specify list of value which will randomly replace identified email addresses.
62+
WordList wordList =
63+
WordList.newBuilder().addWords("izumi@example.com").addWords("alex@example.com").build();
64+
65+
// Specify the Dictionary to use for selecting replacement values for the finding.
66+
ReplaceDictionaryConfig replaceDictionaryConfig =
67+
ReplaceDictionaryConfig.newBuilder().setWordList(wordList).build();
68+
69+
// Define type of de-identification as replacement with items from dictionary.
70+
PrimitiveTransformation primitiveTransformation =
71+
PrimitiveTransformation.newBuilder()
72+
.setReplaceDictionaryConfig(replaceDictionaryConfig)
73+
.build();
74+
75+
InfoTypeTransformations.InfoTypeTransformation transformation =
76+
InfoTypeTransformations.InfoTypeTransformation.newBuilder()
77+
.addInfoTypes(infoType)
78+
.setPrimitiveTransformation(primitiveTransformation)
79+
.build();
80+
81+
DeidentifyConfig deidentifyConfig =
82+
DeidentifyConfig.newBuilder()
83+
.setInfoTypeTransformations(
84+
InfoTypeTransformations.newBuilder().addTransformations(transformation))
85+
.build();
86+
87+
// Combine configurations into a request for the service.
88+
DeidentifyContentRequest request =
89+
DeidentifyContentRequest.newBuilder()
90+
.setParent(LocationName.of(projectId, "global").toString())
91+
.setItem(item)
92+
.setDeidentifyConfig(deidentifyConfig)
93+
.setInspectConfig(inspectConfig)
94+
.build();
95+
96+
// Use the client to send the API request.
97+
DeidentifyContentResponse response = dlp.deidentifyContent(request);
98+
99+
// Parse the response and process results
100+
System.out.print("Text after de-identification: " + response.getItem().getValue());
101+
}
102+
}
103+
}
104+
// [END dlp_deidentify_dictionary_replacement]

dlp/snippets/src/test/java/dlp/snippets/DeIdentificationTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,20 @@ public void testDeIdentifyWithFpeSurrogate() throws IOException, NoSuchAlgorithm
573573
assertThat(output).contains("Text after de-identification: ");
574574
}
575575

576+
@Test
577+
public void testDeIdentifyDataReplaceWithDictionary() throws IOException {
578+
DeIdentifyDataReplaceWithDictionary.deidentifyDataReplaceWithDictionary(
579+
PROJECT_ID, "My name is Alicia Abernathy, and my email address is aabernathy@example.com.");
580+
String output = bout.toString();
581+
assertThat(
582+
ImmutableList.of(
583+
"Text after de-identification: My name is Alicia Abernathy, "
584+
+ "and my email address is izumi@example.com.",
585+
"Text after de-identification: My name is Alicia Abernathy, "
586+
+ "and my email address is alex@example.com."))
587+
.contains(output);
588+
}
589+
576590
@Test
577591
public void testReIdentifyWithFpeSurrogate() throws IOException, NoSuchAlgorithmException {
578592

0 commit comments

Comments
 (0)