Gunakan DetectKeyPhrases dengan AWS SDKatau CLI - Amazon Comprehend

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Gunakan DetectKeyPhrases dengan AWS SDKatau CLI

Contoh kode berikut menunjukkan cara menggunakanDetectKeyPhrases.

Contoh tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Anda dapat melihat tindakan ini dalam konteks dalam contoh kode berikut:

.NET
AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankan di AWS Repositori Contoh Kode.

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

Untuk mendeteksi frase kunci dalam teks masukan

detect-key-phrasesContoh berikut menganalisis teks input dan mengidentifikasi frase kata benda kunci. Skor kepercayaan model yang telah dilatih sebelumnya juga merupakan output untuk setiap prediksi.

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

Untuk informasi selengkapnya, lihat Frasa Kunci di Panduan Pengembang Amazon Comprehend.

Java
SDKuntuk Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankan di AWS Repositori Contoh Kode.

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); } } }
  • Untuk API detailnya, lihat DetectKeyPhrasesdi AWS SDK for Java 2.x APIReferensi.

Python
SDKuntuk Python (Boto3)
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankan di AWS Repositori Contoh Kode.

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
  • Untuk API detailnya, lihat DetectKeyPhrasesdi AWS SDKuntuk Python (Boto3) Referensi. API

Untuk daftar lengkap AWS SDKpanduan pengembang dan contoh kode, lihatMenggunakan Amazon Comprehend dengan SDK AWS. Topik ini juga mencakup informasi tentang memulai dan detail tentang SDK versi sebelumnya.