azure Microsoft Translator API Java IncludeMultipleMTAlternatives
我需要帮助设置请求中的IncludeMultipleMTAlternatives。我不知道如何启用它
我只得到一个结果。有人能帮忙吗?谢谢
我的代码:
// Get the access token
String key = "*********************************";
String authenticationUrl = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken";
HttpsURLConnection authConn = (HttpsURLConnection) new URL(authenticationUrl).openConnection();
authConn.setRequestMethod("POST");
authConn.setDoOutput(true);
authConn.setRequestProperty("Ocp-Apim-Subscription-Key", key);
IOUtils.write("", authConn.getOutputStream(), "UTF-8");
String token = IOUtils.toString(authConn.getInputStream(), "UTF-8");
System.out.println(token);
// Using the access token to build the appid for the request url
String appId = URLEncoder.encode("Bearer " + token, "UTF-8");
String text = URLEncoder.encode("house", "UTF-8");
String from = "en";
String to = "de";
String translatorTextApiUrl = String.format("https://api.microsofttranslator.com/v2/http.svc/Translate?appid=%s&text=%s&from=%s&to=%s", appId, text, from, to + "&maxTranslations=5&IncludeMultipleMTAlternatives=true");
HttpsURLConnection translateConn = (HttpsURLConnection) new URL(translatorTextApiUrl).openConnection();
translateConn.setRequestMethod("GET");
translateConn.setRequestProperty("Accept", "application/xml");
String resp = IOUtils.toString(translateConn.getInputStream(), "UTF-8");
System.out.println(resp);
我想得到这个结果
# 1 楼答案
根据您的需要,您似乎在java代码中调用了不正确的
REST API
你应该使用
https://api.microsofttranslator.com/v2/http.svc/GetTranslations
如果您想设置IncludeMultipleMTAlternatives
属性,并从MT engine
中获取多个备选方案,则不要https://api.microsofttranslator.com/V2/Http.svc/Translate
请参阅official tutorial的完整描述
这是我的示例代码供您参考
希望对你有帮助
# 2 楼答案
遗憾的是,这是不可能的
我想把这幅画从英译德
必应的结果是:
enter image description here
我代码中的结果是:
我试图改变https://api.microsofttranslator.com/v2/http.svc/GetTranslations 到 https://api.microsofttranslator.com/V2/Http.svc/GetTranslationsArray
但有一个错误