Microsoft Translator API Java IncludeMultipleMTAlternatives

时间:2017-10-20 15:42:10

标签: java azure translate

我需要帮助才能在我的请求中设置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

2 个答案:

答案 0 :(得分: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的完整说明。

以下是我的示例代码供您参考。

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://docs.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();
        }


    }
}

希望它对你有所帮助。

答案 1 :(得分:0)

可悲的是,这是不可能的。

我想将图片从en翻译成de。

bing的结果是:

enter image description here

我的代码中的结果是:

&#13;
&#13;
<GetTranslationsResponse xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" 
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><From>en</From><Translations><TranslationMatch>
<Count>0</Count><MatchDegree>100</MatchDegree><MatchedOriginalText/><Rating>5</Rating>
<TranslatedText>Bild</TranslatedText></TranslationMatch></Translations></GetTranslationsResponse>
&#13;
&#13;
&#13;

我尝试更改https://api.microsofttranslator.com/v2/http.svc/GetTranslationshttps://api.microsofttranslator.com/V2/Http.svc/GetTranslationsArray

但是有一个错误

&#13;
&#13;
java.net.SocketException: Connection reset
	at java.net.SocketInputStream.read(SocketInputStream.java:210)
	at java.net.SocketInputStream.read(SocketInputStream.java:141)
	at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
	at sun.security.ssl.InputRecord.read(InputRecord.java:503)
	at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
	at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:940)
	at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
	at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
	at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
	at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
	at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:735)
	at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678)
	at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:706)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1587)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:263)
	at microsoft.translator.Translator.translate(Translator.java:48)
	at microsoft.translator.MicrosoftTranslatorMain.main(MicrosoftTranslatorMain.java:10)
&#13;
&#13;
&#13;