1

我使用 Amazon Lex 构建了一个问答聊天机器人,其所有意图及其话语均为英语。当然,当用户用英语提出一些问题时,Amazon Lex 会处理该问题并以英语给出答案。现在,我还想让例如使用 Amazon Translate 进行其他语言交互成为可能。这意味着:例如,法国用户用法语提出问题,Amazon Translate 将其翻译成英语并自动将其转发到 Amazon Lex,过程就像以前一样继续。由 lambda 函数触发的答案是英文的,因此在回答时,Amazon Translate 应该采用英文答案文本,将其翻译回法语并将其返回给用户。

是否有适当的教程,因为亚马逊的教程似乎只是一般信息。或者是否有一个简单的按钮我可以点击,Amazon Translate 已经准备就绪;)

提前感谢您的回答!

4

1 回答 1

0

使用 AWS 开发工具包,您可以构建此功能。您可以使用 Amazon Comprehend 来确定语言。如果是法语(或其他受支持的语言),请将字符串传递给 AWS Translate 服务以获取英语。

这是一个需要代码的多服务用例。我使用 AWS SDK For Java 编写了一个示例 Web 应用程序,它完美地执行了这个用例。

此 Java 代码使用 Amazon LEX、Comprehend 和 AWS Translation 来解决此用例。如您所见,它处理法语和其他语言。

在此处输入图像描述

此示例也处理其他语言,例如意大利语:

在此处输入图像描述

这是解决此问题的 Java V2 代码。请注意,文本从 HTML 客户端发布到控制器,控制器调用getText方法并传递文本。

import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.lexruntime.LexRuntimeClient;
import org.springframework.stereotype.Component;
import software.amazon.awssdk.services.lexruntime.model.LexRuntimeException;
import software.amazon.awssdk.services.lexruntime.model.PostTextRequest;
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 software.amazon.awssdk.services.translate.TranslateClient;
import software.amazon.awssdk.services.translate.model.TranslateTextRequest;
import software.amazon.awssdk.services.translate.model.TranslateTextResponse;
import software.amazon.awssdk.services.translate.model.TranslateException;
import java.util.*;
import java.text.SimpleDateFormat;
import software.amazon.awssdk.services.lexruntime.model.PostTextResponse;

@Component
public class LexService {

    public String getText(String text) {

        Region region = Region.US_EAST_1;
        LexRuntimeClient lexRuntimeClient = LexRuntimeClient.builder()
                .region(region)
                .build();

        String engMessage ="";
        try {

            // Need to determine the language.
            String lanCode = DetectLanguage(text);

            // If the lanCode is NOT Eng - then we need to translate the message to English to pass to Amazon Lex.
            if (lanCode.compareTo("en")  !=0)
                engMessage = textTranslateToEn(lanCode, text);
            else
                engMessage=text;

             String userId =  "chatbot-demo" ;
             Map<String,String> sessionAttributes = new HashMap<>();
             PostTextRequest textRequest = PostTextRequest.builder()
                    .botName("BookTrip")
                    .botAlias("scott")
                    .inputText(engMessage)
                    .userId(userId)
                    .sessionAttributes(sessionAttributes)
                    .build();

            PostTextResponse textResponse = lexRuntimeClient.postText(textRequest);
            String message = textResponse.message();

            // If not EN, we need to translate the text back
            String outputText ="";
            if (lanCode.compareTo("en")  !=0)
                outputText = textTranslateFromEn(lanCode, message);
            else
                outputText = message;

            return outputText ;

        } catch (LexRuntimeException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        return "";
    }

    private String DetectLanguage(String text) {

        Region region = Region.US_EAST_1;
        ComprehendClient comClient = ComprehendClient.builder()
                .region(region)
                .build();

        try {

            String lanCode = "";
            DetectDominantLanguageRequest request = DetectDominantLanguageRequest.builder()
                    .text(text)
                    .build();

            DetectDominantLanguageResponse resp = comClient.detectDominantLanguage(request);
            List<DominantLanguage> allLanList = resp.languages();
            Iterator<DominantLanguage> lanIterator = allLanList.iterator();

            while (lanIterator.hasNext()) {
                DominantLanguage lang = lanIterator.next();
                lanCode = lang.languageCode();
            }

            return lanCode;

        } catch (ComprehendException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        return "";
    }

    public String textTranslateToEn(String lanCode, String text) {

        Region region = Region.US_EAST_1;
        TranslateClient translateClient = TranslateClient.builder()
                .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
                .region(region)
                .build();
        try {
            TranslateTextRequest textRequest = TranslateTextRequest.builder()
                    .sourceLanguageCode(lanCode)
                    .targetLanguageCode("en")
                    .text(text)
                    .build();

            TranslateTextResponse textResponse = translateClient.translateText(textRequest);
            return textResponse.translatedText();

        } catch (TranslateException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        return "";
    }


    public String textTranslateFromEn(String lanCode, String text) {

        Region region = Region.US_EAST_1;
        TranslateClient translateClient = TranslateClient.builder()
                .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
                .region(region)
                .build();
        try {
            TranslateTextRequest textRequest = TranslateTextRequest.builder()
                    .sourceLanguageCode("en")
                    .targetLanguageCode(lanCode)
                    .text(text)
                    .build();

            TranslateTextResponse textResponse = translateClient.translateText(textRequest);
            return textResponse.translatedText();

        } catch (TranslateException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        return "";
    }
}
于 2021-01-24T14:28:47.687 回答