Skip to content

Instantly share code, notes, and snippets.

@KillerGoldFisch
Last active December 17, 2015 17:59
Show Gist options
  • Save KillerGoldFisch/5649707 to your computer and use it in GitHub Desktop.
Save KillerGoldFisch/5649707 to your computer and use it in GitHub Desktop.
This BeanShell script to Translate Text
/**************************************************
*
* This BeanShell script to Translate Text
*
* usage: bsh <this-file>.bsh "<Text to translate>" <source lang> <dest lang> <email>
*
* dependency:
* - http://mymemory.translated.net/
* - gson
*
* author:
* - Kevin Gliewe
*
***************************************************/
addClassPath("./gson-2.2.4.jar");
import com.google.gson.Gson;
import java.util.ArrayList;
import org.apache.commons.lang.StringEscapeUtils;
import java.io.*;
import java.net.*;
import java.util.regex.*;
/*
ooooo ooooo ooooooooooooo ooooooooooooo ooooooooo.
`888' `888' 8' 888 `8 8' 888 `8 `888 `Y88.
888 888 888 888 888 .d88'
888ooooo888 888 888 888ooo88P'
888 888 888 888 888
888 888 888 888 888
o888o o888o o888o o888o o888o
*/
public class HTTP {
public static String get(String urlToRead) {
URL url;
HttpURLConnection conn;
BufferedReader rd;
String line;
String result = "";
try {
url = new URL(urlToRead);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
result += line;
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
/*
ooooo ooo o8o .o8 oooooooooooo o8o
`888' `8' `"' "888 `888' `8 `"'
888 8 ooo. .oo. oooo .ooooo. .ooooo. .oooo888 .ooooo. 888 oooo oooo ooo
888 8 `888P"Y88b `888 d88' `"Y8 d88' `88b d88' `888 d88' `88b 888oooo8 `888 `88b..8P'
888 8 888 888 888 888 888 888 888 888 888ooo888 888 " 888 Y888'
`88. .8' 888 888 888 888 .o8 888 888 888 888 888 .o 888 888 .o8"'88b
`YbodP' o888o o888o o888o `Y8bod8P' `Y8bod8P' `Y8bod88P" `Y8bod8P' o888o o888o o88' 888o
*/
String UnicodeFix(String string) {
Pattern pattern = Pattern.compile("&#([\\d]+);");
Matcher matcher = pattern.matcher(string);
while(matcher.find())
{
final MatchResult matchResult = matcher.toMatchResult();
int ascii = Integer.parseInt(matchResult.group(1));
final String replacement = "" + (char)ascii;
string = string.substring(0, matchResult.start()) +
replacement + string.substring(matchResult.end());
matcher.reset(string);
}
return string;
}
/*
ooooooooooooo oooo . o8o
8' 888 `8 `888 .o8 `"'
888 oooo d8b .oooo. ooo. .oo. .oooo.o 888 .oooo. .o888oo oooo .ooooo. ooo. .oo.
888 `888""8P `P )88b `888P"Y88b d88( "8 888 `P )88b 888 `888 d88' `88b `888P"Y88b
888 888 .oP"888 888 888 `"Y88b. 888 .oP"888 888 888 888 888 888 888
888 888 d8( 888 888 888 o. )88b 888 d8( 888 888 . 888 888 888 888 888
o888o d888b `Y888""8o o888o o888o 8""888P' o888o `Y888""8o "888" o888o `Y8bod8P' o888o o888o
*/
public class ResponseData {
public String translatedText;
}
public class Match{
public String id;
public String segment;
public String translation;
public String quality;
public String reference;
public int usage_count;
public String subject;
public String created_by;
public String last_updated_by;
public String create_date;
public String last_update_date;
public double match;
}
public class Translation {
public ResponseData responseData;
public String responseDetails;
public int responseStatus;
public Match[] matches;
public static Translation translate(String Text, String from, String to, String email) {
ArrayList noTranslate = new ArrayList();
String string = Text;
Pattern pattern = Pattern.compile("<([^<>]*)>");
Matcher matcher = pattern.matcher(string);
while(matcher.find())
{
final MatchResult matchResult = matcher.toMatchResult();
noTranslate.add(matchResult.group(1));
final String replacement = "[" + (noTranslate.size() - 1).toString() + "]";
string = string.substring(0, matchResult.start()) +
replacement + string.substring(matchResult.end());
matcher.reset(string);
}
Text = string;
Gson gson = new Gson();
String query = "http://api.mymemory.translated.net/get?q=" + java.net.URLEncoder.encode(Text) +
"&langpair=" + from + "|" + to;
if(email != null)
query += "&de=" + email;
//print(query);
Translation translation = gson.fromJson(HTTP.get(query), Translation.class);
string = UnicodeFix(translation.responseData.translatedText);
pattern = Pattern.compile("\\[(\\d+)\\]");
Matcher matcher = pattern.matcher(string);
while(matcher.find())
{
final MatchResult matchResult = matcher.toMatchResult();
int index = Integer.parseInt(matchResult.group(1));
final String replacement = noTranslate.get(index);
string = string.substring(0, matchResult.start()) +
replacement + string.substring(matchResult.end());
matcher.reset(string);
}
translation.responseData.translatedText = string;
return translation;
}
}
//bsh.args
String text = "Hallo <" + System.getProperty("user.name") + ">, was geht?";
String from = "de";
String to = "en";
String email = null;
if(bsh.args.length > 0)
text = bsh.args[0];
if(bsh.args.length > 1)
from = bsh.args[1];
if(bsh.args.length > 2)
to = bsh.args[2];
if(bsh.args.length > 3)
email = bsh.args[3];
print("Text:" + text);
print("From:" + from);
print("To:" + to);
print("email:" + email);
print("Translation:" + Translation.translate(text, from, to, email).responseData.translatedText);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment