Use DetectKeyPhrases with an AWS SDK or CLI - Amazon Comprehend

Use DetectKeyPhrases with an AWS SDK or CLI

The following code examples show how to use DetectKeyPhrases.

Action examples are code excerpts from larger programs and must be run in context. You can see this action in context in the following code example:

.NET
AWS SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

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

To detect key phrases in input text

The following detect-key-phrases example analyzes the input text and identifies the key noun phrases. The pre-trained model's confidence score is also output for each prediction.

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

Output:

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

For more information, see Key Phrases in the Amazon Comprehend Developer Guide.

Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

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); } } }
Python
SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

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
  • For API details, see DetectKeyPhrases in AWS SDK for Python (Boto3) API Reference.

For a complete list of AWS SDK developer guides and code examples, see Using Amazon Comprehend with an AWS SDK. This topic also includes information about getting started and details about previous SDK versions.