有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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);

我想得到这个结果

enter image description here


共 (2) 个答案

  1. # 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();
            }
    
    
        }
    }
    

    希望对你有帮助

  2. # 2 楼答案

    遗憾的是,这是不可能的

    我想把这幅画从英译德

    必应的结果是:

    enter image description here

    我代码中的结果是:

    <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>

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

    但有一个错误

    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)