gpt4 book ai didi

java - Microsoft 翻译 API Java IncludeMultipleMTAlternatives

转载 作者:行者123 更新时间:2023-12-02 12:06:45 28 4
gpt4 key购买 nike

我需要帮助来设置我的请求中的 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);

我想要得到这个结果。

enter image description here

最佳答案

根据您的需求,您似乎在java代码中调用了不正确的REST API

你应该使用https://api.microsofttranslator.com/v2/http.svc/GetTranslations 如果您想设置 IncludeMultipleMTAlternatives 属性并从 MT 引擎获取多个替代方案,而不是https://api.microsofttranslator.com/V2/Http.svc/Translate

请引用 official tutorial 的完整描述.

这是我的示例代码供您引用。

import org.apache.commons.io.IOUtils;

import javax.net.ssl.HttpsURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class Test1 {
private static String key = "<your translator account key>";

public static void main(String[] args) {
try {
// Get the access token
// The key got from Azure portal, please see https://learn.microsoft.com/en-us/azure/cognitive-services/cognitive-services-apis-create-account
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("Hello", "UTF-8");
String from = "en";
String to = "fr";
String translatorTextApiUrl = String.format("https://api.microsofttranslator.com/v2/http.svc/GetTranslations?appid=%s&text=%s&from=%s&to=%s&maxTranslations=5", appId, text, from, to);
HttpsURLConnection translateConn = (HttpsURLConnection) new URL(translatorTextApiUrl).openConnection();
translateConn.setRequestMethod("POST");
translateConn.setRequestProperty("Accept", "application/xml");
translateConn.setRequestProperty("Content-Type", "text/xml");
translateConn.setDoOutput(true);
String TranslationOptions = "<TranslateOptions xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\">" +
"<Category>general</Category>" +
"<ContentType>text/plain</ContentType>" +
"<IncludeMultipleMTAlternatives>True</IncludeMultipleMTAlternatives>" +
"<ReservedFlags></ReservedFlags>" +
"<State>contact with each other</State>" +
"</TranslateOptions>";
translateConn.setRequestProperty("TranslationOptions", TranslationOptions);
IOUtils.write("", translateConn.getOutputStream(), "UTF-8");
String resp = IOUtils.toString(translateConn.getInputStream(), "UTF-8");
System.out.println(resp);
} catch (Exception e) {
e.printStackTrace();
}


}
}

希望对您有帮助。

关于java - Microsoft 翻译 API Java IncludeMultipleMTAlternatives,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46852650/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com