Menu
Amazon Comprehend
Developer Guide

Using the Batch APIs

To send batches of up to 25 documents, you can use the Amazon Comprehend batch operations. Calling a batch operation is identical to calling the single document APIs for each document in the request. Using the batch APIs can result in better performance for you applications. For more information, see .

Batch Processing With the SDK for Java

The following sample program shows how to use the operation with the SDK for Java. The response from the server contains a BatchDetectEntitiesItemResult object for each document that was successfully processed. If there is an error processing a document there will be a record in the error list in the response. The example gets each of the documents with an error and resends them.

Copy
import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.services.comprehend.AmazonComprehend; import com.amazonaws.services.comprehend.AmazonComprehendClientBuilder; import com.amazonaws.services.comprehend.model.BatchDetectEntitiesItemResult; import com.amazonaws.services.comprehend.model.BatchDetectEntitiesRequest; import com.amazonaws.services.comprehend.model.BatchDetectEntitiesResult; import com.amazonaws.services.comprehend.model.BatchItemError; public class App { public static void main( String[] args ) { AmazonComprehend client = AmazonComprehendClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(awsCreds)) .withRegion("region") .build(); String[] textList = {"I love Seattle", "Today is Sunday", "Tomorrow is Monday", "I love Seattle"}; // Call detectEntities API System.out.println("Calling BatchDetectEntities"); BatchDetectEntitiesRequest batchDetectEntitiesRequest = new BatchDetectEntitiesRequest().withTextList(textList) .withLanguageCode("en"); BatchDetectEntitiesResult batchDetectEntitiesResult = client.batchDetectEntities(batchDetectEntitiesRequest); for(BatchDetectEntitiesItemResult item : batchDetectEntitiesResult.getResultList()) { System.out.println(item); } // check if we need to retry failed requests if (batchDetectEntitiesResult.getErrorList().size() != 0) { System.out.println("Retrying Failed Requests"); ArrayList<String> textToRetry = new ArrayList<String>(); for(BatchItemError errorItem : batchDetectEntitiesResult.getErrorList()) { textToRetry.add(textList[errorItem.getIndex()]); } batchDetectEntitiesRequest = new BatchDetectEntitiesRequest().withTextList(textToRetry).withLanguageCode("en"); batchDetectEntitiesResult = client.batchDetectEntities(batchDetectEntitiesRequest); for(BatchDetectEntitiesItemResult item : batchDetectEntitiesResult.getResultList()) { System.out.println(item); } } System.out.println("End of DetectEntities"); } }

Batch Processing With the AWS CLI

These examples show how to use the batch API operations using the AWS Command Line Interface. All of the operations except BatchDetectDominantLanguage use the following JSON file called process.json as input. For that operation the LanguageCode entity is not included.

The third document in the JSON file ("$$$$$$$$") will cause an error during batch processing. It is included so that the operations will include a BatchItemError in the response.

Copy
{ "LanguageCode": "en", "TextList": [ "I have been living in Seattle for almost 4 years", "It is raining today in Seattle", "$$$$$$$$" ] }

The examples are formatted for Unix, Linux, and macOS. For Windows, replace the backslash (\) Unix continuation character at the end of each line with a caret (^).

Detect the Dominant Language Using a Batch (AWS CLI)

The BatchDetectDominantLanguage operation determines the dominant language of each document in a batch. For a list of the languages that Amazon Comprehend can detect, see Detecting the Primary Language . The following AWS CLI command calls the BatchDetectDominantLanguage operation.

Copy
aws insight batch-detect-dominant-language \ --endpoint endpoint \ --region region \ --cli-input-json file://path to input file/process.json

The following is the response from the BatchDetectDominantLanguage operation:

Copy
{ "ResultList": [ { "Index": 0, "Languages":[ { "LanguageCode":"en", "Score": 0.99 } ] }, { "Index": 1 "Languages":[ { "LanguageCode":"en", "Score": 0.82 } ] } ], "ErrorList": [ { "Index": 2, "ErrorCode": "InternalServerException", "ErrorMessage": "Unexpected Server Error. Please try again." } ] }

Detect Entities Using a Batch (AWS CLI)

Use the BatchDetectEntities operation to find the entities present in a batch of documents. For more information about entities, see Detecting Entities. The following AWS CLI command calls the BatchDetectEntities operation.

Copy
aws insight batch-detect-entities \ --endpoint endpoint \ --region region \ --cli-input-json file://path to input file/process.json

Detect Key Phrases Using a Batch (AWS CLI)

The BatchDetectKeyPhrases operation returns the key noun phrases in a batch of documents. The following AWS CLI command calls the BatchDetectKeyNounPhrases operation.

Copy
aws insight batch-detect-key-phrases --endpoint endpoint --region region --cli-input-json file://path to input file/process.json

Detect Sentiment Using a Batch (AWS CLI)

Detect the overall sentiment of a batch of documents using the BatchDetectSentiment operation. The following AWS CLI command calls the BatchDetectSentiment operation.

Copy
aws insight batch-detect-sentiment \ --endpoint endpoint \ --region region \ --cli-input-json file://path to input file/process.json