À utiliser DetectEntities avec un AWS SDK ou CLI - Exemples de code de l'AWS SDK

D'autres AWS SDK exemples sont disponibles dans le GitHub dépôt AWS Doc SDK Examples.

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

À utiliser DetectEntities avec un AWS SDK ou CLI

Les exemples de code suivants montrent comment utiliserDetectEntities.

Les exemples d’actions sont des extraits de code de programmes de plus grande envergure et doivent être exécutés en contexte. Vous pouvez voir cette action en contexte dans l’exemple de code suivant :

.NET
AWS SDK for .NET
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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"); } }
  • Pour API plus de détails, voir DetectEntitiesla section AWS SDK for .NET APIRéférence.

CLI
AWS CLI

Pour détecter les entités nommées dans le texte saisi

L'detect-entitiesexemple suivant analyse le texte saisi et renvoie les entités nommées. Le score de confiance du modèle préentraîné est également généré pour chaque prédiction.

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."

Sortie :

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

Pour plus d'informations, consultez Entities dans le manuel Amazon Comprehend Developer Guide.

  • Pour API plus de détails, voir DetectEntitiesla section Référence des AWS CLI commandes.

Java
SDKpour Java 2.x
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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); } } }
  • Pour API plus de détails, voir DetectEntitiesla section AWS SDK for Java 2.x APIRéférence.

Python
SDKpour Python (Boto3)
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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
  • Pour API plus de détails, reportez-vous DetectEntitiesà la section AWS SDKpour Python (Boto3) Reference. API