Git Product home page Git Product logo

deepltranslator's Introduction

DeepLTranslator

This project is a simple DeepL Translator API written in Java. It translates via the DeepL website sentences. This works without having a premium access and can be used free of charge.

Prerequisites

  • ChromeDriver installed and located in PATH

Install

Maven

Add the JitPack repository in your pom.xml

<repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
</repository>

Add the dependency

<dependency>
    <groupId>com.github.Linus789</groupId>
    <artifactId>DeepLTranslator</artifactId>
    <version>2.1.0</version>
</dependency>

Gradle

Add the JitPack repository in your root build.gradle at the end of repositories

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Add the dependency

dependencies {
    implementation 'com.github.Linus789:DeepLTranslator:2.1.0'
}

Examples

Using a configuration

DeepLConfiguration deepLConfiguration = new DeepLConfiguration.Builder()
        .setTimeout(Duration.ofSeconds(10))
        .setRepetitions(3)
        .setRepetitionsDelay(retryNumber -> Duration.ofMillis(3000 + 5000 * retryNumber))
        .setPostProcessing(false)
        .build();

DeepLTranslator deepLTranslator = new DeepLTranslator(deepLConfiguration);

Synchronous translating

try {
    String translation = deepLTranslator.translate("I ran into a similar problem yesterday.", SourceLanguage.ENGLISH, TargetLanguage.GERMAN);
    System.out.println(translation);
} catch (Exception e) {
    e.printStackTrace();
}

Asynchronous translating

deepLTranslator.translateAsync("Detected cow running backwards.", SourceLanguage.ENGLISH, TargetLanguage.GERMAN)
        .whenComplete((res, ex) -> {
            if (ex != null) {
                ex.printStackTrace();
            } else {
                System.out.println(res);
            }
        });

Await termination

Blocks until all async translations from one DeepLTranslator instance have completed execution, or the timeout occurs, or the current thread is interrupted, whichever happens first.

try {
    deepLTranslator.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
} catch (InterruptedException e) {
    e.printStackTrace();
}

Shutdown

Stops all running threads

DeepLTranslator.shutdown();

Example

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details

deepltranslator's People

Contributors

linus789 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

deepltranslator's Issues

Why is the translation post-processed?

When translating text, I noticed that line breaks are removed. This messes up my text since I want to keep all line breaks. I debugged the library and found this piece of code:

/**
 * Returns the translation, or null if the translation is pending or something went wrong.
 */
private String readResult(JBrowserDriver driver) {
	WebElement webElement = driver.findElementByClassName("lmt__translations_as_text__text_btn");

	if(webElement == null)
		return null;

	String text = webElement.getAttribute("textContent");

	if(text == null)
		return null;

	return text
			.trim()
			.replaceAll("(?:\r?\n)+", " ")
			.replaceAll("\\s{2,}", " ");
}

Is there a particular reason for doing the post-processing with trim() and replaceAll()? Unfortunately it seems like the library does not offer a way to disable this functionality. Removing the post-processing in general or adding an option to disable it would be much appreciated.

Use headless Chrome instead of JBrowserDriver

I had major slowness and stability problems with JBrowserDriver used by this library. With headless Chrome things ran perfectly. Maybe migrate this project to headless Chrome using Selenium as dependency. After all, JBrowserDriver isn't maintained actively anymore and arguably Chrome is far superior anyway:

// Supply the chrome driver path
setProperty("webdriver.chrome.driver", chromeDriverFilePath.toString());

// Configure the Chrome instance as headless
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");

// Start the chrome driver
WebDriver driver = new ChromeDriver(options);

Potentially related to this issue: #6

org.openqa.selenium.StaleElementReferenceException: The element no longer exists within the page.

Sometimes when translating I get the following exception:

org.openqa.selenium.StaleElementReferenceException: The element no longer exists within the page.
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '4.0.0-alpha-2', revision: 'f148142cf8', time: '2019-07-01T20:55:26'
System info: host: 'DESKTOP-F8H276P', ip: '192.168.2.155', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_271'
Driver info: driver.version: unknown
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at com.machinepublishers.jbrowserdriver.Util.handleException(Util.java:128)
	at com.machinepublishers.jbrowserdriver.Element.getAttribute(Element.java:184)
	at de.linus.deepltranslator.DeepLTranslatorBase.readResult(DeepLTranslatorBase.java:136)
	at de.linus.deepltranslator.DeepLTranslatorBase.getTranslation(DeepLTranslatorBase.java:106)
	at de.linus.deepltranslator.DeepLTranslator.translate(DeepLTranslator.java:50)
	at de.linus.deepltranslator.DeepLTranslator.translate(DeepLTranslator.java:42)

This causes an abort of the translation without retries. I'm not sure why it happens, maybe this can be handled better?

private String translate(String text, Language from, Language to, int repeated) throws Exception {
	try {
		return getTranslation(text, from, to);
	} catch (DeepLException e) {
		if(repeat(++repeated)) {
			Thread.sleep(getConfiguration().getRepetitionsDelay().apply(repeated).toMillis());
			return translate(text, from, to, repeated);
		}

		throw e;
	}
}

Error Launch browser

18:51:16 main Main ERROR Error Translate " No Settings class was found" to Language de:
org.openqa.selenium.WebDriverException: Could not launch browser.
Build info: version: '4.0.0-alpha-2', revision: 'f148142cf8', time: '2019-07-01T20:55:26'
System info: host: 'ADMIN', ip: '169.254.127.214', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '11.0.7'
Driver info: driver.version: JBrowserDriver
at com.machinepublishers.jbrowserdriver.Util.handleException(Util.java:136)
at com.machinepublishers.jbrowserdriver.JBrowserDriver.(JBrowserDriver.java:340)
at de.linus.deepltranslator.DeepLTranslatorBase.getTranslation(DeepLTranslatorBase.java:87)
at de.linus.deepltranslator.DeepLTranslator.translate(DeepLTranslator.java:50)
at de.linus.deepltranslator.DeepLTranslator.translate(DeepLTranslator.java:42)
at xyz.custy.apis.messageapi.MessageAPI.lambda$copyDefaults$0(MessageAPI.java:59)
at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:684)
at xyz.custy.apis.messageapi.MessageAPI.copyDefaults(MessageAPI.java:53)
at xyz.custy.apis.messageapi.MessageAPI.loadAndReload(MessageAPI.java:37)
at xyz.custy.apis.messageapi.MessageAPI.(MessageAPI.java:20)
at xyz.custy.Main.(Main.java:147)
at xyz.custy.Main.main(Main.java:222)
Caused by: java.lang.IllegalStateException: Could not launch browser.
... 11 more

Getting Exception in thread "Thread-5" org.openqa.selenium.WebDriverException: NULL_OUTPUT_STREAM

When I try to run below code

`public class Translate1 {

public static void main(String[] args) {
	DeepLConfiguration deepLConfiguration = new DeepLConfiguration.Builder()
			.setTimeout(Duration.ofSeconds(10))
			.setRepetitions(3)
			.setRepetitionsDelay(retryNumber -> Duration.ofMillis(3000 + 5000 * retryNumber))
			.setPostProcessing(true)
			.build();

	DeepLTranslator deepLTranslator = new DeepLTranslator(deepLConfiguration);

	try {
		String translation = deepLTranslator.translate("I ran into a similar problem yesterday.", Language.AUTO_DETECT, Language.GERMAN);
		System.out.println(translation);
	} catch (Exception e) {
		e.printStackTrace();
	}
}

}`

getting below error:
Exception in thread "Thread-5" org.openqa.selenium.WebDriverException: NULL_OUTPUT_STREAM
Build info: version: '4.0.0-alpha-2', revision: 'f148142cf8', time: '2019-07-01T20:55:26'
System info: host: 'DESKTOP-C0OB8V0', ip: '192.168.225.52', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_275'
Driver info: driver.version: JBrowserDriver
at com.machinepublishers.jbrowserdriver.Util.handleException(Util.java:136)
at com.machinepublishers.jbrowserdriver.JBrowserDriver.lambda$launchProcess$2(JBrowserDriver.java:475)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoSuchFieldError: NULL_OUTPUT_STREAM
at org.zeroturnaround.exec.ProcessExecutor.redirectOutput(ProcessExecutor.java:394)
at org.zeroturnaround.exec.ProcessExecutor.(ProcessExecutor.java:149)
at com.machinepublishers.jbrowserdriver.JBrowserDriver.lambda$launchProcess$2(JBrowserDriver.java:427)
... 1 more

Should I follow or install anything to run the deepl Translator?

Using Futures instead of custom callbacks

Java 8 introduced Futures and CompletableFutures. These allow for a Task to either return a value or finish with an exception. Making translateAsync return a completable Future would give the library a standard interface and not require custom implementations.

If wanted i can go ahead and make a pull request for it

Cannot wait for asynchronous translation(s) to finish

Even though the library provides translateAsync(), there doesn't seem to be a way to wait for all asynchronous translations to finish: The ExecutorService is not accessible: static final ExecutorService EXECUTOR = Executors.newCachedThreadPool();
Also, there is no method to wait for all tasks to be finished.

I implemented my own asynchronous wrapper and I found that translations are actually slowed down by multi-threading instead of sped up. I wonder what's up with that? Maybe multi-threaded support isn't properly implemented in the library either? Due to the usage of the WebDriver there should be a way of making translations much faster with multiple instances.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.