• 沒有找到結果。

Getting Started Using the API

在文檔中 Amazon Comprehend (頁 38-75)

12. When you have finished filling out the form, choose Create job to create and start the topic detection job.

The new job appears in the job list with the status field showing the status of the job. The field can be IN_PROGRESS for a job that is processing, COMPLETED for a job that has finished successfully, and FAILED for a job that has an error. You can click on a job to get more information about the job, including any error messages.

Next Step

Step 4: Getting Started Using the Amazon Comprehend API (p. 30)

Step 4: Getting Started Using the Amazon Comprehend API

The following examples demonstrate how to use Amazon Comprehend operations using the AWS CLI, Java, and Python. Use them to learn about Amazon Comprehend operations and as building blocks for your own applications.

To run the AWS CLI and Python examples, you need to install the AWS CLI. For more information, see Step 2: Set Up the AWS Command Line Interface (AWS CLI) (p. 8).

To run the Java examples, you need to install the AWS SDK for Java. For instructions for installing the SDK for Java, see Set up the AWS SDK for Java.

Topics

• Detecting the Dominant Language (p. 30)

• Detecting Named Entities (p. 33)

• Detecting Key Phrases (p. 35)

• Detecting Personally Identifiable Information (PII) (p. 37)

• Labeling Documents with Personally Identifiable Information (PII) (p. 38)

• Detecting Sentiment (p. 39)

• Detecting Syntax (p. 41)

• Using Custom Classification (p. 44)

• Detecting Custom Entities (p. 48)

• Detecting Events (p. 52)

• Topic Modeling (p. 54)

• Using the Batch APIs (p. 60)

Detecting the Dominant Language

To determine the dominant language used in text, use the Amazon Comprehend

DetectDominantLanguage (p. 312) operation. To detect the dominant language in up to 25 documents in a batch, use the BatchDetectDominantLanguage (p. 234) operation. For more information, see Using the Batch APIs (p. 60).

Topics

• Detecting the Dominant Language Using the AWS Command Line Interface (p. 31)

• Detecting the Dominant Language Using the AWS SDK for Java (p. 31)

• Detecting the Dominant Language Using the AWS SDK for Python (Boto) (p. 32)

Detecting the Dominant Language

• Detecting the Dominant Language Using the AWS SDK for .NET (p. 32)

Detecting the Dominant Language Using the AWS Command Line Interface

The following example demonstrates using the DetectDominantLanguage operation with the AWS CLI.

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

aws comprehend detect-dominant-language \ --region region \

--text "It is raining today in Seattle."

Amazon Comprehend responds with the following:

{ "Languages": [ {

"LanguageCode": "en", "Score": 0.9793661236763 }

] }

Detecting the Dominant Language Using the AWS SDK for Java

The following example uses the DetectDominantLanguage operation with Java.

import com.amazonaws.auth.AWSCredentialsProvider;

import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;

import com.amazonaws.services.comprehend.AmazonComprehend;

import com.amazonaws.services.comprehend.AmazonComprehendClientBuilder;

import com.amazonaws.services.comprehend.model.DetectDominantLanguageRequest;

import com.amazonaws.services.comprehend.model.DetectDominantLanguageResult;

public class App

{ public static void main( String[] args ) {

String text = "It is raining today in Seattle";

// Create credentials using a provider chain. For more information, see

// https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html AWSCredentialsProvider awsCreds = DefaultAWSCredentialsProviderChain.getInstance();

AmazonComprehend comprehendClient =

AmazonComprehendClientBuilder.standard()

.withCredentials(awsCreds) .withRegion("region") .build();

// Call detectDominantLanguage API

System.out.println("Calling DetectDominantLanguage");

DetectDominantLanguageRequest detectDominantLanguageRequest = new DetectDominantLanguageRequest().withText(text);

DetectDominantLanguageResult detectDominantLanguageResult = comprehendClient.detectDominantLanguage(detectDominantLanguageRequest);

Detecting the Dominant Language

detectDominantLanguageResult.getLanguages().forEach(System.out::println);

System.out.println("Calling DetectDominantLanguage\n");

System.out.println("Done");

} }

Detecting the Dominant Language Using the AWS SDK for Python (Boto)

The following example demonstrates using the DetectDominantLanguage operation with Python.

import boto3 import json

comprehend = boto3.client(service_name='comprehend', region_name='region') text = "It is raining today in Seattle"

print('Calling DetectDominantLanguage')

print(json.dumps(comprehend.detect_dominant_language(Text = text), sort_keys=True, indent=4))

print("End of DetectDominantLanguage\n")

Detecting the Dominant Language Using the AWS SDK for .NET

The .NET example in this section uses the AWS SDK for .NET. You can use the AWS Toolkit for Visual Studio to develop AWS applications using .NET. It includes helpful templates and the AWS Explorer for deploying applications and managing services. For a .NET developer perspective of AWS, see the AWS Guide for .NET Developers.

Console.WriteLine("Calling DetectDominantLanguage\n");

DetectDominantLanguageRequest detectDominantLanguageRequest = new DetectDominantLanguageRequest()

{

Text = text };

DetectDominantLanguageResponse detectDominantLanguageResponse = comprehendClient.DetectDominantLanguage(detectDominantLanguageRequest);

foreach (DominantLanguage dl in detectDominantLanguageResponse.Languages) Console.WriteLine("Language Code: {0}, Score: {1}", dl.LanguageCode,

Detecting Named Entities

Detecting Named Entities

To determine the named entities in a document, use the Amazon Comprehend DetectEntities (p. 315) operation. To detect entities in up to 25 documents in a batch, use the BatchDetectEntities (p. 237) operation. For more information, see Using the Batch APIs (p. 60).

Topics

• Detecting Named Entities Using the AWS Command Line Interface (p. 33)

• Detecting Named Entities Using the AWS SDK for Java (p. 33)

• Detecting Named Entities Using the AWS SDK for Python (Boto) (p. 34)

• Detecting Entities Using the AWS SDK for .NET (p. 34)

Detecting Named Entities Using the AWS Command Line Interface

The following example demonstrates using the DetectEntities operation using the AWS CLI. You must specify the language of the input text.

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

aws comprehend detect-entities \ --region region \

--language-code "en" \

--text "It is raining today in Seattle."

Amazon Comprehend responds with the following:

{

"Entities": [ {

"Text": "today", "Score": 0.97, "Type": "DATE", "BeginOffset": 14, "EndOffset": 19 },

{

"Text": "Seattle", "Score": 0.95, "Type": "LOCATION", "BeginOffset": 23, "EndOffset": 30 }

],

"LanguageCode": "en"

}

Detecting Named Entities Using the AWS SDK for Java

The following example uses the DetectEntities operation with Java. You must specify the language of the input text.

import com.amazonaws.auth.AWSCredentialsProvider;

import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;

import com.amazonaws.services.comprehend.AmazonComprehend;

Detecting Named Entities

// Create credentials using a provider chain. For more information, see

// https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html AWSCredentialsProvider awsCreds = DefaultAWSCredentialsProviderChain.getInstance();

AmazonComprehend comprehendClient =

System.out.println("Calling DetectEntities");

DetectEntitiesRequest detectEntitiesRequest = new DetectEntitiesRequest().withText(text)

.withLanguageCode("en");

DetectEntitiesResult detectEntitiesResult = comprehendClient.detectEntities(detectEntitiesRequest);

detectEntitiesResult.getEntities().forEach(System.out::println);

System.out.println("End of DetectEntities\n");

} }

Detecting Named Entities Using the AWS SDK for Python (Boto)

The following example uses the DetectEntities operation with Python. You must specify the language of the input text.

import boto3 import json

comprehend = boto3.client(service_name='comprehend', region_name='region') text = "It is raining today in Seattle"

print('Calling DetectEntities')

print(json.dumps(comprehend.detect_entities(Text=text, LanguageCode='en'), sort_keys=True, indent=4))

print('End of DetectEntities\n')

Detecting Entities Using the AWS SDK for .NET

The .NET example in this section uses the AWS SDK for .NET. You can use the AWS Toolkit for Visual Studio to develop AWS applications using .NET. It includes helpful templates and the AWS Explorer for deploying applications and managing services. For a .NET developer perspective of AWS, see the AWS Guide for .NET Developers.

using System;

using Amazon.Comprehend;

using Amazon.Comprehend.Model;

namespace Comprehend

Detecting Key Phrases

{

class Program {

static void Main(string[] args) {

String text = "It is raining today in Seattle";

AmazonComprehendClient comprehendClient = new AmazonComprehendClient(Amazon.RegionEndpoint.USWest2);

// Call DetectEntities API

Console.WriteLine("Calling DetectEntities\n");

DetectEntitiesRequest detectEntitiesRequest = new DetectEntitiesRequest() {

Text = text,

LanguageCode = "en"

};

DetectEntitiesResponse detectEntitiesResponse = comprehendClient.DetectEntities(detectEntitiesRequest);

foreach (Entity e in detectEntitiesResponse.Entities)

Console.WriteLine("Text: {0}, Type: {1}, Score: {2}, BeginOffset: {3}, EndOffset: {4}",

e.Text, e.Type, e.Score, e.BeginOffset, e.EndOffset);

Console.WriteLine("Done");

} } }

Detecting Key Phrases

To determine the key noun phrases used in text, use the Amazon Comprehend

DetectKeyPhrases (p. 319) operation. To detect the key noun phrases in up to 25 documents in a batch, use the BatchDetectKeyPhrases (p. 240) operation. For more information, see Using the Batch APIs (p. 60).

Topics

• Detecting Key Phrases Using the AWS Command Line Interface (p. 35)

• Detecting Key Phrases Using the AWS SDK for Java (p. 36)

• Detecting Key Phrases Using the AWS SDK for Python (Boto) (p. 36)

• Detecting Key Phrases Using the AWS SDK for .NET (p. 37)

Detecting Key Phrases Using the AWS Command Line Interface

The following example demonstrates using the DetectKeyPhrases operation with the AWS CLI. You must specify the language of the input text.

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

aws comprehend detect-key-phrases \ --region region \

--language-code "en" \

--text "It is raining today in Seattle."

Amazon Comprehend responds with the following:

{ "LanguageCode": "en",

Detecting Key Phrases

Detecting Key Phrases Using the AWS SDK for Java

The following example uses the DetectKeyPhrases operation with Java. You must specify the language of the input text.

// Create credentials using a provider chain. For more information, see

// https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html AWSCredentialsProvider awsCreds = DefaultAWSCredentialsProviderChain.getInstance();

AmazonComprehend comprehendClient =

System.out.println("Calling DetectKeyPhrases");

DetectKeyPhrasesRequest detectKeyPhrasesRequest = new DetectKeyPhrasesRequest().withText(text)

.withLanguageCode("en");

DetectKeyPhrasesResult detectKeyPhrasesResult = comprehendClient.detectKeyPhrases(detectKeyPhrasesRequest);

detectKeyPhrasesResult.getKeyPhrases().forEach(System.out::println);

System.out.println("End of DetectKeyPhrases\n");

} }

Detecting Key Phrases Using the AWS SDK for Python (Boto)

The following example uses the DetectKeyPhrases operation with Python. You must specify the language of the input text.

Detecting PII

import boto3 import json

comprehend = boto3.client(service_name='comprehend', region_name='region')

text = "It is raining today in Seattle"

print('Calling DetectKeyPhrases')

print(json.dumps(comprehend.detect_key_phrases(Text=text, LanguageCode='en'), sort_keys=True, indent=4))

print('End of DetectKeyPhrases\n')

Detecting Key Phrases Using the AWS SDK for .NET

The .NET example in this section uses the AWS SDK for .NET. You can use the AWS Toolkit for Visual Studio to develop AWS applications using .NET. It includes helpful templates and the AWS Explorer for deploying applications and managing services. For a .NET developer perspective of AWS, see the AWS Guide for .NET Developers.

using System;

using Amazon.Comprehend;

using Amazon.Comprehend.Model;

namespace Comprehend { class Program {

static void Main(string[] args) {

String text = "It is raining today in Seattle";

AmazonComprehendClient comprehendClient = new AmazonComprehendClient(Amazon.RegionEndpoint.USWest2);

// Call DetectKeyPhrases API

Console.WriteLine("Calling DetectKeyPhrases");

DetectKeyPhrasesRequest detectKeyPhrasesRequest = new DetectKeyPhrasesRequest() {

Text = text,

LanguageCode = "en"

};

DetectKeyPhrasesResponse detectKeyPhrasesResponse = comprehendClient.DetectKeyPhrases(detectKeyPhrasesRequest);

foreach (KeyPhrase kp in detectKeyPhrasesResponse.KeyPhrases)

Console.WriteLine("Text: {1}, Type: {1}, BeginOffset: {2}, EndOffset: {3}", kp.Text, kp.Text, kp.BeginOffset, kp.EndOffset);

Console.WriteLine("Done");

} } }

Detecting Personally Identifiable Information (PII)

To detect entities that contain personally identifiable information (PII) in a document, use the Amazon Comprehend DetectPiiEntities (p. 322) operation.

Detecting PII Using the AWS Command Line Interface

The following example uses the DetectPiiEntities operation with the AWS CLI.

Labeling Documents with PII

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

aws comprehend detect-pii-entities \

--text "Hello Paul Santos. The latest statement for your credit card \ account 1111-0000-1111-0000 was mailed to 123 Any Street, Seattle, WA \ 98109." \

--language-code en

Amazon Comprehend responds with the following:

{ "Entities": [ {

"Score": 0.9999669790267944, "Type": "NAME",

"BeginOffset": 6, "EndOffset": 18 },

{

"Score": 0.8905550241470337, "Type": "CREDIT_DEBIT_NUMBER", "BeginOffset": 69,

"EndOffset": 88 },

{

"Score": 0.9999889731407166, "Type": "ADDRESS",

"BeginOffset": 103, "EndOffset": 138 }

] }

Labeling Documents with Personally Identifiable Information (PII)

To label documents with personally identifiable information (PII), use the Amazon Comprehend ContainsPiiEntities (p. 252) operation.

Labeling Documents with PII Using the AWS Command Line Interface

The following example uses the ContainsPiiEntities operation with the AWS CLI.

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

aws comprehend contains-pii-entities \

--text "Hello Paul Santos. The latest statement for your credit card \ account 1111-0000-1111-0000 was mailed to 123 Any Street, Seattle, WA \ 98109." \

--language-code en

Amazon Comprehend responds with the following:

{ "Labels": [

Detecting Sentiment

{

"Name": "NAME",

"Score": 0.9149109721183777 },

{

"Name": "CREDIT_DEBIT_NUMBER", "Score": 0.8905550241470337 }

{

"Name": "ADDRESS",

"Score": 0.9951046109199524 }

] }

Detecting Sentiment

To determine the overall emotional tone of text, use the DetectSentiment (p. 324) operation. To detect the sentiment in up to 25 documents in a batch, use the BatchDetectSentiment (p. 243) operation. For more information, see Using the Batch APIs (p. 60).

Topics

• Detecting Sentiment Using the AWS Command Line Interface (p. 39)

• Detecting Sentiment Using the AWS SDK for Java (p. 39)

• Detecting Sentiment Using the AWS SDK for Python (Boto) (p. 40)

• Detecting Sentiment Using the AWS SDK for .NET (p. 40)

Detecting Sentiment Using the AWS Command Line Interface

The following example demonstrates using the DetectSentiment operation with the AWS CLI. This example specifies the language of the input text.

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

aws comprehend detect-sentiment \ --region region \

--language-code "en" \

--text "It is raining today in Seattle."

Amazon Comprehend responds with the following:

{ "SentimentScore": {

"Mixed": 0.014585512690246105, "Positive": 0.31592071056365967, "Neutral": 0.5985543131828308, "Negative": 0.07093945890665054 },

"Sentiment": "NEUTRAL", "LanguageCode": "en"

}

Detecting Sentiment Using the AWS SDK for Java

The following example Java program detects the sentiment of input text. You must specify the language of the input text.

Detecting Sentiment

// Create credentials using a provider chain. For more information, see

// https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html AWSCredentialsProvider awsCreds = DefaultAWSCredentialsProviderChain.getInstance();

AmazonComprehend comprehendClient =

System.out.println("Calling DetectSentiment");

DetectSentimentRequest detectSentimentRequest = new DetectSentimentRequest().withText(text)

.withLanguageCode("en");

DetectSentimentResult detectSentimentResult = comprehendClient.detectSentiment(detectSentimentRequest);

System.out.println(detectSentimentResult);

System.out.println("End of DetectSentiment\n");

System.out.println( "Done" );

} }

Detecting Sentiment Using the AWS SDK for Python (Boto)

The following Python program detects the sentiment of input text. You must specify the language of the input text.

import boto3 import json

comprehend = boto3.client(service_name='comprehend', region_name='region')

text = "It is raining today in Seattle"

print('Calling DetectSentiment')

print(json.dumps(comprehend.detect_sentiment(Text=text, LanguageCode='en'), sort_keys=True, indent=4))

print('End of DetectSentiment\n')

Detecting Sentiment Using the AWS SDK for .NET

The .NET example in this section uses the AWS SDK for .NET. You can use the AWS Toolkit for Visual Studio to develop AWS applications using .NET. It includes helpful templates and the AWS Explorer for deploying applications and managing services. For a .NET developer perspective of AWS, see the AWS Guide for .NET Developers.

Detecting Syntax

The .NET example in this section uses the AWS SDK for .NET.

using System;

using Amazon.Comprehend;

using Amazon.Comprehend.Model;

namespace Comprehend { class Program {

static void Main(string[] args) {

String text = "It is raining today in Seattle";

AmazonComprehendClient comprehendClient = new AmazonComprehendClient(Amazon.RegionEndpoint.USWest2);

// Call DetectKeyPhrases API

Console.WriteLine("Calling DetectSentiment");

DetectSentimentRequest detectSentimentRequest = new DetectSentimentRequest() {

Text = text,

LanguageCode = "en"

};

DetectSentimentResponse detectSentimentResponse = comprehendClient.DetectSentiment(detectSentimentRequest);

Console.WriteLine(detectSentimentResponse.Sentiment);

Console.WriteLine("Done");

} } }

Detecting Syntax

To parse text to extract the individual words and determine the parts of speech for each word, use the DetectSyntax (p. 327) operation. To parse the syntax of up to 25 documents in a batch, use the BatchDetectSyntax (p. 246) operation. For more information, see Using the Batch APIs (p. 60).

Topics

• Detecting Syntax Using the AWS Command Line Interface. (p. 41)

• Detecting Syntax Using the AWS SDK for Java (p. 43)

• Detecting Parts of Speech Using the AWS SDK for Python (Boto) (p. 43)

• Detecting Syntax Using the AWS SDK for .NET (p. 44)

Detecting Syntax Using the AWS Command Line Interface.

The following example demonstrates using the DetectSyntax operation with the AWS CLI. This example specifies the language of the input text.

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

aws comprehend detect-syntax \ --region region \

--language-code "en" \

--text "It is raining today in Seattle."

Detecting Syntax

Amazon Comprehend responds with the following:

{ "SyntaxTokens": [

Detecting Syntax

Detecting Syntax Using the AWS SDK for Java

The following Java program detects the syntax of the input text. You must specify the language of the input text.

public static void main( String[] args ) {

String text = "It is raining today in Seattle.";

String region = "region"

// Create credentials using a provider chain. For more information, see

// https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html AWSCredentialsProvider awsCreds = DefaultAWSCredentialsProviderChain.getInstance();

AmazonComprehend comprehendClient = AmazonComprehendClientBuilder.standard() .withCredentials(awsCreds)

.withRegion(region) .build();

// Call detectSyntax API

System.out.println("Calling DetectSyntax");

DetectSyntaxRequest detectSyntaxRequest = new DetectSyntaxRequest() .withText(text)

Detecting Parts of Speech Using the AWS SDK for Python (Boto)

The following Python program detects the parts of speech in the input text. You must specify the language of the input text.

import boto3

Using Custom Classification

import json

comprehend = boto3.client(service_name='comprehend', region_name='region') text = "It is raining today in Seattle"

print('Calling DetectSyntax')

print(json.dumps(comprehend.detect_syntax(Text=text, LanguageCode='en'), sort_keys=True, indent=4))

print('End of DetectSyntax\n')

Detecting Syntax Using the AWS SDK for .NET

The .NET example in this section uses the AWS SDK for .NET. You can use the AWS Toolkit for Visual Studio to develop AWS applications using .NET. It includes helpful templates and the AWS Explorer for deploying applications and managing services. For a .NET developer perspective of AWS, see the AWS Guide for .NET Developers.

using System;

using Amazon.Comprehend;

using Amazon.Comprehend.Model;

namespace Comprehend { class Program

{ static void Main(string[] args)

{ String text = "It is raining today in Seattle";

AmazonComprehendClient comprehendClient = new AmazonComprehendClient(Amazon.RegionEndpoint.region);

// Call DetectSyntax API

Console.WriteLine("Calling DetectSyntax\n");

DetectSyntaxRequest detectSyntaxRequest = new DetectSyntaxRequest() {

Text = text, LanguageCode = "en"

};

DetectSyntaxResponse detectSyntaxResponse = comprehendClient.DetectSyntax(detectSyntaxRequest);

foreach (SyntaxToken s in detectSyntaxResponse.SyntaxTokens)

Console.WriteLine("Text: {0}, PartOfSpeech: {1}, Score: {2}, BeginOffset: {3}, EndOffset: {4}",

e.Text, e.PartOfSpeech, e.Score, e.BeginOffset, e.EndOffset);

Console.WriteLine("Done");

} } }

Using Custom Classification

To create and train a custom classifier, use the Amazon Comprehend the section called

“CreateDocumentClassifier” (p. 254). To identify custom classifiers in a corpus of documents, use the the section called “StartDocumentClassificationJob” (p. 377) operation.

Topics

• Using Custom Classification With the AWS Command Line Interface (p. 45)

• Using Custom Classification Using the AWS SDK for Java (p. 46)

Using Custom Classification

• Using Custom Classification Using the AWS SDK for Python (Boto) (p. 47)

Using Custom Classification With the AWS Command Line Interface

The following examples demonstrates using the CreateDocumentClassifier operation,

StartDocumentClassificationJob operation, and other custom classifier APIs with the AWS CLI.

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 (^).

Create a custom classifier using the create-document-classifier operation.

aws comprehend create-document-classifier \ --region region \

--document-classifier-name testDelete \ --language-code en \

--input-data-config S3Uri=s3://S3Bucket/docclass/file name \

--data-access-role-arn arn:aws:iam::account number:role/testDeepInsightDataAccess

Get information on a custom classifier with the document classifier arn using the DescribeDocumentClassifier operation.

aws comprehend describe-document-classifier \ --region region \

--document-classifier-arn arn:aws:comprehend:region:account number:document-classifier/file name

Delete a custom classifier using the DeleteDocumentClassifier operation.

aws comprehend delete-document-classifier \ --region region \

--document-classifier-arn arn:aws:comprehend:region:account number:document-classifier/testDelete

List all custom classifiers in the account using the ListDocumentClassifiers operation.

aws comprehend list-document-classifiers --region region

Run a custom classification job using the StartDocumentClassificationJob operation.

aws comprehend start-document-classification-job \ --region region \

--document-classifier-arn arn:aws:comprehend:region:account number:document-classifier/testDelete \

--input-data-config S3Uri=s3://S3Bucket/docclass/file name,InputFormat=ONE_DOC_PER_LINE \

--output-data-config S3Uri=s3://S3Bucket/output \

--data-access-role-arn arn:aws:iam::account number:role/resource name

Get information on a custom classifier with the job id using the DescribeDocumentClassificationJob operation.

aws comprehend describe-document-classification-job \

Using Custom Classification

--region region \ --job-id job id

List all custom classification jobs in your account using the ListDocumentClassificationJobs operation.

aws comprehend list-document-classification-jobs --region region

Create an endpoint associated with a specific custom model using the CreateEndpoint operation.

aws comprehend create-endpoint \

--desired-inference-units number of inference units \ --endpoint-name endpoint name \

--model-arn arn:aws:comprehend:region:account-id:model/example \ --tags Key=My1stTag,Value=Value1

Run a custom classification request using an endpoint by using the ClassifyDocument operation.

aws comprehend classify-document \

--endpoint-arn arn:aws:comprehend:region:account-id:endpoint/endpoint name \ --text 'text.'

Update an endpoint using the UpdateEndpoint operation.

aws comprehend update-endpoint \

--desired-inference-units updated number of inference units \

--endpoint-arn arn:aws:comprehend:region:account-id:endpoint/endpoint name

Delete an endpoint using the DeleteEndpoint operation.

aws comprehend delete-endpoint \

--endpoint-arn arn:aws:comprehend:region:account-idendpoint/endpoint name

Get information on an endpoint with the endpoint Arn using the DescribeEndpoint operation.

aws comprehend describe-endpoint \

--endpoint-arn arn:aws:comprehend:region:account-id:endpoint/endpoint name

List all endpoints in your account using the ListEndpoints operation.

aws comprehend list-endpoint \ --filter status=Ready --max-results 50

Using Custom Classification Using the AWS SDK for Java

This example creates a custom classifier and trains it using Java

import com.amazonaws.services.comprehend.AmazonComprehend;

import com.amazonaws.services.comprehend.AmazonComprehendClientBuilder;

import com.amazonaws.services.comprehend.model.CreateDocumentClassifierRequest;

import com.amazonaws.services.comprehend.model.CreateDocumentClassifierResult;

Using Custom Classification public static void main(String[] args) { final AmazonComprehend comprehendClient = AmazonComprehendClientBuilder.standard()

.withRegion("us-west-2") .build();

final String dataAccessRoleArn = "arn:aws:iam::account number:role/resource name";

final CreateDocumentClassifierRequest createDocumentClassifierRequest = new CreateDocumentClassifierRequest()

.withDocumentClassifierName("SampleCodeClassifier") .withDataAccessRoleArn(dataAccessRoleArn)

.withLanguageCode(LanguageCode.En)

.withInputDataConfig(new DocumentClassifierInputDataConfig() .withS3Uri("s3://S3Bucket/docclass/file name"));

final CreateDocumentClassifierResult createDocumentClassifierResult =

comprehendClient.createDocumentClassifier(createDocumentClassifierRequest);

final String documentClassifierArn =

createDocumentClassifierResult.getDocumentClassifierArn();

System.out.println("Document Classifier ARN: " + documentClassifierArn);

final DescribeDocumentClassifierRequest describeDocumentClassifierRequest = new DescribeDocumentClassifierRequest()

.withDocumentClassifierArn(documentClassifierArn);

final DescribeDocumentClassifierResult describeDocumentClassifierResult = comprehendClient.describeDocumentClassifier(describeDocumentClassifierRequest);

System.out.println("DescribeDocumentClassifierResult: " + describeDocumentClassifierResult);

final ListDocumentClassifiersRequest listDocumentClassifiersRequest = new ListDocumentClassifiersRequest();

final ListDocumentClassifiersResult listDocumentClassifiersResult = comprehendClient

.listDocumentClassifiers(listDocumentClassifiersRequest);

System.out.println("ListDocumentClassifierResult: " + listDocumentClassifiersResult );

} }

Using Custom Classification Using the AWS SDK for Python (Boto)

This example creates a custom classifier and trains it using Python

import boto3

# Instantiate Boto3 SDK:

client = boto3.client('comprehend', region_name='region')

# Create a document classifier

Detecting Custom Entities

create_response = client.create_document_classifier(

InputDataConfig={

'S3Uri': 's3://S3Bucket/docclass/file name' },

DataAccessRoleArn='arn:aws:iam::account number:role/resource name', DocumentClassifierName='SampleCodeClassifier1',

LanguageCode='en'

)print("Create response: %s\n", create_response)

# Check the status of the classifier

# Check the status of the classifier

在文檔中 Amazon Comprehend (頁 38-75)

相關文件