DetectEntities搭配使用 AWS SDK或 CLI - Amazon Comprehend

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

DetectEntities搭配使用 AWS SDK或 CLI

以下代码示例演示如何使用 DetectEntities

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

.NET
AWS SDK for .NET
注意

还有更多相关信息 GitHub。查找完整的示例,学习如何设置和运行 AWS 代码示例存储库

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详细信息,请参阅DetectEntities中的 AWS SDK for .NET API参考

CLI
AWS CLI

检测输入文本中的命名实体

以下 detect-entities 示例分析输入文本并返回命名实体。预训练模型的置信度分数也是每个预测的输出。

aws comprehend detect-entities \ --language-code en \ --text "Hello Zhang Wei, I am John. Your AnyCompany Financial Services, LLC credit card \ account 1111-XXXX-1111-XXXX has a minimum payment of $24.53 that is due by July 31st. Based on your autopay settings, \ we will withdraw your payment on the due date from your bank account number XXXXXX1111 with the routing number XXXXX0000. \ Customer feedback for Sunshine Spa, 123 Main St, Anywhere. Send comments to Alice at AnySpa@example.com."

输出:

{ "Entities": [ { "Score": 0.9994556307792664, "Type": "PERSON", "Text": "Zhang Wei", "BeginOffset": 6, "EndOffset": 15 }, { "Score": 0.9981022477149963, "Type": "PERSON", "Text": "John", "BeginOffset": 22, "EndOffset": 26 }, { "Score": 0.9986887574195862, "Type": "ORGANIZATION", "Text": "AnyCompany Financial Services, LLC", "BeginOffset": 33, "EndOffset": 67 }, { "Score": 0.9959119558334351, "Type": "OTHER", "Text": "1111-XXXX-1111-XXXX", "BeginOffset": 88, "EndOffset": 107 }, { "Score": 0.9708039164543152, "Type": "QUANTITY", "Text": ".53", "BeginOffset": 133, "EndOffset": 136 }, { "Score": 0.9987268447875977, "Type": "DATE", "Text": "July 31st", "BeginOffset": 152, "EndOffset": 161 }, { "Score": 0.9858865737915039, "Type": "OTHER", "Text": "XXXXXX1111", "BeginOffset": 271, "EndOffset": 281 }, { "Score": 0.9700471758842468, "Type": "OTHER", "Text": "XXXXX0000", "BeginOffset": 306, "EndOffset": 315 }, { "Score": 0.9591118693351746, "Type": "ORGANIZATION", "Text": "Sunshine Spa", "BeginOffset": 340, "EndOffset": 352 }, { "Score": 0.9797496795654297, "Type": "LOCATION", "Text": "123 Main St", "BeginOffset": 354, "EndOffset": 365 }, { "Score": 0.994929313659668, "Type": "PERSON", "Text": "Alice", "BeginOffset": 394, "EndOffset": 399 }, { "Score": 0.9949769377708435, "Type": "OTHER", "Text": "AnySpa@example.com", "BeginOffset": 403, "EndOffset": 418 } ] }

有关更多信息,请参阅《Amazon Comprehend 开发人员指南》中的实体

  • 有关API详细信息,请参阅DetectEntities中的 AWS CLI 命令参考

Java
SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整的示例,学习如何设置和运行 AWS 代码示例存储库

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.comprehend.ComprehendClient; import software.amazon.awssdk.services.comprehend.model.DetectEntitiesRequest; import software.amazon.awssdk.services.comprehend.model.DetectEntitiesResponse; import software.amazon.awssdk.services.comprehend.model.Entity; import software.amazon.awssdk.services.comprehend.model.ComprehendException; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DetectEntities { public static void main(String[] args) { String text = "Amazon.com, Inc. is located in Seattle, WA and was founded July 5th, 1994 by Jeff Bezos, allowing customers to buy everything from books to blenders. Seattle is north of Portland and south of Vancouver, BC. Other notable Seattle - based companies are Starbucks and Boeing."; Region region = Region.US_EAST_1; ComprehendClient comClient = ComprehendClient.builder() .region(region) .build(); System.out.println("Calling DetectEntities"); detectAllEntities(comClient, text); comClient.close(); } public static void detectAllEntities(ComprehendClient comClient, String text) { try { DetectEntitiesRequest detectEntitiesRequest = DetectEntitiesRequest.builder() .text(text) .languageCode("en") .build(); DetectEntitiesResponse detectEntitiesResult = comClient.detectEntities(detectEntitiesRequest); List<Entity> entList = detectEntitiesResult.entities(); for (Entity entity : entList) { System.out.println("Entity text is " + entity.text()); } } catch (ComprehendException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • 有关API详细信息,请参阅DetectEntities中的 AWS SDK for Java 2.x API参考

Python
SDK适用于 Python (Boto3)
注意

还有更多相关信息 GitHub。查找完整的示例,学习如何设置和运行 AWS 代码示例存储库

class ComprehendDetect: """Encapsulates Comprehend detection functions.""" def __init__(self, comprehend_client): """ :param comprehend_client: A Boto3 Comprehend client. """ self.comprehend_client = comprehend_client def detect_entities(self, text, language_code): """ Detects entities in a document. Entities can be things like people and places or other common terms. :param text: The document to inspect. :param language_code: The language of the document. :return: The list of entities along with their confidence scores. """ try: response = self.comprehend_client.detect_entities( Text=text, LanguageCode=language_code ) entities = response["Entities"] logger.info("Detected %s entities.", len(entities)) except ClientError: logger.exception("Couldn't detect entities.") raise else: return entities
  • 有关API详细信息,请参阅DetectEntities中的 AWS SDK供参考 Python (Boto3) API。

有关完整列表 AWS SDK开发者指南和代码示例,请参阅将 Amazon Comprehend 与 SDK 配合 AWS 使用。本主题还包括有关入门的信息以及有关先前SDK版本的详细信息。