Skip to content

Commit 619173b

Browse files
committed
Add Translate.detect methods and tests
1 parent 4c43dc8 commit 619173b

4 files changed

Lines changed: 224 additions & 6 deletions

File tree

gcloud-java-translate/src/main/java/com/google/cloud/translate/Translate.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,46 @@ public static LanguageListOption targetLanguage(String targetLanguage) {
7070
* }</pre>
7171
*/
7272
List<Language> listSupportedLanguages(LanguageListOption... options);
73+
74+
/**
75+
* Detects the language of the provided texts.
76+
*
77+
* <p>Example of detecting the language of some texts:
78+
* <pre> {@code
79+
* List<String> texts = new LinkedList<>();
80+
* texts.add("Hello, World!");
81+
* texts.add("¡Hola Mundo!");
82+
* List<Detection> detections = translate.detect(texts);
83+
* }</pre>
84+
*
85+
* @param texts the texts for which language should be detected
86+
* @return a list of objects containing information on the language detection, one for each
87+
* provided text, in order.
88+
*/
89+
List<Detection> detect(List<String> texts);
90+
91+
/**
92+
* Detects the language of the provided texts.
93+
*
94+
* <p>Example of detecting the language of some texts:
95+
* <pre> {@code
96+
* List<Detection> detections = translate.detect("Hello, World!", "¡Hola Mundo!");
97+
* }</pre>
98+
*
99+
* @param texts the texts for which language should be detected
100+
* @return a list of objects containing information on the language detection, one for each
101+
* provided text, in order.
102+
*/
103+
List<Detection> detect(String... texts);
104+
105+
/**
106+
* Detects the language of the provided text. Returns an object containing information on the
107+
* language detection.
108+
*
109+
* <p>Example of detecting the language a text:
110+
* <pre> {@code
111+
* Detection detection = translate.detect("Hello, World!");
112+
* }</pre>
113+
*/
114+
Detection detect(String text);
73115
}

gcloud-java-translate/src/main/java/com/google/cloud/translate/TranslateImpl.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,20 @@
1818

1919
import static com.google.cloud.RetryHelper.runWithRetries;
2020
import static com.google.common.base.Preconditions.checkArgument;
21+
import static com.google.common.base.Preconditions.checkState;
2122

23+
import com.google.api.services.translate.model.DetectionsResourceItems;
2224
import com.google.api.services.translate.model.LanguagesResource;
2325
import com.google.cloud.BaseService;
2426
import com.google.cloud.RetryHelper.RetryHelperException;
2527
import com.google.cloud.translate.spi.TranslateRpc;
28+
import com.google.common.base.Function;
2629
import com.google.common.collect.Lists;
2730
import com.google.common.collect.Maps;
2831

32+
import java.util.Arrays;
33+
import java.util.Collections;
34+
import java.util.Iterator;
2935
import java.util.List;
3036
import java.util.Map;
3137
import java.util.concurrent.Callable;
@@ -34,6 +40,14 @@ final class TranslateImpl extends BaseService<TranslateOptions> implements Trans
3440

3541
private final TranslateRpc translateRpc;
3642

43+
private static final Function<List<DetectionsResourceItems>, Detection>
44+
DETECTION_FROM_PB_FUNCTION = new Function<List<DetectionsResourceItems>, Detection>() {
45+
@Override
46+
public Detection apply(List<DetectionsResourceItems> detectionPb) {
47+
return Detection.fromPb(detectionPb.get(0));
48+
}
49+
};
50+
3751
TranslateImpl(TranslateOptions options) {
3852
super(options);
3953
translateRpc = options.rpc();
@@ -53,6 +67,41 @@ public List<LanguagesResource> call() {
5367
}
5468
}
5569

70+
@Override
71+
public List<Detection> detect(final List<String> texts) {
72+
try {
73+
List<List<DetectionsResourceItems>> detectionsPb =
74+
runWithRetries(new Callable<List<List<DetectionsResourceItems>>>() {
75+
@Override
76+
public List<List<DetectionsResourceItems>> call() {
77+
return translateRpc.detect(texts);
78+
}
79+
}, options().retryParams(), EXCEPTION_HANDLER, options().clock());
80+
Iterator<List<DetectionsResourceItems>> detectionIterator = detectionsPb.iterator();
81+
Iterator<String> textIterator = texts.iterator();
82+
while (detectionIterator.hasNext() && textIterator.hasNext()) {
83+
List<DetectionsResourceItems> detectionPb = detectionIterator.next();
84+
String text = textIterator.next();
85+
checkState(detectionPb != null && !detectionPb.isEmpty(),
86+
"No detection found for text: %s", text);
87+
checkState(detectionPb.size() == 1, "Multiple detections found for text: %s", text);
88+
}
89+
return Lists.transform(detectionsPb, DETECTION_FROM_PB_FUNCTION);
90+
} catch (RetryHelperException e) {
91+
throw TranslateException.translateAndThrow(e);
92+
}
93+
}
94+
95+
@Override
96+
public List<Detection> detect(String... texts) {
97+
return detect(Arrays.asList(texts));
98+
}
99+
100+
@Override
101+
public Detection detect(String text) {
102+
return detect(Collections.singletonList(text)).get(0);
103+
}
104+
56105
private Map<TranslateRpc.Option, ?> optionMap(Option... options) {
57106
Map<TranslateRpc.Option, Object> optionMap = Maps.newEnumMap(TranslateRpc.Option.class);
58107
for (Option option : options) {

gcloud-java-translate/src/main/java/com/google/cloud/translate/spi/DefaultTranslateRpc.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ private static TranslateException translate(IOException exception) {
5757
}
5858

5959
@Override
60-
public List<List<DetectionsResourceItems>> detect(List<String> text) {
60+
public List<List<DetectionsResourceItems>> detect(List<String> texts) {
6161
try {
6262
List<List<DetectionsResourceItems>> detections =
63-
translate.detections().list(text).setKey(options.apiKey()).execute().getDetections();
63+
translate.detections().list(texts).setKey(options.apiKey()).execute().getDetections();
6464
return detections != null ? detections : ImmutableList.<List<DetectionsResourceItems>>of();
6565
} catch (IOException ex) {
6666
throw translate(ex);
@@ -82,14 +82,14 @@ public List<LanguagesResource> listSupportedLanguages(Map<Option, ?> optionMap)
8282
}
8383

8484
@Override
85-
public List<TranslationsResource> translate(List<String> text, Map<Option, ?> optionMap) {
85+
public List<TranslationsResource> translate(List<String> texts, Map<Option, ?> optionMap) {
8686
try {
8787
String targetLanguage =
8888
firstNonNull(TARGET_LANGUAGE.getString(optionMap), options.targetLanguage());
8989
final String sourceLanguage = SOURCE_LANGUAGE.getString(optionMap);
9090
List<TranslationsResource> translations =
9191
translate.translations()
92-
.list(text, targetLanguage)
92+
.list(texts, targetLanguage)
9393
.setSource(sourceLanguage)
9494
.setKey(options.apiKey())
9595
.execute()

gcloud-java-translate/src/test/java/com/google/cloud/translate/TranslateImplTest.java

Lines changed: 129 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertSame;
2121

22+
import com.google.api.services.translate.model.DetectionsResourceItems;
2223
import com.google.api.services.translate.model.LanguagesResource;
2324
import com.google.cloud.RetryParams;
2425
import com.google.cloud.translate.Translate.LanguageListOption;
@@ -56,6 +57,12 @@ public class TranslateImplTest {
5657
private static final Language LANGUAGE4 = Language.fromPb(LANGUAGE4_PB);
5758
private static final List<Language> LANGUAGES1 = ImmutableList.of(LANGUAGE1, LANGUAGE2);
5859
private static final List<Language> LANGUAGES2 = ImmutableList.of(LANGUAGE3, LANGUAGE4);
60+
private static final DetectionsResourceItems DETECTION1_PB =
61+
new DetectionsResourceItems().setLanguage("en").setConfidence(0.9F);
62+
private static final DetectionsResourceItems DETECTION2_PB =
63+
new DetectionsResourceItems().setLanguage("en").setConfidence(0.8F);
64+
private static final Detection DETECTION1 = Detection.fromPb(DETECTION1_PB);
65+
private static final Detection DETECTION2 = Detection.fromPb(DETECTION2_PB);
5966

6067
// Empty TranslateRpc options
6168
private static final Map<TranslateRpc.Option, ?> EMPTY_RPC_OPTIONS = ImmutableMap.of();
@@ -104,7 +111,7 @@ public void testGetOptions() {
104111
}
105112

106113
@Test
107-
public void testListSupporteLanguages() {
114+
public void testListSupportedLanguages() {
108115
EasyMock.expect(translateRpcMock.listSupportedLanguages(EMPTY_RPC_OPTIONS))
109116
.andReturn(ImmutableList.of(LANGUAGE1_PB, LANGUAGE2_PB));
110117
EasyMock.replay(translateRpcMock);
@@ -113,7 +120,7 @@ public void testListSupporteLanguages() {
113120
}
114121

115122
@Test
116-
public void testListSupporteLanguagesWithOptions() {
123+
public void testListSupportedLanguagesWithOptions() {
117124
EasyMock.expect(translateRpcMock.listSupportedLanguages(LANGUAGE_LIST_OPTIONS))
118125
.andReturn(ImmutableList.of(LANGUAGE3_PB, LANGUAGE4_PB));
119126
EasyMock.replay(translateRpcMock);
@@ -122,6 +129,126 @@ public void testListSupporteLanguagesWithOptions() {
122129
LanguageListOption.targetLanguage(TARGET_LANGUAGE)));
123130
}
124131

132+
@Test
133+
public void testDetect() {
134+
String text = "text";
135+
EasyMock.expect(translateRpcMock.detect(ImmutableList.of(text)))
136+
.andReturn(
137+
ImmutableList.<List<DetectionsResourceItems>>of(ImmutableList.of(DETECTION1_PB)));
138+
EasyMock.replay(translateRpcMock);
139+
initializeService();
140+
assertEquals(DETECTION1, translate.detect(text));
141+
}
142+
143+
@Test
144+
public void testDetectMultipleDetections() {
145+
String text = "text";
146+
EasyMock.expect(translateRpcMock.detect(ImmutableList.of(text)))
147+
.andReturn(ImmutableList.<List<DetectionsResourceItems>>of(
148+
ImmutableList.of(DETECTION1_PB, DETECTION2_PB)));
149+
EasyMock.replay(translateRpcMock);
150+
initializeService();
151+
thrown.expect(IllegalStateException.class);
152+
thrown.expectMessage("Multiple detections found for text: text");
153+
translate.detect(text);
154+
}
155+
156+
@Test
157+
public void testDetectNoDetection() {
158+
String text = "text";
159+
EasyMock.expect(translateRpcMock.detect(ImmutableList.of(text)))
160+
.andReturn(ImmutableList.<List<DetectionsResourceItems>>of(
161+
ImmutableList.<DetectionsResourceItems>of()));
162+
EasyMock.replay(translateRpcMock);
163+
initializeService();
164+
thrown.expect(IllegalStateException.class);
165+
thrown.expectMessage("No detection found for text: text");
166+
translate.detect(text);
167+
}
168+
169+
@Test
170+
public void testDetectList() {
171+
String text1 = "text";
172+
String text2 = "other text";
173+
List<String> texts = ImmutableList.of(text1, text2);
174+
EasyMock.expect(translateRpcMock.detect(texts))
175+
.andReturn(ImmutableList.<List<DetectionsResourceItems>>of(
176+
ImmutableList.of(DETECTION1_PB), ImmutableList.of(DETECTION2_PB)));
177+
EasyMock.replay(translateRpcMock);
178+
initializeService();
179+
assertEquals(ImmutableList.of(DETECTION1, DETECTION2), translate.detect(texts));
180+
}
181+
182+
@Test
183+
public void testDetectListMultipleDetections() {
184+
String text1 = "text";
185+
String text2 = "other text";
186+
List<String> texts = ImmutableList.of(text1, text2);
187+
EasyMock.expect(translateRpcMock.detect(texts))
188+
.andReturn(ImmutableList.<List<DetectionsResourceItems>>of(
189+
ImmutableList.of(DETECTION1_PB, DETECTION2_PB), ImmutableList.of(DETECTION1_PB)));
190+
EasyMock.replay(translateRpcMock);
191+
initializeService();
192+
thrown.expect(IllegalStateException.class);
193+
thrown.expectMessage("Multiple detections found for text: text");
194+
translate.detect(texts);
195+
}
196+
197+
@Test
198+
public void testDetectListNoDetection() {
199+
String text1 = "text";
200+
String text2 = "other text";
201+
List<String> texts = ImmutableList.of(text1, text2);
202+
EasyMock.expect(translateRpcMock.detect(texts))
203+
.andReturn(ImmutableList.<List<DetectionsResourceItems>>of(
204+
ImmutableList.of(DETECTION1_PB), ImmutableList.<DetectionsResourceItems>of()));
205+
EasyMock.replay(translateRpcMock);
206+
initializeService();
207+
thrown.expect(IllegalStateException.class);
208+
thrown.expectMessage("No detection found for text: other text");
209+
translate.detect(texts);
210+
}
211+
212+
@Test
213+
public void testDetectVararg() {
214+
String text1 = "text";
215+
String text2 = "other text";
216+
EasyMock.expect(translateRpcMock.detect(ImmutableList.of(text1, text2)))
217+
.andReturn(ImmutableList.<List<DetectionsResourceItems>>of(
218+
ImmutableList.of(DETECTION1_PB), ImmutableList.of(DETECTION2_PB)));
219+
EasyMock.replay(translateRpcMock);
220+
initializeService();
221+
assertEquals(ImmutableList.of(DETECTION1, DETECTION2), translate.detect(text1, text2));
222+
}
223+
224+
@Test
225+
public void testDetectVarargMultipleDetections() {
226+
String text1 = "text";
227+
String text2 = "other text";
228+
EasyMock.expect(translateRpcMock.detect(ImmutableList.of(text1, text2)))
229+
.andReturn(ImmutableList.<List<DetectionsResourceItems>>of(
230+
ImmutableList.of(DETECTION1_PB, DETECTION2_PB), ImmutableList.of(DETECTION1_PB)));
231+
EasyMock.replay(translateRpcMock);
232+
initializeService();
233+
thrown.expect(IllegalStateException.class);
234+
thrown.expectMessage("Multiple detections found for text: text");
235+
translate.detect(text1, text2);
236+
}
237+
238+
@Test
239+
public void testDetectVarargNoDetection() {
240+
String text1 = "text";
241+
String text2 = "other text";
242+
EasyMock.expect(translateRpcMock.detect(ImmutableList.of(text1, text2)))
243+
.andReturn(ImmutableList.<List<DetectionsResourceItems>>of(
244+
ImmutableList.of(DETECTION1_PB), ImmutableList.<DetectionsResourceItems>of()));
245+
EasyMock.replay(translateRpcMock);
246+
initializeService();
247+
thrown.expect(IllegalStateException.class);
248+
thrown.expectMessage("No detection found for text: other text");
249+
translate.detect(text1, text2);
250+
}
251+
125252
@Test
126253
public void testRetryableException() {
127254
EasyMock.expect(translateRpcMock.listSupportedLanguages(EMPTY_RPC_OPTIONS))

0 commit comments

Comments
 (0)