async function inspectStringMultipleRules()

in dlp/inspectStringMultipleRules.js [35:117]


  async function inspectStringMultipleRules() {
    // Construct item to inspect
    const item = {value: string};

    // Construct hotword rules
    const patientRule = {
      hotwordRegex: {pattern: 'patient'},
      proximity: {windowBefore: 10},
      likelihoodAdjustment: {
        fixedLikelihood:
          DLP.protos.google.privacy.dlp.v2.Likelihood.VERY_LIKELY,
      },
    };

    const doctorRule = {
      hotwordRegex: {pattern: 'doctor'},
      proximity: {windowBefore: 10},
      likelihoodAdjustment: {
        fixedLikelihood: DLP.protos.google.privacy.dlp.v2.Likelihood.UNLIKELY,
      },
    };

    // Construct exclusion rules
    const quasimodoRule = {
      dictionary: {wordList: {words: ['quasimodo']}},
      matchingType:
        DLP.protos.google.privacy.dlp.v2.MatchingType
          .MATCHING_TYPE_PARTIAL_MATCH,
    };

    const redactedRule = {
      regex: {pattern: 'REDACTED'},
      matchingType:
        DLP.protos.google.privacy.dlp.v2.MatchingType
          .MATCHING_TYPE_PARTIAL_MATCH,
    };

    // The infoTypes of information to match
    const infoTypes = [{name: 'PERSON_NAME'}];

    // Construct a ruleset that applies the rules to the PERSON_NAME infotype.
    const ruleSet = [
      {
        infoTypes: infoTypes,
        rules: [
          {hotwordRule: patientRule},
          {hotwordRule: doctorRule},
          {exclusionRule: quasimodoRule},
          {exclusionRule: redactedRule},
        ],
      },
    ];

    // Construct the configuration for the Inspect request, including the ruleset.
    const inspectConfig = {
      infoTypes: infoTypes,
      ruleSet: ruleSet,
      includeQuote: true,
    };

    // Construct the Inspect request to be sent by the client.
    const request = {
      parent: `projects/${projectId}/locations/global`,
      inspectConfig: inspectConfig,
      item: item,
    };

    // Use the client to send the API request.
    const [response] = await dlp.inspectContent(request);

    // Print findings.
    const findings = response.result.findings;
    if (findings.length > 0) {
      console.log(`Findings: ${findings.length}\n`);
      findings.forEach(finding => {
        console.log(`InfoType: ${finding.infoType.name}`);
        console.log(`\tQuote: ${finding.quote}`);
        console.log(`\tLikelihood: ${finding.likelihood} \n`);
      });
    } else {
      console.log('No findings.');
    }
  }