Building a digital wingman to assist busy clinical staff
Tags: go, ai, aws, comprehend
Published: 2019-04-08 14:20:44

---
title: Building a digital wingman to assist busy clinical staff
published: true
description: creating a "digital wingman" to assist busy clinical staff, by detecting if particular drugs have been missed off a discharge letter
tags: go, ai, aws, comprehend
canonical_url: https://paulbradley.org/comprehend/
---

# Building a digital wingman to assist busy clinical staff

I've been working on creating a "digital wingman" to assist busy clinical staff, by detecting if particular drugs have been missed off a discharge letter.

{% youtube 9KJzKkSG5jo %}

Sorry, the video doesn't have any voice over so this is what you're seeing:

- An unstructured discharge letter is sent to AWS's Medical Comprehend AI
- A list of medications, dosage and route information is detected within the text, along with forty items of interest. These include medical procedurals and diagnosis.
- A visual model of the medical terms and their relationship with each other is then produced and displayed.
- We then remove the drug that treats hypertension (high blood pressure) and rerun the program. As hypertension is mentioned within the clinical narrative the system then checks all possible drugs that could be prescribed to treat hypertension.
- Not finding one, it then sends an alert to the caregiver for investigation.

## AWS medical comprehend 

The medical comprehend API is a recent offering from AWS which accepts unstructured medical text and returns a JSON object outlining all the medical, procedural and diagnosis terms found within the text. For medications, it also returns the dose, route and frequency if found within the text. For each medical term found a confidence score is returned to help you decided if you should trust the value. An offset position is also returned indicating the starting point for the medical term found.

## Example using Go

The first step is to install the AWS software development kit (SDK) for Go. This is done by using the following Go get command issued at the terminal or command prompt.

 
```go
go get github.com/aws/aws-sdk-go/...
```

Once the AWS SDK has been installed, you’ll then need to import the relevant sections into your program to be able to interact with the medical comprehend API.

    
```go
package main

import (
    "bytes"
    "fmt"
    "log"
    "net/http"
    "os"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/credentials"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/comprehendmedical"
)
``` 

You’ll need to create an AWS session select a region which supports the medical comprehend API (not all do). In the example below the newly created session is assigned to the `s` variable. The session object is created by passing in the region identifier and your AWS credentials for id and secret key. Your id and secret key can be obtained from your AWS account.

```go
func main() {

    // create an AWS session which can be
    // reused if we're uploading many files
    s, err := session.NewSession(&aws.Config{
        Region: aws.String("us-east-1"),
        Credentials: credentials.NewStaticCredentials(
            "XXX",
            "YYY",
            ""),
    })
                
    if err != nil {
        log.Fatal(err)
    }

    client := comprehendmedical.New(s)
    input  := comprehendmedical.DetectEntitiesInput{}

    input.SetText(`
        Pt is 40yo mother, highschool teacher
        HPI   : Sleeping trouble on present dosage of Clonidine.
                Severe Rash  on face and leg, slightly itchy
        Meds  : Vyvanse 50 mgs po at breakfast daily,
                Clonidine 0.2 mgs -- 1 and 1 / 2 tabs po qhs
        HEENT : Boggy inferior turbinates, No oropharyngeal lesion
        Lungs : clear
        Heart : Regular rhythm
        Skin  : Mild erythematous eruption to hairline
    `)

    result, err := client.DetectEntities(&input)

    if err != nil {
        log.Fatal(err)
    }
}
``` 

Start by defining a medical comprehend client using the New function passing in your AWS session variable `s`. Then define an input object and use the `SetText` function to define the medical text you want to process. The processing of the medical text is done when the `DetectEntities` function is called and the results are stored in the results variable.

To get the JSON string returned you can use the `GoString` function as shown below. This is useful if you want to store the JSON in a Postgres JSONB database field to perform search queries across many patients.

```go
fmt.Println(result.GoString())
```

You can loop over the detected terms and only print out the medications found by using the following pattern:

```go
for _, entity := range result.Entities {
    if *entity.Category == "MEDICATION" {
        fmt.Println(*entity.Text)
        fmt.Println(*entity.Type)
        fmt.Println(*entity.Score)
        fmt.Println("-----------")
    }
}        
```

Which will output the following results:

```go
Clonidine
GENERIC_NAME
0.9948381781578064
-----------
Vyvanse
BRAND_NAME
0.9995348453521729
-----------
Clonidine
GENERIC_NAME
0.997945249080658
-----------
```

## Categories
Other useful category names include:-

- DX_NAME
- DIAGNOSIS
- SYMPTON
- SYSTEM_ORGAN_SITE
- DOSAGE & DIRECTION.