DetectKeyPhrasesÚselo con un AWS SDKo CLI - Amazon Comprehend

Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.

DetectKeyPhrasesÚselo con un AWS SDKo CLI

En los siguientes ejemplos de código, se muestra cómo utilizar DetectKeyPhrases.

Los ejemplos de acciones son extractos de código de programas más grandes y deben ejecutarse en contexto. Puede ver esta acción en contexto en el siguiente ejemplo de código:

.NET
AWS SDK for .NET
nota

Hay más información GitHub. Consulta el ejemplo completo y aprende a configurarlo y ejecutarlo en el AWS Repositorio de ejemplos de código.

using System; using System.Threading.Tasks; using Amazon.Comprehend; using Amazon.Comprehend.Model; /// <summary> /// This example shows how to use the Amazon Comprehend service to /// search text for key phrases. /// </summary> public static class DetectKeyPhrase { /// <summary> /// This method calls the Amazon Comprehend method DetectKeyPhrasesAsync /// to detect any key phrases in the sample text. /// </summary> public static async Task Main() { string text = "It is raining today in Seattle"; var comprehendClient = new AmazonComprehendClient(Amazon.RegionEndpoint.USWest2); // Call DetectKeyPhrases API Console.WriteLine("Calling DetectKeyPhrases"); var detectKeyPhrasesRequest = new DetectKeyPhrasesRequest() { Text = text, LanguageCode = "en", }; var detectKeyPhrasesResponse = await comprehendClient.DetectKeyPhrasesAsync(detectKeyPhrasesRequest); foreach (var kp in detectKeyPhrasesResponse.KeyPhrases) { Console.WriteLine($"Text: {kp.Text}, Score: {kp.Score}, BeginOffset: {kp.BeginOffset}, EndOffset: {kp.EndOffset}"); } Console.WriteLine("Done"); } }
  • Para API obtener más información, consulte DetectKeyPhrasesen AWS SDK for .NET APIReferencia.

CLI
AWS CLI

Para detectar frases clave en el texto de entrada

El siguiente ejemplo de detect-key-phrases analiza el texto de entrada e identifica las frases nominales clave. La puntuación de confianza del modelo previamente entrenado también se muestra para cada predicción.

aws comprehend detect-key-phrases \ --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."

Salida:

{ "KeyPhrases": [ { "Score": 0.8996376395225525, "Text": "Zhang Wei", "BeginOffset": 6, "EndOffset": 15 }, { "Score": 0.9992469549179077, "Text": "John", "BeginOffset": 22, "EndOffset": 26 }, { "Score": 0.988385021686554, "Text": "Your AnyCompany Financial Services", "BeginOffset": 28, "EndOffset": 62 }, { "Score": 0.8740853071212769, "Text": "LLC credit card account 1111-XXXX-1111-XXXX", "BeginOffset": 64, "EndOffset": 107 }, { "Score": 0.9999437928199768, "Text": "a minimum payment", "BeginOffset": 112, "EndOffset": 129 }, { "Score": 0.9998900890350342, "Text": ".53", "BeginOffset": 133, "EndOffset": 136 }, { "Score": 0.9979453086853027, "Text": "July 31st", "BeginOffset": 152, "EndOffset": 161 }, { "Score": 0.9983011484146118, "Text": "your autopay settings", "BeginOffset": 172, "EndOffset": 193 }, { "Score": 0.9996572136878967, "Text": "your payment", "BeginOffset": 211, "EndOffset": 223 }, { "Score": 0.9995037317276001, "Text": "the due date", "BeginOffset": 227, "EndOffset": 239 }, { "Score": 0.9702621698379517, "Text": "your bank account number XXXXXX1111", "BeginOffset": 245, "EndOffset": 280 }, { "Score": 0.9179925918579102, "Text": "the routing number XXXXX0000.Customer feedback", "BeginOffset": 286, "EndOffset": 332 }, { "Score": 0.9978160858154297, "Text": "Sunshine Spa", "BeginOffset": 337, "EndOffset": 349 }, { "Score": 0.9706913232803345, "Text": "123 Main St", "BeginOffset": 351, "EndOffset": 362 }, { "Score": 0.9941995143890381, "Text": "comments", "BeginOffset": 379, "EndOffset": 387 }, { "Score": 0.9759287238121033, "Text": "Alice", "BeginOffset": 391, "EndOffset": 396 }, { "Score": 0.8376792669296265, "Text": "AnySpa@example.com", "BeginOffset": 400, "EndOffset": 415 } ] }

Para obtener más información, consulte Frases clave en la Guía para desarrolladores de Amazon Comprehend.

  • Para API obtener más información, consulte DetectKeyPhrasesen AWS CLI Referencia de comandos.

Java
SDKpara Java 2.x
nota

Hay más información. GitHub Consulta el ejemplo completo y aprende a configurarlo y ejecutarlo en el AWS Repositorio de ejemplos de código.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.comprehend.ComprehendClient; import software.amazon.awssdk.services.comprehend.model.DetectKeyPhrasesRequest; import software.amazon.awssdk.services.comprehend.model.DetectKeyPhrasesResponse; import software.amazon.awssdk.services.comprehend.model.KeyPhrase; 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 DetectKeyPhrases { 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 DetectKeyPhrases"); detectAllKeyPhrases(comClient, text); comClient.close(); } public static void detectAllKeyPhrases(ComprehendClient comClient, String text) { try { DetectKeyPhrasesRequest detectKeyPhrasesRequest = DetectKeyPhrasesRequest.builder() .text(text) .languageCode("en") .build(); DetectKeyPhrasesResponse detectKeyPhrasesResult = comClient.detectKeyPhrases(detectKeyPhrasesRequest); List<KeyPhrase> phraseList = detectKeyPhrasesResult.keyPhrases(); for (KeyPhrase keyPhrase : phraseList) { System.out.println("Key phrase text is " + keyPhrase.text()); } } catch (ComprehendException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • Para API obtener más información, consulte DetectKeyPhrasesen AWS SDK for Java 2.x APIReferencia.

Python
SDKpara Python (Boto3)
nota

Hay más información. GitHub Consulta el ejemplo completo y aprende a configurarlo y ejecutarlo en el AWS Repositorio de ejemplos de código.

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_key_phrases(self, text, language_code): """ Detects key phrases in a document. A key phrase is typically a noun and its modifiers. :param text: The document to inspect. :param language_code: The language of the document. :return: The list of key phrases along with their confidence scores. """ try: response = self.comprehend_client.detect_key_phrases( Text=text, LanguageCode=language_code ) phrases = response["KeyPhrases"] logger.info("Detected %s phrases.", len(phrases)) except ClientError: logger.exception("Couldn't detect phrases.") raise else: return phrases
  • Para API obtener más información, consulte DetectKeyPhrasesen AWS SDKpara referencia de Python (Boto3). API

Para obtener una lista completa de AWS SDKguías para desarrolladores y ejemplos de código, consulteUso de Amazon Comprehend con un SDK AWS. En este tema también se incluye información sobre cómo empezar y detalles sobre SDK las versiones anteriores.