Skip to content

Commit 81b2672

Browse files
authored
Feature: detect
1 parent f8b5c19 commit 81b2672

4 files changed

Lines changed: 95 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export LARA_ACCESS_KEY_SECRET="your-access-key-secret"
9292
- Auto-detect source language
9393
- Advanced translation options
9494
- Get available languages
95+
- Detect language
9596

9697
```bash
9798
cd examples

examples/TextTranslation.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,27 @@ public static void main(String[] args) {
129129
System.out.println("Error getting languages: " + e.getMessage());
130130
return;
131131
}
132+
133+
// Example 8: Detect language of a given text
134+
System.out.println("=== Language Detection ===");
135+
try {
136+
DetectResult detectResult = lara.detect("Hola, ¿cómo estás?");
137+
System.out.println("Text: Hola, ¿cómo estás?");
138+
System.out.println("Detected Language: " + detectResult.getLanguage());
139+
} catch (LaraException e) {
140+
System.out.println("Error detecting language: " + e.getMessage());
141+
return;
142+
}
143+
144+
// Example 9: Detect languages with hint and passlist
145+
System.out.println("=== Language Detection with Hint and Passlist ===");
146+
try {
147+
DetectResult detectResult = lara.detect("Hola, ¿cómo estás?", "es", {"es", "pt", "it"});
148+
System.out.println("Text: Hola, ¿cómo estás?");
149+
System.out.println("Detected Language: " + detectResult.getLanguage());
150+
} catch (LaraException e) {
151+
System.out.println("Error detecting language with hint/passlist: " + e.getMessage());
152+
return;
153+
}
132154
}
133155
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.translated.lara.translator;
2+
3+
public class DetectResult {
4+
private final String language;
5+
private final String contentType;
6+
7+
public DetectResult(String language, String contentType) {
8+
this.language = language;
9+
this.contentType = contentType;
10+
}
11+
12+
public String getLanguage() {
13+
return language;
14+
}
15+
16+
public String getContentType() {
17+
return contentType;
18+
}
19+
}

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

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
import com.translated.lara.net.HttpParams;
77
import com.translated.lara.net.LaraClient;
88

9-
import java.util.HashMap;
10-
import java.util.List;
11-
import java.util.Map;
9+
import java.util.*;
1210

1311
public class Translator {
1412

@@ -32,6 +30,58 @@ public List<String> getLanguages() throws LaraException {
3230
return client.get("/languages").asList(String.class);
3331
}
3432

33+
public DetectResult detect(String text) throws LaraException {
34+
return detectAny(text, null, null);
35+
}
36+
public DetectResult detect(String text, String hint) throws LaraException {
37+
return detectAny(text, hint, null);
38+
}
39+
public DetectResult detect(String text, String hint, List<String> passlist) throws LaraException {
40+
return detectAny(text, hint, passlist);
41+
}
42+
public DetectResult detect(String text, String hint, String[] passlist) throws LaraException {
43+
return detectAny(text, hint, Arrays.asList(passlist));
44+
}
45+
46+
public DetectResult detect(String[] text) throws LaraException {
47+
return detectAny(text, null, null);
48+
}
49+
public DetectResult detect(String[] text, String hint) throws LaraException {
50+
return detectAny(text, hint, null);
51+
}
52+
public DetectResult detect(String[] text, String hint, List<String> passlist) throws LaraException {
53+
return detectAny(text, hint, passlist);
54+
}
55+
public DetectResult detect(String[] text, String hint, String[] passlist) throws LaraException {
56+
return detectAny(text, hint, Arrays.asList(passlist));
57+
}
58+
59+
public DetectResult detect(List<String> text) throws LaraException {
60+
return detectAny(text, null, null);
61+
}
62+
public DetectResult detect(List<String> text, String hint) throws LaraException {
63+
return detectAny(text, hint, null);
64+
}
65+
public DetectResult detect(List<String> text, String hint, List<String> passlist) throws LaraException {
66+
return detectAny(text, hint, passlist);
67+
}
68+
public DetectResult detect(List<String> text, String hint, String[] passlist) throws LaraException {
69+
return detectAny(text, hint, Arrays.asList(passlist));
70+
}
71+
72+
public DetectResult detectAny(Object text, String hint, Collection<String> passlist) throws LaraException {
73+
HttpParams<Object> params = new HttpParams<>();
74+
params.set("q", text);
75+
if (hint != null) {
76+
params.set("hint", hint);
77+
}
78+
if (passlist != null && !passlist.isEmpty()) {
79+
params.set("passlist", passlist);
80+
}
81+
82+
return client.post("/detect", params.build()).as(DetectResult.class);
83+
}
84+
3585
public TextResult translate(String text, String source, String target) throws LaraException {
3686
return translateAny(text, source, target, null);
3787
}

0 commit comments

Comments
 (0)