Skip to content

Commit

Permalink
Update error and exception model design for Text Analytics (#7635)
Browse files Browse the repository at this point in the history
  • Loading branch information
samvaity authored Jan 31, 2020
1 parent 607efcc commit 42e3e90
Show file tree
Hide file tree
Showing 28 changed files with 405 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@

<!-- Report AMQP retry attempts in static withRetry method. -->
<suppress checks="com.azure.tools.checkstyle.checks.GoodLoggingCheck" files="com.azure.core.amqp.implementation.RetryUtil.java"/>

<!-- Use the logger in a Utility static method. -->
<suppress checks="com.azure.tools.checkstyle.checks.GoodLoggingCheck" files="com.azure.ai.textanalytics.Transforms.java" />

<!-- Throws a non-runtime exception -->
<suppress checks="com.azure.tools.checkstyle.checks.ThrowFromClientLogger" files="com.azure.core.http.netty.implementation.HttpProxyHandler.java"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import com.azure.ai.textanalytics.models.DocumentResultCollection;
import com.azure.ai.textanalytics.models.TextAnalyticsRequestOptions;
import com.azure.ai.textanalytics.models.TextDocumentInput;
import com.azure.ai.textanalytics.models.TextSentimentClass;
import com.azure.ai.textanalytics.models.TextSentiment;
import com.azure.ai.textanalytics.models.TextSentimentClass;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.util.Context;
Expand All @@ -24,6 +24,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -55,7 +56,7 @@ Mono<Response<AnalyzeSentimentResult>> analyzeSentimentWithResponse(String text,

return analyzeBatchSentimentWithResponse(
Collections.singletonList(new TextDocumentInput("0", text, language)), null, context)
.map(response -> new SimpleResponse<>(response, response.getValue().iterator().next()));
.map(Transforms::processSingleResponseErrorResult);
}

Mono<Response<DocumentResultCollection<AnalyzeSentimentResult>>> analyzeSentimentWithResponse(
Expand Down Expand Up @@ -122,7 +123,7 @@ private AnalyzeSentimentResult convertToTextSentimentResult(final DocumentSentim
// Not throw exception for an invalid Sentiment type because we should not skip processing the
// other response. It is a service issue.
logger.logExceptionAsWarning(
new RuntimeException(String.format("'%s' is not valid text sentiment.",
new RuntimeException(String.format(Locale.ROOT, "'%s' is not valid text sentiment.",
documentSentiment.getSentiment())));
}
final SentimentConfidenceScorePerLabel confidenceScorePerLabel = documentSentiment.getDocumentScores();
Expand All @@ -136,7 +137,7 @@ private AnalyzeSentimentResult convertToTextSentimentResult(final DocumentSentim
// Not throw exception for an invalid Sentiment type because we should not skip processing the
// other response. It is a service issue.
logger.logExceptionAsWarning(
new RuntimeException(String.format("'%s' is not valid text sentiment.",
new RuntimeException(String.format(Locale.ROOT, "'%s' is not valid text sentiment.",
sentenceSentiment.getSentiment())));
}
SentimentConfidenceScorePerLabel confidenceScorePerSentence = sentenceSentiment.getSentenceScores();
Expand All @@ -155,5 +156,4 @@ private AnalyzeSentimentResult convertToTextSentimentResult(final DocumentSentim
sentenceSentimentTexts.stream().mapToInt(TextSentiment::getLength).sum(), 0),
sentenceSentimentTexts);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ Mono<Response<DetectLanguageResult>> detectLanguageWithResponse(String text, Str
Objects.requireNonNull(text, "'text' cannot be null.");
List<DetectLanguageInput> languageInputs = Collections.singletonList(new DetectLanguageInput("0",
text, countryHint));
return detectBatchLanguagesWithResponse(languageInputs, null, context).map(response ->
new SimpleResponse<>(response, response.getValue().iterator().next()));
return detectBatchLanguagesWithResponse(languageInputs, null, context)
.map(Transforms::processSingleResponseErrorResult);
}

Mono<Response<DocumentResultCollection<DetectLanguageResult>>> detectLanguagesWithResponse(List<String> textInputs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Mono<Response<ExtractKeyPhraseResult>> extractKeyPhrasesWithResponse(String text

return extractBatchKeyPhrasesWithResponse(
Collections.singletonList(new TextDocumentInput("0", text, language)), null, context)
.map(response -> new SimpleResponse<>(response, response.getValue().iterator().next()));
.map(Transforms::processSingleResponseErrorResult);
}

Mono<Response<DocumentResultCollection<ExtractKeyPhraseResult>>> extractKeyPhrasesWithResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import java.util.Objects;
import java.util.stream.Collectors;

import static com.azure.ai.textanalytics.Transforms.mapByIndex;
import static com.azure.ai.textanalytics.Transforms.toBatchStatistics;
import static com.azure.ai.textanalytics.Transforms.toTextAnalyticsError;
import static com.azure.ai.textanalytics.Transforms.toTextDocumentStatistics;
import static com.azure.ai.textanalytics.Transforms.toBatchStatistics;
import static com.azure.ai.textanalytics.Transforms.mapByIndex;

/**
* Helper class for managing recognize entity endpoint.
Expand All @@ -54,7 +54,7 @@ Mono<Response<RecognizeEntitiesResult>> recognizeEntitiesWithResponse(String tex

return recognizeBatchEntitiesWithResponse(
Collections.singletonList(new TextDocumentInput("0", text, language)), null, context)
.map(response -> new SimpleResponse<>(response, response.getValue().iterator().next()));
.map(Transforms::processSingleResponseErrorResult);
}

Mono<Response<DocumentResultCollection<RecognizeEntitiesResult>>> recognizeEntitiesWithResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Mono<Response<RecognizeLinkedEntitiesResult>> recognizeLinkedEntitiesWithRespons

return recognizeBatchLinkedEntitiesWithResponse(
Collections.singletonList(new TextDocumentInput("0", text, language)), null, context)
.map(response -> new SimpleResponse<>(response, response.getValue().iterator().next()));
.map(Transforms::processSingleResponseErrorResult);
}

Mono<Response<DocumentResultCollection<RecognizeLinkedEntitiesResult>>> recognizeLinkedEntitiesWithResponse(
Expand Down Expand Up @@ -122,6 +122,4 @@ private DocumentResultCollection<RecognizeLinkedEntitiesResult> toDocumentResult
entityLinkingResult.getModelVersion(), entityLinkingResult.getStatistics() == null ? null
: toBatchStatistics(entityLinkingResult.getStatistics()));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Mono<Response<RecognizePiiEntitiesResult>> recognizePiiEntitiesWithResponse(Stri

return recognizeBatchPiiEntitiesWithResponse(
Collections.singletonList(new TextDocumentInput("0", text, language)), null, context)
.map(response -> new SimpleResponse<>(response, response.getValue().iterator().next()));
.map(Transforms::processSingleResponseErrorResult);
}

Mono<Response<DocumentResultCollection<RecognizePiiEntitiesResult>>> recognizePiiEntitiesWithResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public TextAnalyticsServiceVersion getServiceVersion() {
* @return A {@link Mono} containing the {@link DetectLanguageResult detected language} of the text.
*
* @throws NullPointerException if {@code text} is {@code null}.
* @throws com.azure.ai.textanalytics.models.TextAnalyticsException if the response returned with
* an {@link com.azure.ai.textanalytics.models.TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DetectLanguageResult> detectLanguage(String text) {
Expand Down Expand Up @@ -149,6 +151,8 @@ public Mono<DetectLanguageResult> detectLanguage(String text) {
* {@link DetectLanguageResult detected language} of the text.
*
* @throws NullPointerException if {@code text} is {@code null}.
* @throws com.azure.ai.textanalytics.models.TextAnalyticsException if the response returned with
* an {@link com.azure.ai.textanalytics.models.TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<DetectLanguageResult>> detectLanguageWithResponse(String text, String countryHint) {
Expand Down Expand Up @@ -286,6 +290,8 @@ public Mono<Response<DocumentResultCollection<DetectLanguageResult>>> detectBatc
* @return A {@link Mono} containing the {@link RecognizeEntitiesResult named entity} of the text.
*
* @throws NullPointerException if {@code text} is {@code null}.
* @throws com.azure.ai.textanalytics.models.TextAnalyticsException if the response returned with
* an {@link com.azure.ai.textanalytics.models.TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<RecognizeEntitiesResult> recognizeEntities(String text) {
Expand Down Expand Up @@ -315,6 +321,8 @@ public Mono<RecognizeEntitiesResult> recognizeEntities(String text) {
* {@link RecognizeEntitiesResult named entity} of the text.
*
* @throws NullPointerException if {@code text} is {@code null}.
* @throws com.azure.ai.textanalytics.models.TextAnalyticsException if the response returned with
* an {@link com.azure.ai.textanalytics.models.TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<RecognizeEntitiesResult>> recognizeEntitiesWithResponse(String text, String language) {
Expand Down Expand Up @@ -451,6 +459,8 @@ public Mono<Response<DocumentResultCollection<RecognizeEntitiesResult>>> recogni
* @return A {@link Mono} containing the {@link RecognizeEntitiesResult PII entity} of the text.
*
* @throws NullPointerException if {@code text} is {@code null}.
* @throws com.azure.ai.textanalytics.models.TextAnalyticsException if the response returned with
* an {@link com.azure.ai.textanalytics.models.TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<RecognizePiiEntitiesResult> recognizePiiEntities(String text) {
Expand Down Expand Up @@ -479,6 +489,8 @@ public Mono<RecognizePiiEntitiesResult> recognizePiiEntities(String text) {
* {@link RecognizeEntitiesResult named entity} of the text.
*
* @throws NullPointerException if {@code text} is {@code null}.
* @throws com.azure.ai.textanalytics.models.TextAnalyticsException if the response returned with
* an {@link com.azure.ai.textanalytics.models.TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<RecognizePiiEntitiesResult>> recognizePiiEntitiesWithResponse(String text, String language) {
Expand Down Expand Up @@ -619,6 +631,8 @@ public Mono<Response<DocumentResultCollection<RecognizePiiEntitiesResult>>> reco
* @return A {@link Mono} containing the {@link RecognizeLinkedEntitiesResult linked entity} of the text.
*
* @throws NullPointerException if {@code text} is {@code null}.
* @throws com.azure.ai.textanalytics.models.TextAnalyticsException if the response returned with
* an {@link com.azure.ai.textanalytics.models.TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<RecognizeLinkedEntitiesResult> recognizeLinkedEntities(String text) {
Expand Down Expand Up @@ -646,6 +660,8 @@ public Mono<RecognizeLinkedEntitiesResult> recognizeLinkedEntities(String text)
* {@link RecognizeLinkedEntitiesResult named entity} of the text.
*
* @throws NullPointerException if {@code text} is {@code null}.
* @throws com.azure.ai.textanalytics.models.TextAnalyticsException if the response returned with
* an {@link com.azure.ai.textanalytics.models.TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<RecognizeLinkedEntitiesResult>> recognizeLinkedEntitiesWithResponse(String text,
Expand Down Expand Up @@ -784,6 +800,8 @@ public Mono<DocumentResultCollection<RecognizeLinkedEntitiesResult>> recognizeBa
* @return A {@link Mono} containing the {@link ExtractKeyPhraseResult key phrases} of the text.
*
* @throws NullPointerException if {@code text} is {@code null}.
* @throws com.azure.ai.textanalytics.models.TextAnalyticsException if the response returned with
* an {@link com.azure.ai.textanalytics.models.TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<ExtractKeyPhraseResult> extractKeyPhrases(String text) {
Expand Down Expand Up @@ -811,6 +829,8 @@ public Mono<ExtractKeyPhraseResult> extractKeyPhrases(String text) {
* {@link ExtractKeyPhraseResult key phrases} of the text.
*
* @throws NullPointerException if {@code text} is {@code null}.
* @throws com.azure.ai.textanalytics.models.TextAnalyticsException if the response returned with
* an {@link com.azure.ai.textanalytics.models.TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<ExtractKeyPhraseResult>> extractKeyPhrasesWithResponse(String text, String language) {
Expand Down Expand Up @@ -944,6 +964,8 @@ public Mono<Response<DocumentResultCollection<ExtractKeyPhraseResult>>> extractB
* @return A {@link Mono} containing the {@link AnalyzeSentimentResult text sentiment} of the text.
*
* @throws NullPointerException if {@code text} is {@code null}.
* @throws com.azure.ai.textanalytics.models.TextAnalyticsException if the response returned with
* an {@link com.azure.ai.textanalytics.models.TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<AnalyzeSentimentResult> analyzeSentiment(String text) {
Expand Down Expand Up @@ -971,6 +993,8 @@ public Mono<AnalyzeSentimentResult> analyzeSentiment(String text) {
* {@link AnalyzeSentimentResult text sentiment} of the text.
*
* @throws NullPointerException if {@code text} is {@code null}.
* @throws com.azure.ai.textanalytics.models.TextAnalyticsException if the response returned with
* an {@link com.azure.ai.textanalytics.models.TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<AnalyzeSentimentResult>> analyzeSentimentWithResponse(String text, String language) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ public TextAnalyticsClient buildClient() {
return new TextAnalyticsClient(buildAsyncClient());
}


/**
* Creates a {@link TextAnalyticsAsyncClient} based on options set in the builder. Every time
* {@code buildAsyncClient()} is called a new instance of {@link TextAnalyticsAsyncClient} is created.
Expand Down
Loading

0 comments on commit 42e3e90

Please sign in to comment.