Gunakan StartTopicsDetectionJob dengan AWS SDKatau CLI - Amazon Comprehend

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Gunakan StartTopicsDetectionJob dengan AWS SDKatau CLI

Contoh kode berikut menunjukkan cara menggunakanStartTopicsDetectionJob.

Contoh tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Anda dapat melihat tindakan ini dalam konteks dalam contoh kode berikut:

.NET
AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankan di AWS Repositori Contoh Kode.

using System; using System.Threading.Tasks; using Amazon.Comprehend; using Amazon.Comprehend.Model; /// <summary> /// This example scans the documents in an Amazon Simple Storage Service /// (Amazon S3) bucket and analyzes it for topics. The results are stored /// in another bucket and then the resulting job properties are displayed /// on the screen. This example was created using the AWS SDK for .NEt /// version 3.7 and .NET Core version 5.0. /// </summary> public static class TopicModeling { /// <summary> /// This methos calls a topic detection job by calling the Amazon /// Comprehend StartTopicsDetectionJobRequest. /// </summary> public static async Task Main() { var comprehendClient = new AmazonComprehendClient(); string inputS3Uri = "s3://input bucket/input path"; InputFormat inputDocFormat = InputFormat.ONE_DOC_PER_FILE; string outputS3Uri = "s3://output bucket/output path"; string dataAccessRoleArn = "arn:aws:iam::account ID:role/data access role"; int numberOfTopics = 10; var startTopicsDetectionJobRequest = new StartTopicsDetectionJobRequest() { InputDataConfig = new InputDataConfig() { S3Uri = inputS3Uri, InputFormat = inputDocFormat, }, OutputDataConfig = new OutputDataConfig() { S3Uri = outputS3Uri, }, DataAccessRoleArn = dataAccessRoleArn, NumberOfTopics = numberOfTopics, }; var startTopicsDetectionJobResponse = await comprehendClient.StartTopicsDetectionJobAsync(startTopicsDetectionJobRequest); var jobId = startTopicsDetectionJobResponse.JobId; Console.WriteLine("JobId: " + jobId); var describeTopicsDetectionJobRequest = new DescribeTopicsDetectionJobRequest() { JobId = jobId, }; var describeTopicsDetectionJobResponse = await comprehendClient.DescribeTopicsDetectionJobAsync(describeTopicsDetectionJobRequest); PrintJobProperties(describeTopicsDetectionJobResponse.TopicsDetectionJobProperties); var listTopicsDetectionJobsResponse = await comprehendClient.ListTopicsDetectionJobsAsync(new ListTopicsDetectionJobsRequest()); foreach (var props in listTopicsDetectionJobsResponse.TopicsDetectionJobPropertiesList) { PrintJobProperties(props); } } /// <summary> /// This method is a helper method that displays the job properties /// from the call to StartTopicsDetectionJobRequest. /// </summary> /// <param name="props">A list of properties from the call to /// StartTopicsDetectionJobRequest.</param> private static void PrintJobProperties(TopicsDetectionJobProperties props) { Console.WriteLine($"JobId: {props.JobId}, JobName: {props.JobName}, JobStatus: {props.JobStatus}"); Console.WriteLine($"NumberOfTopics: {props.NumberOfTopics}\nInputS3Uri: {props.InputDataConfig.S3Uri}"); Console.WriteLine($"InputFormat: {props.InputDataConfig.InputFormat}, OutputS3Uri: {props.OutputDataConfig.S3Uri}"); } }
CLI
AWS CLI

Untuk memulai pekerjaan analisis deteksi topik

start-topics-detection-jobContoh berikut memulai pekerjaan deteksi topik asinkron untuk semua file yang terletak di alamat yang ditentukan oleh tag. --input-data-config Ketika pekerjaan selesai, folder,output, ditempatkan di lokasi yang ditentukan oleh --ouput-data-config tag. outputberisi topic-terms.csv dan doc-topics.csv. File keluaran pertama, topic-terms.csv, adalah daftar topik dalam koleksi. Untuk setiap topik, daftar tersebut mencakup, secara default, istilah teratas berdasarkan topik sesuai dengan bobotnya. File kedua,doc-topics.csv, mencantumkan dokumen yang terkait dengan topik dan proporsi dokumen yang berkaitan dengan topik tersebut.

aws comprehend start-topics-detection-job \ --job-name example_topics_detection_job \ --language-code en \ --input-data-config "S3Uri=s3://DOC-EXAMPLE-BUCKET/" \ --output-data-config "S3Uri=s3://DOC-EXAMPLE-DESTINATION-BUCKET/testfolder/" \ --data-access-role-arn arn:aws:iam::111122223333:role/service-role/AmazonComprehendServiceRole-example-role \ --language-code en

Output:

{ "JobId": "123456abcdeb0e11022f22a11EXAMPLE", "JobArn": "arn:aws:comprehend:us-west-2:111122223333:key-phrases-detection-job/123456abcdeb0e11022f22a11EXAMPLE", "JobStatus": "SUBMITTED" }

Untuk informasi selengkapnya, lihat Pemodelan Topik di Panduan Pengembang Amazon Comprehend.

Python
SDKuntuk Python (Boto3)
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankan di AWS Repositori Contoh Kode.

class ComprehendTopicModeler: """Encapsulates a Comprehend topic modeler.""" def __init__(self, comprehend_client): """ :param comprehend_client: A Boto3 Comprehend client. """ self.comprehend_client = comprehend_client def start_job( self, job_name, input_bucket, input_key, input_format, output_bucket, output_key, data_access_role_arn, ): """ Starts a topic modeling job. Input is read from the specified Amazon S3 input bucket and written to the specified output bucket. Output data is stored in a tar archive compressed in gzip format. The job runs asynchronously, so you can call `describe_topics_detection_job` to get job status until it returns a status of SUCCEEDED. :param job_name: The name of the job. :param input_bucket: An Amazon S3 bucket that contains job input. :param input_key: The prefix used to find input data in the input bucket. If multiple objects have the same prefix, all of them are used. :param input_format: The format of the input data, either one document per file or one document per line. :param output_bucket: The Amazon S3 bucket where output data is written. :param output_key: The prefix prepended to the output data. :param data_access_role_arn: The Amazon Resource Name (ARN) of a role that grants Comprehend permission to read from the input bucket and write to the output bucket. :return: Information about the job, including the job ID. """ try: response = self.comprehend_client.start_topics_detection_job( JobName=job_name, DataAccessRoleArn=data_access_role_arn, InputDataConfig={ "S3Uri": f"s3://{input_bucket}/{input_key}", "InputFormat": input_format.value, }, OutputDataConfig={"S3Uri": f"s3://{output_bucket}/{output_key}"}, ) logger.info("Started topic modeling job %s.", response["JobId"]) except ClientError: logger.exception("Couldn't start topic modeling job.") raise else: return response

Untuk daftar lengkap AWS SDKpanduan pengembang dan contoh kode, lihatMenggunakan Amazon Comprehend dengan SDK AWS. Topik ini juga mencakup informasi tentang memulai dan detail tentang SDK versi sebelumnya.