使用 Amazon SDK for .NET 的 Amazon Translate 示例 - Amazon SDK for .NET
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Amazon SDK for .NET 的 Amazon Translate 示例

以下代码示例演示如何将 Amazon SDK for .NET 与 Amazon Translate 结合使用来执行操作和实现常见场景。

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景和跨服务示例的上下文查看操作。

场景是指显示如何通过在同一服务中调用多个函数来完成特定任务的代码示例。

每个示例都包含一个指向的链接 GitHub,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例演示如何描述 Amazon Translate 翻译任务。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

using System; using System.Threading.Tasks; using Amazon.Translate; using Amazon.Translate.Model; /// <summary> /// The following example shows how to retrieve the details of /// a text translation job using Amazon Translate. /// </summary> public class DescribeTextTranslation { public static async Task Main() { var client = new AmazonTranslateClient(); // The Job Id is generated when the text translation job is started // with a call to the StartTextTranslationJob method. var jobId = "1234567890abcdef01234567890abcde"; var request = new DescribeTextTranslationJobRequest { JobId = jobId, }; var jobProperties = await DescribeTranslationJobAsync(client, request); DisplayTranslationJobDetails(jobProperties); } /// <summary> /// Retrieve information about an Amazon Translate text translation job. /// </summary> /// <param name="client">The initialized Amazon Translate client object.</param> /// <param name="request">The DescribeTextTranslationJobRequest object.</param> /// <returns>The TextTranslationJobProperties object containing /// information about the text translation job..</returns> public static async Task<TextTranslationJobProperties> DescribeTranslationJobAsync( AmazonTranslateClient client, DescribeTextTranslationJobRequest request) { var response = await client.DescribeTextTranslationJobAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { return response.TextTranslationJobProperties; } else { return null; } } /// <summary> /// Displays the properties of the text translation job. /// </summary> /// <param name="jobProperties">The properties of the text translation /// job returned by the call to DescribeTextTranslationJobAsync.</param> public static void DisplayTranslationJobDetails(TextTranslationJobProperties jobProperties) { if (jobProperties is null) { Console.WriteLine("No text translation job properties found."); return; } // Display the details of the text translation job. Console.WriteLine($"{jobProperties.JobId}: {jobProperties.JobName}"); } }

以下代码示例演示如何列出 Amazon Translate 翻译任务。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.Translate; using Amazon.Translate.Model; /// <summary> /// List Amazon Translate translation jobs, along with details about each job. /// </summary> public class ListTranslationJobs { public static async Task Main() { var client = new AmazonTranslateClient(); var filter = new TextTranslationJobFilter { JobStatus = "COMPLETED", }; var request = new ListTextTranslationJobsRequest { MaxResults = 10, Filter = filter, }; await ListJobsAsync(client, request); } /// <summary> /// List Amazon Translate text translation jobs. /// </summary> /// <param name="client">The initialized Amazon Translate client object.</param> /// <param name="request">An Amazon Translate /// ListTextTranslationJobsRequest object detailing which text /// translation jobs are of interest.</param> public static async Task ListJobsAsync( AmazonTranslateClient client, ListTextTranslationJobsRequest request) { ListTextTranslationJobsResponse response; do { response = await client.ListTextTranslationJobsAsync(request); ShowTranslationJobDetails(response.TextTranslationJobPropertiesList); request.NextToken = response.NextToken; } while (response.NextToken is not null); } /// <summary> /// List existing translation job details. /// </summary> /// <param name="properties">A list of Amazon Translate text /// translation jobs.</param> public static void ShowTranslationJobDetails(List<TextTranslationJobProperties> properties) { properties.ForEach(prop => { Console.WriteLine($"{prop.JobId}: {prop.JobName}"); Console.WriteLine($"Status: {prop.JobStatus}"); Console.WriteLine($"Submitted time: {prop.SubmittedTime}"); }); } }

以下代码示例演示如何启动 Amazon Translate 翻译任务。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.Translate; using Amazon.Translate.Model; /// <summary> /// This example shows how to use Amazon Translate to process the files in /// an Amazon Simple Storage Service (Amazon S3) bucket. The translated results /// will also be stored in an Amazon S3 bucket. /// </summary> public class BatchTranslate { public static async Task Main() { var contentType = "text/plain"; // Set this variable to an S3 bucket location with a folder." // Input files must be in a folder and not at the bucket root." var s3InputUri = "s3://DOC-EXAMPLE-BUCKET1/FOLDER/"; var s3OutputUri = "s3://DOC-EXAMPLE-BUCKET2/"; // This role must have permissions to read the source bucket and to read and // write to the destination bucket where the translated text will be stored. var dataAccessRoleArn = "arn:aws:iam::0123456789ab:role/S3TranslateRole"; var client = new AmazonTranslateClient(); var inputConfig = new InputDataConfig { ContentType = contentType, S3Uri = s3InputUri, }; var outputConfig = new OutputDataConfig { S3Uri = s3OutputUri, }; var request = new StartTextTranslationJobRequest { JobName = "ExampleTranslationJob", DataAccessRoleArn = dataAccessRoleArn, InputDataConfig = inputConfig, OutputDataConfig = outputConfig, SourceLanguageCode = "en", TargetLanguageCodes = new List<string> { "fr" }, }; var response = await StartTextTranslationAsync(client, request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"{response.JobId}: {response.JobStatus}"); } } /// <summary> /// Start the Amazon Translate text translation job. /// </summary> /// <param name="client">The initialized AmazonTranslateClient object.</param> /// <param name="request">The request object that includes details such /// as source and destination bucket names and the IAM Role that will /// be used to access the buckets.</param> /// <returns>The StartTextTranslationResponse object that includes the /// details of the request response.</returns> public static async Task<StartTextTranslationJobResponse> StartTextTranslationAsync(AmazonTranslateClient client, StartTextTranslationJobRequest request) { var response = await client.StartTextTranslationJobAsync(request); return response; } }

以下代码示例演示如何停止 Amazon Translate 翻译任务。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

using System; using System.Threading.Tasks; using Amazon.Translate; using Amazon.Translate.Model; /// <summary> /// Shows how to stop a running Amazon Translation Service text translation /// job. /// </summary> public class StopTextTranslationJob { public static async Task Main() { var client = new AmazonTranslateClient(); var jobId = "1234567890abcdef01234567890abcde"; var request = new StopTextTranslationJobRequest { JobId = jobId, }; await StopTranslationJobAsync(client, request); } /// <summary> /// Sends a request to stop a text translation job. /// </summary> /// <param name="client">Initialized AmazonTrnslateClient object.</param> /// <param name="request">The request object to be passed to the /// StopTextJobAsync method.</param> public static async Task StopTranslationJobAsync( AmazonTranslateClient client, StopTextTranslationJobRequest request) { var response = await client.StopTextTranslationJobAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"{response.JobId} as status: {response.JobStatus}"); } } }

以下代码示例演示如何使用 Amazon Translate 翻译文本。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

using System; using System.IO; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Transfer; using Amazon.Translate; using Amazon.Translate.Model; /// <summary> /// Take text from a file stored a Amazon Simple Storage Service (Amazon S3) /// object and translate it using the Amazon Transfer Service. /// </summary> public class TranslateText { public static async Task Main() { // If the region you want to use is different from the region // defined for the default user, supply it as a parameter to the // Amazon Translate client object constructor. var client = new AmazonTranslateClient(); // Set the source language to "auto" to request Amazon Translate to // automatically detect te language of the source text. // You can get a list of the languages supposed by Amazon Translate // in the Amazon Translate Developer's Guide here: // https://docs.aws.amazon.com/translate/latest/dg/what-is.html string srcLang = "en"; // English. string destLang = "fr"; // French. // The Amazon Simple Storage Service (Amazon S3) bucket where the // source text file is stored. string srcBucket = "DOC-EXAMPLE-BUCKET"; string srcTextFile = "source.txt"; var srcText = await GetSourceTextAsync(srcBucket, srcTextFile); var destText = await TranslatingTextAsync(client, srcLang, destLang, srcText); ShowText(srcText, destText); } /// <summary> /// Use the Amazon S3 TransferUtility to retrieve the text to translate /// from an object in an S3 bucket. /// </summary> /// <param name="srcBucket">The name of the S3 bucket where the /// text is stored. /// </param> /// <param name="srcTextFile">The key of the S3 object that /// contains the text to translate.</param> /// <returns>A string representing the source text.</returns> public static async Task<string> GetSourceTextAsync(string srcBucket, string srcTextFile) { string srcText = string.Empty; var s3Client = new AmazonS3Client(); TransferUtility utility = new TransferUtility(s3Client); using var stream = await utility.OpenStreamAsync(srcBucket, srcTextFile); StreamReader file = new System.IO.StreamReader(stream); srcText = file.ReadToEnd(); return srcText; } /// <summary> /// Use the Amazon Translate Service to translate the document from the /// source language to the specified destination language. /// </summary> /// <param name="client">The Amazon Translate Service client used to /// perform the translation.</param> /// <param name="srcLang">The language of the source text.</param> /// <param name="destLang">The destination language for the translated /// text.</param> /// <param name="text">A string representing the text to ranslate.</param> /// <returns>The text that has been translated to the destination /// language.</returns> public static async Task<string> TranslatingTextAsync(AmazonTranslateClient client, string srcLang, string destLang, string text) { var request = new TranslateTextRequest { SourceLanguageCode = srcLang, TargetLanguageCode = destLang, Text = text, }; var response = await client.TranslateTextAsync(request); return response.TranslatedText; } /// <summary> /// Show the original text followed by the translated text. /// </summary> /// <param name="srcText">The original text to be translated.</param> /// <param name="destText">The translated text.</param> public static void ShowText(string srcText, string destText) { Console.WriteLine("Source text:"); Console.WriteLine(srcText); Console.WriteLine(); Console.WriteLine("Translated text:"); Console.WriteLine(destText); } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考TranslateText中的。