Skip to content

Commit

Permalink
Merge pull request Azure#8 from samvaity/text-analytics
Browse files Browse the repository at this point in the history
Text analytics detect api testing
  • Loading branch information
mssfang authored Dec 6, 2019
2 parents 150b623 + fef3b0f commit 9f87de1
Show file tree
Hide file tree
Showing 11 changed files with 379 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -81,6 +82,7 @@ public Mono<Response<DetectLanguageResult>> detectLanguageWithResponse(String te

@ServiceMethod(returns = ReturnType.SINGLE)
Mono<Response<DetectLanguageResult>> detectLanguageWithResponse(String text, String countryHint, Context context) {
Objects.requireNonNull(text, "'text' cannot be null.");
List<DetectLanguageInput> languageInputs = new ArrayList<>();
languageInputs.add(new DetectLanguageInput(Integer.toString(0), text, countryHint));
// TODO: should this be a random number generator?
Expand Down Expand Up @@ -145,6 +147,7 @@ public Mono<Response<DocumentResultCollection<DetectLanguageResult>>> detectBatc

Mono<Response<DocumentResultCollection<DetectLanguageResult>>> detectBatchLanguagesWithResponse(
List<DetectLanguageInput> inputs, TextAnalyticsRequestOptions options, Context context) {
Objects.requireNonNull(inputs, "'inputs' cannot be null.");
// TODO: validate inputs?
final LanguageBatchInput languageBatchInput = new LanguageBatchInput().setDocuments(inputs);
// TODO: confirm if options null is fine?
Expand Down Expand Up @@ -173,8 +176,7 @@ private List<DetectLanguageResult> getDocumentLanguages(final LanguageResult lan
}

private DetectLanguageResult convertToErrorDetectLanguageResult(final DocumentError errorDocument) {
return new DetectLanguageResult(errorDocument.getId(), null,
errorDocument.getError(),
return new DetectLanguageResult(errorDocument.getId(), null, errorDocument.getError(),
null, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.azure.core.annotation.ServiceInterface;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.annotation.UnexpectedResponseExceptionType;
import com.azure.core.exception.HttpResponseException;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpPipelineBuilder;
import com.azure.core.http.policy.CookiePolicy;
Expand All @@ -24,7 +25,6 @@
import com.azure.core.util.Context;
import com.azure.cs.textanalytics.implementation.models.EntitiesResult;
import com.azure.cs.textanalytics.implementation.models.EntityLinkingResult;
import com.azure.cs.textanalytics.implementation.models.ErrorException;
import com.azure.cs.textanalytics.implementation.models.KeyPhraseResult;
import com.azure.cs.textanalytics.implementation.models.LanguageBatchInput;
import com.azure.cs.textanalytics.implementation.models.LanguageResult;
Expand Down Expand Up @@ -105,32 +105,32 @@ public TextAnalyticsClientImpl(HttpPipeline httpPipeline) {
private interface TextAnalyticsClientService {
@Post("entities/recognition/general")
@ExpectedResponses({200})
@UnexpectedResponseExceptionType(ErrorException.class)
@UnexpectedResponseExceptionType(HttpResponseException.class)
Mono<SimpleResponse<EntitiesResult>> entitiesRecognitionGeneral(@HostParam("Endpoint") String endpoint, @QueryParam("model-version") String modelVersion, @QueryParam("showStats") Boolean showStats, @BodyParam("application/json; charset=utf-8") MultiLanguageBatchInput input, Context context);

@Post("entities/recognition/pii")
@ExpectedResponses({200})
@UnexpectedResponseExceptionType(ErrorException.class)
@UnexpectedResponseExceptionType(HttpResponseException.class)
Mono<SimpleResponse<EntitiesResult>> entitiesRecognitionPii(@HostParam("Endpoint") String endpoint, @QueryParam("model-version") String modelVersion, @QueryParam("showStats") Boolean showStats, @BodyParam("application/json; charset=utf-8") MultiLanguageBatchInput input, Context context);

@Post("entities/linking")
@ExpectedResponses({200})
@UnexpectedResponseExceptionType(ErrorException.class)
@UnexpectedResponseExceptionType(HttpResponseException.class)
Mono<SimpleResponse<EntityLinkingResult>> entitiesLinking(@HostParam("Endpoint") String endpoint, @QueryParam("model-version") String modelVersion, @QueryParam("showStats") Boolean showStats, @BodyParam("application/json; charset=utf-8") MultiLanguageBatchInput input, Context context);

@Post("keyPhrases")
@ExpectedResponses({200})
@UnexpectedResponseExceptionType(ErrorException.class)
@UnexpectedResponseExceptionType(HttpResponseException.class)
Mono<SimpleResponse<KeyPhraseResult>> keyPhrases(@HostParam("Endpoint") String endpoint, @QueryParam("model-version") String modelVersion, @QueryParam("showStats") Boolean showStats, @BodyParam("application/json; charset=utf-8") MultiLanguageBatchInput input, Context context);

@Post("languages")
@ExpectedResponses({200})
@UnexpectedResponseExceptionType(ErrorException.class)
@UnexpectedResponseExceptionType(HttpResponseException.class)
Mono<SimpleResponse<LanguageResult>> languages(@HostParam("Endpoint") String endpoint, @QueryParam("model-version") String modelVersion, @QueryParam("showStats") Boolean showStats, @BodyParam("application/json; charset=utf-8") LanguageBatchInput input, Context context);

@Post("sentiment")
@ExpectedResponses({200})
@UnexpectedResponseExceptionType(ErrorException.class)
@UnexpectedResponseExceptionType(HttpResponseException.class)
Mono<SimpleResponse<SentimentResponse>> sentiment(@HostParam("Endpoint") String endpoint, @QueryParam("model-version") String modelVersion, @QueryParam("showStats") Boolean showStats, @BodyParam("application/json; charset=utf-8") MultiLanguageBatchInput input, Context context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public String getId() {
* @param id the id value to set.
* @return the DocumentError object itself.
*/
DocumentError setId(String id) {
public DocumentError setId(String id) {
this.id = id;
return this;
}
Expand All @@ -59,7 +59,7 @@ public Object getError() {
* @param error the error value to set.
* @return the DocumentError object itself.
*/
DocumentError setError(Object error) {
public DocumentError setError(Object error) {
this.error = error;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public int getDocumentCount() {
* @param documentsCount the documentsCount value to set.
* @return the DocumentBatchStatistics object itself.
*/
TextBatchStatistics setDocumentCount(int documentsCount) {
public TextBatchStatistics setDocumentCount(int documentsCount) {
this.documentCount = documentsCount;
return this;
}
Expand All @@ -78,7 +78,7 @@ public int getValidDocumentCount() {
* @param validDocumentCount the validDocumentCount value to set.
* @return the DocumentBatchStatistics object itself.
*/
TextBatchStatistics setValidDocumentCount(int validDocumentCount) {
public TextBatchStatistics setValidDocumentCount(int validDocumentCount) {
this.validDocumentCount = validDocumentCount;
return this;
}
Expand All @@ -102,7 +102,7 @@ public int getErroneousDocumentCount() {
* @param erroneousDocumentCount the erroneousDocumentCount value to set.
* @return the DocumentBatchStatistics object itself.
*/
TextBatchStatistics setErroneousDocumentCount(int erroneousDocumentCount) {
public TextBatchStatistics setErroneousDocumentCount(int erroneousDocumentCount) {
this.erroneousDocumentCount = erroneousDocumentCount;
return this;
}
Expand All @@ -124,7 +124,7 @@ public long getTransactionCount() {
* @param transactionCount the transactionsCount value to set.
* @return the DocumentBatchStatistics object itself.
*/
TextBatchStatistics setTransactionCount(long transactionCount) {
public TextBatchStatistics setTransactionCount(long transactionCount) {
this.transactionCount = transactionCount;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public int getCharacterCount() {
* @param characterCount the charactersCount value to set.
* @return the TextDocumentStatistics object itself.
*/
TextDocumentStatistics setCharacterCount(int characterCount) {
public TextDocumentStatistics setCharacterCount(int characterCount) {
this.characterCount = characterCount;
return this;
}
Expand All @@ -64,7 +64,7 @@ public int getTransactionCount() {
* @param transactionCount the transactionsCount value to set.
* @return the TextDocumentStatistics object itself.
*/
TextDocumentStatistics setTransactionCount(int transactionCount) {
public TextDocumentStatistics setTransactionCount(int transactionCount) {
this.transactionCount = transactionCount;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class TextSentiment {
// sentence
private String length;

private int offSet;
private int offset;

// sentiment string
private TextSentimentClass textSentimentClass;
Expand Down Expand Up @@ -62,12 +62,12 @@ TextSentiment setPositiveScore(double positiveScore) {
return this;
}

public int getOffSet() {
return offSet;
public int getOffset() {
return offset;
}

TextSentiment setOffSet(int offSet) {
this.offSet = offSet;
TextSentiment setOffset(int offset) {
this.offset = offset;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

opens com.azure.cs.textanalytics.implementation to com.fasterxml.jackson.databind;
opens com.azure.cs.textanalytics.models to com.fasterxml.jackson.databind;
opens com.azure.cs.textanalytics.implementation.models to com.fasterxml.jackson.databind;

exports com.azure.cs.textanalytics;
exports com.azure.cs.textanalytics.models;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static void main(String[] args) {
documentSentiment.getNeutralScore(),
documentSentiment.getNegativeScore(),
documentSentiment.getLength(),
documentSentiment.getOffSet());
documentSentiment.getOffset());

result.getSentenceSentiments().forEach(sentenceSentiment ->
System.out.printf("Recognized sentence sentiment: %s, Positive Score: %s, Neutral Score: %s, Negative Score: %s. Length of sentence: %s, Offset of sentence: %s",
Expand All @@ -58,7 +58,7 @@ public static void main(String[] args) {
sentenceSentiment.getNeutralScore(),
sentenceSentiment.getNegativeScore(),
sentenceSentiment.getLength(),
sentenceSentiment.getOffSet()));
sentenceSentiment.getOffset()));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@

package com.azure.cs.textanalytics;

import com.azure.core.util.logging.ClientLogger;
import com.azure.cs.textanalytics.models.DetectedLanguage;
import org.junit.jupiter.api.Test;
import reactor.test.StepVerifier;

import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TextAnalyticsAsyncClientTest extends TextAnalyticsClientTestBase{
private final ClientLogger logger = new ClientLogger(TextAnalyticsAsyncClientTest.class);
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class TextAnalyticsAsyncClientTest extends TextAnalyticsClientTestBase{
private TextAnalyticsAsyncClient client;

@Override
Expand All @@ -23,13 +26,144 @@ protected void beforeTest() {
.buildAsyncClient());
}

/**
* Test Detect batch input langugaes with show statistics.
*/
@Test
public void detectLanguage() {
detectLanguageRunner((inputs, options) -> {
public void detectLanguagesBatchInputShowStatistics() {
detectLanguageShowStatisticsRunner((inputs, options) -> {
StepVerifier.create(client.detectBatchLanguages(inputs, options))
.assertNext(response -> assertTrue(true))
.assertNext(response -> validateBatchResult(response, getExpectedBatchDetectedLanguages(), "Language"))
.verifyComplete();
});
}

/**
* Test Detect batch input langugaes with show statistics.
*/
@Test
public void detectLanguagesBatchInputShowStatisticsNew() {
detectLanguageShowStatisticsRunner((inputs, options) -> {
StepVerifier.create(client.detectBatchLanguages(inputs, options))
.assertNext(response -> validateBatchResult(response, getExpectedBatchDetectedLanguages(), "Language"))
.verifyComplete();
});
}

/**
* Verifies that a batch collection is returned on batch input for detectLanguages.
*/
@Test
public void detectLanguagesBatchStringList() {
detectLanguagesCountryHintRunner((inputs, countryHint) -> {
StepVerifier.create(client.detectLanguages(inputs, countryHint))
.assertNext(response -> validateBatchResult(response, getExpectedBatchDetectedLanguages(), "Language"))
.verifyComplete();
System.out.println("Completed!");
});
}

/**
* Verifies that a batch collection is returned on batch input for detectLanguages.
*/
@Test
public void detectLanguagesBatchStringInput() {
detectLanguageStringInputRunner((inputs) -> {
StepVerifier.create(client.detectLanguages(inputs))
.assertNext(response -> validateBatchResult(response, getExpectedBatchDetectedLanguages(), "Language"))
.verifyComplete();
});
}

/**
* Verifies that a batch collection is returned on batch input for detectLanguages.
*/
@Test
public void detectLanguagesBatchInput() {
detectLanguageRunner((inputs) -> {
StepVerifier.create(client.detectBatchLanguages(inputs))
.assertNext(response -> validateBatchResult(response, getExpectedBatchDetectedLanguages(), "Language"))
.verifyComplete();
});
}

/**
* Verifies that a Null pointer exception is thrown when null text is passed.
*/
@Test
public void detectLanguagesNullInput() {
detectLanguageRunner((inputs) -> {
StepVerifier.create(client.detectBatchLanguagesWithResponse(null, null))
.verifyError(NullPointerException.class);
});
}

/**
* Verifies that a single DetectLanguageResult is returned for a single text input to detectLanguages.
*
*/
@Test
public void detectLanguage() {
DetectedLanguage primaryLanguage = new DetectedLanguage().setName("English").setIso6391Name("en").setScore(1.0);
List<DetectedLanguage> expectedLanguageList = new ArrayList<>(Arrays.asList(primaryLanguage));
StepVerifier.create(client.detectLanguage("This is a test English Text"))
.assertNext(response -> validateDetectedLanguages(expectedLanguageList, response.getDetectedLanguages()))
.verifyComplete();
}

/**
* Verifies that a single error DetectLanguageResult is returned for a single text input with invalid country hint.
*
* TODO: update error Model. #6559
*/
// @Test
// public void detectLanguageInvalidCountryHint() {
// DetectedLanguage primaryLanguage = new DetectedLanguage().setName("English").setIso6391Name("en").setScore(1.0);
// Error error = new Error().setCode("Invalid Hint");
// StepVerifier.create(client.detectLanguage("Este es un document escrito en Español.", "en"))
// .assertNext(response -> assertEquals(error.getCode(), ((Error)response.getError()).getCode()))
// .verifyComplete();
// }

/**
* Verifies that a single DetectLanguageResult is returned for a single text input with country hint.
*
* TODO: update error Model. #6559
*/
// @Test
// public void detectLanguageCountryHint() {
// DetectedLanguage primaryLanguage = new DetectedLanguage().setName("Spanish").setIso6391Name("es").setScore(1.0);
// Error error = new Error().setCode("Invalid Hint");
// StepVerifier.create(client.detectLanguage("Este es un document escrito en Español.", "es"))
// .assertNext(response -> assertEquals(error.getCode(), ((Error)response.getError()).getCode()))
// .verifyComplete();
// }

/**
* Verifies that the error result is returned when empty text is passed.
*/
@Test
public void detectLanguageEmptyText() {
StepVerifier.create(client.detectLanguage(""))
.assertNext(response -> assertNotNull(response.getError()))
.verifyComplete();
}

/**
* Verifies that it returns an exception is thrown when null text is passed.
*/
@Test
public void detectLanguageNullText() {
StepVerifier.create(client.detectLanguage(null)).verifyError(NullPointerException.class);
}

/**
* Verifies that an document returns with an error when error text is passed.
*/
@Test
public void detectLanguageFaultyText() {
StepVerifier.create(client.detectLanguage("!@#%%"))
.assertNext(response ->
assertEquals(response.getPrimaryLanguage().getIso6391Name(), "(Unknown)"))
.verifyComplete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ protected void beforeTest() {
public void detectLanguage() {

}

@Override
public void detectLanguagesBatchInput() {

}
}
Loading

0 comments on commit 9f87de1

Please sign in to comment.