使用适用于 JavaScript 的 SDK(v3)的 Amazon Translate 示例 - 适用于 JavaScript 的 Amazon SDK
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

适用于 JavaScript 的 Amazon SDKV3 API 参考指南详细描述了 适用于 JavaScript 的 Amazon SDK 版本 3 (V3) 的所有 API 操作。

使用适用于 JavaScript 的 SDK(v3)的 Amazon Translate 示例

以下代码示例演示如何通过将适用于 JavaScript 的 Amazon SDK(v3)与 Amazon Translate 结合使用,来执行操作和实现常见场景。

场景是向您演示如何通过在一个服务中调用多个函数或与其他 Amazon Web Services 服务 结合来完成特定任务的代码示例。

每个示例都包含一个指向完整源代码的链接,您可以从中找到有关如何在上下文中设置和运行代码的说明。

主题

场景

以下代码示例展示如何构建可实时录制、转录与翻译实时音频,并通过电子邮件发送结果的应用程序。

SDK for JavaScript (v3)

演示了如何使用 Amazon Transcribe 构建可实时录制、转录与翻译实时音频,并通过 Amazon Simple Email Service (Amazon SES) 以电子邮件发送结果的应用程序。

有关完整的源代码以及如何设置和运行的说明,请参阅 GitHub 上的完整示例。

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

  • Amazon SES

  • Amazon Transcribe

  • Amazon Translate

以下代码示例展示了如何创建聊天机器人来与网站访客互动。

SDK for JavaScript (v3)

展示如何使用 Amazon Lex API 在 Web 应用程序中创建聊天机器人,以吸引网站访客。

有关完整的源代码以及如何设置和运行的说明,请参阅《适用于 JavaScript 的 Amazon SDK 开发人员指南》中的完整示例构建 Amazon Lex 聊天机器人

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

  • Amazon Lex

  • Amazon Translate

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

SDK for JavaScript (v3)

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

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

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

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

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

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