Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1606 Add Deepl translator module - second set of corrections #1

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions translator/deepl-translate/src/main/kotlin/DeeplClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import ai.tock.shared.jackson.mapper
import ai.tock.shared.property
import ai.tock.shared.propertyOrNull
import com.fasterxml.jackson.module.kotlin.readValue
import java.io.IOException
import java.util.regex.Pattern
import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.IOException
import java.util.regex.Pattern

internal data class TranslationResponse(
val translations: List<Translation>
Expand All @@ -50,8 +50,12 @@ interface DeeplClient {
class OkHttpDeeplClient(
private val apiURL: String = property("tock_translator_deepl_api_url", "https://api.deepl.com/v2/translate"),
private val apiKey: String? = propertyOrNull("tock_translator_deepl_api_key"),
okHttpCustomizer: OkHttpClient.Builder.() -> Unit = {}
) : DeeplClient {
private val client = OkHttpClient.Builder().apply(TockProxyAuthenticator::install).build()
private val client = OkHttpClient.Builder()
.apply(TockProxyAuthenticator::install)
.apply(okHttpCustomizer)
.build()

private fun replaceSpecificPlaceholders(text: String): Pair<String, List<String>> {
// Store original placeholders for later restoration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import com.github.salomonbrys.kodein.Kodein
import com.github.salomonbrys.kodein.bind
import com.github.salomonbrys.kodein.provider

fun deeplTranslatorModule(client: DeeplClient = OkHttpDeeplClient()) = Kodein.Module {
/**
* The default Deepl translator module, for use in a Kodein injector.
*/
val deeplTranslatorModule = configureDeeplTranslatorModule()

fun configureDeeplTranslatorModule(client: DeeplClient = OkHttpDeeplClient()) = Kodein.Module {
bind<TranslatorEngine>(overrides = true) with provider { DeeplTranslatorEngine(client) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@

package ai.tock.translator.deepl

import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import java.util.Locale
import kotlin.test.assertEquals
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test

/**
* All these tests are disabled because it uses Deepl pro api that can be expensive
*/
class DeeplTranslateIntegrationTest {
private val deeplTranslatorEngine = DeeplTranslatorEngine(OkHttpDeeplClient())

@Test
@Disabled
fun simpleTest() {
val result = DeeplTranslatorEngine(OkHttpDeeplClient()).translate(
val result = deeplTranslatorEngine.translate(
"Bonjour, je voudrais me rendre à New-York Mardi prochain",
Locale.FRENCH,
Locale.ENGLISH
Expand All @@ -39,7 +41,7 @@ class DeeplTranslateIntegrationTest {
@Test
@Disabled
fun testWithEmoticonAndAntislash() {
val result = DeeplTranslatorEngine(OkHttpDeeplClient()).translate(
val result = deeplTranslatorEngine.translate(
"Bonjour, je suis l'Agent virtuel SNCF Voyageurs! \uD83E\uDD16\n" +
"Je vous informe sur l'état du trafic en temps réel.\n" +
"Dites-moi par exemple \"Mon train 6111 est-il à l'heure ?\", \"Aller à Saint-Lazare\", \"Prochains départs Gare de Lyon\" ...",
Expand All @@ -58,7 +60,7 @@ class DeeplTranslateIntegrationTest {
@Test
@Disabled
fun testWithParameters() {
val result = DeeplTranslatorEngine(OkHttpDeeplClient()).translate(
val result = deeplTranslatorEngine.translate(
"Bonjour, je voudrais me rendre à {:city} {:date}",
Locale.FRENCH,
Locale.GERMAN
Expand All @@ -69,11 +71,11 @@ class DeeplTranslateIntegrationTest {
@Test
@Disabled
fun testWithHTML() {
val result = DeeplTranslatorEngine(OkHttpDeeplClient()).translate(
val result = deeplTranslatorEngine.translate(
"Bonjour, je voudrais me rendre à Paris <br><br/> demain soir",
Locale.FRENCH,
Locale.GERMAN
)
assertEquals("Hallo, ich möchte morgen Abend nach Paris <br><br/> fahren", result)
}
}
}