Created
December 25, 2014 17:33
-
-
Save FriedEgg/779c2199845dc8c5acd6 to your computer and use it in GitHub Desktop.
Utilize the Bing/Microsoft Translator API for Multi-Language Translating of text in SAS using PROC GROOVY
This file contains 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
/*Sign up for the Bing Translator, 2million characters/month of free translating!*/ | |
%let client_id=<<Your Client ID>>; | |
%let client_secret=<<Your Client Secret>>; | |
%let text=Merry Christmas; | |
%let from=en; /*English*/ | |
%let to=es; /*Spanish*/ | |
/*Link to language codes: http://msdn.microsoft.com/en-us/library/hh456380.aspx*/ | |
filename ivy "%sysfunc(pathname(work,l))/ivy.jar"; | |
proc http | |
method = "get" | |
url = "http://central.maven.org/maven2/org/apache/ivy/ivy/2.3.0-rc1/ivy-2.3.0-rc1.jar" | |
out = ivy; | |
run; | |
filename cp temp; | |
proc groovy classpath=cp; | |
add classpath=ivy; | |
add sasjar="groovy_2.1.3" version="2.1.3.0_SAS_20130517000930"; | |
submit parseonly; | |
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7') | |
import groovy.json.JsonSlurper | |
import groovyx.net.http.HTTPBuilder | |
import static groovyx.net.http.ContentType.JSON | |
import static groovyx.net.http.ContentType.XML | |
import static groovyx.net.http.Method.GET | |
import static groovyx.net.http.Method.POST | |
class TranslateApi { | |
def client_id | |
def client_secret | |
def scope = "http://api.microsofttranslator.com" | |
def grant_type = "client_credentials" | |
def translate(text, from, to){ | |
def token = getToken() | |
def http = new HTTPBuilder("http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + java.net.URLEncoder.encode(text) + "&from=" + from + "&to=" + to) | |
def translate = "" | |
http.request(GET, XML) { req -> | |
headers."Authorization" = "Bearer " + token | |
response.failure = { resp -> | |
println "Unexpected error:" | |
resp.headers.each { println "${it.name} : ${it.value}" } | |
} | |
response.success = { resp, xml -> | |
resp.headers.each { println "${it.name} : ${it.value}" } | |
translate = xml.toString() | |
} | |
} | |
return translate | |
} | |
private getToken(){ | |
def http = new HTTPBuilder("https://datamarket.accesscontrol.windows.net/v2/OAuth2-13") | |
def token | |
http.request(POST, JSON) { req -> | |
headers."Content-Type" = "application/x-www-form-urlencoded;charset=UTF-8" | |
body = "grant_type=${grant_type}&client_id=${client_id}&client_secret=${client_secret}&scope=${scope}" | |
response.failure = { resp, json -> | |
println "Unexpected error:" | |
resp.headers.each { println "---> ${it.name} : ${it.value}" } | |
println "---> " + json | |
} | |
response.success = { resp, json -> | |
token = json.access_token | |
} | |
} | |
return token | |
} | |
} | |
endsubmit; | |
submit "&client_id" "&client_secret" "&text" "&from" "&to"; | |
x = new TranslateApi() | |
x.client_id = args[0] | |
x.client_secret = args[1] | |
y = x.translate(args[2], args[3], args[4]) | |
println y | |
exports.translated_text = y | |
endsubmit; | |
quit; | |
%put &translated_text; | |
/*Feliz Navidad*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment