本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
Real-time 使用 API 进行分析
以下示例演示如何使用、、和,使用亚马逊 Comprehend API 进行实时分析。 AWS CLI 适用于 .NET 的 AWS SDK 适用于 Java 的 AWS SDK 适用于 Python (Boto3) 的 AWS SDK通过示例了解 Amazon Comprehend 同步操作,并将其作为您自己的应用程序的构建块。
本节中的 .NET 示例使用 适用于 .NET 的 AWS SDK。您可以使用使用 .NET 开发 AWS 应用程序。AWS Toolkit for Visual Studio它包括有用的模板和 AWS Explorer,用于部署应用程序和管理服务。从 .NET 开发者的角度来看 AWS,请参阅 .NET 开发人员AWS 指南。
检测占主要语言
要确定文本中使用的主要语言,请使用DetectDominantLanguage操作。要批量检测多达 25 个文档中的主要语言,请使用该BatchDetectDominantLanguage操作。有关更多信息,请参阅 Real-time 批处理 API。
使用 AWS Command Line Interface
以下示例演示了如何在 AWS CLI中使用 DetectDominantLanguage 操作。
此示例的格式适用于 Unix、Linux 和 macOS。对于 Windows,请将每行末尾的反斜杠 (\) Unix 行继续符替换为脱字号 (^)。
aws comprehend detect-dominant-language \ --regionregion\ --text "It is raining today in Seattle."
Amazon Comprehend 的响应如下:
{ "Languages": [ { "LanguageCode": "en", "Score": 0.9793661236763 } ] }
使用 适用于 Java 的 AWS SDK、适用于 Python 的 SDK,或 适用于 .NET 的 SDK
有关如何确定主要语言的 SDK 示例,请参阅 DetectDominantLanguage搭配使用 AWS SDK 或 CLI。
检测命名实体
要确定文档中的命名实体,请使用DetectEntities操作。要批量检测最多 25 个文档中的实体,请使用该BatchDetectEntities操作。有关更多信息,请参阅 Real-time 批处理 API。
使用 AWS Command Line Interface
以下示例演示了如何在 AWS CLI中使用 DetectEntities 操作。您必须指定输入文本的语言。
此示例的格式适用于 Unix、Linux 和 macOS。对于 Windows,请将每行末尾的反斜杠 (\) Unix 行继续符替换为脱字号 (^)。
aws comprehend detect-entities \ --regionregion\ --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" }
使用 适用于 Java 的 AWS SDK、适用于 Python 的 SDK,或 适用于 .NET 的 SDK
有关如何确定主要语言的 SDK 示例,请参阅 DetectEntities搭配使用 AWS SDK 或 CLI。
检测关键短语
要确定文本中使用的关键名词短语,请使用该DetectKeyPhrases操作。要批量检测最多 25 个文档中的关键名词短语,请使用该BatchDetectKeyPhrases操作。有关更多信息,请参阅 Real-time 批处理 API。
使用 AWS Command Line Interface
以下示例演示了如何在 AWS CLI中使用 DetectKeyPhrases 操作。您必须指定输入文本的语言。
此示例的格式适用于 Unix、Linux 和 macOS。对于 Windows,请将每行末尾的反斜杠 (\) Unix 行继续符替换为脱字号 (^)。
aws comprehend detect-key-phrases \ --regionregion\ --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 } ] }
使用 适用于 Java 的 AWS SDK、适用于 Python 的 SDK,或 适用于 .NET 的 SDK
有关检测关键短语的 SDK 示例,请参阅 DetectKeyPhrases搭配使用 AWS SDK 或 CLI。
确定情绪
Amazon Comprehend 提供以下 API 操作,以便分析情绪:
DetectSentiment— 决定文档的整体情感情绪。
-
BatchDetectSentiment— 一批确定最多 25 个文档的整体情绪。有关更多信息,请参阅 Real-time 批处理 API。
StartSentimentDetectionJob— 为一组文档启动异步情绪检测作业。
ListSentimentDetectionJobs— 返回您提交的情绪检测任务列表。
DescribeSentimentDetectionJob— 获取与指定情绪检测任务相关的属性(包括状态)。
StopSentimentDetectionJob— 停止指定的正在进行的情绪作业。
使用 AWS Command Line Interface
以下示例演示了如何在 AWS CLI中使用 DetectSentiment 操作。此示例指定了输入文本的语言。
此示例的格式适用于 Unix、Linux 和 macOS。对于 Windows,请将每行末尾的反斜杠 (\) Unix 行继续符替换为脱字号 (^)。
aws comprehend detect-sentiment \ --regionregion\ --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"
}
使用 适用于 Java 的 AWS SDK、适用于 Python 的 SDK,或 适用于 .NET 的 SDK
有关确定输入文本情绪的 SDK 示例,请参阅 DetectSentiment搭配使用 AWS SDK 或 CLI。
Real-time 目标情绪分析
Amazon Comprehend 提供以下 API 操作,用于目标情绪的实时分析:
DetectTargetedSentiment— 分析文档中提及的实体的情绪。
-
BatchDetectTargetedSentiment— 批量分析多达 25 个文档的目标情绪。有关更多信息,请参阅 Real-time 批处理 API。
如果您正在分析的文本不包含任何目标情绪 实体类型,则 API 将返回一个空的实体数组。
使用 AWS Command Line Interface
以下示例演示了如何在 AWS CLI中使用 DetectTargetedSentiment 操作。此示例指定了输入文本的语言。
此示例的格式适用于 Unix、Linux 和 macOS。对于 Windows,请将每行末尾的反斜杠 (\) Unix 行继续符替换为脱字号 (^)。
aws comprehend detect-targeted-sentiment \ --regionregion\ --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操作。有关更多信息,请参阅 Real-time 批处理 API。
使用 AWS Command Line Interface.
以下示例演示了如何在 AWS CLI中使用 DetectSyntax 操作。此示例指定了输入文本的语言。
此示例的格式适用于 Unix、Linux 和 macOS。对于 Windows,请将每行末尾的反斜杠 (\) Unix 行继续符替换为脱字号 (^)。
aws comprehend detect-syntax \ --regionregion\ --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 } ] }
使用 适用于 Java 的 AWS SDK、适用于 Python 的 SDK,或 适用于 .NET 的 SDK
有关检测输入文本语法的 SDK 示例,请参阅 DetectSyntax搭配使用 AWS SDK 或 CLI。
Real-time 批处理 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 \ --endpointendpoint\ --regionregion\ --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 \ --endpointendpoint\ --regionregion\ --cli-input-json file://path to input file/process.json
使用批量检测关键短语 (AWS CLI)
该BatchDetectKeyPhrases操作返回一批文档中的关键名词短语。以下 AWS CLI 命令调用该BatchDetectKeyNounPhrases操作。
aws comprehend batch-detect-key-phrases --endpointendpoint--regionregion--cli-input-json file://path to input file/process.json
使用批量检测情绪 (AWS CLI)
使用该BatchDetectSentiment操作检测一批文档的整体情绪。以下 AWS CLI 命令调用该BatchDetectSentiment操作。
aws comprehend batch-detect-sentiment \ --endpointendpoint\ --regionregion\ --cli-input-json file://path to input file/process.json
使用批处理 适用于 .NET 的 AWS SDK
以下示例程序显示了如何将该BatchDetectEntities操作与配合使用 适用于 .NET 的 SDK。来自服务器的响应包含每个成功处理的文档的BatchDetectEntitiesItemResult对象。如果处理文档时出错,则响应中的错误列表中显示记录。该示例获取了每个有错误的文档,然后重新发送它们。
本节中的 .NET 示例使用 适用于 .NET 的 AWS SDK。您可以使用使用 .NET 开发 AWS 应用程序。AWS Toolkit for Visual Studio它包括有用的模板和 AWS Explorer,用于部署应用程序和管理服务。从 .NET 开发者的角度来看 AWS,请参阅 .NET 开发人员AWS 指南。
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"); } } }