是否可以集成 Translation before/after Amazon Lex Bot?
Is there a possibility to integrate Translation before/after Amazon Lex Bot?
我已经使用 Amazon Lex 构建了一个 Q&A 聊天机器人,所有意图和它的话语都是英文的。当然,现在当用户用英语提出问题时,Amazon Lex 会处理问题并用英语给出答案。
现在我还想让它成为可能,例如使用 Amazon Translate 进行其他语言交互的可能性。这意味着:例如法国用户用法语提问,Amazon Translate 将其翻译成英语并自动将其转发给 Amazon Lex,该过程将像以前一样继续进行。由 lambda 函数触发的答案是英文的,因此在回答时 Amazon Translate 应该采用英文答案文本,将其翻译回法语并 return 给用户。
是否有合适的教程,因为亚马逊的教程似乎只是一般信息。或者是否有一个我可以点击的简单按钮,Amazon Translate 已经准备就绪 ;)
提前感谢您的回答!
使用 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 "";
}
}
我已经使用 Amazon Lex 构建了一个 Q&A 聊天机器人,所有意图和它的话语都是英文的。当然,现在当用户用英语提出问题时,Amazon Lex 会处理问题并用英语给出答案。 现在我还想让它成为可能,例如使用 Amazon Translate 进行其他语言交互的可能性。这意味着:例如法国用户用法语提问,Amazon Translate 将其翻译成英语并自动将其转发给 Amazon Lex,该过程将像以前一样继续进行。由 lambda 函数触发的答案是英文的,因此在回答时 Amazon Translate 应该采用英文答案文本,将其翻译回法语并 return 给用户。
是否有合适的教程,因为亚马逊的教程似乎只是一般信息。或者是否有一个我可以点击的简单按钮,Amazon Translate 已经准备就绪 ;)
提前感谢您的回答!
使用 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 "";
}
}