使用 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 "";
}
}