[translate] Don't translate white space
Some checks are pending
ci/woodpecker/push/jfmod Pipeline is pending
ci/woodpecker/push/docs Pipeline was successful
ci/woodpecker/tag/docs Pipeline was successful
ci/woodpecker/tag/jfmod Pipeline was successful

This commit is contained in:
Johannes Frohnmeyer 2023-02-15 19:49:08 +01:00
parent e588fa379b
commit 349d6587d6
Signed by: Johannes
GPG Key ID: E76429612C2929F4
3 changed files with 49 additions and 23 deletions

View File

@ -0,0 +1,27 @@
package io.gitlab.jfronny.libjf.translate.impl;
import io.gitlab.jfronny.libjf.translate.api.*;
import java.util.regex.Pattern;
public abstract class AbstractTranslateService<T extends Language> implements TranslateService<T> {
private static final Pattern WHITE_SPACE = Pattern.compile("\\s*");
@Override
public String translate(String textToTranslate, T translateFrom, T translateTo) throws TranslateException {
if (textToTranslate == null) throw new TranslateException("textToTranslate must not be null");
if (translateFrom == null) translateFrom = getAutoDetectLang();
if (translateTo == null) throw new TranslateException("translateTo must not be null");
if (WHITE_SPACE.matcher(textToTranslate).matches()) return textToTranslate;
try {
return performTranslate(textToTranslate, translateFrom, translateTo);
} catch (TranslateException e) {
throw e;
} catch (Throwable e) {
throw new TranslateException("Could not translate text", e);
}
}
protected abstract T getAutoDetectLang();
protected abstract String performTranslate(String textToTranslate, T translateFrom, T translateTo) throws Exception;
}

View File

@ -2,7 +2,7 @@ package io.gitlab.jfronny.libjf.translate.impl.google;
import io.gitlab.jfronny.commons.HttpUtils; import io.gitlab.jfronny.commons.HttpUtils;
import io.gitlab.jfronny.libjf.translate.api.TranslateException; import io.gitlab.jfronny.libjf.translate.api.TranslateException;
import io.gitlab.jfronny.libjf.translate.api.TranslateService; import io.gitlab.jfronny.libjf.translate.impl.AbstractTranslateService;
import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringEscapeUtils;
import java.io.IOException; import java.io.IOException;
@ -15,7 +15,7 @@ import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class GoogleTranslateService implements TranslateService<GoogleTranslateLanguage> { public class GoogleTranslateService extends AbstractTranslateService<GoogleTranslateLanguage> {
public static final String NAME = "Google"; public static final String NAME = "Google";
private static GoogleTranslateService INSTANCE; private static GoogleTranslateService INSTANCE;
private static final Pattern TRANSLATION_RESULT = Pattern.compile("class=\"result-container\">([^<]*)</div>", Pattern.MULTILINE); private static final Pattern TRANSLATION_RESULT = Pattern.compile("class=\"result-container\">([^<]*)</div>", Pattern.MULTILINE);
@ -43,10 +43,12 @@ public class GoogleTranslateService implements TranslateService<GoogleTranslateL
} }
@Override @Override
public String translate(String textToTranslate, GoogleTranslateLanguage translateFrom, GoogleTranslateLanguage translateTo) throws TranslateException { protected GoogleTranslateLanguage getAutoDetectLang() {
if (textToTranslate == null) throw new TranslateException("textToTranslate must not be null"); return GoogleTranslateLanguage.AUTO_DETECT;
if (translateFrom == null) translateFrom = GoogleTranslateLanguage.AUTO_DETECT; }
if (translateTo == null) throw new TranslateException("translateTo must not be null");
@Override
protected String performTranslate(String textToTranslate, GoogleTranslateLanguage translateFrom, GoogleTranslateLanguage translateTo) throws Exception {
String pageSource = ""; String pageSource = "";
try { try {
pageSource = getPageSource(textToTranslate, translateFrom.getIdentifier(), translateTo.getIdentifier()); pageSource = getPageSource(textToTranslate, translateFrom.getIdentifier(), translateTo.getIdentifier());
@ -91,7 +93,7 @@ public class GoogleTranslateService implements TranslateService<GoogleTranslateL
return NAME; return NAME;
} }
private static String getPageSource(String textToTranslate, String translateFrom, String translateTo) throws Exception { private static String getPageSource(String textToTranslate, String translateFrom, String translateTo) throws URISyntaxException, IOException {
if (textToTranslate == null) if (textToTranslate == null)
return null; return null;
String pageUrl = String.format("https://translate.google.com/m?hl=en&sl=%s&tl=%s&ie=UTF-8&prev=_m&q=%s", String pageUrl = String.format("https://translate.google.com/m?hl=en&sl=%s&tl=%s&ie=UTF-8&prev=_m&q=%s",

View File

@ -3,7 +3,7 @@ package io.gitlab.jfronny.libjf.translate.impl.libretranslate;
import io.gitlab.jfronny.commons.HttpUtils; import io.gitlab.jfronny.commons.HttpUtils;
import io.gitlab.jfronny.gson.reflect.TypeToken; import io.gitlab.jfronny.gson.reflect.TypeToken;
import io.gitlab.jfronny.libjf.translate.api.TranslateException; import io.gitlab.jfronny.libjf.translate.api.TranslateException;
import io.gitlab.jfronny.libjf.translate.api.TranslateService; import io.gitlab.jfronny.libjf.translate.impl.AbstractTranslateService;
import io.gitlab.jfronny.libjf.translate.impl.libretranslate.model.*; import io.gitlab.jfronny.libjf.translate.impl.libretranslate.model.*;
import java.io.IOException; import java.io.IOException;
@ -11,7 +11,7 @@ import java.lang.reflect.Type;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.*; import java.util.*;
public class LibreTranslateService implements TranslateService<LibreTranslateLanguage> { public class LibreTranslateService extends AbstractTranslateService<LibreTranslateLanguage> {
public static final String NAME = "LibreTranslate"; public static final String NAME = "LibreTranslate";
private static final Type languageListType = new TypeToken<List<LibreTranslateLanguage.ApiResult>>(){}.getType(); private static final Type languageListType = new TypeToken<List<LibreTranslateLanguage.ApiResult>>(){}.getType();
private static final Type translateDetectResultListType = new TypeToken<List<LibreTranslateDetectResult>>(){}.getType(); private static final Type translateDetectResultListType = new TypeToken<List<LibreTranslateDetectResult>>(){}.getType();
@ -56,20 +56,17 @@ public class LibreTranslateService implements TranslateService<LibreTranslateLan
} }
@Override @Override
public String translate(String textToTranslate, LibreTranslateLanguage translateFrom, LibreTranslateLanguage translateTo) throws TranslateException { protected LibreTranslateLanguage getAutoDetectLang() {
if (textToTranslate == null) throw new TranslateException("textToTranslate must not be null"); return autoDetect;
if (translateFrom == null) translateFrom = autoDetect; }
if (translateTo == null) throw new TranslateException("translateTo must not be null");
LibreTranslateResult result = null; @Override
try { protected String performTranslate(String textToTranslate, LibreTranslateLanguage translateFrom, LibreTranslateLanguage translateTo) throws Exception {
result = HttpUtils.post(host + "/translate").bodyForm(Map.of( LibreTranslateResult result = HttpUtils.post(host + "/translate").bodyForm(Map.of(
"q", textToTranslate, "q", textToTranslate,
"source", translateFrom.getIdentifier(), "source", translateFrom.getIdentifier(),
"target", translateTo.getIdentifier() "target", translateTo.getIdentifier()
)).sendSerialized(LibreTranslateResult.class); )).sendSerialized(LibreTranslateResult.class);
} catch (IOException | URISyntaxException e) {
throw new TranslateException("Could not translate text", e);
}
return result.translatedText; return result.translatedText;
} }