本文介绍了示例Java程序,以使用Microsoft Azure Text Translator API翻译示例文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Java程序使用Microsoft Azure文本翻译器API将示例文本从一种语言翻译成另一种语言.

请提供给我一些步骤,以创建appId,Microsoft Azure Text Translator API的密钥和示例Java程序,以将示例文本从一种语言转换为另一种语言.

谢谢.

解决方案

根据我的理解,我认为您想使用

然后,请参考以下文档以了解如何获取 access_token &转换API.

  1. 获取访问令牌, https://msdn.microsoft.com/zh-CN/library/hh454950.aspx
  2. 使用HTTP接口, https://msdn.microsoft.com/zh-CN/library/ff512387.aspx
  3. 翻译器语言代码, https://msdn.microsoft.com/en-us/library/hh456380.aspx

作为参考,这是我带有Java库的示例代码 apachecommons-io & fastjson .

 包装样品;导入java.io.IOException;导入java.io.UnsupportedEncodingException;导入java.net.HttpURLConnection;导入java.net.MalformedURLException;导入java.net.URL;导入java.net.URLEncoder;导入java.nio.charset.StandardCharsets;导入org.apache.commons.io.IOUtils;导入com.alibaba.fastjson.JSON;公共类TextTranslatorTest {公共静态字符串getAccessToken(字符串字符集,字符串clientId,字符串clientSecret,字符串作用域,字符串GrantType)引发MalformedURLException,IOException {字符串url ="https://datamarket.accesscontrol.windows.net/v2/OAuth2-13";字符串paramsTemplate ="client_id =%s& client_secret =%s& scope =%s& grant_type =%s";字符串参数= String.format(paramsTemplate,URLEncoder.encode(clientId,charset),URLEncoder.encode(clientSecret,charset),作用域,grantType);System.out.println(url);HttpURLConnection conn =(HttpURLConnection)新的URL(url).openConnection();conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded; charset =" + charset);conn.setRequestProperty("Accept-Charset",charset);conn.setRequestMethod("POST");conn.setDoOutput(true);IOUtils.write(params,conn.getOutputStream(),"UTF-8");;字符串resp = IOUtils.toString(conn.getInputStream(),"UTF-8");System.out.println(resp);字符串accessToken = JSON.parseObject(resp).getString("access_token");返回accessToken;}公共静态字符串翻译(字符串字符集,字符串accessToken,字符串文本,字符串从,字符串到)抛出MalformedURLException,IOException {字符串url ="http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + URLEncoder.encode(text,charset)+& from =" + from +& to ="+至;HttpURLConnection conn =(HttpURLConnection)新的URL(url).openConnection();conn.setRequestProperty("Authorization","Bearer" +" + accessToken);字符串resp = IOUtils.toString(conn.getInputStream(),"UTF-8");回报}公共静态void main(String [] args)抛出MalformedURLException,IOException {字符串charset = StandardCharsets.UTF_8.name();字符串clientId ="peter-translator-test";字符串clientSecret ="xxxxxxxxxxxxxxxxxx";字符串范围="http://api.microsofttranslator.com";字符串grantType ="client_credentials";字符串accessToken = getAccessToken(charset,clientId,clientSecret,scope,grantType);System.out.println(accessToken);字符串文本="happy";字符串from ="en";字符串="de";字符串resp = translation(字符集,accessToken,文本,从,到);System.out.println(resp);}} 

根据页面 https://translatorbusiness.uservoice.com/knowledgebase/articles/1078534-microsoft-translator-on-azure ,您只需在2017年4月30日之前使用上述答案即可.然后,您需要遵循新文档 http://docs.microsofttranslator.com/text-translate.html 以便在Azure上使用Text Translator API.但是通过我的测试,Azure上的文本翻译器的新服务似乎尚未准备就绪.因此,我只列出以下简单步骤作为参考.

  1. 获取Azure访问令牌,请参考 http://docs.microsofttranslator.com/oauth-token.html .要获取< your-key> ,您可以参考新文档的步骤12以转到Keys选项并复制您的订阅密钥以访问服务.

    //使用标题传递密码curl --header'Ocp-Apim-Subscription-Key:'--data"' https://api.cognitive.microsoft.com/sts/v1.0/issueToken '//使用查询字符串参数传递密钥curl --data"' https://api.ognitive.microsoft.com/sts/v1.0/issueToken?Subscription-Key= '

  2. 要调用/Translate HTTP接口,请参考 http://docs.microsofttranslator.com/text-translate.html#!/default/get_Translate .

I am trying to translate sample text from one language to another language using Microsoft Azure Text Translator API through Java Program.

Please, provide me steps to create appId, Secret key for Microsoft Azure Text Translator API and sample Java program to convert sample text from one language to another language.

Thanks in advance.

解决方案

Based on my understanding, I think you want to use Microsoft Translator - Text Translation from Microsoft DataMarket in Java program, but seems to not know how to get started with it.

First of all, you need to register an application via https://datamarket.azure.com/developer/applications/register to get the client_id & client_secret after login Microsoft DataMarket

Then, please refer to the documents below to know how to get access_token & translate API.

  1. Obtaining an Access Token, https://msdn.microsoft.com/en-us/library/hh454950.aspx
  2. Using the HTTP Interface, https://msdn.microsoft.com/en-us/library/ff512387.aspx
  3. Translator Language Codes, https://msdn.microsoft.com/en-us/library/hh456380.aspx

As reference, here is my sample code with Java libraries apache commons-io & fastjson.

package sample;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;

import com.alibaba.fastjson.JSON;

public class TextTranslatorTest {

    public static String getAccessToken(String charset, String clientId, String clientSecret, String scope,
            String grantType) throws MalformedURLException, IOException {
        String url = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13";
        String paramsTemplate = "client_id=%s&client_secret=%s&scope=%s&grant_type=%s";
        String params = String.format(paramsTemplate, URLEncoder.encode(clientId, charset),
                URLEncoder.encode(clientSecret, charset), scope, grantType);
        System.out.println(url);
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=" + charset);
        conn.setRequestProperty("Accept-Charset", charset);
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        IOUtils.write(params, conn.getOutputStream(), "UTF-8");;
        String resp = IOUtils.toString(conn.getInputStream(), "UTF-8");
        System.out.println(resp);
        String accessToken = JSON.parseObject(resp).getString("access_token");
        return accessToken;
    }

    public static String translate(String charset, String accessToken, String text, String from, String to) throws MalformedURLException, IOException {
        String url = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + URLEncoder.encode(text, charset) + "&from=" + from + "&to=" + to;
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setRequestProperty("Authorization", "Bearer" + " " + accessToken);
        String resp = IOUtils.toString(conn.getInputStream(), "UTF-8");
        return resp;
    }

    public static void main(String[] args) throws MalformedURLException, IOException {
        String charset = StandardCharsets.UTF_8.name();
        String clientId = "peter-translator-test";
        String clientSecret = "xxxxxxxxxxxxxxxxxx";
        String scope = "http://api.microsofttranslator.com";
        String grantType = "client_credentials";
        String accessToken = getAccessToken(charset, clientId, clientSecret, scope, grantType);
        System.out.println(accessToken);
        String text = "happy";
        String from = "en";
        String to = "de";
        String resp = translate(charset, accessToken, text, from, to);
        System.out.println(resp);
    }
}

Notice, according to the page https://translatorbusiness.uservoice.com/knowledgebase/articles/1078534-microsoft-translator-on-azure, you just use the answer above before April 30, 2017. Then, you need to follow the new document http://docs.microsofttranslator.com/text-translate.html to use Text Translator API on Azure. But the new service of Text Translator on Azure seems to be not ready via my testing. So I just list the simple steps below as reference.

  1. Get Azure access token, please refer to http://docs.microsofttranslator.com/oauth-token.html. To get <your-key>, you can refer to the steps 12 of the new document to Go to the Keys option and copy your subscription key to access the service.

    // Pass key using headercurl --header 'Ocp-Apim-Subscription-Key: ' --data "" 'https://api.cognitive.microsoft.com/sts/v1.0/issueToken'// Pass key using query string parametercurl --data "" 'https://api.cognitive.microsoft.com/sts/v1.0/issueToken?Subscription-Key='

  2. To call the /Translate HTTP interface, please refer to http://docs.microsofttranslator.com/text-translate.html#!/default/get_Translate.

这篇关于示例Java程序,以使用Microsoft Azure Text Translator API翻译示例文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 10:50