Skip to content

Commit 3b52172

Browse files
authored
Add support for profanity filter in translation
1 parent cd1e317 commit 3b52172

6 files changed

Lines changed: 188 additions & 5 deletions

File tree

examples/TextTranslation.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,35 @@ public static void main(String[] args) {
121121
return;
122122
}
123123

124-
// Example 7: Translation with reasoning
124+
// Example 7: Translation with profanity filter
125+
System.out.println("=== Translation with Profanity Filter ===");
126+
try {
127+
String profanityText = "Don't be such a tool.";
128+
TranslateOptions detectOptions = new TranslateOptions()
129+
.setProfanityFilter(ProfanityFilter.DETECT);
130+
TextResult detectResult = lara.translate(profanityText, "en-US", "it-IT", detectOptions);
131+
System.out.println("Original: " + profanityText);
132+
System.out.println("Detect mode: " + detectResult.getTranslation());
133+
if (detectResult.getProfanities() != null) {
134+
System.out.println("Masked text: " + detectResult.getProfanities().getMaskedText());
135+
System.out.println("Profanities found: " + detectResult.getProfanities().getProfanities().size());
136+
}
137+
138+
TranslateOptions hideOptions = new TranslateOptions()
139+
.setProfanityFilter(ProfanityFilter.HIDE);
140+
TextResult hideResult = lara.translate(profanityText, "en-US", "it-IT", hideOptions);
141+
System.out.println("Hide mode: " + hideResult.getTranslation());
142+
143+
TranslateOptions avoidOptions = new TranslateOptions()
144+
.setProfanityFilter(ProfanityFilter.AVOID);
145+
TextResult avoidResult = lara.translate(profanityText, "en-US", "it-IT", avoidOptions);
146+
System.out.println("Avoid mode: " + avoidResult.getTranslation() + "\n");
147+
} catch (LaraException e) {
148+
System.out.println("Error with profanity filter: " + e.getMessage() + "\n");
149+
return;
150+
}
151+
152+
// Example 8: Translation with reasoning
125153
System.out.println("=== Translation with Reasoning ===");
126154
try {
127155
TranslateOptions reasoningOptions = new TranslateOptions()
@@ -142,7 +170,7 @@ public static void main(String[] args) {
142170
return;
143171
}
144172

145-
// Example 8: Get available languages
173+
// Example 9: Get available languages
146174
System.out.println("=== Available Languages ===");
147175
try {
148176
List<String> languages = lara.getLanguages();
@@ -152,7 +180,7 @@ public static void main(String[] args) {
152180
return;
153181
}
154182

155-
// Example 9: Detect language of a given text
183+
// Example 10: Detect language of a given text
156184
System.out.println("=== Language Detection ===");
157185
try {
158186
DetectResult detectResult = lara.detect("Hola, ¿cómo estás?");
@@ -163,7 +191,7 @@ public static void main(String[] args) {
163191
return;
164192
}
165193

166-
// Example 10: Detect languages with hint and passlist
194+
// Example 11: Detect languages with hint and passlist
167195
System.out.println("=== Language Detection with Hint and Passlist ===");
168196
try {
169197
DetectResult detectResult = lara.detect("Hola, ¿cómo estás?", "es", new String[]{"es", "pt", "it"});
@@ -174,7 +202,7 @@ public static void main(String[] args) {
174202
return;
175203
}
176204

177-
// Example 10: Translation with reasoning
205+
// Example 12: Translation with reasoning (streaming)
178206
System.out.println("=== Translation with Reasoning ===");
179207
try {
180208
TranslateOptions reasoningOptions = new TranslateOptions()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.translated.lara.translator;
2+
3+
public class DetectedProfanity {
4+
private final String text;
5+
private final int startCharIndex;
6+
private final int endCharIndex;
7+
private final double score;
8+
9+
public DetectedProfanity(String text, int startCharIndex, int endCharIndex, double score) {
10+
this.text = text;
11+
this.startCharIndex = startCharIndex;
12+
this.endCharIndex = endCharIndex;
13+
this.score = score;
14+
}
15+
16+
public String getText() {
17+
return text;
18+
}
19+
20+
public int getStartCharIndex() {
21+
return startCharIndex;
22+
}
23+
24+
public int getEndCharIndex() {
25+
return endCharIndex;
26+
}
27+
28+
public double getScore() {
29+
return score;
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return "DetectedProfanity{text='" + text + "', startCharIndex=" + startCharIndex +
35+
", endCharIndex=" + endCharIndex + ", score=" + score + "}";
36+
}
37+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.translated.lara.translator;
2+
3+
import java.util.List;
4+
5+
public class ProfanityDetectResult {
6+
private final String maskedText;
7+
private final List<DetectedProfanity> profanities;
8+
9+
public ProfanityDetectResult(String maskedText, List<DetectedProfanity> profanities) {
10+
this.maskedText = maskedText;
11+
this.profanities = profanities;
12+
}
13+
14+
public String getMaskedText() {
15+
return maskedText;
16+
}
17+
18+
public List<DetectedProfanity> getProfanities() {
19+
return profanities;
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return "ProfanityDetectResult{maskedText='" + maskedText + "', profanities=" + profanities + "}";
25+
}
26+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.translated.lara.translator;
2+
3+
public enum ProfanityFilter {
4+
5+
DETECT("detect"),
6+
AVOID("avoid"),
7+
HIDE("hide");
8+
9+
private final String value;
10+
11+
ProfanityFilter(String value) {
12+
this.value = value;
13+
}
14+
15+
@Override
16+
public String toString() {
17+
return value;
18+
}
19+
20+
public static String toString(ProfanityFilter filter) {
21+
return filter != null ? filter.toString() : null;
22+
}
23+
}

src/main/java/com/translated/lara/translator/TextResult.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,52 @@ public GlossariesMatchesValue deserialize(JsonElement json, Type typeOfT, JsonDe
229229
}
230230
}
231231

232+
@JsonAdapter(ProfanitiesValueDeserializer.class)
233+
public static class ProfanitiesValue {
234+
private final ProfanityDetectResult single;
235+
private final List<ProfanityDetectResult> list;
236+
237+
public ProfanitiesValue(ProfanityDetectResult single) {
238+
this.single = single;
239+
this.list = null;
240+
}
241+
242+
public ProfanitiesValue(List<ProfanityDetectResult> list) {
243+
this.single = null;
244+
this.list = list;
245+
}
246+
247+
public ProfanityDetectResult getSingle() {
248+
return single;
249+
}
250+
251+
public List<ProfanityDetectResult> getList() {
252+
return list;
253+
}
254+
255+
@Override
256+
public String toString() {
257+
if (single != null) return single.toString();
258+
if (list != null) return list.toString();
259+
return null;
260+
}
261+
}
262+
263+
public static class ProfanitiesValueDeserializer implements JsonDeserializer<ProfanitiesValue> {
264+
@Override
265+
public ProfanitiesValue deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
266+
if (json.isJsonObject()) {
267+
ProfanityDetectResult single = context.deserialize(json, ProfanityDetectResult.class);
268+
return new ProfanitiesValue(single);
269+
} else if (json.isJsonArray()) {
270+
Type listType = new TypeToken<List<ProfanityDetectResult>>(){}.getType();
271+
List<ProfanityDetectResult> list = context.deserialize(json, listType);
272+
return new ProfanitiesValue(list);
273+
}
274+
throw new JsonParseException("Expected JSON object or array for profanities");
275+
}
276+
}
277+
232278
private final String contentType;
233279
private final String sourceLanguage;
234280
private final List<String> adaptedTo;
@@ -237,6 +283,7 @@ public GlossariesMatchesValue deserialize(JsonElement json, Type typeOfT, JsonDe
237283
private final Value translation;
238284
private final AdaptedToMatchesValue adaptedToMatches;
239285
private final GlossariesMatchesValue glossariesMatches;
286+
private final ProfanitiesValue profanities;
240287

241288
public TextResult(String contentType, String sourceLanguage, String translation, String[] adaptedTo, String[] glossaries) {
242289
this.contentType = contentType;
@@ -247,6 +294,7 @@ public TextResult(String contentType, String sourceLanguage, String translation,
247294
this.translation = new Value(translation);
248295
this.adaptedToMatches = null;
249296
this.glossariesMatches = null;
297+
this.profanities = null;
250298
}
251299

252300
public TextResult(String contentType, String sourceLanguage, String[] translation, String[] adaptedTo, String[] glossaries) {
@@ -258,6 +306,7 @@ public TextResult(String contentType, String sourceLanguage, String[] translatio
258306
this.translation = new Value(translation);
259307
this.adaptedToMatches = null;
260308
this.glossariesMatches = null;
309+
this.profanities = null;
261310
}
262311

263312
public TextResult(String contentType, String sourceLanguage, TextBlock[] translation, String[] adaptedTo, String[] glossaries) {
@@ -269,6 +318,7 @@ public TextResult(String contentType, String sourceLanguage, TextBlock[] transla
269318
this.translation = new Value(translation);
270319
this.adaptedToMatches = null;
271320
this.glossariesMatches = null;
321+
this.profanities = null;
272322
}
273323

274324

@@ -316,6 +366,14 @@ public List<List<NGGlossaryMatch>> getGlossariesMatchesList() {
316366
return glossariesMatches != null ? glossariesMatches.getMatchesList() : null;
317367
}
318368

369+
public ProfanityDetectResult getProfanities() {
370+
return profanities != null ? profanities.getSingle() : null;
371+
}
372+
373+
public List<ProfanityDetectResult> getProfanitiesList() {
374+
return profanities != null ? profanities.getList() : null;
375+
}
376+
319377
@Override
320378
public String toString() {
321379
return this.translation.toString();

src/main/java/com/translated/lara/translator/TranslateOptions.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public enum UseCache {
3131
private Boolean reasoning = null;
3232
private Map<String, String> headers = null;
3333
private Object metadata = null;
34+
private ProfanityFilter profanityFilter = null;
3435

3536
public String getSourceHint() {
3637
return sourceHint;
@@ -196,6 +197,15 @@ public TranslateOptions setReasoning(boolean reasoning) {
196197
return this;
197198
}
198199

200+
public ProfanityFilter getProfanityFilter() {
201+
return profanityFilter;
202+
}
203+
204+
public TranslateOptions setProfanityFilter(ProfanityFilter profanityFilter) {
205+
this.profanityFilter = profanityFilter;
206+
return this;
207+
}
208+
199209
public HttpParams<Object> toParams() {
200210
HttpParams<Object> params = new HttpParams<>();
201211
params.set("source_hint", sourceHint);
@@ -212,6 +222,7 @@ public HttpParams<Object> toParams() {
212222
params.set("style", TranslationStyle.toString(style));
213223
params.set("reasoning", reasoning);
214224
params.set("metadata", metadata);
225+
params.set("profanity_filter", ProfanityFilter.toString(profanityFilter));
215226

216227
return params;
217228
}

0 commit comments

Comments
 (0)