@@ -84,6 +84,78 @@ def omit_name_if_also_email(
8484
8585# [END dlp_omit_name_if_also_email]
8686
87+
88+ # [START inspect_with_person_name_w_custom_hotword]
89+ def inspect_with_person_name_w_custom_hotword (
90+ project ,
91+ content_string ,
92+ custom_hotword = "patient"
93+ ):
94+ """Uses the Data Loss Prevention API increase likelihood for matches on
95+ PERSON_NAME if the user specified custom hotword is present. Only
96+ includes findings with the increased likelihood by setting a minimum
97+ likelihood threshold of VERY_LIKELY.
98+ Args:
99+ project: The Google Cloud project id to use as a parent resource.
100+ content_string: The string to inspect.
101+ custom_hotword: The custom hotword used for likelihood boosting.
102+ Returns:
103+ None; the response from the API is printed to the terminal.
104+ """
105+
106+ # Import the client library.
107+ import google .cloud .dlp
108+
109+ # Instantiate a client.
110+ dlp = google .cloud .dlp_v2 .DlpServiceClient ()
111+
112+ # Construct a rule set with caller provided hotword, with a likelihood
113+ # boost to VERY_LIKELY when the hotword are present within the 50 character-
114+ # window preceding the PII finding.
115+ hotword_rule = {
116+ "hotword_regex" : {"pattern" : custom_hotword },
117+ "likelihood_adjustment" : {"fixed_likelihood" : "VERY_LIKELY" },
118+ "proximity" : {"window_before" : 50 },
119+ }
120+
121+ rule_set = [
122+ {
123+ "info_types" : [{"name" : "PERSON_NAME" }],
124+ "rules" : [{"hotword_rule" : hotword_rule }],
125+ }
126+ ]
127+
128+ # Construct the configuration dictionary with the custom regex info type.
129+ inspect_config = {
130+ "rule_set" : rule_set ,
131+ "min_likelihood" : "VERY_LIKELY" ,
132+ }
133+
134+ # Construct the `item`.
135+ item = {"value" : content_string }
136+
137+ # Convert the project id into a full resource id.
138+ parent = dlp .project_path (project )
139+
140+ # Call the API.
141+ response = dlp .inspect_content (parent , inspect_config , item )
142+
143+ # Print out the results.
144+ if response .result .findings :
145+ for finding in response .result .findings :
146+ try :
147+ if finding .quote :
148+ print (f"Quote: { finding .quote } " )
149+ except AttributeError :
150+ pass
151+ print (f"Info type: { finding .info_type .name } " )
152+ print (f"Likelihood: { finding .likelihood } " )
153+ else :
154+ print ("No findings." )
155+
156+ # [END inspect_with_person_name_w_custom_hotword]
157+
158+
87159# [START dlp_inspect_with_medical_record_number_custom_regex_detector]
88160def inspect_with_medical_record_number_custom_regex_detector (
89161 project ,
0 commit comments