Skip to content

Commit

Permalink
refactor Translation Response
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoepics committed Feb 2, 2024
1 parent 9f4965d commit b52c576
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 67 deletions.
5 changes: 3 additions & 2 deletions src/main/java/io/github/brenoepics/at4j/AzureApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.github.brenoepics.at4j.data.request.DetectLanguageParams;
import io.github.brenoepics.at4j.data.request.TranslateParams;
import io.github.brenoepics.at4j.data.response.TranslationResponse;
import io.github.brenoepics.at4j.data.response.TranslationResult;

import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -71,9 +72,9 @@ public interface AzureApi {
* Translates the given text from the given source language to the given target language.
*
* @param params The {@link TranslateParams} to translate.
* @return The {@link TranslationResponse} containing the translation.
* @return The {@link TranslationResult} containing the translation.
*/
CompletableFuture<Optional<List<TranslationResponse>>> translate(TranslateParams params);
CompletableFuture<Optional<TranslationResponse>> translate(TranslateParams params);

/**
* Gets the available languages for translation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.github.brenoepics.at4j.data.request.DetectLanguageParams;
import io.github.brenoepics.at4j.data.request.TranslateParams;
import io.github.brenoepics.at4j.data.response.TranslationResponse;
import io.github.brenoepics.at4j.data.response.TranslationResult;
import io.github.brenoepics.at4j.util.rest.RestEndpoint;
import io.github.brenoepics.at4j.util.rest.RestMethod;
import io.github.brenoepics.at4j.util.rest.RestRequest;
Expand Down Expand Up @@ -89,13 +90,13 @@ public ThreadPool getThreadPool() {
}

@Override
public CompletableFuture<Optional<List<TranslationResponse>>> translate(TranslateParams params) {
public CompletableFuture<Optional<TranslationResponse>> translate(TranslateParams params) {
if (params.getTexts() == null || params.getTexts().isEmpty()) {
return CompletableFuture.completedFuture(Optional.empty());
}

RestRequest<Optional<List<TranslationResponse>>> request =
new RestRequest<Optional<List<TranslationResponse>>>(
RestRequest<Optional<TranslationResponse>> request =
new RestRequest<Optional<TranslationResponse>>(
this, RestMethod.POST, RestEndpoint.TRANSLATE)
.setBody(params.getBody());
params.getQueryParameters().forEach(request::addQueryParameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.github.brenoepics.at4j.data.request.optional.ProfanityMarker;
import io.github.brenoepics.at4j.data.request.optional.TextType;
import io.github.brenoepics.at4j.data.response.TranslationResponse;
import io.github.brenoepics.at4j.data.response.TranslationResult;
import io.github.brenoepics.at4j.util.rest.RestRequestResult;

import java.util.*;
Expand Down Expand Up @@ -289,12 +290,12 @@ public JsonNode getBody() {
return body;
}

public Optional<List<TranslationResponse>> handleTranslations(
RestRequestResult<Optional<List<TranslationResponse>>> response) {
public Optional<TranslationResponse> handleTranslations(
RestRequestResult<Optional<TranslationResponse>> response) {
if (response.getJsonBody().isNull() || response.getJsonBody().isEmpty())
return Optional.empty();

List<TranslationResponse> responses = new ArrayList<>();
TranslationResponse responses = new TranslationResponse();
getTexts()
.forEach(
(index, baseText) -> {
Expand All @@ -308,15 +309,15 @@ public Optional<List<TranslationResponse>> handleTranslations(

if (jsonNode.has("detectedLanguage")) {
JsonNode detectedLanguage = jsonNode.get("detectedLanguage");
responses.add(
new TranslationResponse(
responses.addResult(
new TranslationResult(
baseText,
DetectedLanguage.ofJSON((ObjectNode) detectedLanguage),
translations));
return;
}

responses.add(new TranslationResponse(baseText, translations));
responses.addResult(new TranslationResult(baseText, translations));
});

return Optional.of(responses);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,76 +1,66 @@
package io.github.brenoepics.at4j.data.response;

import io.github.brenoepics.at4j.data.DetectedLanguage;
import io.github.brenoepics.at4j.data.Translation;

import java.util.Collection;
import java.util.ArrayList;
import java.util.List;

/**
* This class represents a response from a translation service. It contains the detected language of
* the input and a collection of translations.
* This class represents a response that contains a list of translation results.
*/
public class TranslationResponse {
// List to store the translation results
private final List<TranslationResult> resultList;

/**
* The detected language of the input text. It can be null if the language could not be detected.
*
* @see DetectedLanguage
* Default constructor that initializes an empty list of translation results.
*/
private DetectedLanguage detectedLanguage = null;

private final String baseText;

// A collection of translations for the input text.
private final Collection<Translation> translations;
public TranslationResponse() {
resultList = new ArrayList<>();
}

/**
* Constructs a TranslationResponse with a detected language and a collection of translations.
* Constructor that initializes the list of translation results with the provided list.
*
* @param detectedLanguage The detected language of the input text.
* @param translations A collection of translations for the input text.
* @param results The list of translation results to be stored.
*/
public TranslationResponse(
String baseText, DetectedLanguage detectedLanguage, Collection<Translation> translations) {
this.baseText = baseText;
this.detectedLanguage = detectedLanguage;
this.translations = translations;
public TranslationResponse(List<TranslationResult> results) {
resultList = results;
}

/**
* Constructs a TranslationResponse with a collection of translations. The detected language is
* set to null.
* Method to add a translation result to the list.
*
* @param translations A collection of translations for the input text.
* @param result The translation result to be added.
*/
public TranslationResponse(String baseText, Collection<Translation> translations) {
this.baseText = baseText;
this.translations = translations;
public void addResult(TranslationResult result) {
resultList.add(result);
}

/**
* Returns the detected language of the input text.
* Method to retrieve the list of translation results.
*
* @return The detected language of the input text.
* @return The list of translation results.
*/
public DetectedLanguage getDetectedLanguage() {
return detectedLanguage;
public List<TranslationResult> getResultList() {
return resultList;
}

/**
* Returns the collection of translations for the input text.
* Static factory method to create a new TranslationResponse with the provided list of results.
*
* @return The collection of translations for the input text.
* @param results The list of translation results to be stored.
* @return A new TranslationResponse instance.
*/
public Collection<Translation> getTranslations() {
return translations;
public static TranslationResponse of(List<TranslationResult> results) {
return new TranslationResponse(results);
}

/**
* Returns the base texts that were translated
* Static factory method to create a new TranslationResponse with the provided result.
*
* @return the base text
* @param result The translation result to be stored.
* @return A new TranslationResponse instance.
*/
public String getBaseText() {
return baseText;
public static TranslationResponse of(TranslationResult result) {
return new TranslationResponse(List.of(result));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.github.brenoepics.at4j.data.response;

import io.github.brenoepics.at4j.data.DetectedLanguage;
import io.github.brenoepics.at4j.data.Translation;

import java.util.Collection;

/**
* This class represents a response from a translation service. It contains the detected language of
* the input and a collection of translations.
*/
public class TranslationResult {

/**
* The detected language of the input text. It can be null if the language could not be detected.
*
* @see DetectedLanguage
*/
private DetectedLanguage detectedLanguage = null;

private final String baseText;

// A collection of translations for the input text.
private final Collection<Translation> translations;

/**
* Constructs a TranslationResponse with a detected language and a collection of translations.
*
* @param detectedLanguage The detected language of the input text.
* @param translations A collection of translations for the input text.
*/
public TranslationResult(
String baseText, DetectedLanguage detectedLanguage, Collection<Translation> translations) {
this.baseText = baseText;
this.detectedLanguage = detectedLanguage;
this.translations = translations;
}

/**
* Constructs a TranslationResponse with a collection of translations. The detected language is
* set to null.
*
* @param translations A collection of translations for the input text.
*/
public TranslationResult(String baseText, Collection<Translation> translations) {
this.baseText = baseText;
this.translations = translations;
}

/**
* Returns the detected language of the input text.
*
* @return The detected language of the input text.
*/
public DetectedLanguage getDetectedLanguage() {
return detectedLanguage;
}

/**
* Returns the collection of translations for the input text.
*
* @return The collection of translations for the input text.
*/
public Collection<Translation> getTranslations() {
return translations;
}

/**
* Returns the base texts that were translated
*
* @return the base text
*/
public String getBaseText() {
return baseText;
}
}
15 changes: 7 additions & 8 deletions src/test/java/io/github/brenoepics/at4j/AzureApiTest.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package io.github.brenoepics.at4j;

import static org.junit.jupiter.api.Assertions.*;

import io.github.brenoepics.at4j.azure.BaseURL;
import io.github.brenoepics.at4j.azure.lang.Language;
import io.github.brenoepics.at4j.data.DetectedLanguage;
import io.github.brenoepics.at4j.data.request.AvailableLanguagesParams;
import io.github.brenoepics.at4j.data.request.DetectLanguageParams;
import io.github.brenoepics.at4j.data.request.TranslateParams;
import io.github.brenoepics.at4j.data.response.TranslationResponse;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;

import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class AzureApiTest {
@Test
Expand Down Expand Up @@ -70,7 +69,7 @@ void translateEmptyKey() {
AzureApi api = new AzureApiBuilder().baseURL(BaseURL.GLOBAL).setKey("").region("test").build();

TranslateParams params = new TranslateParams("test", List.of("pt")).setSourceLanguage("en");
CompletableFuture<Optional<List<TranslationResponse>>> translation = api.translate(params);
CompletableFuture<Optional<TranslationResponse>> translation = api.translate(params);
assertThrows(CompletionException.class, translation::join);
api.disconnect();
}
Expand All @@ -88,9 +87,9 @@ void translateHelloWorld() {
AzureApi api = builder.build();

TranslateParams params = new TranslateParams("Hello World!", List.of("pt", "es"));
Optional<List<TranslationResponse>> translate = api.translate(params).join();
Optional<TranslationResponse> translate = api.translate(params).join();
assertTrue(translate.isPresent());
assertEquals(2, translate.get().get(0).getTranslations().size());
assertEquals(2, translate.get().getResultList().get(0).getTranslations().size());
}

@Test
Expand All @@ -107,10 +106,10 @@ void translateMultiText() {

TranslateParams params =
new TranslateParams(List.of("Hello World!", "How are you?"), List.of("pt", "es"));
Optional<List<TranslationResponse>> translate = api.translate(params).join();
Optional<TranslationResponse> translate = api.translate(params).join();
assertTrue(translate.isPresent());

assertEquals(2, translate.get().size());
assertEquals(2, translate.get().getResultList().size());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.github.brenoepics.at4j.core.exceptions.AzureException;
import io.github.brenoepics.at4j.data.request.TranslateParams;
import io.github.brenoepics.at4j.data.response.TranslationResponse;
import io.github.brenoepics.at4j.data.response.TranslationResult;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
Expand Down Expand Up @@ -40,7 +41,7 @@ void returnsEmptyOnInvalidInput() {
}
});

CompletableFuture<Optional<List<TranslationResponse>>> response =
CompletableFuture<Optional<TranslationResponse>> response =
azureApi.translate(translateParams);

assertFalse(response.join().isPresent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

class TranslationResponseTest {
class TranslationResultTest {

@Test
void createsTranslationResponseWithDetectedLanguageAndTranslations() {
DetectedLanguage detectedLanguage = new DetectedLanguage("en", 1.0f);
Translation translation = new Translation("pt", "Olá, mundo!");
TranslationResponse response =
new TranslationResponse(translation.getText(), detectedLanguage, List.of(translation));
TranslationResult response =
new TranslationResult(translation.getText(), detectedLanguage, List.of(translation));

assertEquals(detectedLanguage, response.getDetectedLanguage());
assertEquals(1, response.getTranslations().size());
Expand All @@ -27,8 +27,8 @@ void createsTranslationResponseWithDetectedLanguageAndTranslations() {
@Test
void createsTranslationResponseWithTranslationsOnly() {
Translation translation = new Translation("pt", "Olá, mundo!");
TranslationResponse response =
new TranslationResponse(translation.getText(), List.of(translation));
TranslationResult response =
new TranslationResult(translation.getText(), List.of(translation));

assertNull(response.getDetectedLanguage());
assertEquals(1, response.getTranslations().size());
Expand All @@ -37,7 +37,7 @@ void createsTranslationResponseWithTranslationsOnly() {

@Test
void returnsEmptyTranslationsWhenNoneProvided() {
TranslationResponse response = new TranslationResponse("", Collections.emptyList());
TranslationResult response = new TranslationResult("", Collections.emptyList());
assertEquals(0, response.getTranslations().size());
}
}

0 comments on commit b52c576

Please sign in to comment.