使用 API 的实时分析 - Amazon Comprehend

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

使用 API 的实时分析

以下示例演示如何通过 Amazon Comprehend API 使用亚马逊理解 API 进行实时分析AWS CLI、Java 和 Python。通过示例了解有关 Amazon Comprehend 同步操作的信息,并将其作为您自己的应用程序的构建块。

检测占主导地位的语言

要确定文本中使用的主要语言,请使用DetectDominantLanguageoperation. 要在批量中检测多达 25 个文档中的主导语言,请使用BatchDetectDominantLanguageoperation. 有关更多信息,请参阅 实时批处理 API

使用检测占主导地位的语言AWS Command Line Interface

以下示例演示如何使用DetectDominantLanguage使用operation.AWS CLI.

此示例的格式适用于 Unix、Linux 和 macOS。对于 Windows,请将每行末尾的反斜杠 (\) Unix 行继续符替换为脱字号 (^)。

aws comprehend detect-dominant-language \ --region region \ --text "It is raining today in Seattle."

亚马逊 Comprehend 如下:

{ "Languages": [ { "LanguageCode": "en", "Score": 0.9793661236763 } ] }

使用检测占主导地位的语言AWS SDK for Java

以下示例使用DetectDominantLanguage使用 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); detectDominantLanguageResult.getLanguages().forEach(System.out::println); System.out.println("Calling DetectDominantLanguage\n"); System.out.println("Done"); } }

使用检测占主导地位的语言AWS SDK for Python (Boto)

以下示例演示如何使用DetectDominantLanguage使用 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")

使用检测占主导地位的语言AWS SDK for .NET

本节中的 .NET 示例使用AWS SDK for .NET. 您可以使用AWS Toolkit for Visual Studio使用.NET 开发 AWS 应用程序。它包括有用模板和 AWS Explorer,用于部署应用程序和管理服务。有关 .NET 开发人员对于 AWS 的观点,请参阅适用AWS .NET 开发人员的 AW.

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 DetectDominantLanguage API 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, dl.Score); Console.WriteLine("Done"); } } }

检测命名实体

要确定文档中的命名实体,请使用DetectEntitiesoperation. 要在批量中检测多达 25 个文档中的实体,请使用BatchDetectEntitiesoperation. 有关更多信息,请参阅 实时批处理 API

使用检测命名实体AWS Command Line Interface

以下示例演示如何使用DetectEntities使用operation.AWS CLI. 您必须指定输入文本的语言。

此示例的格式适用于 Unix、Linux 和 macOS。对于 Windows,请将每行末尾的反斜杠 (\) Unix 行继续符替换为脱字号 (^)。

aws comprehend detect-entities \ --region region \ --language-code "en" \ --text "It is raining today in Seattle."

亚马逊 Comprehend 如下:

{ "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" }

使用检测命名实体AWS SDK for Java

以下示例使用DetectEntities使用 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.DetectEntitiesRequest; import com.amazonaws.services.comprehend.model.DetectEntitiesResult; 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 detectEntities API 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"); } }

使用检测命名实体AWS SDK for Python (Boto)

以下示例使用DetectEntities使用 Python。您必须指定输入文本的语言。

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')

使用检测实体AWS SDK for .NET

本节中的 .NET 示例使用AWS SDK for .NET. 您可以使用AWS Toolkit for Visual Studio使用.NET 开发 AWS 应用程序。它包括有用模板和 AWS Explorer,用于部署应用程序和管理服务。有关 .NET 开发人员对于 AWS 的观点,请参阅适用AWS .NET 开发人员的 AW.

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

检测关键短语

要确定文本中使用的关键名词短语,请使用DetectKeyPhrasesoperation. 要在一批中检测多达 25 个文档中的关键名词短语,请使用BatchDetectKeyPhrasesoperation. 有关更多信息,请参阅 实时批处理 API

使用检测关键短语AWS Command Line Interface

以下示例演示如何使用DetectKeyPhrases使用operation.AWS CLI. 您必须指定输入文本的语言。

此示例的格式适用于 Unix、Linux 和 macOS。对于 Windows,请将每行末尾的反斜杠 (\) Unix 行继续符替换为脱字号 (^)。

aws comprehend detect-key-phrases \ --region region \ --language-code "en" \ --text "It is raining today in Seattle."

亚马逊 Comprehend 如下:

{ "LanguageCode": "en", "KeyPhrases": [ { "Text": "today", "Score": 0.89, "BeginOffset": 14, "EndOffset": 19 }, { "Text": "Seattle", "Score": 0.91, "BeginOffset": 23, "EndOffset": 30 } ] }

使用检测关键短语AWS SDK for Java

以下示例使用DetectKeyPhrases使用 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.DetectKeyPhrasesRequest; import com.amazonaws.services.comprehend.model.DetectKeyPhrasesResult; 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 detectKeyPhrases API 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"); } }

使用检测关键短语AWS SDK for Python (Boto)

以下示例使用DetectKeyPhrases使用 Python。您必须指定输入文本的语言。

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')

使用检测关键短语AWS SDK for .NET

本节中的 .NET 示例使用AWS SDK for .NET. 您可以使用AWS Toolkit for Visual Studio使用.NET 开发 AWS 应用程序。它包括有用模板和 AWS Explorer,用于部署应用程序和管理服务。有关 .NET 开发人员对于 AWS 的观点,请参阅适用AWS .NET 开发人员的 AW.

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

确定情绪

Amazon Comprehend 提供了以下 API 操作以下 API 操作以下 API 操作以下 API 操作以下

使用确定情绪AWS Command Line Interface

以下示例演示如何使用DetectSentiment使用operation.AWS CLI. 此示例指定输入文本的语言。

此示例的格式适用于 Unix、Linux 和 macOS。对于 Windows,请将每行末尾的反斜杠 (\) Unix 行继续符替换为脱字号 (^)。

aws comprehend detect-sentiment \ --region region \ --language-code "en" \ --text "It is raining today in Seattle."

亚马逊 Comprehend 如下:

{ "SentimentScore": { "Mixed": 0.014585512690246105, "Positive": 0.31592071056365967, "Neutral": 0.5985543131828308, "Negative": 0.07093945890665054 }, "Sentiment": "NEUTRAL", "LanguageCode": "en" }

使用确定情绪AWS SDK for Java

以下示例 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.DetectSentimentRequest; import com.amazonaws.services.comprehend.model.DetectSentimentResult; 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 detectSentiment API 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" ); } }

使用确定情绪AWS SDK for Python (Boto)

以下 Python 程序检测输入文本的情绪。您必须指定输入文本的语言。

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')

使用确定情绪AWS SDK for .NET

本节中的 .NET 示例使用AWS SDK for .NET. 您可以使用AWS Toolkit for Visual Studio使用.NET 开发 AWS 应用程序。它包括有用模板和 AWS Explorer,用于部署应用程序和管理服务。有关 .NET 开发人员对于 AWS 的观点,请参阅适用AWS .NET 开发人员的 AW.

本节中的 .NET 示例使用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"); } } }

检测语法

要解析文本以提取单个单词并确定每个单词的语音部分,请使用DetectSyntaxoperation. 要批量解析最多 25 个文档的语法,请使用BatchDetectSyntaxoperation. 有关更多信息,请参阅 实时批处理 API

使用检测语法AWS Command Line Interface.

下面的示例演示如何通过DetectSyntax使用operation.AWS CLI. 此示例指定输入文本的语言。

此示例的格式适用于 Unix、Linux 和 macOS。对于 Windows,请将每行末尾的反斜杠 (\) Unix 行继续符替换为脱字号 (^)。

aws comprehend detect-syntax \ --region region \ --language-code "en" \ --text "It is raining today in Seattle."

亚马逊 Comprehend 如下:

{ "SyntaxTokens": [ { "Text": "It", "EndOffset": 2, "BeginOffset": 0, "PartOfSpeech": { "Tag": "PRON", "Score": 0.8389829397201538 }, "TokenId": 1 }, { "Text": "is", "EndOffset": 5, "BeginOffset": 3, "PartOfSpeech": { "Tag": "AUX", "Score": 0.9189288020133972 }, "TokenId": 2 }, { "Text": "raining", "EndOffset": 13, "BeginOffset": 6, "PartOfSpeech": { "Tag": "VERB", "Score": 0.9977611303329468 }, "TokenId": 3 }, { "Text": "today", "EndOffset": 19, "BeginOffset": 14, "PartOfSpeech": { "Tag": "NOUN", "Score": 0.9993606209754944 }, "TokenId": 4 }, { "Text": "in", "EndOffset": 22, "BeginOffset": 20, "PartOfSpeech": { "Tag": "ADP", "Score": 0.9999061822891235 }, "TokenId": 5 }, { "Text": "Seattle", "EndOffset": 30, "BeginOffset": 23, "PartOfSpeech": { "Tag": "PROPN", "Score": 0.9940338730812073 }, "TokenId": 6 }, { "Text": ".", "EndOffset": 31, "BeginOffset": 30, "PartOfSpeech": { "Tag": "PUNCT", "Score": 0.9999997615814209 }, "TokenId": 7 } ] }

使用检测语法AWS SDK for Java

以下 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.DetectSyntaxRequest; import com.amazonaws.services.comprehend.model.DetectSyntaxResult; public class App { 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) .withLanguageCode("en"); DetectSyntaxResult detectSyntaxResult = comprehendClient.detectSyntax(detectSyntaxRequest); detectSyntaxResult.getSyntaxTokens().forEach(System.out::println); System.out.println("End of DetectSyntax\n"); System.out.println( "Done" ); } }

使用检测语音部分AWS SDK for Python (Boto)

以下 Python 程序检测输入文本中的语音部分。您必须指定输入文本的语言。

import boto3 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')

使用检测语法AWS SDK for .NET

本节中的 .NET 示例使用AWS SDK for .NET. 您可以使用AWS Toolkit for Visual Studio使用.NET 开发 AWS 应用程序。它包括有用模板和 AWS Explorer,用于部署应用程序和管理服务。有关 .NET 开发人员对于 AWS 的观点,请参阅适用AWS .NET 开发人员的 AW.

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

实时批处理 API

要批量发送最多 25 份文档,您可以使用 Amazon Comprehend 实时批量操作。调用批处理操作与为请求中的每个文档调用单个文档 API 相同。使用批处理 API 可以提高应用程序的性能。有关更多信息,请参阅 多文档同步处理

使用适用于 Java 的开发工具包的Batch 处理

下面的示例程序演示如何使用BatchDetectEntities使用适用于 SDK for Java 操作。服务器的响应包含BatchDetectEntitiesItemResult每个成功处理的文档的对象。如果处理文档时出错,则响应中的错误列表中会有一条记录。该示例获取每个有错误的文档,然后重新发送它们。

import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.DefaultAWSCredentialsProviderChain; import com.amazonaws.services.comprehend.AmazonComprehend; import com.amazonaws.services.comprehend.AmazonComprehendClientBuilder; import com.amazonaws.services.comprehend.model.BatchDetectEntitiesItemResult; import com.amazonaws.services.comprehend.model.BatchDetectEntitiesRequest; import com.amazonaws.services.comprehend.model.BatchDetectEntitiesResult; import com.amazonaws.services.comprehend.model.BatchItemError; public class App { public static void main( String[] args ) { // 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(); String[] textList = {"I love Seattle", "Today is Sunday", "Tomorrow is Monday", "I love Seattle"}; // Call detectEntities API System.out.println("Calling BatchDetectEntities"); BatchDetectEntitiesRequest batchDetectEntitiesRequest = new BatchDetectEntitiesRequest().withTextList(textList) .withLanguageCode("en"); BatchDetectEntitiesResult batchDetectEntitiesResult = client.batchDetectEntities(batchDetectEntitiesRequest); for(BatchDetectEntitiesItemResult item : batchDetectEntitiesResult.getResultList()) { System.out.println(item); } // check if we need to retry failed requests if (batchDetectEntitiesResult.getErrorList().size() != 0) { System.out.println("Retrying Failed Requests"); ArrayList<String> textToRetry = new ArrayList<String>(); for(BatchItemError errorItem : batchDetectEntitiesResult.getErrorList()) { textToRetry.add(textList[errorItem.getIndex()]); } batchDetectEntitiesRequest = new BatchDetectEntitiesRequest().withTextList(textToRetry).withLanguageCode("en"); batchDetectEntitiesResult = client.batchDetectEntities(batchDetectEntitiesRequest); for(BatchDetectEntitiesItemResult item : batchDetectEntitiesResult.getResultList()) { System.out.println(item); } } System.out.println("End of DetectEntities"); } }

使用Batch 处理AWS SDK for .NET

下面的示例程序演示如何使用BatchDetectEntities使用operation.AWS SDK for .NET. 服务器的响应包含BatchDetectEntitiesItemResult每个成功处理的文档的对象。如果处理文档时出错,则响应中的错误列表中会有一条记录。该示例获取每个有错误的文档,然后重新发送它们。

本节中的 .NET 示例使用AWS SDK for .NET. 您可以使用AWS Toolkit for Visual Studio使用.NET 开发 AWS 应用程序。它包括有用模板和 AWS Explorer,用于部署应用程序和管理服务。有关 .NET 开发人员对于 AWS 的观点,请参阅适用AWS .NET 开发人员的 AW.

using System; using System.Collections.Generic; using Amazon.Comprehend; using Amazon.Comprehend.Model; namespace Comprehend { class Program { // Helper method for printing properties static private void PrintEntity(Entity entity) { Console.WriteLine(" Text: {0}, Type: {1}, Score: {2}, BeginOffset: {3} EndOffset: {4}", entity.Text, entity.Type, entity.Score, entity.BeginOffset, entity.EndOffset); } static void Main(string[] args) { AmazonComprehendClient comprehendClient = new AmazonComprehendClient(Amazon.RegionEndpoint.USWest2); List<String> textList = new List<String>() { { "I love Seattle" }, { "Today is Sunday" }, { "Tomorrow is Monday" }, { "I love Seattle" } }; // Call detectEntities API Console.WriteLine("Calling BatchDetectEntities"); BatchDetectEntitiesRequest batchDetectEntitiesRequest = new BatchDetectEntitiesRequest() { TextList = textList, LanguageCode = "en" }; BatchDetectEntitiesResponse batchDetectEntitiesResponse = comprehendClient.BatchDetectEntities(batchDetectEntitiesRequest); foreach (BatchDetectEntitiesItemResult item in batchDetectEntitiesResponse.ResultList) { Console.WriteLine("Entities in {0}:", textList[item.Index]); foreach (Entity entity in item.Entities) PrintEntity(entity); } // check if we need to retry failed requests if (batchDetectEntitiesResponse.ErrorList.Count != 0) { Console.WriteLine("Retrying Failed Requests"); List<String> textToRetry = new List<String>(); foreach(BatchItemError errorItem in batchDetectEntitiesResponse.ErrorList) textToRetry.Add(textList[errorItem.Index]); batchDetectEntitiesRequest = new BatchDetectEntitiesRequest() { TextList = textToRetry, LanguageCode = "en" }; batchDetectEntitiesResponse = comprehendClient.BatchDetectEntities(batchDetectEntitiesRequest); foreach(BatchDetectEntitiesItemResult item in batchDetectEntitiesResponse.ResultList) { Console.WriteLine("Entities in {0}:", textList[item.Index]); foreach (Entity entity in item.Entities) PrintEntity(entity); } } Console.WriteLine("End of DetectEntities"); } } }

使用Batch 处理AWS CLI

这些示例演示如何通过通过AWS Command Line Interface. 所有操作除外BatchDetectDominantLanguage使用以下名为process.json作为输入。对于那个手术LanguageCode实体不包括在内。

JSON 文件中的第三个文档 ("$$$$$$$$") 会在批处理过程中引发错误。它包含在内,因此操作将包含BatchItemError在响应中。

{ "LanguageCode": "en", "TextList": [ "I have been living in Seattle for almost 4 years", "It is raining today in Seattle", "$$$$$$$$" ] }

这些示例的格式适用于 Unix、Linux 和 macOS。对于 Windows,请将每行末尾的反斜杠 (\) Unix 行继续符替换为脱字号 (^)。

使用批量检测占主导地位的语言 (AWS CLI)

这些区域有:BatchDetectDominantLanguage操作决定了批处理中每个文档的主要语言。有关 Amazon Comprehend 可以检测到的语言的列表,请参阅占优势的语言. 以下AWS CLI命令调用BatchDetectDominantLanguageoperation.

aws comprehend batch-detect-dominant-language \ --endpoint endpoint \ --region region \ --cli-input-json file://path to input file/process.json

以下是来自以下的响应BatchDetectDominantLanguageoperation.

{ "ResultList": [ { "Index": 0, "Languages":[ { "LanguageCode":"en", "Score": 0.99 } ] }, { "Index": 1 "Languages":[ { "LanguageCode":"en", "Score": 0.82 } ] } ], "ErrorList": [ { "Index": 2, "ErrorCode": "InternalServerException", "ErrorMessage": "Unexpected Server Error. Please try again." } ] }

使用批量检测实体 (AWS CLI)

使用BatchDetectEntities操作来查找一批文档中存在的实体。有关实体的更多信息,请参阅实体。以下AWS CLI命令调用BatchDetectEntitiesoperation.

aws comprehend batch-detect-entities \ --endpoint endpoint \ --region region \ --cli-input-json file://path to input file/process.json

使用批量检测关键短语 (AWS CLI)

这些区域有:BatchDetectKeyPhrases操作返回一批文档中的关键名词短语。以下AWS CLI命令调用BatchDetectKeyNounPhrasesoperation.

aws comprehend batch-detect-key-phrases --endpoint endpoint --region region --cli-input-json file://path to input file/process.json

使用批量检测情绪 (AWS CLI)

使用检测一批文档的整体情绪BatchDetectSentimentoperation. 以下AWS CLI命令调用BatchDetectSentimentoperation.

aws comprehend batch-detect-sentiment \ --endpoint endpoint \ --region region \ --cli-input-json file://path to input file/process.json