Amazon Comprehend 使用 Java 2.x SDK 的例子 - AWS SDK 程式碼範例

AWS 文檔 AWS SDK示例 GitHub 回購中有更多SDK示例

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

Amazon Comprehend 使用 Java 2.x SDK 的例子

下列程式碼範例說明如何透過 AWS SDK for Java 2.x 搭配 Amazon Comprehend 使用來執行動作和實作常見案例。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會顯示如何呼叫個別服務函數,但您可以在其相關案例中查看內容中的動作。

例是程式碼範例,向您展示如何透過呼叫服務中的多個函數或與其他函式結合來完成特定工作 AWS 服務。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

動作

下列程式碼範例會示範如何使用CreateDocumentClassifier

SDK對於爪哇 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.CreateDocumentClassifierRequest; import software.amazon.awssdk.services.comprehend.model.CreateDocumentClassifierResponse; import software.amazon.awssdk.services.comprehend.model.DocumentClassifierInputDataConfig; /** * Before running this code example, you can setup the necessary resources, such * as the CSV file and IAM Roles, by following this document: * https://aws.amazon.com/blogs/machine-learning/building-a-custom-classifier-using-amazon-comprehend/ * * Also, 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 DocumentClassifierDemo { public static void main(String[] args) { final String usage = """ Usage: <dataAccessRoleArn> <s3Uri> <documentClassifierName> Where: dataAccessRoleArn - The ARN value of the role used for this operation. s3Uri - The Amazon S3 bucket that contains the CSV file. documentClassifierName - The name of the document classifier. """; if (args.length != 3) { System.out.println(usage); System.exit(1); } String dataAccessRoleArn = args[0]; String s3Uri = args[1]; String documentClassifierName = args[2]; Region region = Region.US_EAST_1; ComprehendClient comClient = ComprehendClient.builder() .region(region) .build(); createDocumentClassifier(comClient, dataAccessRoleArn, s3Uri, documentClassifierName); comClient.close(); } public static void createDocumentClassifier(ComprehendClient comClient, String dataAccessRoleArn, String s3Uri, String documentClassifierName) { try { DocumentClassifierInputDataConfig config = DocumentClassifierInputDataConfig.builder() .s3Uri(s3Uri) .build(); CreateDocumentClassifierRequest createDocumentClassifierRequest = CreateDocumentClassifierRequest.builder() .documentClassifierName(documentClassifierName) .dataAccessRoleArn(dataAccessRoleArn) .languageCode("en") .inputDataConfig(config) .build(); CreateDocumentClassifierResponse createDocumentClassifierResult = comClient .createDocumentClassifier(createDocumentClassifierRequest); String documentClassifierArn = createDocumentClassifierResult.documentClassifierArn(); System.out.println("Document Classifier ARN: " + documentClassifierArn); } catch (ComprehendException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }

下列程式碼範例會示範如何使用DetectDominantLanguage

SDK對於爪哇 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.DetectDominantLanguageRequest; import software.amazon.awssdk.services.comprehend.model.DetectDominantLanguageResponse; import software.amazon.awssdk.services.comprehend.model.DominantLanguage; 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 DetectLanguage { public static void main(String[] args) { // Specify French text - "It is raining today in Seattle". String text = "Il pleut aujourd'hui à Seattle"; Region region = Region.US_EAST_1; ComprehendClient comClient = ComprehendClient.builder() .region(region) .build(); System.out.println("Calling DetectDominantLanguage"); detectTheDominantLanguage(comClient, text); comClient.close(); } public static void detectTheDominantLanguage(ComprehendClient comClient, String text) { try { DetectDominantLanguageRequest request = DetectDominantLanguageRequest.builder() .text(text) .build(); DetectDominantLanguageResponse resp = comClient.detectDominantLanguage(request); List<DominantLanguage> allLanList = resp.languages(); for (DominantLanguage lang : allLanList) { System.out.println("Language is " + lang.languageCode()); } } catch (ComprehendException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }

下列程式碼範例會示範如何使用DetectEntities

SDK對於爪哇 2.x
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.comprehend.ComprehendClient; import software.amazon.awssdk.services.comprehend.model.DetectEntitiesRequest; import software.amazon.awssdk.services.comprehend.model.DetectEntitiesResponse; import software.amazon.awssdk.services.comprehend.model.Entity; 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 DetectEntities { 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 DetectEntities"); detectAllEntities(comClient, text); comClient.close(); } public static void detectAllEntities(ComprehendClient comClient, String text) { try { DetectEntitiesRequest detectEntitiesRequest = DetectEntitiesRequest.builder() .text(text) .languageCode("en") .build(); DetectEntitiesResponse detectEntitiesResult = comClient.detectEntities(detectEntitiesRequest); List<Entity> entList = detectEntitiesResult.entities(); for (Entity entity : entList) { System.out.println("Entity text is " + entity.text()); } } catch (ComprehendException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • 如需詳API細資訊,請參閱AWS SDK for Java 2.x API參考DetectEntities中的。

下列程式碼範例會示範如何使用DetectKeyPhrases

SDK對於爪哇 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細資訊,請參閱AWS SDK for Java 2.x API參考DetectKeyPhrases中的。

下列程式碼範例會示範如何使用DetectSentiment

SDK對於爪哇 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.DetectSentimentRequest; import software.amazon.awssdk.services.comprehend.model.DetectSentimentResponse; /** * 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 DetectSentiment { 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 DetectSentiment"); detectSentiments(comClient, text); comClient.close(); } public static void detectSentiments(ComprehendClient comClient, String text) { try { DetectSentimentRequest detectSentimentRequest = DetectSentimentRequest.builder() .text(text) .languageCode("en") .build(); DetectSentimentResponse detectSentimentResult = comClient.detectSentiment(detectSentimentRequest); System.out.println("The Neutral value is " + detectSentimentResult.sentimentScore().neutral()); } catch (ComprehendException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • 如需詳API細資訊,請參閱AWS SDK for Java 2.x API參考DetectSentiment中的。

下列程式碼範例會示範如何使用DetectSyntax

SDK對於爪哇 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細資訊,請參閱AWS SDK for Java 2.x API參考DetectSyntax中的。

案例

以下代碼示例演示瞭如何創建聊天機器人以吸引您的網站訪問者。

SDK對於爪哇 2.x

示範如何使用 Amazon Lex 在 Web 應API用程式中建立 Chatbot,以吸引您的網站訪客。

有關如何設置和運行的完整源代碼和說明,請參閱中的完整示例GitHub

此範例中使用的服務
  • Amazon Comprehend

  • Amazon Lex

  • Amazon Translate

下列程式碼範例示範如何使用 Amazon 建立簡訊應用程式SQS。

SDK對於爪哇 2.x

演示如何使用 Amazon SQS API 開發發送和檢索消息RESTAPI的 Spring。

有關如何設置和運行的完整源代碼和說明,請參閱中的完整示例GitHub

此範例中使用的服務
  • Amazon Comprehend

  • Amazon SQS

下列程式碼範例會示範如何建立可分析客戶評論卡、從其原始語言進行翻譯、判斷對方情緒,以及透過翻譯後的文字產生音訊檔案的應用程式。

SDK對於爪哇 2.x

此範例應用程式會分析和存儲客戶的意見回饋卡。具體來說,它滿足了紐約市一家虛構飯店的需求。飯店以實體評論卡的形式收到賓客以各種語言撰寫的意見回饋。這些意見回饋透過 Web 用戶端上傳至應用程式。評論卡的影像上傳後,系統會執行下列步驟:

  • 文字內容是使用 Amazon Textract 從影像中擷取。

  • Amazon Comprehend 會決定擷取文字及其用語的情感。

  • 擷取的文字內容會使用 Amazon Translate 翻譯成英文。

  • Amazon Polly 會使用擷取的文字內容合成音訊檔案。

完整的應用程式可透過  AWS CDK 部署。如需原始程式碼和部署指示,請參閱中的專案 GitHub

此範例中使用的服務
  • Amazon Comprehend

  • Lambda

  • Amazon Polly

  • Amazon Textract

  • Amazon Translate