创建用于分析客户反馈和合成音频的应用程序 - Amazon Lambda
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

创建用于分析客户反馈和合成音频的应用程序

以下代码示例显示如何创建应用程序来分析客户意见卡、翻译其母语、确定其情绪并根据译后的文本生成音频文件。

.NET
Amazon SDK for .NET

此示例应用程序可分析并存储客户反馈卡。具体来说,它满足了纽约市一家虚构酒店的需求。酒店以实体意见卡的形式收集来自不同语种的客人的反馈。该反馈通过 Web 客户端上传到应用程序中。意见卡图片上传后,将执行以下步骤:

  • 使用 Amazon Textract 从图片中提取文本。

  • Amazon Comprehend 确定所提取文本的情绪及其语言。

  • 使用 Amazon Translate 将所提取文本翻译为英语。

  • Amazon Polly 根据所提取文本合成音频文件。

完整的应用程序可使用 Amazon CDK 进行部署。有关源代码和部署说明,请参阅 GitHub 中的项目。

本示例中使用的服务
  • Amazon Comprehend

  • Lambda

  • Amazon Polly

  • Amazon Textract

  • Amazon Translate

Java
适用于 Java 2.x 的 SDK

此示例应用程序可分析并存储客户反馈卡。具体来说,它满足了纽约市一家虚构酒店的需求。酒店以实体意见卡的形式收集来自不同语种的客人的反馈。该反馈通过 Web 客户端上传到应用程序中。意见卡图片上传后,将执行以下步骤:

  • 使用 Amazon Textract 从图片中提取文本。

  • Amazon Comprehend 确定所提取文本的情绪及其语言。

  • 使用 Amazon Translate 将所提取文本翻译为英语。

  • Amazon Polly 根据所提取文本合成音频文件。

完整的应用程序可使用 Amazon CDK 进行部署。有关源代码和部署说明,请参阅 GitHub 中的项目。

本示例中使用的服务
  • Amazon Comprehend

  • Lambda

  • Amazon Polly

  • Amazon Textract

  • Amazon Translate

JavaScript
SDK for JavaScript (v3)

此示例应用程序可分析并存储客户反馈卡。具体来说,它满足了纽约市一家虚构酒店的需求。酒店以实体意见卡的形式收集来自不同语种的客人的反馈。该反馈通过 Web 客户端上传到应用程序中。意见卡图片上传后,将执行以下步骤:

  • 使用 Amazon Textract 从图片中提取文本。

  • Amazon Comprehend 确定所提取文本的情绪及其语言。

  • 使用 Amazon Translate 将所提取文本翻译为英语。

  • Amazon Polly 根据所提取文本合成音频文件。

完整的应用程序可使用 Amazon CDK 进行部署。有关源代码和部署说明,请参阅 GitHub 中的项目。以下摘录显示了 Amazon 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
适用于 Ruby 的 SDK

此示例应用程序可分析并存储客户反馈卡。具体来说,它满足了纽约市一家虚构酒店的需求。酒店以实体意见卡的形式收集来自不同语种的客人的反馈。该反馈通过 Web 客户端上传到应用程序中。意见卡图片上传后,将执行以下步骤:

  • 使用 Amazon Textract 从图片中提取文本。

  • Amazon Comprehend 确定所提取文本的情绪及其语言。

  • 使用 Amazon Translate 将所提取文本翻译为英语。

  • Amazon Polly 根据所提取文本合成音频文件。

完整的应用程序可使用 Amazon CDK 进行部署。有关源代码和部署说明,请参阅 GitHub 中的项目。

本示例中使用的服务
  • Amazon Comprehend

  • Lambda

  • Amazon Polly

  • Amazon Textract

  • Amazon Translate

有关 Amazon 软件开发工具包开发人员指南和代码示例的完整列表,请参阅 将 Lambda 与 Amazon SDK 配合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。