|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2023 Google LLC. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * For instructions on how to run the full sample: |
| 21 | + * |
| 22 | + * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/bigquery/api/README.md |
| 23 | + */ |
| 24 | + |
| 25 | +namespace Google\Cloud\Samples\Dlp; |
| 26 | + |
| 27 | +// [START dlp_inspect_hotword_rule] |
| 28 | +use Google\Cloud\Dlp\V2\DlpServiceClient; |
| 29 | +use Google\Cloud\Dlp\V2\ContentItem; |
| 30 | +use Google\Cloud\Dlp\V2\CustomInfoType; |
| 31 | +use Google\Cloud\Dlp\V2\CustomInfoType\DetectionRule\HotwordRule; |
| 32 | +use Google\Cloud\Dlp\V2\CustomInfoType\DetectionRule\LikelihoodAdjustment; |
| 33 | +use Google\Cloud\Dlp\V2\CustomInfoType\DetectionRule\Proximity; |
| 34 | +use Google\Cloud\Dlp\V2\CustomInfoType\Regex; |
| 35 | +use Google\Cloud\Dlp\V2\InfoType; |
| 36 | +use Google\Cloud\Dlp\V2\InspectConfig; |
| 37 | +use Google\Cloud\Dlp\V2\InspectionRule; |
| 38 | +use Google\Cloud\Dlp\V2\InspectionRuleSet; |
| 39 | +use Google\Cloud\Dlp\V2\Likelihood; |
| 40 | + |
| 41 | +/** |
| 42 | + * Inspect data with a hotword rule |
| 43 | + * This sample uses a custom regex with a hotword rule to increase the likelihood of match. |
| 44 | + * |
| 45 | + * @param string $projectId The Google Cloud project id to use as a parent resource. |
| 46 | + * @param string $textToInspect The string to inspect. |
| 47 | + */ |
| 48 | +function inspect_hotword_rule( |
| 49 | + // TODO(developer): Replace sample parameters before running the code. |
| 50 | + string $projectId, |
| 51 | + string $textToInspect = "Patient's MRN 444-5-22222 and just a number 333-2-33333" |
| 52 | +): void { |
| 53 | + // Instantiate a client. |
| 54 | + $dlp = new DlpServiceClient(); |
| 55 | + |
| 56 | + $parent = "projects/$projectId/locations/global"; |
| 57 | + |
| 58 | + // Specify what content you want the service to Inspect. |
| 59 | + $item = (new ContentItem()) |
| 60 | + ->setValue($textToInspect); |
| 61 | + |
| 62 | + // Specify the regex pattern the inspection will look for. |
| 63 | + $customRegexPattern = '[1-9]{3}-[1-9]{1}-[1-9]{5}'; |
| 64 | + $hotwordRegexPattern = '(?i)(mrn|medical)(?-i)'; |
| 65 | + |
| 66 | + // Construct the custom regex detector. |
| 67 | + $cMrnDetector = (new InfoType()) |
| 68 | + ->setName('C_MRN'); |
| 69 | + $customInfoType = (new CustomInfoType()) |
| 70 | + ->setInfoType($cMrnDetector) |
| 71 | + ->setLikelihood(Likelihood::POSSIBLE) |
| 72 | + ->setRegex((new Regex()) |
| 73 | + ->setPattern($customRegexPattern)); |
| 74 | + |
| 75 | + // Specify hotword likelihood adjustment. |
| 76 | + $likelihoodAdjustment = (new LikelihoodAdjustment()) |
| 77 | + ->setFixedLikelihood(Likelihood::VERY_LIKELY); |
| 78 | + |
| 79 | + // Specify a window around a finding to apply a detection rule. |
| 80 | + $proximity = (new Proximity()) |
| 81 | + ->setWindowBefore(10); |
| 82 | + |
| 83 | + $hotwordRule = (new HotwordRule()) |
| 84 | + ->setHotwordRegex((new Regex()) |
| 85 | + ->setPattern($hotwordRegexPattern)) |
| 86 | + ->setLikelihoodAdjustment($likelihoodAdjustment) |
| 87 | + ->setProximity($proximity); |
| 88 | + |
| 89 | + // Construct rule set for the inspect config. |
| 90 | + $inspectionRuleSet = (new InspectionRuleSet()) |
| 91 | + ->setInfoTypes([$cMrnDetector]) |
| 92 | + ->setRules([ |
| 93 | + (new InspectionRule()) |
| 94 | + ->setHotwordRule($hotwordRule) |
| 95 | + ]); |
| 96 | + |
| 97 | + // Construct the configuration for the Inspect request. |
| 98 | + $inspectConfig = (new InspectConfig()) |
| 99 | + ->setCustomInfoTypes([$customInfoType]) |
| 100 | + ->setIncludeQuote(true) |
| 101 | + ->setRuleSet([$inspectionRuleSet]); |
| 102 | + |
| 103 | + // Run request |
| 104 | + $response = $dlp->inspectContent([ |
| 105 | + 'parent' => $parent, |
| 106 | + 'inspectConfig' => $inspectConfig, |
| 107 | + 'item' => $item |
| 108 | + ]); |
| 109 | + |
| 110 | + // Print the results |
| 111 | + $findings = $response->getResult()->getFindings(); |
| 112 | + if (count($findings) == 0) { |
| 113 | + printf('No findings.' . PHP_EOL); |
| 114 | + } else { |
| 115 | + printf('Findings:' . PHP_EOL); |
| 116 | + foreach ($findings as $finding) { |
| 117 | + printf(' Quote: %s' . PHP_EOL, $finding->getQuote()); |
| 118 | + printf(' Info type: %s' . PHP_EOL, $finding->getInfoType()->getName()); |
| 119 | + printf(' Likelihood: %s' . PHP_EOL, Likelihood::name($finding->getLikelihood())); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | +// [END dlp_inspect_hotword_rule] |
| 124 | + |
| 125 | +// The following 2 lines are only needed to run the samples |
| 126 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 127 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments