Amazon Comprehend examples using SDK for Java 2.x - Amazon SDK for Java 2.x
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Amazon Comprehend examples using SDK for Java 2.x

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for Java 2.x with Amazon Comprehend.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use CreateDocumentClassifier.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.comprehend.ComprehendClient; import software.amazon.awssdk.services.comprehend.model.ComprehendException; import software.amazon.awssdk.services.comprehend.model.CreateDocumentClassifierRequest; import software.amazon.awssdk.services.comprehend.model.CreateDocumentClassifierResponse; import software.amazon.awssdk.services.comprehend.model.DocumentClassifierInputDataConfig; /** * Before running this code example, you can setup the necessary resources, such * as the CSV file and IAM Roles, by following this document: * https://aws.amazon.com/blogs/machine-learning/building-a-custom-classifier-using-amazon-comprehend/ * * Also, set up your development environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DocumentClassifierDemo { public static void main(String[] args) { final String usage = """ Usage: <dataAccessRoleArn> <s3Uri> <documentClassifierName> Where: dataAccessRoleArn - The ARN value of the role used for this operation. s3Uri - The Amazon S3 bucket that contains the CSV file. documentClassifierName - The name of the document classifier. """; if (args.length != 3) { System.out.println(usage); System.exit(1); } String dataAccessRoleArn = args[0]; String s3Uri = args[1]; String documentClassifierName = args[2]; Region region = Region.US_EAST_1; ComprehendClient comClient = ComprehendClient.builder() .region(region) .build(); createDocumentClassifier(comClient, dataAccessRoleArn, s3Uri, documentClassifierName); comClient.close(); } public static void createDocumentClassifier(ComprehendClient comClient, String dataAccessRoleArn, String s3Uri, String documentClassifierName) { try { DocumentClassifierInputDataConfig config = DocumentClassifierInputDataConfig.builder() .s3Uri(s3Uri) .build(); CreateDocumentClassifierRequest createDocumentClassifierRequest = CreateDocumentClassifierRequest.builder() .documentClassifierName(documentClassifierName) .dataAccessRoleArn(dataAccessRoleArn) .languageCode("en") .inputDataConfig(config) .build(); CreateDocumentClassifierResponse createDocumentClassifierResult = comClient .createDocumentClassifier(createDocumentClassifierRequest); String documentClassifierArn = createDocumentClassifierResult.documentClassifierArn(); System.out.println("Document Classifier ARN: " + documentClassifierArn); } catch (ComprehendException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }

The following code example shows how to use DetectDominantLanguage.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.comprehend.ComprehendClient; import software.amazon.awssdk.services.comprehend.model.ComprehendException; import software.amazon.awssdk.services.comprehend.model.DetectDominantLanguageRequest; import software.amazon.awssdk.services.comprehend.model.DetectDominantLanguageResponse; import software.amazon.awssdk.services.comprehend.model.DominantLanguage; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DetectLanguage { public static void main(String[] args) { // Specify French text - "It is raining today in Seattle". String text = "Il pleut aujourd'hui à Seattle"; Region region = Region.US_EAST_1; ComprehendClient comClient = ComprehendClient.builder() .region(region) .build(); System.out.println("Calling DetectDominantLanguage"); detectTheDominantLanguage(comClient, text); comClient.close(); } public static void detectTheDominantLanguage(ComprehendClient comClient, String text) { try { DetectDominantLanguageRequest request = DetectDominantLanguageRequest.builder() .text(text) .build(); DetectDominantLanguageResponse resp = comClient.detectDominantLanguage(request); List<DominantLanguage> allLanList = resp.languages(); for (DominantLanguage lang : allLanList) { System.out.println("Language is " + lang.languageCode()); } } catch (ComprehendException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }

The following code example shows how to use DetectEntities.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.comprehend.ComprehendClient; import software.amazon.awssdk.services.comprehend.model.DetectEntitiesRequest; import software.amazon.awssdk.services.comprehend.model.DetectEntitiesResponse; import software.amazon.awssdk.services.comprehend.model.Entity; import software.amazon.awssdk.services.comprehend.model.ComprehendException; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DetectEntities { public static void main(String[] args) { String text = "Amazon.com, Inc. is located in Seattle, WA and was founded July 5th, 1994 by Jeff Bezos, allowing customers to buy everything from books to blenders. Seattle is north of Portland and south of Vancouver, BC. Other notable Seattle - based companies are Starbucks and Boeing."; Region region = Region.US_EAST_1; ComprehendClient comClient = ComprehendClient.builder() .region(region) .build(); System.out.println("Calling DetectEntities"); detectAllEntities(comClient, text); comClient.close(); } public static void detectAllEntities(ComprehendClient comClient, String text) { try { DetectEntitiesRequest detectEntitiesRequest = DetectEntitiesRequest.builder() .text(text) .languageCode("en") .build(); DetectEntitiesResponse detectEntitiesResult = comClient.detectEntities(detectEntitiesRequest); List<Entity> entList = detectEntitiesResult.entities(); for (Entity entity : entList) { System.out.println("Entity text is " + entity.text()); } } catch (ComprehendException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • For API details, see DetectEntities in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use DetectKeyPhrases.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.comprehend.ComprehendClient; import software.amazon.awssdk.services.comprehend.model.DetectKeyPhrasesRequest; import software.amazon.awssdk.services.comprehend.model.DetectKeyPhrasesResponse; import software.amazon.awssdk.services.comprehend.model.KeyPhrase; import software.amazon.awssdk.services.comprehend.model.ComprehendException; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DetectKeyPhrases { public static void main(String[] args) { String text = "Amazon.com, Inc. is located in Seattle, WA and was founded July 5th, 1994 by Jeff Bezos, allowing customers to buy everything from books to blenders. Seattle is north of Portland and south of Vancouver, BC. Other notable Seattle - based companies are Starbucks and Boeing."; Region region = Region.US_EAST_1; ComprehendClient comClient = ComprehendClient.builder() .region(region) .build(); System.out.println("Calling DetectKeyPhrases"); detectAllKeyPhrases(comClient, text); comClient.close(); } public static void detectAllKeyPhrases(ComprehendClient comClient, String text) { try { DetectKeyPhrasesRequest detectKeyPhrasesRequest = DetectKeyPhrasesRequest.builder() .text(text) .languageCode("en") .build(); DetectKeyPhrasesResponse detectKeyPhrasesResult = comClient.detectKeyPhrases(detectKeyPhrasesRequest); List<KeyPhrase> phraseList = detectKeyPhrasesResult.keyPhrases(); for (KeyPhrase keyPhrase : phraseList) { System.out.println("Key phrase text is " + keyPhrase.text()); } } catch (ComprehendException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • For API details, see DetectKeyPhrases in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use DetectSentiment.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.comprehend.ComprehendClient; import software.amazon.awssdk.services.comprehend.model.ComprehendException; import software.amazon.awssdk.services.comprehend.model.DetectSentimentRequest; import software.amazon.awssdk.services.comprehend.model.DetectSentimentResponse; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DetectSentiment { public static void main(String[] args) { String text = "Amazon.com, Inc. is located in Seattle, WA and was founded July 5th, 1994 by Jeff Bezos, allowing customers to buy everything from books to blenders. Seattle is north of Portland and south of Vancouver, BC. Other notable Seattle - based companies are Starbucks and Boeing."; Region region = Region.US_EAST_1; ComprehendClient comClient = ComprehendClient.builder() .region(region) .build(); System.out.println("Calling DetectSentiment"); detectSentiments(comClient, text); comClient.close(); } public static void detectSentiments(ComprehendClient comClient, String text) { try { DetectSentimentRequest detectSentimentRequest = DetectSentimentRequest.builder() .text(text) .languageCode("en") .build(); DetectSentimentResponse detectSentimentResult = comClient.detectSentiment(detectSentimentRequest); System.out.println("The Neutral value is " + detectSentimentResult.sentimentScore().neutral()); } catch (ComprehendException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • For API details, see DetectSentiment in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use DetectSyntax.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.comprehend.ComprehendClient; import software.amazon.awssdk.services.comprehend.model.ComprehendException; import software.amazon.awssdk.services.comprehend.model.DetectSyntaxRequest; import software.amazon.awssdk.services.comprehend.model.DetectSyntaxResponse; import software.amazon.awssdk.services.comprehend.model.SyntaxToken; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DetectSyntax { public static void main(String[] args) { String text = "Amazon.com, Inc. is located in Seattle, WA and was founded July 5th, 1994 by Jeff Bezos, allowing customers to buy everything from books to blenders. Seattle is north of Portland and south of Vancouver, BC. Other notable Seattle - based companies are Starbucks and Boeing."; Region region = Region.US_EAST_1; ComprehendClient comClient = ComprehendClient.builder() .region(region) .build(); System.out.println("Calling DetectSyntax"); detectAllSyntax(comClient, text); comClient.close(); } public static void detectAllSyntax(ComprehendClient comClient, String text) { try { DetectSyntaxRequest detectSyntaxRequest = DetectSyntaxRequest.builder() .text(text) .languageCode("en") .build(); DetectSyntaxResponse detectSyntaxResult = comClient.detectSyntax(detectSyntaxRequest); List<SyntaxToken> syntaxTokens = detectSyntaxResult.syntaxTokens(); for (SyntaxToken token : syntaxTokens) { System.out.println("Language is " + token.text()); System.out.println("Part of speech is " + token.partOfSpeech().tagAsString()); } } catch (ComprehendException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • For API details, see DetectSyntax in Amazon SDK for Java 2.x API Reference.