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

CLI
AWS CLI

入力テキスト内のキーフレーズを検出するには

次の detect-key-phrases の例では、入力テキストを分析し、主要な名詞フレーズを特定します。予測ごとに、事前トレーニング済みモデルの信頼スコアも出力されます。

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

出力:

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

詳細については、「Amazon Comprehend 開発者ガイド」の「キーフレーズ」を参照してください。

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