How to use AWS Comprehend with Spring in Java In AWS

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.

Step 1: Create the TextData.java class

This class will contain the data that we want to analyze. This is the input to the Comprehend APIs.

Java
public class TextData {
    protected static String DATA="In the whimsical world of Geekland, " +
            "there lived a boy named Geek, whose curiosity was as " +
            "boundless as the digital plains he roamed. On a sunny " +
            "morning of April 14, 2024, Geek embarked on an adventure" +
            " to decode the secrets of the Cloud Mountains. With" +
            " his trusty 4111 1111 1111 1111 credit card, he" +
            " purchased the rarest of gadgets from the Binary Bazaar." +
            " His journey was not without challenges, and Geek often " +
            "found solace in calling his mentor at (555) 123-4567 or" +
            " emailing geek@geekland.net for guidance. As the binary" +
            " sunset cast a warm glow over the pixelated landscape, " +
            "Geek felt a surge of triumph and gratitude. His heart, " +
            "though made of circuits and code, swelled with a sentiment" +
            " that felt remarkably like joy. For in Geekland, even a boy" +
            " named Geek could experience the wonders of connection" +
            " and discovery.";
}

Step 2: Create the ComprehendService.java class

This class is responsible for making API calls to the AWS Comprehend service. It also receives the output and passes it on to the controller class that displays it to the user.

Java
import com.amazonaws.services.comprehend.AmazonComprehend;
import com.amazonaws.services.comprehend.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ComprehendService {

    private String text=TextData.DATA;

    @Autowired
    private AmazonComprehend client;

    public List<SyntaxToken> syntaxService() {
        List<SyntaxToken> result=new ArrayList<>();
        try {
            DetectSyntaxRequest detectSyntaxRequest=new DetectSyntaxRequest().withText(text).withLanguageCode("en");
            DetectSyntaxResult detectSyntaxResult=client.detectSyntax(detectSyntaxRequest);
            result=detectSyntaxResult.getSyntaxTokens();
            return result;
        }
        catch (AmazonComprehendException e) {
            System.out.println(e.getCause().getMessage());
        }
        return result;
    }
    public List<PiiEntity> piiService() {
        List<PiiEntity> piiEntities=new ArrayList<>();
        try {
            DetectPiiEntitiesRequest detectPiiEntitiesRequest=new DetectPiiEntitiesRequest().withText(text).withLanguageCode("en");
            DetectPiiEntitiesResult detectPiiEntitiesResult=client.detectPiiEntities(detectPiiEntitiesRequest);
            piiEntities=detectPiiEntitiesResult.getEntities();
            return piiEntities;
        }
        catch (AmazonComprehendException e) {
            System.out.println(e.getCause().getMessage());
        }
        return piiEntities;
    }
    public List<Entity> entityService() {
        List<Entity> entities=new ArrayList<>();
        try {
            DetectEntitiesRequest detectEntitiesRequest= new DetectEntitiesRequest().withText(text).withLanguageCode("en");
            DetectEntitiesResult detectEntitiesResult=client.detectEntities(detectEntitiesRequest);
            entities=detectEntitiesResult.getEntities();
            return entities;
        }
        catch (AmazonComprehendException e) {
            System.err.println(e.getCause().getMessage());
        }
        return entities;
    }
    public String sentimentService() {
        String sentiment=null;

        try {
            DetectSentimentRequest detectSentimentRequest = new DetectSentimentRequest().withText(text).withLanguageCode("en");
            DetectSentimentResult detectSentimentResult = client.detectSentiment(detectSentimentRequest);
            sentiment=detectSentimentResult.getSentiment();
            return sentiment;
        } catch (AmazonComprehendException e) {
            System.err.println(e.getCause().getMessage());
        }
        return sentiment;
    }
    public String languageService() {
        String dominantLanguageCode;
        try {
            DetectDominantLanguageRequest dominantLanguageRequest=new DetectDominantLanguageRequest().withText(text);
            DetectDominantLanguageResult detectDominantLanguageResult=client.detectDominantLanguage(dominantLanguageRequest);
            dominantLanguageCode = detectDominantLanguageResult.getLanguages().get(0).getLanguageCode();
            return dominantLanguageCode;
        }
        catch (AmazonComprehendException e) {
            System.out.println(e.getCause().getMessage());
        }
        return "";
    }
    public List<KeyPhrase> keyPhraseService() {
        List<KeyPhrase> keyPhrases=new ArrayList<>();
        try {
            DetectKeyPhrasesRequest detectKeyPhrasesRequest=new DetectKeyPhrasesRequest().withText(text).withLanguageCode("en");
            DetectKeyPhrasesResult detectKeyPhrasesResult=client.detectKeyPhrases(detectKeyPhrasesRequest);
            keyPhrases=detectKeyPhrasesResult.getKeyPhrases();
            return keyPhrases;
        }
        catch (AmazonComprehendException e) {
            System.err.println(e.getCause().getMessage());
        }
        return keyPhrases;
    }
}


Step 3: Create the ComprehendController.java class

This class is concerned with the user interactions and providing the output to the user.

Java
import com.amazonaws.services.comprehend.model.Entity;
import com.amazonaws.services.comprehend.model.KeyPhrase;
import com.amazonaws.services.comprehend.model.PiiEntity;
import com.amazonaws.services.comprehend.model.SyntaxToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/analysis")
public class ComprehendController {

    String output;

    @Autowired
    private ComprehendService service;

    @GetMapping("/syntax")
    public String syntax() {
        output="";
        try {
            List<SyntaxToken> tokens=service.syntaxService();
            for(SyntaxToken token: tokens) {
                output = output + token.getText() + "--->" + token.getPartOfSpeech().getTag() + "\n";
            }
        }
        catch (NullPointerException e) {
            output="Could not Detect the syntax";
        }
        return output;
    }
    @GetMapping("/pii")
    public String pii() {
        output="";
        String text=TextData.DATA;
        try {
            List<PiiEntity> piiEntities=service.piiService();
            for(PiiEntity piiEntity: piiEntities) {
                int begOff=piiEntity.getBeginOffset();
                int endOff=piiEntity.getEndOffset();
                output = output + piiEntity.getType() + "===" + text.substring(begOff,endOff) + "\n";
            }
        }
        catch (NullPointerException e) {
            output="Could not Detect the pii entities";
        }
        return output;
    }
    @GetMapping("/keyPhrase")
    public String keyPhrase() {
        output="";
        try {
            List<KeyPhrase> keyPhrases=service.keyPhraseService();
            for(KeyPhrase keyPhrase: keyPhrases) {
                output = output + keyPhrase.getText() +", " + "\n";
            }
        }
        catch (NullPointerException e) {
            output="Could not Detect the key phrases";
        }
        return output;
    }
    @GetMapping("/entity")
    public String entity() {
        output="";
        String text=TextData.DATA;
        try {
            List<Entity> entities=service.entityService();
            for(Entity entity: entities) {
                output = output + entity.getType() + "--->" + entity.getText() + "\n";
            }
        }
        catch (NullPointerException e) {
            output="Could not Detect the entities";
        }
        return output;
    }
    @GetMapping("/sentiment")
    public String sentiment() {
        output="";
        try {
            output=service.sentimentService();
            if (output.length()!=0) {
                return output;
            }
            else {
                throw new NullPointerException();
            }
        }
        catch (NullPointerException e) {
            output="Could not Detect the sentiment";
        }
        return output;
    }
    @GetMapping("/language")
    public String language() {
        output="";
        try {
            output=service.languageService();
            if(output.length()!=0) {
                return output;
            }
            else {
                throw new NullPointerException();
            }
        }
        catch (NullPointerException e) {
            output="Could not Detect the language";
        }
        return output;
    }
}

After completing this much, start the application and proceed to “http://localhost:8080/analysis/pii”.

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?...