-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
475 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2025 DeepL SE (https://www.deepl.com) | ||
// Use of this source code is governed by an MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
package com.deepl.api; | ||
|
||
import com.deepl.api.http.HttpResponse; | ||
import com.deepl.api.utils.KeyValuePair; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class DeepLClient extends Translator { | ||
|
||
/** {@inheritDoc} */ | ||
public DeepLClient(String authKey, TranslatorOptions options) throws IllegalArgumentException { | ||
super(authKey, options); | ||
} | ||
|
||
public WriteResult rephraseText( | ||
String text, @Nullable String targetLang, @Nullable TextRephraseOptions options) | ||
throws InterruptedException, DeepLException { | ||
ArrayList<String> texts = new ArrayList<>(); | ||
texts.add(text); | ||
return this.rephraseText(texts, targetLang, options).get(0); | ||
} | ||
|
||
public List<WriteResult> rephraseText( | ||
List<String> texts, @Nullable String targetLang, @Nullable TextRephraseOptions options) | ||
throws InterruptedException, DeepLException { | ||
Iterable<KeyValuePair<String, String>> params = | ||
createWriteHttpParams(texts, targetLang, options); | ||
HttpResponse response = httpClientWrapper.sendRequestWithBackoff("/v2/write/rephrase", params); | ||
checkResponse(response, false, false); | ||
return jsonParser.parseWriteResult(response.getBody()); | ||
} | ||
|
||
protected static ArrayList<KeyValuePair<String, String>> createWriteHttpParams( | ||
List<String> texts, @Nullable String targetLang, @Nullable TextRephraseOptions options) { | ||
targetLang = LanguageCode.standardize(targetLang); | ||
checkValidLanguages(null, targetLang); | ||
|
||
ArrayList<KeyValuePair<String, String>> params = new ArrayList<>(); | ||
if (targetLang != null) { | ||
params.add(new KeyValuePair<>("target_lang", targetLang)); | ||
} | ||
if (options != null && options.getWritingStyle() != null) { | ||
params.add(new KeyValuePair<>("writing_style", options.getWritingStyle())); | ||
} | ||
if (options != null && options.getTone() != null) { | ||
params.add(new KeyValuePair<>("tone", options.getTone())); | ||
} | ||
|
||
texts.forEach( | ||
(text) -> { | ||
if (text.isEmpty()) throw new IllegalArgumentException("text must not be empty"); | ||
params.add(new KeyValuePair<>("text", text)); | ||
}); | ||
|
||
return params; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
deepl-java/src/main/java/com/deepl/api/DeepLClientOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Copyright 2025 DeepL SE (https://www.deepl.com) | ||
// Use of this source code is governed by an MIT | ||
// license that can be found in the LICENSE file. | ||
package com.deepl.api; | ||
|
||
/** {@inheritDoc} */ | ||
public class DeepLClientOptions extends TranslatorOptions {} |
49 changes: 49 additions & 0 deletions
49
deepl-java/src/main/java/com/deepl/api/TextRephraseOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2025 DeepL SE (https://www.deepl.com) | ||
// Use of this source code is governed by an MIT | ||
// license that can be found in the LICENSE file. | ||
package com.deepl.api; | ||
|
||
/** | ||
* Options to control text rephrasing behaviour. These options may be provided to {@link | ||
* DeepLClient#rephraseText} overloads. | ||
* | ||
* <p>All properties have corresponding setters in fluent-style, so the following is possible: | ||
* <code> | ||
* TextRephraseOptions options = new TextRephraseOptions() | ||
* .WritingStyle(WritingStyle.Business.getValue()); | ||
* </code> | ||
*/ | ||
public class TextRephraseOptions { | ||
private String writingStyle; | ||
private String tone; | ||
|
||
/** | ||
* Sets a style the improved text should be in. Note that only style OR tone can be set. | ||
* | ||
* @see WritingStyle | ||
*/ | ||
public TextRephraseOptions setWritingStyle(String style) { | ||
this.writingStyle = style; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets a tone the improved text should be in. Note that only style OR tone can be set. | ||
* | ||
* @see WritingTone | ||
*/ | ||
public TextRephraseOptions setTone(String tone) { | ||
this.tone = tone; | ||
return this; | ||
} | ||
|
||
/** Gets the current style setting. */ | ||
public String getWritingStyle() { | ||
return writingStyle; | ||
} | ||
|
||
/** Gets the current tone setting. */ | ||
public String getTone() { | ||
return tone; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2025 DeepL SE (https://www.deepl.com) | ||
// Use of this source code is governed by an MIT | ||
// license that can be found in the LICENSE file. | ||
package com.deepl.api; | ||
|
||
/** The result of a text translation. */ | ||
public class WriteResult { | ||
private final String text; | ||
private final String detectedSourceLanguage; | ||
private final String targetLanguage; | ||
|
||
/** Constructs a new instance. */ | ||
public WriteResult(String text, String detectedSourceLanguage, String targetLanguage) { | ||
this.text = text; | ||
this.detectedSourceLanguage = LanguageCode.standardize(detectedSourceLanguage); | ||
this.targetLanguage = targetLanguage; | ||
} | ||
|
||
/** The translated text. */ | ||
public String getText() { | ||
return text; | ||
} | ||
|
||
/** The language code of the source text detected by DeepL. */ | ||
public String getDetectedSourceLanguage() { | ||
return detectedSourceLanguage; | ||
} | ||
|
||
/** The language code of the target language set by the request. */ | ||
public String getTargetLanguage() { | ||
return targetLanguage; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2025 DeepL SE (https://www.deepl.com) | ||
// Use of this source code is governed by an MIT | ||
// license that can be found in the LICENSE file. | ||
package com.deepl.api; | ||
|
||
/** Represents the style the improved text should be in in a rephrase request. */ | ||
public enum WritingStyle { | ||
Academic("academic"), | ||
Business("business"), | ||
Casual("casual"), | ||
Default("default"), | ||
PreferAcademic("prefer_academic"), | ||
PreferBusiness("prefer_business"), | ||
PreferCasual("prefer_casual"), | ||
PreferSimple("prefer_simple"), | ||
Simple("simple"); | ||
|
||
private final String value; | ||
|
||
WritingStyle(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2025 DeepL SE (https://www.deepl.com) | ||
// Use of this source code is governed by an MIT | ||
// license that can be found in the LICENSE file. | ||
package com.deepl.api; | ||
|
||
/** Represents the tone the improved text should be in in a rephrase request. */ | ||
public enum WritingTone { | ||
Confident("confident"), | ||
Default("default"), | ||
Diplomatic("diplomatic"), | ||
Enthusiastic("enthusiastic"), | ||
Friendly("friendly"), | ||
PreferConfident("prefer_confident"), | ||
PreferDiplomatic("prefer_diplomatic"), | ||
PreferEnthusiastic("prefer_enthusiastic"), | ||
PreferFriendly("prefer_friendly"); | ||
|
||
private final String value; | ||
|
||
WritingTone(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
deepl-java/src/main/java/com/deepl/api/parsing/WriteResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2025 DeepL SE (https://www.deepl.com) | ||
// Use of this source code is governed by an MIT | ||
// license that can be found in the LICENSE file. | ||
package com.deepl.api.parsing; | ||
|
||
import com.deepl.api.WriteResult; | ||
import java.util.List; | ||
|
||
/** | ||
* Class representing text rephrase responses from the DeepL API. | ||
* | ||
* <p>This class is internal; you should not use this class directly. | ||
*/ | ||
class WriteResponse { | ||
public List<WriteResult> improvements; | ||
} |
24 changes: 24 additions & 0 deletions
24
deepl-java/src/main/java/com/deepl/api/parsing/WriteResultDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2025 DeepL SE (https://www.deepl.com) | ||
// Use of this source code is governed by an MIT | ||
// license that can be found in the LICENSE file. | ||
package com.deepl.api.parsing; | ||
|
||
import com.deepl.api.WriteResult; | ||
import com.google.gson.*; | ||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* Utility class for deserializing text rephrase results returned by the DeepL API. | ||
* | ||
* <p>This class is internal; you should not use this class directly. | ||
*/ | ||
class WriteResultDeserializer implements JsonDeserializer<WriteResult> { | ||
public WriteResult deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) | ||
throws JsonParseException { | ||
JsonObject jsonObject = json.getAsJsonObject(); | ||
return new WriteResult( | ||
jsonObject.get("text").getAsString(), | ||
jsonObject.get("detected_source_language").getAsString(), | ||
jsonObject.get("target_language").getAsString()); | ||
} | ||
} |
Oops, something went wrong.