Use DetectSyntax with an AWS SDK or CLI - Amazon Comprehend

Use DetectSyntax with an AWS SDK or CLI

The following code examples show how to use DetectSyntax.

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 Amazon Comprehend to detect syntax /// elements by calling the DetectSyntaxAsync method. /// </summary> public class DetectingSyntax { /// <summary> /// This method calls DetectSynaxAsync to identify the syntax elements /// in the sample text. /// </summary> public static async Task Main() { string text = "It is raining today in Seattle"; var comprehendClient = new AmazonComprehendClient(); // Call DetectSyntax API Console.WriteLine("Calling DetectSyntaxAsync\n"); var detectSyntaxRequest = new DetectSyntaxRequest() { Text = text, LanguageCode = "en", }; DetectSyntaxResponse detectSyntaxResponse = await comprehendClient.DetectSyntaxAsync(detectSyntaxRequest); foreach (SyntaxToken s in detectSyntaxResponse.SyntaxTokens) { Console.WriteLine($"Text: {s.Text}, PartOfSpeech: {s.PartOfSpeech.Tag}, BeginOffset: {s.BeginOffset}, EndOffset: {s.EndOffset}"); } Console.WriteLine("Done"); } }
  • For API details, see DetectSyntax in AWS SDK for .NET API Reference.

CLI
AWS CLI

To detect the parts of speech in an input text

The following detect-syntax example analyzes the syntax of the input text and returns the different parts of speech. The pre-trained model's confidence score is also output for each prediction.

aws comprehend detect-syntax \ --language-code en \ --text "It is a beautiful day in Seattle."

Output:

{ "SyntaxTokens": [ { "TokenId": 1, "Text": "It", "BeginOffset": 0, "EndOffset": 2, "PartOfSpeech": { "Tag": "PRON", "Score": 0.9999740719795227 } }, { "TokenId": 2, "Text": "is", "BeginOffset": 3, "EndOffset": 5, "PartOfSpeech": { "Tag": "VERB", "Score": 0.999901294708252 } }, { "TokenId": 3, "Text": "a", "BeginOffset": 6, "EndOffset": 7, "PartOfSpeech": { "Tag": "DET", "Score": 0.9999938607215881 } }, { "TokenId": 4, "Text": "beautiful", "BeginOffset": 8, "EndOffset": 17, "PartOfSpeech": { "Tag": "ADJ", "Score": 0.9987351894378662 } }, { "TokenId": 5, "Text": "day", "BeginOffset": 18, "EndOffset": 21, "PartOfSpeech": { "Tag": "NOUN", "Score": 0.9999796748161316 } }, { "TokenId": 6, "Text": "in", "BeginOffset": 22, "EndOffset": 24, "PartOfSpeech": { "Tag": "ADP", "Score": 0.9998047947883606 } }, { "TokenId": 7, "Text": "Seattle", "BeginOffset": 25, "EndOffset": 32, "PartOfSpeech": { "Tag": "PROPN", "Score": 0.9940530061721802 } } ] }

For more information, see Syntax Analysis in the Amazon Comprehend Developer Guide.

  • For API details, see DetectSyntax in AWS CLI Command Reference.

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.ComprehendException; import software.amazon.awssdk.services.comprehend.model.DetectSyntaxRequest; import software.amazon.awssdk.services.comprehend.model.DetectSyntaxResponse; import software.amazon.awssdk.services.comprehend.model.SyntaxToken; 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 DetectSyntax { 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 DetectSyntax"); detectAllSyntax(comClient, text); comClient.close(); } public static void detectAllSyntax(ComprehendClient comClient, String text) { try { DetectSyntaxRequest detectSyntaxRequest = DetectSyntaxRequest.builder() .text(text) .languageCode("en") .build(); DetectSyntaxResponse detectSyntaxResult = comClient.detectSyntax(detectSyntaxRequest); List<SyntaxToken> syntaxTokens = detectSyntaxResult.syntaxTokens(); for (SyntaxToken token : syntaxTokens) { System.out.println("Language is " + token.text()); System.out.println("Part of speech is " + token.partOfSpeech().tagAsString()); } } catch (ComprehendException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • For API details, see DetectSyntax in AWS SDK for Java 2.x API Reference.

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_syntax(self, text, language_code): """ Detects syntactical elements of a document. Syntax tokens are portions of text along with their use as parts of speech, such as nouns, verbs, and interjections. :param text: The document to inspect. :param language_code: The language of the document. :return: The list of syntax tokens along with their confidence scores. """ try: response = self.comprehend_client.detect_syntax( Text=text, LanguageCode=language_code ) tokens = response["SyntaxTokens"] logger.info("Detected %s syntax tokens.", len(tokens)) except ClientError: logger.exception("Couldn't detect syntax.") raise else: return tokens
  • For API details, see DetectSyntax 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.