in dlp/src/inspect_column_values_w_custom_hotwords.php [53:134]
function inspect_column_values_w_custom_hotwords(string $projectId): void
{
// Instantiate a client.
$dlp = new DlpServiceClient();
$parent = "projects/$projectId/locations/global";
// Specify the table to be inspected.
$tableToDeIdentify = (new Table())
->setHeaders([
(new FieldId())
->setName('Fake Social Security Number'),
(new FieldId())
->setName('Real Social Security Number'),
])
->setRows([
(new Row())->setValues([
(new Value())
->setStringValue('111-11-1111'),
(new Value())
->setStringValue('222-22-2222')
])
]);
$item = (new ContentItem())
->setTable($tableToDeIdentify);
// Specify the regex pattern the inspection will look for.
$hotwordRegexPattern = 'Fake Social Security Number';
// Specify hotword likelihood adjustment.
$likelihoodAdjustment = (new LikelihoodAdjustment())
->setFixedLikelihood(Likelihood::VERY_UNLIKELY);
// Specify a window around a finding to apply a detection rule.
$proximity = (new Proximity())
->setWindowBefore(1);
// Construct the hotword rule.
$hotwordRule = (new HotwordRule())
->setHotwordRegex((new Regex())
->setPattern($hotwordRegexPattern))
->setLikelihoodAdjustment($likelihoodAdjustment)
->setProximity($proximity);
// Construct rule set for the inspect config.
$infotype = (new InfoType())
->setName('US_SOCIAL_SECURITY_NUMBER');
$inspectionRuleSet = (new InspectionRuleSet())
->setInfoTypes([$infotype])
->setRules([
(new InspectionRule())
->setHotwordRule($hotwordRule)
]);
// Construct the configuration for the Inspect request.
$inspectConfig = (new InspectConfig())
->setInfoTypes([$infotype])
->setIncludeQuote(true)
->setRuleSet([$inspectionRuleSet])
->setMinLikelihood(Likelihood::POSSIBLE);
// Run request.
$inspectContentRequest = (new InspectContentRequest())
->setParent($parent)
->setInspectConfig($inspectConfig)
->setItem($item);
$response = $dlp->inspectContent($inspectContentRequest);
// Print the results.
$findings = $response->getResult()->getFindings();
if (count($findings) == 0) {
printf('No findings.' . PHP_EOL);
} else {
printf('Findings:' . PHP_EOL);
foreach ($findings as $finding) {
printf(' Quote: %s' . PHP_EOL, $finding->getQuote());
printf(' Info type: %s' . PHP_EOL, $finding->getInfoType()->getName());
printf(' Likelihood: %s' . PHP_EOL, Likelihood::name($finding->getLikelihood()));
}
}
}