package com.example.comprehend.medical;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.comprehendmedical.ComprehendMedicalClient;
import software.amazon.awssdk.services.comprehendmedical.model.ComprehendMedicalException;
import software.amazon.awssdk.services.comprehendmedical.model.DetectPhiRequest;
import software.amazon.awssdk.services.comprehendmedical.model.DetectPhiResponse;
import software.amazon.awssdk.services.comprehendmedical.model.Entity;

import java.util.Iterator;
import java.util.List;

public class DetectPHI {

    public static void main(String[] args) {

        String text = "Pt is 87 yo woman, highschool teacher with past medical history that includes\n" +
                "   - status post cardiac catheterization in April 2019.\n" +
                "She presents today with palpitations and chest pressure.\n" +
                "HPI : Sleeping trouble on present dosage of Clonidine. Severe Rash  on face and leg, slightly itchy  \n" +
                "Meds : Vyvanse 50 mgs po at breakfast daily, \n" +
                "            Clonidine 0.2 mgs -- 1 and 1 / 2 tabs po qhs \n" +
                "HEENT : Boggy inferior turbinates, No oropharyngeal lesion \n" +
                "Lungs : clear \n" +
                "Heart : Regular rhythm \n" +
                "Skin :  Mild erythematous eruption to hairline \n" +
                "\n" +
                "Follow-up as scheduled";
        Region region = Region.US_EAST_1;
        ComprehendMedicalClient medClient = ComprehendMedicalClient.builder()
                .region(region)
                .build();

        System.out.println("Calling Detect Medical Entities");
        detectPHIValues(medClient, text) ;

    }
    public static void detectPHIValues(ComprehendMedicalClient medClient, String text ) {

        try {
            DetectPhiRequest detectRequest = DetectPhiRequest.builder()
                    .text(text)
                    .build();

            DetectPhiResponse detectResult = medClient.detectPHI(detectRequest);
            List<Entity> entList = detectResult.entities();
            Iterator<Entity> lanIterator = entList.iterator();

            while (lanIterator.hasNext()) {
                Entity entity = lanIterator.next();
                System.out.println("Entity text is " + entity.text());
            }

        } catch (ComprehendMedicalException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
}

@@ -1,44 +1,1041 @@
-package com.example.comprehend.medical;%0A
+// snippet-sourcedescription:%5BDetectPHI demonstrates how to retrieve protected health information (PHI).%5D%0A// snippet-service:%5BAmazon Comprehend Medical%5D%0A// snippet-keyword:%5BJava%5D%0A// snippet-sourcesyntax:%5Bjava%5D%0A// snippet-keyword:%5BAmazon Comprehend Medical%5D%0A// snippet-keyword:%5BCode Sample%5D%0A// snippet-sourcetype:%5Bfull-example%5D%0A// snippet-sourcedate:%5B6/15/2020%5D%0A// snippet-sourceauthor:%5Bscmacdon AWS%5D%0A%0A%0A/**%0A * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.%0A *%0A * This file is licensed under the Apache License, Version 2.0 (the %22License%22).%0A * You may not use this file except in compliance with the License. A copy of%0A * the License is located at%0A *%0A * http://aws.amazon.com/apache2.0/%0A *%0A * This file is distributed on an %22AS IS%22 BASIS, WITHOUT WARRANTIES OR%0A * CONDITIONS OF ANY KIND, either express or implied. See the License for the%0A * specific language governing permissions and limitations under the License.%0A */%0A%0A%0Apackage com.example.comprehend.medical;%0A%0A//snippet-start:%5Bcomprehendmed.java2.detect_phi.import%5D
 %0Aimp
@@ -1484,17 +1484,16 @@
 Entity;%0A
-%0A
 import j
@@ -1533,16 +1533,70 @@
 il.List;
+%0A//snippet-end:%5Bcomprehendmed.java2.detect_phi.import%5D
 %0A%0Apublic
@@ -2784,24 +2784,82 @@
 t) ;%0A%0A    %7D%0A
+    //snippet-start:%5Bcomprehendmed.java2.detect_phi.main%5D%0A
     public s
@@ -3624,20 +3624,80 @@
 ;%0A        %7D%0A
+        //snippet-end:%5Bcomprehendmed.java2.detect_phi.main%5D%0A
     %7D%0A%7D%0A

Update DetectPHI.java