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

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

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

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

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

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

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

主题

操作

以下代码示例演示如何通过 Amazon Comprehend 检测文档中的实体。

Amazon SDK for .NET
注意

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

using System; using System.Threading.Tasks; using Amazon.Comprehend; using Amazon.Comprehend.Model; /// <summary> /// This example shows how to use the AmazonComprehend service detect any /// entities in submitted text. /// </summary> public static class DetectEntities { /// <summary> /// The main method calls the DetectEntitiesAsync method to find any /// entities in the sample code. /// </summary> public static async Task Main() { string text = "It is raining today in Seattle"; var comprehendClient = new AmazonComprehendClient(); Console.WriteLine("Calling DetectEntities\n"); var detectEntitiesRequest = new DetectEntitiesRequest() { Text = text, LanguageCode = "en", }; var detectEntitiesResponse = await comprehendClient.DetectEntitiesAsync(detectEntitiesRequest); foreach (var e in detectEntitiesResponse.Entities) { Console.WriteLine($"Text: {e.Text}, Type: {e.Type}, Score: {e.Score}, BeginOffset: {e.BeginOffset}, EndOffset: {e.EndOffset}"); } Console.WriteLine("Done"); } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考DetectEntities中的。

以下代码示例演示如何通过 Amazon Comprehend 检测文档中的关键短语。

Amazon SDK for .NET
注意

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

using System; using System.Threading.Tasks; using Amazon.Comprehend; using Amazon.Comprehend.Model; /// <summary> /// This example shows how to use the Amazon Comprehend service to /// search text for key phrases. /// </summary> public static class DetectKeyPhrase { /// <summary> /// This method calls the Amazon Comprehend method DetectKeyPhrasesAsync /// to detect any key phrases in the sample text. /// </summary> public static async Task Main() { string text = "It is raining today in Seattle"; var comprehendClient = new AmazonComprehendClient(Amazon.RegionEndpoint.USWest2); // Call DetectKeyPhrases API Console.WriteLine("Calling DetectKeyPhrases"); var detectKeyPhrasesRequest = new DetectKeyPhrasesRequest() { Text = text, LanguageCode = "en", }; var detectKeyPhrasesResponse = await comprehendClient.DetectKeyPhrasesAsync(detectKeyPhrasesRequest); foreach (var kp in detectKeyPhrasesResponse.KeyPhrases) { Console.WriteLine($"Text: {kp.Text}, Score: {kp.Score}, BeginOffset: {kp.BeginOffset}, EndOffset: {kp.EndOffset}"); } Console.WriteLine("Done"); } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考DetectKeyPhrases中的。

以下代码示例演示如何通过 Amazon Comprehend 检测文档中的个人身份信息 (PII)。

Amazon SDK for .NET
注意

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

using System; using System.Threading.Tasks; using Amazon.Comprehend; using Amazon.Comprehend.Model; /// <summary> /// This example shows how to use the Amazon Comprehend service to find /// personally identifiable information (PII) within text submitted to the /// DetectPiiEntitiesAsync method. /// </summary> public class DetectingPII { /// <summary> /// This method calls the DetectPiiEntitiesAsync method to locate any /// personally dientifiable information within the supplied text. /// </summary> public static async Task Main() { var comprehendClient = new AmazonComprehendClient(); var 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."; var request = new DetectPiiEntitiesRequest { Text = text, LanguageCode = "EN", }; var response = await comprehendClient.DetectPiiEntitiesAsync(request); if (response.Entities.Count > 0) { foreach (var entity in response.Entities) { var entityValue = text.Substring(entity.BeginOffset, entity.EndOffset - entity.BeginOffset); Console.WriteLine($"{entity.Type}: {entityValue}"); } } } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考DetectPiiEntities中的。

以下代码示例演示如何通过 Amazon Comprehend 检测文档的语法元素。

Amazon SDK for .NET
注意

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

using System; using System.Threading.Tasks; using Amazon.Comprehend; using Amazon.Comprehend.Model; /// <summary> /// This example shows how to use Amazon Comprehend to detect syntax /// elements by calling the DetectSyntaxAsync method. /// </summary> public class DetectingSyntax { /// <summary> /// This method calls DetectSynaxAsync to identify the syntax elements /// in the sample text. /// </summary> public static async Task Main() { string text = "It is raining today in Seattle"; var comprehendClient = new AmazonComprehendClient(); // Call DetectSyntax API Console.WriteLine("Calling DetectSyntaxAsync\n"); var detectSyntaxRequest = new DetectSyntaxRequest() { Text = text, LanguageCode = "en", }; DetectSyntaxResponse detectSyntaxResponse = await comprehendClient.DetectSyntaxAsync(detectSyntaxRequest); foreach (SyntaxToken s in detectSyntaxResponse.SyntaxTokens) { Console.WriteLine($"Text: {s.Text}, PartOfSpeech: {s.PartOfSpeech.Tag}, BeginOffset: {s.BeginOffset}, EndOffset: {s.EndOffset}"); } Console.WriteLine("Done"); } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考DetectSyntax中的。

以下代码示例演示如何通过 Amazon Comprehend 检测文档中的主导语言。

Amazon SDK for .NET
注意

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

using System; using System.Threading.Tasks; using Amazon.Comprehend; using Amazon.Comprehend.Model; /// <summary> /// This example calls the Amazon Comprehend service to determine the /// dominant language. /// </summary> public static class DetectDominantLanguage { /// <summary> /// Calls Amazon Comprehend to determine the dominant language used in /// the sample text. /// </summary> public static async Task Main() { string text = "It is raining today in Seattle."; var comprehendClient = new AmazonComprehendClient(Amazon.RegionEndpoint.USWest2); Console.WriteLine("Calling DetectDominantLanguage\n"); var detectDominantLanguageRequest = new DetectDominantLanguageRequest() { Text = text, }; var detectDominantLanguageResponse = await comprehendClient.DetectDominantLanguageAsync(detectDominantLanguageRequest); foreach (var dl in detectDominantLanguageResponse.Languages) { Console.WriteLine($"Language Code: {dl.LanguageCode}, Score: {dl.Score}"); } Console.WriteLine("Done"); } }

以下代码示例演示如何通过 Amazon Comprehend 检测文档的情绪。

Amazon SDK for .NET
注意

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

using System; using System.Threading.Tasks; using Amazon.Comprehend; using Amazon.Comprehend.Model; /// <summary> /// This example shows how to detect the overall sentiment of the supplied /// text using the Amazon Comprehend service. /// </summary> public static class DetectSentiment { /// <summary> /// This method calls the DetetectSentimentAsync method to analyze the /// supplied text and determine the overal sentiment. /// </summary> public static async Task Main() { string text = "It is raining today in Seattle"; var comprehendClient = new AmazonComprehendClient(Amazon.RegionEndpoint.USWest2); // Call DetectKeyPhrases API Console.WriteLine("Calling DetectSentiment"); var detectSentimentRequest = new DetectSentimentRequest() { Text = text, LanguageCode = "en", }; var detectSentimentResponse = await comprehendClient.DetectSentimentAsync(detectSentimentRequest); Console.WriteLine($"Sentiment: {detectSentimentResponse.Sentiment}"); Console.WriteLine("Done"); } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考DetectSentiment中的。

以下代码示例演示如何启动 Amazon Comprehend 主题建模任务。

Amazon SDK for .NET
注意

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

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}"); } }