AWS SDK 또는 CreateDocumentClassifier CLI와 함께 사용 - Amazon Comprehend

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

AWS SDK 또는 CreateDocumentClassifier CLI와 함께 사용

다음 코드 예제는 CreateDocumentClassifier의 사용 방법을 보여줍니다.

작업 예제는 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 다음 코드 예제에서는 컨텍스트 내에서 이 작업을 확인할 수 있습니다.

CLI
AWS CLI

문서 분류자를 만들어 문서 분류

다음 create-document-classifier 예제에서는 문서 분류자 모델의 학습 프로세스를 시작합니다. 교육 데이터 파일 training.csv--input-data-config 태그에 있습니다. training.csv는 첫 번째 열에 레이블 또는 분류가 제공되고 두 번째 열에 문서가 제공되는 2열 문서입니다.

aws comprehend create-document-classifier \ --document-classifier-name example-classifier \ --data-access-arn arn:aws:comprehend:us-west-2:111122223333:pii-entities-detection-job/123456abcdeb0e11022f22a11EXAMPLE \ --input-data-config "S3Uri=s3://DOC-EXAMPLE-BUCKET/" \ --language-code en

출력:

{ "DocumentClassifierArn": "arn:aws:comprehend:us-west-2:111122223333:document-classifier/example-classifier" }

자세한 내용은 Amazon Comprehend 개발자 가이드사용자 지정 분류를 참조하세요.

Java
SDK for Java 2.x
참고

자세한 내용은 에서 확인할 수 GitHub 있습니다. AWS 코드 예제 리포지토리에서 전체 예제를 찾고 설정 및 실행하는 방법을 배워보세요.

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); } } }
Python
SDK for Python(Boto3)
참고

자세한 내용은 다음과 같습니다 GitHub. AWS 코드 예제 리포지토리에서 전체 예제를 찾고 설정 및 실행하는 방법을 배워보세요.

class ComprehendClassifier: """Encapsulates an Amazon Comprehend custom classifier.""" def __init__(self, comprehend_client): """ :param comprehend_client: A Boto3 Comprehend client. """ self.comprehend_client = comprehend_client self.classifier_arn = None def create( self, name, language_code, training_bucket, training_key, data_access_role_arn, mode, ): """ Creates a custom classifier. After the classifier is created, it immediately starts training on the data found in the specified Amazon S3 bucket. Training can take 30 minutes or longer. The `describe_document_classifier` function can be used to get training status and returns a status of TRAINED when the classifier is ready to use. :param name: The name of the classifier. :param language_code: The language the classifier can operate on. :param training_bucket: The Amazon S3 bucket that contains the training data. :param training_key: The prefix used to find training data in the training bucket. If multiple objects have the same prefix, all of them are used. :param data_access_role_arn: The Amazon Resource Name (ARN) of a role that grants Comprehend permission to read from the training bucket. :return: The ARN of the newly created classifier. """ try: response = self.comprehend_client.create_document_classifier( DocumentClassifierName=name, LanguageCode=language_code, InputDataConfig={"S3Uri": f"s3://{training_bucket}/{training_key}"}, DataAccessRoleArn=data_access_role_arn, Mode=mode.value, ) self.classifier_arn = response["DocumentClassifierArn"] logger.info("Started classifier creation. Arn is: %s.", self.classifier_arn) except ClientError: logger.exception("Couldn't create classifier %s.", name) raise else: return self.classifier_arn
  • API에 대한 자세한 내용은 파이썬용AWS SDK (Boto3) API 레퍼런스를 참조하십시오 CreateDocumentClassifier.

AWS SDK 개발자 가이드 및 코드 예제의 전체 목록은 을 참조하십시오. 아마존 Comprehend를 SDK와 함께 사용하기 AWS 이 주제에는 시작하기에 대한 정보와 이전 SDK 버전에 대한 세부 정보도 포함되어 있습니다.