建立可分析客戶意見回饋並合成音訊的應用程式 - Amazon Translate

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

建立可分析客戶意見回饋並合成音訊的應用程式

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

.NET
AWS SDK for .NET

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

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

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

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

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

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

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

  • Lambda

  • Amazon Polly

  • Amazon Textract

  • Amazon Translate

Java
SDK對於爪哇 2.x

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

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

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

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

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

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

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

  • Lambda

  • Amazon Polly

  • Amazon Textract

  • Amazon Translate

JavaScript
SDK對於 JavaScript (3)

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

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

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

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

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

完整的應用程式可以部署 AWS CDK。 如需原始程式碼和部署指示,請參閱中的專案 GitHub。下面的摘錄顯示了如何 AWS SDK for JavaScript 在 Lambda 函數內部使用。

import { ComprehendClient, DetectDominantLanguageCommand, DetectSentimentCommand, } from "@aws-sdk/client-comprehend"; /** * Determine the language and sentiment of the extracted text. * * @param {{ source_text: string}} extractTextOutput */ export const handler = async (extractTextOutput) => { const comprehendClient = new ComprehendClient({}); const detectDominantLanguageCommand = new DetectDominantLanguageCommand({ Text: extractTextOutput.source_text, }); // The source language is required for sentiment analysis and // translation in the next step. const { Languages } = await comprehendClient.send( detectDominantLanguageCommand, ); const languageCode = Languages[0].LanguageCode; const detectSentimentCommand = new DetectSentimentCommand({ Text: extractTextOutput.source_text, LanguageCode: languageCode, }); const { Sentiment } = await comprehendClient.send(detectSentimentCommand); return { sentiment: Sentiment, language_code: languageCode, }; };
import { DetectDocumentTextCommand, TextractClient, } from "@aws-sdk/client-textract"; /** * Fetch the S3 object from the event and analyze it using Amazon Textract. * * @param {import("@types/aws-lambda").EventBridgeEvent<"Object Created">} eventBridgeS3Event */ export const handler = async (eventBridgeS3Event) => { const textractClient = new TextractClient(); const detectDocumentTextCommand = new DetectDocumentTextCommand({ Document: { S3Object: { Bucket: eventBridgeS3Event.bucket, Name: eventBridgeS3Event.object, }, }, }); // Textract returns a list of blocks. A block can be a line, a page, word, etc. // Each block also contains geometry of the detected text. // For more information on the Block type, see https://docs.aws.amazon.com/textract/latest/dg/API_Block.html. const { Blocks } = await textractClient.send(detectDocumentTextCommand); // For the purpose of this example, we are only interested in words. const extractedWords = Blocks.filter((b) => b.BlockType === "WORD").map( (b) => b.Text, ); return extractedWords.join(" "); };
import { PollyClient, SynthesizeSpeechCommand } from "@aws-sdk/client-polly"; import { S3Client } from "@aws-sdk/client-s3"; import { Upload } from "@aws-sdk/lib-storage"; /** * Synthesize an audio file from text. * * @param {{ bucket: string, translated_text: string, object: string}} sourceDestinationConfig */ export const handler = async (sourceDestinationConfig) => { const pollyClient = new PollyClient({}); const synthesizeSpeechCommand = new SynthesizeSpeechCommand({ Engine: "neural", Text: sourceDestinationConfig.translated_text, VoiceId: "Ruth", OutputFormat: "mp3", }); const { AudioStream } = await pollyClient.send(synthesizeSpeechCommand); const audioKey = `${sourceDestinationConfig.object}.mp3`; // Store the audio file in S3. const s3Client = new S3Client(); const upload = new Upload({ client: s3Client, params: { Bucket: sourceDestinationConfig.bucket, Key: audioKey, Body: AudioStream, ContentType: "audio/mp3", }, }); await upload.done(); return audioKey; };
import { TranslateClient, TranslateTextCommand, } from "@aws-sdk/client-translate"; /** * Translate the extracted text to English. * * @param {{ extracted_text: string, source_language_code: string}} textAndSourceLanguage */ export const handler = async (textAndSourceLanguage) => { const translateClient = new TranslateClient({}); const translateCommand = new TranslateTextCommand({ SourceLanguageCode: textAndSourceLanguage.source_language_code, TargetLanguageCode: "en", Text: textAndSourceLanguage.extracted_text, }); const { TranslatedText } = await translateClient.send(translateCommand); return { translated_text: TranslatedText }; };
此範例中使用的服務
  • Amazon Comprehend

  • Lambda

  • Amazon Polly

  • Amazon Textract

  • Amazon Translate

Ruby
SDK對於紅寶石

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

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

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

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

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

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

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

  • Lambda

  • Amazon Polly

  • Amazon Textract

  • Amazon Translate

有關的完整列表 AWS SDK開發人員指南和代碼示例,請參閱使用此服務 AWS SDK。本主題也包含有關入門的資訊以及舊SDK版的詳細資訊。