/** * Translate using Microsoft Translate API * @param sourceLanguageCode Source language code, for example, "en" * @param targetLanguageCode Target language code, for example, "es" * @param sourceText Text to send for translation * @return Translated text */ static String translate(String sourceLanguageCode, String targetLanguageCode, String sourceText) { Translate.setClientId(CLIENT_ID); Translate.setClientSecret(CLIENT_SECRET); try { Log.d(TAG, sourceLanguageCode + " -> " + targetLanguageCode); return Translate.execute(sourceText, Language.fromString(sourceLanguageCode), Language.fromString(targetLanguageCode)); } catch (Exception e) { Log.e(TAG, "Caught exeption in translation request."); e.printStackTrace(); return Translator.BAD_TRANSLATION_MSG; } }
/** * Translates text from a given Language to another given Language using Microsoft Translator. * * Default the from to AUTO_DETECT * * @param text The String to translate. * @param to The language code to translate to. * @return The translated String. * @throws Exception on error. */ public static String execute(final String text, final Language to) throws Exception { return execute(text,Language.AUTO_DETECT,to); }
public MicrosoftParaphraser(String clientId, String clientSecret) { languages.add(Language.FRENCH); languages.add(Language.GERMAN); languages.add(Language.SPANISH); Translate.setClientId(clientId); Translate.setClientSecret(clientSecret); }
/** * Translates an array of texts from a given Language to another given Language using Microsoft Translator's TranslateArray * service * * Note that the Microsoft Translator expects all source texts to be of the SAME language. * * @param texts The Strings Array to translate. * @param from The language code to translate from. * @param to The language code to translate to. * @return The translated Strings Array[]. * @throws Exception on error. */ public static String[] execute(final String[] texts, final Language from, final Language to) throws Exception { //Run the basic service validations first validateServiceState(texts); final String params = (apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "") + PARAM_FROM_LANG + URLEncoder.encode(from.toString(),ENCODING) + PARAM_TO_LANG + URLEncoder.encode(to.toString(),ENCODING) + PARAM_TEXT_ARRAY + URLEncoder.encode(buildStringArrayParam(texts),ENCODING); final URL url = new URL(ARRAY_SERVICE_URL + params); final String[] response = retrieveStringArr(url,ARRAY_JSON_OBJECT_PROPERTY); return response; }
/** * Translates text from a given Language to another given Language using Microsoft Translator. * * @param text The String to translate. * @param from The language code to translate from. * @param to The language code to translate to. * @return The translated String. * @throws Exception on error. */ public static String execute(final String text, final Language from, final Language to) throws Exception { //Run the basic service validations first validateServiceState(text); final String params = (apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "") + PARAM_FROM_LANG + URLEncoder.encode(from.toString(),ENCODING) + PARAM_TO_LANG + URLEncoder.encode(to.toString(),ENCODING) + PARAM_TEXT_SINGLE + URLEncoder.encode(text,ENCODING); final URL url = new URL(SERVICE_URL + params); final String response = retrieveString(url); return response; }
private static void validateServiceState(final String[] texts) throws Exception { int length = 0; for(String text : texts) { length+=text.getBytes(ENCODING).length; } if(length>10240) { throw new RuntimeException("TEXT_TOO_LARGE - Microsoft Translator (Translate) can handle up to 10,240 bytes per request"); } validateServiceState(); }
/** * Use the Microsoft service to translate the given text to the given target language. The source language * is automatically detected by Microsoft. You must set the client keys in translator.microsoft.properties. * @param text The text to translate. * @param targetLanguage The desired language to translate to (for example, "hi"). * @return The translated text. If translation is unavailable, returns the unchanged text. * @throws Exception * @see org.apache.tika.language.translate.Translator * @since Tika 1.6 */ public String translate(String text, String targetLanguage) throws TikaException, IOException { if (!available) return text; Language target = Language.fromString(targetLanguage); Translate.setClientId(clientId); Translate.setClientSecret(clientSecret); try { return Translate.execute(text, target); } catch (Exception e) { throw new TikaException("Error with Microsoft Translation: " + e.getMessage()); } }
/** * Translates text from a given Language to another given Language using Microsoft Translator. * * Default the from to AUTO_DETECT * * @param text The String to translate. * @param to The language code to translate to. * @return The translated String. * @throws Exception on error. */ public static String execute(final String text, final Language to) throws Exception { return execute(text,Language.AUTO_DETECT,to); }
/** * Translates an array of texts from a given Language to another given Language using Microsoft Translator's TranslateArray * service * * Note that the Microsoft Translator expects all source texts to be of the SAME language. * * @param texts The Strings Array to translate. * @param from The language code to translate from. * @param to The language code to translate to. * @return The translated Strings Array[]. * @throws Exception on error. */ public static String[] execute(final String[] texts, final Language from, final Language to) throws Exception { //Run the basic service validations first validateServiceState(texts); final String params = (apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "") + PARAM_FROM_LANG + URLEncoder.encode(from.toString(),ENCODING) + PARAM_TO_LANG + URLEncoder.encode(to.toString(),ENCODING) + PARAM_TEXT_ARRAY + URLEncoder.encode(buildStringArrayParam(texts),ENCODING); final URL url = new URL(ARRAY_SERVICE_URL + params); final String[] response = retrieveStringArr(url,ARRAY_JSON_OBJECT_PROPERTY); return response; }
/** * Translates text from a given Language to another given Language using Microsoft Translator. * * @param text The String to translate. * @param from The language code to translate from. * @param to The language code to translate to. * @return The translated String. * @throws Exception on error. */ public static String execute(final String text, final Language from, final Language to) throws Exception { //Run the basic service validations first validateServiceState(text); final String params = (apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "") + PARAM_FROM_LANG + URLEncoder.encode(from.toString(),ENCODING) + PARAM_TO_LANG + URLEncoder.encode(to.toString(),ENCODING) + PARAM_TEXT_SINGLE + URLEncoder.encode(text,ENCODING); final URL url = new URL(SERVICE_URL + params); final String response = retrieveString(url); return response; }
private static void validateServiceState(final String[] texts) throws Exception { int length = 0; for(String text : texts) { length+=text.getBytes(ENCODING).length; } if(length>10240) { throw new RuntimeException("TEXT_TOO_LARGE - Microsoft Translator (Translate) can handle up to 10,240 bytes per request"); } validateServiceState(); }
/** * Use the Microsoft service to translate the given text from the given source language to the given target. * You must set the client keys in translator.microsoft.properties. * * @param text The text to translate. * @param sourceLanguage The input text language (for example, "en"). * @param targetLanguage The desired language to translate to (for example, "fr"). * @return The translated text. If translation is unavailable, returns the unchanged text. * @throws Exception * @see org.apache.tika.language.translate.Translator * @since Tika 1.6 */ public String translate(String text, String sourceLanguage, String targetLanguage) throws TikaException, IOException { if (!available) return text; Language source = Language.fromString(sourceLanguage); Language target = Language.fromString(targetLanguage); Translate.setClientId(clientId); Translate.setClientSecret(clientSecret); try { return Translate.execute(text, source, target); } catch (Exception e) { throw new TikaException("Error with Microsoft Translation: " + e.getMessage()); } }
/** * Translates an array of texts from an Automatically detected language to another given Language using Microsoft Translator's TranslateArray * service * * Note that the Microsoft Translator expects all source texts to be of the SAME language. * * This is an overloaded convenience method that passes Language.AUTO_DETECT as fromLang to * execute(texts[],fromLang,toLang) * * @param texts The Strings Array to translate. * @param from The language code to translate from. * @param to The language code to translate to. * @return The translated Strings Array[]. * @throws Exception on error. */ public static String[] execute(final String[] texts, final Language to) throws Exception { return execute(texts,Language.AUTO_DETECT,to); }
private static void validateServiceState(final String text) throws Exception { final int byteLength = text.getBytes(ENCODING).length; if(byteLength>10240) { throw new RuntimeException("TEXT_TOO_LARGE - Microsoft Translator (Translate) can handle up to 10,240 bytes per request"); } validateServiceState(); }
/** * Use the Microsoft service to translate the given text to the given target language. The source language * is automatically detected by Microsoft. You must set the client keys in translator.microsoft.properties. * @param text The text to translate. * @param targetLanguage The desired language to translate to (for example, "hi"). * @return The translated text. If translation is unavailable, returns the unchanged text. * @throws Exception * @see org.apache.tika.language.translate.Translator * @since Tika 1.6 */ public String translate(String text, String targetLanguage) throws TikaException, IOException { if (!available) return text; Language target = Language.fromString(targetLanguage); Translate.setClientId(clientId); Translate.setClientSecret(clientSecret); try { return Translate.execute(text, target); } catch (Exception e) { throw new TikaException("Error with Microsoft Translation: " + e.getMessage()); } }
/** * Translates an array of texts from an Automatically detected language to another given Language using Microsoft Translator's TranslateArray * service * * Note that the Microsoft Translator expects all source texts to be of the SAME language. * * This is an overloaded convenience method that passes Language.AUTO_DETECT as fromLang to * execute(texts[],fromLang,toLang) * * @param texts The Strings Array to translate. * @param from The language code to translate from. * @param to The language code to translate to. * @return The translated Strings Array[]. * @throws Exception on error. */ public static String[] execute(final String[] texts, final Language to) throws Exception { return execute(texts,Language.AUTO_DETECT,to); }
private static void validateServiceState(final String text) throws Exception { final int byteLength = text.getBytes(ENCODING).length; if(byteLength>10240) { throw new RuntimeException("TEXT_TOO_LARGE - Microsoft Translator (Translate) can handle up to 10,240 bytes per request"); } validateServiceState(); }
/** * Use the Microsoft service to translate the given text from the given source language to the given target. * You must set the client keys in translator.microsoft.properties. * * @param text The text to translate. * @param sourceLanguage The input text language (for example, "en"). * @param targetLanguage The desired language to translate to (for example, "fr"). * @return The translated text. If translation is unavailable, returns the unchanged text. * @throws Exception * @see org.apache.tika.language.translate.Translator * @since Tika 1.6 */ public String translate(String text, String sourceLanguage, String targetLanguage) throws TikaException, IOException { if (!available) return text; Language source = Language.fromString(sourceLanguage); Language target = Language.fromString(targetLanguage); Translate.setClientId(clientId); Translate.setClientSecret(clientSecret); try { return Translate.execute(text, source, target); } catch (Exception e) { throw new TikaException("Error with Microsoft Translation: " + e.getMessage()); } }
@Override public List<String> getParaphrases(String question) { List<String> result = new ArrayList<String>(); try { for (Language l : languages) { result.add(Translate.execute(Translate.execute(question, Language.ENGLISH, l), l, Language.ENGLISH)); } } catch (Exception e) { e.printStackTrace(); } return result; }