Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
import com.azure.ai.textanalytics.implementation.TextAnalyticsClientImpl;
import com.azure.ai.textanalytics.implementation.models.DocumentError;
import com.azure.ai.textanalytics.implementation.models.DocumentSentiment;
import com.azure.ai.textanalytics.implementation.models.DocumentSentimentValue;
import com.azure.ai.textanalytics.implementation.models.MultiLanguageBatchInput;
import com.azure.ai.textanalytics.implementation.models.SentenceSentimentValue;
import com.azure.ai.textanalytics.implementation.models.SentimentConfidenceScorePerLabel;
import com.azure.ai.textanalytics.implementation.models.SentimentResponse;
import com.azure.ai.textanalytics.implementation.models.WarningCodeValue;
import com.azure.ai.textanalytics.models.AnalyzeSentimentResult;
import com.azure.ai.textanalytics.models.SentenceSentiment;
import com.azure.ai.textanalytics.models.SentimentConfidenceScores;
import com.azure.ai.textanalytics.models.TextAnalyticsRequestOptions;
import com.azure.ai.textanalytics.models.TextAnalyticsWarning;
import com.azure.ai.textanalytics.models.TextDocumentInput;
import com.azure.ai.textanalytics.models.TextSentiment;
import com.azure.ai.textanalytics.models.WarningCode;
import com.azure.ai.textanalytics.util.TextAnalyticsPagedFlux;
import com.azure.ai.textanalytics.util.TextAnalyticsPagedResponse;
import com.azure.core.exception.HttpResponseException;
Expand All @@ -28,7 +29,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

import static com.azure.ai.textanalytics.TextAnalyticsAsyncClient.COGNITIVE_TRACING_NAMESPACE_VALUE;
Expand Down Expand Up @@ -146,51 +146,35 @@ private TextAnalyticsPagedResponse<AnalyzeSentimentResult> toTextAnalyticsPagedR
*/
private AnalyzeSentimentResult convertToAnalyzeSentimentResult(DocumentSentiment documentSentiment) {
// Document text sentiment
final TextSentiment documentSentimentLabel = TextSentiment.fromString(
documentSentiment.getSentiment().toString());
if (documentSentimentLabel == null) {
Copy link
Contributor Author

@mssfang mssfang May 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant null check that catched in the code quality process.

[ERROR] Redundant nullcheck of documentSentimentLabel, which is known to be non-null in com.azure.ai.textanalytics.AnalyzeSentimentAsyncClient.convertToAnalyzeSentimentResult(DocumentSentiment) [com.azure.ai.textanalytics.AnalyzeSentimentAsyncClient] Redundant null check at AnalyzeSentimentAsyncClient.java:[line 147] RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE
[ERROR] Redundant nullcheck of sentenceSentimentLabel, which is known to be non-null in com.azure.ai.textanalytics.AnalyzeSentimentAsyncClient.lambda$convertToAnalyzeSentimentResult$9(SentenceSentiment) [com.azure.ai.textanalytics.AnalyzeSentimentAsyncClient] Redundant null check at AnalyzeSentimentAsyncClient.java:[line 161] RCN_REDUNDANT_NULLCHECK_OF_NONNULL_

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This started coming up from the current change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Null checking here is an redundant check. As the error message explained here.

// 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(Locale.ROOT, "'%s' is not valid text sentiment.",
documentSentiment.getSentiment())));
}

final SentimentConfidenceScorePerLabel confidenceScorePerLabel = documentSentiment.getConfidenceScores();

// Sentence text sentiment
final List<SentenceSentiment> sentenceSentiments = documentSentiment.getSentences().stream()
.map(sentenceSentiment -> {
final TextSentiment sentenceSentimentLabel = TextSentiment.fromString(
sentenceSentiment.getSentiment().toString());
if (sentenceSentimentLabel == null) {
// 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(Locale.ROOT, "'%s' is not valid text sentiment.",
sentenceSentiment.getSentiment())));
}
final SentimentConfidenceScorePerLabel confidenceScorePerSentence =
sentenceSentiment.getConfidenceScores();

final SentenceSentimentValue sentenceSentimentValue = sentenceSentiment.getSentiment();
return new SentenceSentiment(sentenceSentiment.getText(),
sentenceSentimentLabel,
sentenceSentimentValue == null ? null : sentenceSentimentValue.toString(),
new SentimentConfidenceScores(confidenceScorePerSentence.getNegative(),
confidenceScorePerSentence.getNeutral(), confidenceScorePerSentence.getPositive()));
}).collect(Collectors.toList());

// Warnings
final List<TextAnalyticsWarning> warnings = documentSentiment.getWarnings().stream().map(
warning -> new TextAnalyticsWarning(WarningCode.fromString(warning.getCode().toString()),
warning.getMessage())).collect(Collectors.toList());
warning -> {
final WarningCodeValue warningCodeValue = warning.getCode();
return new TextAnalyticsWarning(warningCodeValue == null ? null : warningCodeValue.toString(),
warning.getMessage());
}).collect(Collectors.toList());

final DocumentSentimentValue documentSentimentValue = documentSentiment.getSentiment();
return new AnalyzeSentimentResult(
documentSentiment.getId(),
documentSentiment.getStatistics() == null
? null : toTextDocumentStatistics(documentSentiment.getStatistics()),
null,
new com.azure.ai.textanalytics.models.DocumentSentiment(
documentSentimentLabel,
documentSentimentValue == null ? null : documentSentimentValue.toString(),
new SentimentConfidenceScores(
confidenceScorePerLabel.getNegative(),
confidenceScorePerLabel.getNeutral(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import com.azure.ai.textanalytics.implementation.models.DocumentLanguage;
import com.azure.ai.textanalytics.implementation.models.LanguageBatchInput;
import com.azure.ai.textanalytics.implementation.models.LanguageResult;
import com.azure.ai.textanalytics.implementation.models.WarningCodeValue;
import com.azure.ai.textanalytics.models.DetectLanguageInput;
import com.azure.ai.textanalytics.models.DetectLanguageResult;
import com.azure.ai.textanalytics.models.DetectedLanguage;
import com.azure.ai.textanalytics.models.TextAnalyticsRequestOptions;
import com.azure.ai.textanalytics.models.TextAnalyticsWarning;
import com.azure.ai.textanalytics.models.WarningCode;
import com.azure.ai.textanalytics.util.TextAnalyticsPagedFlux;
import com.azure.ai.textanalytics.util.TextAnalyticsPagedResponse;
import com.azure.core.exception.HttpResponseException;
Expand Down Expand Up @@ -114,10 +114,12 @@ private TextAnalyticsPagedResponse<DetectLanguageResult> toTextAnalyticsPagedRes
documentLanguage.getDetectedLanguage();

// warnings
final List<TextAnalyticsWarning> warnings = documentLanguage.getWarnings().stream().map(warning ->
new TextAnalyticsWarning(WarningCode.fromString(warning.getCode().toString()),
warning.getMessage())).collect(Collectors.toList());

final List<TextAnalyticsWarning> warnings = documentLanguage.getWarnings().stream()
.map(warning -> {
final WarningCodeValue warningCodeValue = warning.getCode();
return new TextAnalyticsWarning(warningCodeValue == null ? null : warningCodeValue.toString(),
warning.getMessage());
}).collect(Collectors.toList());

detectLanguageResults.add(new DetectLanguageResult(
documentLanguage.getId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import com.azure.ai.textanalytics.implementation.models.DocumentKeyPhrases;
import com.azure.ai.textanalytics.implementation.models.KeyPhraseResult;
import com.azure.ai.textanalytics.implementation.models.MultiLanguageBatchInput;
import com.azure.ai.textanalytics.implementation.models.WarningCodeValue;
import com.azure.ai.textanalytics.models.ExtractKeyPhraseResult;
import com.azure.ai.textanalytics.models.KeyPhrasesCollection;
import com.azure.ai.textanalytics.models.TextAnalyticsError;
import com.azure.ai.textanalytics.models.TextAnalyticsRequestOptions;
import com.azure.ai.textanalytics.models.TextAnalyticsWarning;
import com.azure.ai.textanalytics.models.TextDocumentInput;
import com.azure.ai.textanalytics.models.WarningCode;
import com.azure.ai.textanalytics.util.TextAnalyticsPagedFlux;
import com.azure.ai.textanalytics.util.TextAnalyticsPagedResponse;
import com.azure.core.exception.HttpResponseException;
Expand Down Expand Up @@ -150,10 +150,11 @@ private TextAnalyticsPagedResponse<ExtractKeyPhraseResult> toTextAnalyticsPagedR
: toTextDocumentStatistics(documentKeyPhrases.getStatistics()), null,
new KeyPhrasesCollection(
new IterableStream<>(documentKeyPhrases.getKeyPhrases()),
new IterableStream<>(documentKeyPhrases.getWarnings().stream().map(warning ->
new TextAnalyticsWarning(WarningCode.fromString(warning.getCode().toString()),
warning.getMessage()))
.collect(Collectors.toList())))));
new IterableStream<>(documentKeyPhrases.getWarnings().stream().map(warning -> {
final WarningCodeValue warningCodeValue = warning.getCode();
return new TextAnalyticsWarning(warningCodeValue == null ? null : warningCodeValue.toString(),
warning.getMessage());
}).collect(Collectors.toList())))));
}
// Document errors
for (DocumentError documentError : keyPhraseResult.getErrors()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
import com.azure.ai.textanalytics.implementation.TextAnalyticsClientImpl;
import com.azure.ai.textanalytics.implementation.models.EntitiesResult;
import com.azure.ai.textanalytics.implementation.models.MultiLanguageBatchInput;
import com.azure.ai.textanalytics.implementation.models.WarningCodeValue;
import com.azure.ai.textanalytics.models.CategorizedEntity;
import com.azure.ai.textanalytics.models.CategorizedEntityCollection;
import com.azure.ai.textanalytics.models.EntityCategory;
import com.azure.ai.textanalytics.models.RecognizeEntitiesResult;
import com.azure.ai.textanalytics.models.TextAnalyticsRequestOptions;
import com.azure.ai.textanalytics.models.TextAnalyticsWarning;
import com.azure.ai.textanalytics.models.TextDocumentInput;
import com.azure.ai.textanalytics.models.WarningCode;
import com.azure.ai.textanalytics.util.TextAnalyticsPagedFlux;
import com.azure.ai.textanalytics.util.TextAnalyticsPagedResponse;
import com.azure.core.exception.HttpResponseException;
Expand Down Expand Up @@ -151,13 +150,15 @@ private TextAnalyticsPagedResponse<RecognizeEntitiesResult> toTextAnalyticsPaged
null,
new CategorizedEntityCollection(
new IterableStream<>(documentEntities.getEntities().stream().map(entity ->
new CategorizedEntity(entity.getText(), EntityCategory.fromString(entity.getCategory()),
new CategorizedEntity(entity.getText(), entity.getCategory(),
entity.getSubcategory(), entity.getConfidenceScore()))
.collect(Collectors.toList())),
new IterableStream<>(documentEntities.getWarnings().stream().map(warning ->
new TextAnalyticsWarning(WarningCode.fromString(warning.getCode().toString()),
warning.getMessage()))
.collect(Collectors.toList())))
new IterableStream<>(documentEntities.getWarnings().stream()
.map(warning -> {
final WarningCodeValue warningCodeValue = warning.getCode();
return new TextAnalyticsWarning(
warningCodeValue == null ? null : warningCodeValue.toString(), warning.getMessage());
}).collect(Collectors.toList())))
)));
// Document errors
entitiesResult.getErrors().forEach(documentError -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import com.azure.ai.textanalytics.implementation.TextAnalyticsClientImpl;
import com.azure.ai.textanalytics.implementation.models.EntityLinkingResult;
import com.azure.ai.textanalytics.implementation.models.MultiLanguageBatchInput;
import com.azure.ai.textanalytics.implementation.models.WarningCodeValue;
import com.azure.ai.textanalytics.models.LinkedEntity;
import com.azure.ai.textanalytics.models.LinkedEntityCollection;
import com.azure.ai.textanalytics.models.LinkedEntityMatch;
import com.azure.ai.textanalytics.models.RecognizeLinkedEntitiesResult;
import com.azure.ai.textanalytics.models.TextAnalyticsRequestOptions;
import com.azure.ai.textanalytics.models.TextAnalyticsWarning;
import com.azure.ai.textanalytics.models.TextDocumentInput;
import com.azure.ai.textanalytics.models.WarningCode;
import com.azure.ai.textanalytics.util.TextAnalyticsPagedFlux;
import com.azure.ai.textanalytics.util.TextAnalyticsPagedResponse;
import com.azure.core.exception.HttpResponseException;
Expand Down Expand Up @@ -102,8 +102,7 @@ TextAnalyticsPagedFlux<RecognizeLinkedEntitiesResult> recognizeLinkedEntitiesBat
return new TextAnalyticsPagedFlux<>(() -> (continuationToken, pageSize) -> withContext(context ->
getRecognizedLinkedEntitiesResponseInPage(documents, options, context)).flux());
} catch (RuntimeException ex) {
return new TextAnalyticsPagedFlux<>(() ->
(continuationToken, pageSize) -> fluxError(logger, ex));
return new TextAnalyticsPagedFlux<>(() -> (continuationToken, pageSize) -> fluxError(logger, ex));
}
}

Expand Down Expand Up @@ -150,10 +149,12 @@ private TextAnalyticsPagedResponse<RecognizeLinkedEntitiesResult> toTextAnalytic
null,
new LinkedEntityCollection(
mapLinkedEntity(documentLinkedEntities.getEntities()),
new IterableStream<>(documentLinkedEntities.getWarnings().stream().map(warning ->
new TextAnalyticsWarning(WarningCode.fromString(warning.getCode().toString()),
warning.getMessage()))
.collect(Collectors.toList())))
new IterableStream<>(documentLinkedEntities.getWarnings().stream()
.map(warning -> {
final WarningCodeValue warningCodeValue = warning.getCode();
return new TextAnalyticsWarning(
warningCodeValue == null ? null : warningCodeValue.toString(), warning.getMessage());
}).collect(Collectors.toList())))
)));
// Document errors
entityLinkingResult.getErrors().forEach(documentError -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.azure.ai.textanalytics.implementation.models.TextAnalyticsError;
import com.azure.ai.textanalytics.implementation.models.TextAnalyticsErrorException;
import com.azure.ai.textanalytics.models.DetectLanguageInput;
import com.azure.ai.textanalytics.models.TextAnalyticsErrorCode;
import com.azure.ai.textanalytics.models.TextAnalyticsException;
import com.azure.ai.textanalytics.models.TextDocumentBatchStatistics;
import com.azure.ai.textanalytics.models.TextDocumentInput;
Expand Down Expand Up @@ -174,14 +173,14 @@ public static com.azure.ai.textanalytics.models.TextAnalyticsError toTextAnalyti
if (innerError == null) {
final ErrorCodeValue errorCodeValue = textAnalyticsError.getCode();
return new com.azure.ai.textanalytics.models.TextAnalyticsError(
TextAnalyticsErrorCode.fromString(errorCodeValue == null ? null : errorCodeValue.toString()),
errorCodeValue == null ? null : errorCodeValue.toString(),
textAnalyticsError.getMessage(),
textAnalyticsError.getTarget());
}

final InnerErrorCodeValue innerErrorCodeValue = innerError.getCode();
return new com.azure.ai.textanalytics.models.TextAnalyticsError(
TextAnalyticsErrorCode.fromString(innerErrorCodeValue == null ? null : innerErrorCodeValue.toString()),
innerErrorCodeValue == null ? null : innerErrorCodeValue.toString(),
innerError.getMessage(),
innerError.getTarget());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public final class CategorizedEntity {
* @param subcategory The entity subcategory, such as Age/Year/TimeRange etc.
* @param confidenceScore A confidence score between 0 and 1 of the extracted entity.
*/
public CategorizedEntity(String text, EntityCategory category, String subcategory, double confidenceScore) {
public CategorizedEntity(String text, String category, String subcategory, double confidenceScore) {
this.text = text;
this.category = category;
this.category = EntityCategory.fromString(category);
this.subcategory = subcategory;
this.confidenceScore = confidenceScore;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public final class DocumentSentiment {
* @param sentences An {@link IterableStream} of sentence sentiments.
* @param warnings An {@link IterableStream} of {@link TextAnalyticsWarning}.
*/
public DocumentSentiment(TextSentiment sentiment, SentimentConfidenceScores confidenceScores,
public DocumentSentiment(String sentiment, SentimentConfidenceScores confidenceScores,
IterableStream<SentenceSentiment> sentences, IterableStream<TextAnalyticsWarning> warnings) {
this.sentiment = sentiment;
this.sentiment = TextSentiment.fromString(sentiment);
this.confidenceScores = confidenceScores;
this.sentences = sentences;
this.warnings = warnings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public final class EntityCategory extends ExpandableStringEnum<EntityCategory> {
* @return The corresponding {@link EntityCategory}.
*/
@JsonCreator
public static EntityCategory fromString(String name) {
static EntityCategory fromString(String name) {
return fromString(name, EntityCategory.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public final class SentenceSentiment {
* @param confidenceScores The sentiment confidence score (Softmax score) between 0 and 1, for each sentiment label.
* Higher values signify higher confidence.
*/
public SentenceSentiment(String text, TextSentiment sentiment, SentimentConfidenceScores confidenceScores) {
public SentenceSentiment(String text, String sentiment, SentimentConfidenceScores confidenceScores) {
this.text = text;
this.sentiment = sentiment;
this.sentiment = TextSentiment.fromString(sentiment);
this.confidenceScores = confidenceScores;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public final class TextAnalyticsError {
* Error code. Possible values include: 'invalidRequest',
* 'invalidArgument', 'internalServerError', 'serviceUnavailable'
*/
private final TextAnalyticsErrorCode code;
private final TextAnalyticsErrorCode errorCode;

/*
* Error message.
Expand All @@ -29,13 +29,12 @@ public final class TextAnalyticsError {

/**
* Creates a {@code TextAnalyticsError} model that describes text analytics error.
*
* @param code The error code.
* @param errorCode The error code.
* @param message The error message.
* @param target The error target.
*/
public TextAnalyticsError(TextAnalyticsErrorCode code, String message, String target) {
this.code = code;
public TextAnalyticsError(String errorCode, String message, String target) {
this.errorCode = TextAnalyticsErrorCode.fromString(errorCode);
this.message = message;
this.target = target;
}
Expand All @@ -48,7 +47,7 @@ public TextAnalyticsError(TextAnalyticsErrorCode code, String message, String ta
* @return The code value.
*/
public TextAnalyticsErrorCode getErrorCode() {
return this.code;
return this.errorCode;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public final class TextAnalyticsErrorCode extends ExpandableStringEnum<TextAnaly
* @return The corresponding TextAnalyticsErrorCode.
*/
@JsonCreator
public static TextAnalyticsErrorCode fromString(String name) {
static TextAnalyticsErrorCode fromString(String name) {
return fromString(name, TextAnalyticsErrorCode.class);
}
}
Loading