API Development

AWS Comprehend and Comprehend Medical Natural Language Processing Plugins for API Builder

Learn to Leverage AWS Comprehend and Comprehend Medical Natural Language Processing (NLP) Plugins for API Builder

Natural language processing (NLP) is the ability of a computer program to understand written or spoken human language.

This blog post describes two Natural Language Processing (NLP) Plugins for API Builder based on AWS (Amazon Web Services) Comprehend and Comprehend Medical. The plugins are intended to help create API Builder based APIs and Integrations that include NLP. For example, you can create a product review API that can immediately determine the sentiment of a review and notify pertinent stakeholders or store the sentiment to a database for upstream processing. This is known as sentiment analysis and was previously discussed in this blog post.

In this blog post we’ll describe how to install and leverage the plugins in an API.

You can find and read about the plugins here:

* API Builder Plugin for AWS Comprehend for NLP
* API Builder Plugin for AWS Medical Comprehend for NLP

The current methods exposed for each plugin are:

  • Comprehend
    • detectDominantLanguageidentifies the dominant language in a string
    • detectEntitiesreturns a list of entities, such as people, places, and locations identified in a string
    • detectPiiEntitiesdetects personal data that could be used to identify an individual, such as an address, bank account number, or phone number in a string
    • detectSentimentdetermines the emotional sentiment of a string
  • Comprehend Medical
    • detectEntitiesV2detects entities, which are textual references to medical information such as medical conditions, medications, or Protected Health Information (PHI) in a string
    • detectPHIdetects only Protected Health Information (PHI) data in string

Prerequisites

In order to use the plugins you will need an AWS access key ID and secret access key, which are used to sign programmatic requests that you make to AWS (through the plugin). You will use these to set the following two environment variables as per the AWS SDK for JavaScript online docs:

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY

Refer to the AWS online docs for instructions on how to get an access key id and secret access key.

Install Plugin

If you don’t already have an API Builder project created, then create a new API Builder project using the API Builder Getting Started guide here. For example:

axway builder init nlpplugintest

Install the Comprehend plugin by executing the following command in your API Builder project folder:

npm install api-builder-plugin-comprehend-sdk

One way to set environment variables for an API Builder project is to edit the /conf/.env environment file and add the following two environment variables:

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY

For example:

PORT=8080
LOG_LEVEL=debug
AWS_ACCESS_KEY_ID=AK........UA
AWS_SECRET_ACCESS_KEY=nie......oKE

At this point the plugin is installed and ready to use in the flow editor in an integration or an API and you will find the plugin in the NLP section of the Flow-Nodes palette.

Test Plugin

For convenience sake, we’ll modify the built-in Greeting API to test the plugin since the Greeting API takes a username parameter which we can used to pass a string into the plugin as the text to be analyzed.

Start your API Builder project using npm start.

Start the console and open the Greeting API flow. You will see the Comprehend plugin in the lower left hand corner of the screen:

Now, let’s do the following:

  • Drag the Comprehend-SDK plugin to the canvas and connect it between the Start node and the Success HTTP Response node
  • Select “Detect Sentiment” from the Comprehend-SDK Method picker
  • Set the Text Parameter to Selector and enter $.params.username as it’s value

  • Click on the Success HTTP node and change the Body from $.greeting to $.sentimentResponse

  • Click the debug icon and enter a string for the username (for example, “this is cool”) and click the Execute Flow button

  • See the response:

{
  "status": 200,
  "body": {
    "Sentiment": "POSITIVE",
    "SentimentScore": {
      "Positive": 0.9868705868721008,
      "Negative": 0.0007539316429756582,
      "Neutral": 0.01085262093693018,
      "Mixed": 0.0015227935509756207
    }
  }
}

Now that you see how easy it is to use the plugins in the Greeting API, you can now integrate NLP in your own API Builder APIs and Integrations.

Summary

AWS Comprehend and Comprehend Medical are powerful NLP services. The two API Builder Plugins described here can help developers leverage these services easily in API Builder APIs and Integrations.

Need help with API Builder? Extend your skills with Axway University.