Last active
April 15, 2017 10:37
-
-
Save kazuhito-m/de716a2f8c8b804b154c7be8569d7c55 to your computer and use it in GitHub Desktop.
Microsoft Translator APIの新と旧
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.net.ssl.HttpsURLConnection | |
import java.net.URLEncoder | |
def getToken(key) { | |
def authenticationUrl = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken" | |
def conn = new URL(authenticationUrl).openConnection() | |
conn.setRequestMethod("POST") | |
conn.setDoOutput(true) | |
conn.setRequestProperty("Ocp-Apim-Subscription-Key", key) | |
conn.getOutputStream().write(0) | |
return conn.getInputStream().text | |
} | |
def translate(target, from, to, token) { | |
def appId = URLEncoder.encode("Bearer ${token}", "UTF-8") | |
def text = URLEncoder.encode(target, "UTF-8") | |
def translatorTextApiUrl = "https://api.microsofttranslator.com/v2/http.svc/Translate?appid=${appId}&text=${text}&from=${from}&to=${to}" | |
def conn = new URL(translatorTextApiUrl).openConnection() | |
conn.setRequestMethod("GET") | |
conn.setRequestProperty("Accept", "application/xml") | |
def withTag = conn.getInputStream().text | |
return withTag.replaceAll("<.+?>", ""); | |
} | |
// 実行部 | |
def SUBSCRIPTION_KEY = "[ご自身の取得されたサブスクリプションキーを設定]" | |
def from = "ja" | |
def to = "en" | |
def target = '日本語から英語へと訳すことは可能ですか?' | |
def token = getToken(SUBSCRIPTION_KEY) | |
def result = translate(target, from, to, token) | |
println "翻訳結果 : ${result}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab('org.codehaus.groovy.modules.http-builder:http-builder') | |
import groovyx.net.http.RESTClient | |
import static groovyx.net.http.ContentType.* | |
def getAccessToken() { | |
def http = new RESTClient('https://datamarket.accesscontrol.windows.net') | |
http.post( | |
path: '/v2/OAuth2-13', | |
body: [ | |
client_id: '[ご自身のID]', | |
client_secret: '[ご自身のなんかハッシュ]', | |
scope: 'http://api.microsofttranslator.com', | |
grant_type: 'client_credentials' | |
], | |
contentType: JSON | |
) { response,reader -> | |
if (response.status == 200) { | |
return reader.access_token | |
} else { | |
println "Unexpected failure: ${response.statusLine}" | |
} | |
} | |
} | |
def translateJpToEn(token,target){ | |
def http = new RESTClient( 'http://api.microsofttranslator.com' ) | |
http.get( | |
path : '/V2/Ajax.svc/Translate', | |
query : [ | |
appId: "Bearer ${token}", | |
text: target, from: 'ja', to: 'en' | |
], | |
contentType : TEXT | |
) { response, reader -> | |
if (response.status == 200) { | |
return reader.text; | |
} else { | |
println "Unexpected failure: ${resp.statusLine}" | |
} | |
} | |
} | |
def target = '日本語から英語へと訳すことは可能ですか?' | |
def token = getAccessToken() | |
def result = translateJpToEn(token, target) | |
println "翻訳結果 : ${result}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
記事にしたのもあって、あまり意味は薄いのですが…。
「構文的に何かあとで見返す資産に成るかな?」と思って残します。
https://kazuhito-m.github.io/tech/2017/04/15/microsoft-traslate-201704