使用 API 进行实时分析 - Amazon Comprehend

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

使用 API 进行实时分析

以下示例演示如何使用 AWS CLI 和适用于 .NET、Java和 Python 的 AWS SDK 使用 Amazon Comprehend API 进行实时分析。通过示例了解 Amazon Comprehend 同步操作,并将其作为您自己的应用程序的构建块。

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

检测占主要语言

要确定文本中使用的主导语言,请使用DetectDominantLanguage操作。要批量检测多达 25 个文档中的主要语言,请使用该BatchDetectDominantLanguage操作。有关更多信息,请参阅 实时批处理 API

使用 AWS Command Line Interface

以下示例演示了如何在 AWS CLI 中使用 DetectDominantLanguage 操作。

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

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

Amazon Comprehend 的响应如下:

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

使用 AWS SDK for Java、适用于 Python 的 SDK 或 AWS SDK for .NET

有关如何确定主要语言的 SDK 示例,请参阅 与 AWS SDK或DetectDominantLanguage一起使用 CLI

检测命名实体

要确定文档中的命名实体,请使用DetectEntities操作。要批量检测最多 25 个文档中的实体,请使用BatchDetectEntities操作。有关更多信息,请参阅 实时批处理 API

使用 AWS Command Line Interface

以下示例演示了如何在 AWS CLI 中使用 DetectEntities 操作。您必须指定输入文本的语言。

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

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

Amazon 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、适用于 Python 的 SDK 或 AWS SDK for .NET

有关如何确定主要语言的 SDK 示例,请参阅 与 AWS SDK或DetectEntities一起使用 CLI

检测关键短语

要确定文本中使用的关键名词短语,请使用DetectKeyPhrases操作。要批量检测多达 25 个文档中的关键名词短语,请使用该BatchDetectKeyPhrases操作。有关更多信息,请参阅 实时批处理 API

使用 AWS Command Line Interface

以下示例演示了如何在 AWS CLI 中使用 DetectKeyPhrases 操作。您必须指定输入文本的语言。

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

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

Amazon 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、适用于 Python 的 SDK 或 AWS SDK for .NET

有关检测关键短语的 SDK 示例,请参阅 与 AWS SDK或DetectKeyPhrases一起使用 CLI

确定情绪

Amazon Comprehend 提供以下 API 操作,以便分析情绪:

使用AWS Command Line Interface

以下示例演示了如何在 AWS CLI 中使用 DetectSentiment 操作。此示例指定了输入文本的语言。

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

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

Amazon Comprehend 的响应如下:

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

使用 AWS SDK for Java、适用于 Python 的 SDK 或 AWS SDK for .NET

有关确定输入文本情绪的 SDK 示例,请参阅 与 AWS SDK或DetectSentiment一起使用 CLI

目标情绪的实时分析

Amazon Comprehend 提供以下 API 操作,用于目标情绪的实时分析:

如果您正在分析的文本不包含任何目标情绪 实体类型,则 API 将返回一个空的实体数组。

使用AWS Command Line Interface

以下示例演示了如何在 AWS CLI 中使用 DetectTargetedSentiment 操作。此示例指定了输入文本的语言。

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

aws comprehend detect-targeted-sentiment \ --region region \ --language-code "en" \ --text "The burger was cooked perfectly but it was cold. The service was OK."

Amazon Comprehend 的响应如下:

{ "Entities": [ { "DescriptiveMentionIndex": [ 0 ], "Mentions": [ { "BeginOffset": 4, "EndOffset": 10, "Score": 1, "GroupScore": 1, "Text": "burger", "Type": "OTHER", "MentionSentiment": { "Sentiment": "POSITIVE", "SentimentScore": { "Mixed": 0.001515, "Negative": 0.000822, "Neutral": 0.000243, "Positive": 0.99742 } } }, { "BeginOffset": 36, "EndOffset": 38, "Score": 0.999843, "GroupScore": 0.999661, "Text": "it", "Type": "OTHER", "MentionSentiment": { "Sentiment": "NEGATIVE", "SentimentScore": { "Mixed": 0, "Negative": 0.999996, "Neutral": 0.000004, "Positive": 0 } } } ] }, { "DescriptiveMentionIndex": [ 0 ], "Mentions": [ { "BeginOffset": 53, "EndOffset": 60, "Score": 1, "GroupScore": 1, "Text": "service", "Type": "ATTRIBUTE", "MentionSentiment": { "Sentiment": "NEUTRAL", "SentimentScore": { "Mixed": 0.000033, "Negative": 0.000089, "Neutral": 0.993325, "Positive": 0.006553 } } } ] } ] }

检测语法

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

使用 AWS Command Line Interface。

以下示例演示了如何在 AWS CLI 中使用 DetectSyntax 操作。此示例指定了输入文本的语言。

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

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

Amazon 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、适用于 Python 的 SDK 或 AWS SDK for .NET

有关检测输入文本语法的 SDK 示例,请参阅 与 AWS SDK或DetectSyntax一起使用 CLI

实时批处理 API

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

使用 AWS CLI 进行批处理

这些示例显示如何通过 AWS Command Line Interface 使用批处理 API 操作。除 BatchDetectDominantLanguage 之外的所有操作都使用以下名为 process.json 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 命令调用了 BatchDetectDominantLanguage 操作。

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

以下示例是 BatchDetectDominantLanguage 操作的响应:

{ "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 命令调用了 BatchDetectEntities 操作。

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

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

BatchDetectKeyPhrases操作返回一批文档中的关键名词短语。以下 AWS CLI 命令调用了 BatchDetectKeyNounPhrases 操作。

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

使用批处理 (AWS CLI) 检测情绪

使用该BatchDetectSentiment操作检测一批文档的整体情绪。以下 AWS CLI 命令调用了 BatchDetectSentiment 操作。

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

使用 AWS SDK for .NET 进行批处理

以下示例程序显示了如何将该BatchDetectEntities操作与配合使用AWS SDK for .NET。来自服务器的响应包含成功处理的每个文档的BatchDetectEntitiesItemResult对象。如果处理文档时出错,则响应中的错误列表中显示记录。该示例获取了每个有错误的文档,然后重新发送它们。

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

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