How to use AWS Comprehend with Spring in Java In AWS

Is AWS Comprehend free?

AWS Comprehend has a free quota available under the free tier. However, you can still be eligible for billing if you exceed the maximum number of tokens that AWS provides during the free tier. And after you have exhausted the first year – which starts from the moment you make your first function call to AWS comprehend, you will be charged on a pay as you go basis. Please refer the official pricing page of AWS comprehend for up to date information.

How can I analyze text from pdf directly using AWS Comprehend?

To achieve something like this, you can make use of additional AWS OCR technology like Textract and directly feed the output of this into Comprehend for automatic analysis of pdf and text documents.

Can I train Comprehend on my custom dataset?

Yes, AWS provides the facility to users to be able to train Comprehend on their personal dataset.

I have medical dataset. Can I analyze it using the API used in the above article?

No, AWS provides a separate API for medical datasets called the Amazon Comprehend Medical API. You must use that for medical data analysis.



Using AWS Comprehend with Spring in Java

AWS Comprehend is a natural language processing service for text that makes use of machine learning under the hood to help you derive important insights from your data. Comprehend is a powerful service because it provides many kinds of analysis and even offers multi-lingual support. Amazon comprehend offers the following kind of analysis till date:

  1. Entity Recognition
  2. Sentiment Analysis
  3. Syntax Analysis
  4. Key phrase Extraction
  5. Language Detection
  6. Detect PII
  7. Contains PII
  8. Toxicity Detection – Generative AI

Apart from this, it also offers a feature called Custom Comprehend wherein you can provide your data as input to any of these services and train them specifically on your dataset. This helps if you have a very different use scenario and also it costs more to train your data.

AWS Comprehend can be used in many ways. One is simply to go on the dashboard and perform all the analysis you need by manually configuring the data and the setting. But as you must have guessed by now, this way is not very suitable if you are looking to use this to process huge amounts of data in a real-world application. For this, AWS offers a set of APIs that help us to communicate with these features. In this article we will be seeing how we can use the java SDK to include these in a real world application.

Step 1: Obtain Access Keys from AWS Management Console

Open the AWS management console and in the top right corner, open the drop down next to the user-name. Click on the `security credentials` option.

This will take you to the IAM Credential section. When there, scroll to the part where you can see ‘Access-Keys’. Click on the access-key option and follow the prompts to create an access key for SDK access. Please refer the following screenshot.

To know more on Access Keys, please visit this page.

Step 2: Create a new project from spring starter


The version used is Java 17 and the only dependency we need to add right now is spring web. Click on generate and open the project in your IDE. For the purpose of this tutorial, we will use IntelliJ Idea.

Step 3: Adding AWS Comprehend SDK to your Project

Add the following dependencies to your project. This dependency will pull the comprehend code from maven central and make it available for you to use in your local environment.

        <dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.12.579</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-comprehend -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-comprehend</artifactId>
<version>1.12.578</version>
</dependency>

Step 4: Configure your AWS Credentials

When interacting with any cloud APIs like that of Comprehend, with each request your send, you also send your access key and secret key with that request. This is how AWS cloud uniquely identifies you for billing purposes. Without these credentials, you will not be able to use the Comprehend features in your code.

Import the project that you have generated in the previous step in your IDE. Head over to the application.properties file in the resources folder of the spring project that you just created and imported in your IDE. Next, add the following four properties:

  • cloud.aws.credentials.access-key={paste-your-access-key-here}
  • cloud.aws.credentials.secret-key={paste-your-secret-key-here}
  • cloud.aws.region.static={the-region-of-cloud eg: ap-south-1}

This will allow your application to interact with AWS APIs. Remember to remove your secret keys and access keys, incase you want to share your code.

Step 5: Create a Spring Bean For Comprehend Client

Create a class called CredentialsConfig as:

Java
@Configuration
public class CredentialsConfig {
    @Value("${cloud.aws.region.static}")
    private String region; 
    @Value("${cloud.aws.credentials.access-key}")
    private String accessKey;
    @Value("${cloud.aws.credentials.secret-key}")
    private String secretKey;
    @Bean
    public AmazonComprehend comprehendClient() {
        BasicAWSCredentials credentials=new BasicAWSCredentials(accessKey,secretKey);
        return AmazonComprehendClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .withRegion(region)
                .build();
      //the above statement provides your credentials, each time you try to access the resources.
    }
}

We will call this method each time we send data over to comprehend for analysis. This class provides us the credentials in the manner that AWS expects us to provide it to.

This completes the setup process. Now we will see a hands-on example of how to work with Comprehend in Spring.

Similar Reads

Using AWS Comprehend with Spring in Java

For the purpose of this tutorial, we will take a sample piece of text and use it to obtain the result analysis from Amazon Comprehend. We will create a class that will contain our static test string. Apart from this we will create two more classes called ComprehendController and ComprehendService class. When the user visits a particular page, we will trigger that particular analysis and display the results on the browser page. Please be careful with how you implement your API calls. This is essential to avoid calling any unintended APIs as this is a paid service. You should have a clear picture of what you are doing to avoid unnecessary bills....

Output

To see how the various comprehend APIs yield output, just navigate to that particular api. Notice the URL in the following screenshots:...

Conclusion

In this article, we learnt about what is AWS Comprehend and some points related to its pricing. Next we looked at how we can analyze some sample text using this service. If this helped you, please like the article and comment below if you face any difficulties....

Using AWS Comprehend with Spring in Java – FAQ’s

Is AWS Comprehend free?...