-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDeepLClient.java
62 lines (52 loc) · 2.27 KB
/
DeepLClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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;
}
}