2017-01-13 20 views
-5

JavaプログラムによるMicrosoft Azure Text Translator APIを使用して、サンプルテキストをある言語から別の言語に翻訳しようとしています。Microsoft Azure Text Translator APIを使用してサンプルテキストを翻訳するためのサンプルJavaプログラム

Microsoft Azure Text Translator APIのためのappId、秘密鍵を作成する手順と、サンプルテキストをある言語から別の言語に変換するためのサンプルJavaプログラムを提供してください。

ありがとうございます。

+1

よく質問された質問をするには、http://stackoverflow.com/helpを参照してください。 – Chris311

答えて

1

私の理解に基づいて、私はあなたがJavaプログラムでMicrosoft DataMarketのMicrosoft Translator - Text Translationを使用したいと思っていますが、それをどうやって使い始めるべきかわかりません。

まず第一に、あなたは次にclient_id & client_secretログインマイクロソフトDataMarket後

enter image description here

を取得するためにhttps://datamarket.azure.com/developer/applications/registerを介してアプリケーションを登録する必要があり、以下を参照してください。以下の文書を取得する方法を知っているaccess_token & translate API。アクセストークンを取得

  1. 、HTTPインタフェースを使用したhttps://msdn.microsoft.com/en-us/library/hh454950.aspx
  2. https://msdn.microsoft.com/en-us/library/ff512387.aspx
  3. 翻訳言語コード、https://msdn.microsoft.com/en-us/library/hh456380.aspx

は参考として、ここではJavaライブラリ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); 
    } 
} 

お知らせ、ページhttps://translatorbusiness.uservoice.com/knowledgebase/articles/1078534-microsoft-translator-on-azureによると、あなただけのその後4月30日、2017年前に上記の回答を使用するには、Azureの上のテキスト翻訳のAPIを使用する新しい文書http://docs.microsofttranslator.com/text-translate.htmlに従う必要があります。しかし、AzureのText Translatorの新しいサービスは私のテストでは準備ができていないようです。だから私はちょうど参照として以下の簡単な手順を記載します。

  1. アズールのアクセストークンを取得するには、http://docs.microsofttranslator.com/oauth-token.htmlを参照してください。 <your-key>を取得するには、新しい文書のステップ12をGo to the Keys option and copy your subscription key to access the serviceに参照してください。

    //ヘッダを使用してパスキー カール--header 'OCP-APIM-スクリプションキー:' --data "" 'https://api.cognitive.microsoft.com/sts/v1.0/issueToken' //パスのキー使用してクエリ文字列パラメータ --dataカール "" ' https://api.cognitive.microsoft.com/sts/v1.0/issueToken?Subscription-Key= '

  2. HTTPインターフェイスを呼び出すには、http://docs.microsofttranslator.com/text-translate.html#!/default/get_Translateを参照してください。