Amazon Comprehend と AWS SDK を使用してドキュメントの構文要素を検出する - AWS SDK コードサンプル

Doc AWS SDK Examples リポジトリには、他にも SDK の例があります。 AWS GitHub

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

Amazon Comprehend と AWS SDK を使用してドキュメントの構文要素を検出する

次のコード例は、Amazon Comprehend でドキュメント内の構文要素を検出する方法を示しています。

アクション例は、より大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。次のコード例で、このアクションのコンテキストを確認できます。

.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 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"); } }
  • API の詳細については、「 API リファレンスDetectSyntax」の「」を参照してください。 AWS SDK for .NET

CLI
AWS CLI

入力テキスト内の品詞を検出するには

次の detect-syntax の例では、入力テキストの構文を分析し、さまざまな品詞を返します。予測ごとに、事前トレーニング済みモデルの信頼スコアも出力されます。

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

出力:

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

詳細については、「Amazon Comprehend デベロッパーガイド」の「構文分析」を参照してください。

  • API の詳細については、「 コマンドリファレンスDetectSyntax」の「」を参照してください。 AWS CLI

Java
SDK for 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.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); } } }
  • API の詳細については、「 API リファレンスDetectSyntax」の「」を参照してください。 AWS SDK for Java 2.x

Python
SDK for 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_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
  • API の詳細については、DetectSyntaxAWS「 SDK for Python (Boto3) API リファレンス」の「」を参照してください。