Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -15,8 +15,6 @@
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 +26,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 @@ -144,43 +141,21 @@ private TextAnalyticsPagedResponse<AnalyzeSentimentResult> toTextAnalyticsPagedR
* @return The {@link AnalyzeSentimentResult} to be returned by the SDK.
*/
private AnalyzeSentimentResult convertToAnalyzeSentimentResult(DocumentSentiment documentSentiment) {
// Document text sentiment
final TextSentiment documentSentimentLabel = TextSentiment.fromString(
documentSentiment.getSentiment().toString());
if (documentSentimentLabel == null) {

@mssfang mssfang May 16, 2020

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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
Copy Markdown
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();

return new SentenceSentiment(sentenceSentiment.getText(),
sentenceSentimentLabel,
sentenceSentiment.getSentiment().toString(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We don't want to check for nulls anymore?
Initially, you were checking for null for sentenceSentimentLabel and documentSentimentLabel and throwing an exception.
Is it fine for sentimentLabel and documentSentimentLabel to be null?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 -> new TextAnalyticsWarning(warning.getCode().toString(),
warning.getMessage())).collect(Collectors.toList());

return new AnalyzeSentimentResult(
Expand All @@ -189,7 +164,7 @@ private AnalyzeSentimentResult convertToAnalyzeSentimentResult(DocumentSentiment
? null : toTextDocumentStatistics(documentSentiment.getStatistics()),
null,
new com.azure.ai.textanalytics.models.DocumentSentiment(
documentSentimentLabel,
documentSentiment.getSentiment().toString(),
new SentimentConfidenceScores(
confidenceScorePerLabel.getNegative(),
confidenceScorePerLabel.getNeutral(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
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 @@ -113,10 +112,9 @@ 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 -> new TextAnalyticsWarning(warning.getCode().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 @@ -14,7 +14,6 @@
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,8 +150,7 @@ private TextAnalyticsPagedResponse<ExtractKeyPhraseResult> toTextAnalyticsPagedR
new KeyPhrasesCollection(
new IterableStream<>(documentKeyPhrases.getKeyPhrases()),
new IterableStream<>(documentKeyPhrases.getWarnings().stream().map(warning ->
new TextAnalyticsWarning(WarningCode.fromString(warning.getCode().toString()),
warning.getMessage()))
new TextAnalyticsWarning(warning.getCode().toString(), warning.getMessage()))
.collect(Collectors.toList())))));
}
// Document errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
import com.azure.ai.textanalytics.implementation.models.MultiLanguageBatchInput;
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,12 +149,11 @@ 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()))
new IterableStream<>(documentEntities.getWarnings().stream()
.map(warning -> new TextAnalyticsWarning(warning.getCode().toString(), warning.getMessage()))
.collect(Collectors.toList())))
)));
// Document errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
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 @@ -101,8 +100,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 @@ -149,9 +147,8 @@ 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()))
new IterableStream<>(documentLinkedEntities.getWarnings().stream()
.map(warning -> new TextAnalyticsWarning(warning.getCode().toString(), warning.getMessage()))
.collect(Collectors.toList())))
)));
// Document errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
import com.azure.ai.textanalytics.implementation.models.MultiLanguageInput;
import com.azure.ai.textanalytics.implementation.models.RequestStatistics;
import com.azure.ai.textanalytics.implementation.models.TextAnalyticsError;
import com.azure.ai.textanalytics.models.TextDocumentBatchStatistics;
import com.azure.ai.textanalytics.models.TextDocumentStatistics;
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;
import com.azure.ai.textanalytics.models.TextDocumentStatistics;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -79,12 +78,12 @@ static com.azure.ai.textanalytics.models.TextAnalyticsError toTextAnalyticsError
TextAnalyticsError textAnalyticsError) {
if (textAnalyticsError.getInnererror() == null) {
return new com.azure.ai.textanalytics.models.TextAnalyticsError(
TextAnalyticsErrorCode.fromString(textAnalyticsError.getCode().toString()),
textAnalyticsError.getCode().toString(),
textAnalyticsError.getMessage(),
textAnalyticsError.getTarget());
}
return new com.azure.ai.textanalytics.models.TextAnalyticsError(
TextAnalyticsErrorCode.fromString(textAnalyticsError.getInnererror().getCode().toString()),
textAnalyticsError.getInnererror().getCode().toString(),
textAnalyticsError.getInnererror().getMessage(),
textAnalyticsError.getInnererror().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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,25 @@ public class TextAnalyticsException extends AzureException {
private static final String ERROR_CODE = "ErrorCodeValue";
private static final String TARGET = "target";

private final String code;
private final String errorCode;
private final String target;

/**
* Initializes a new instance of the {@link TextAnalyticsException} class.
*
* @param message Text contains any additional details of the exception.
* @param code The service returned error code value.
* @param errorCode The service returned error code value.
* @param target The target for this exception.
*/
public TextAnalyticsException(String message, String code, String target) {
public TextAnalyticsException(String message, String errorCode, String target) {
super(message);
this.code = code;
this.errorCode = errorCode;
this.target = target;
}

@Override
public String getMessage() {
StringBuilder baseMessage = new StringBuilder().append(super.getMessage()).append(" ").append(ERROR_CODE)
.append(": {").append(code).append("}");
.append(": {").append(errorCode).append("}");

if (this.target == null) {
return baseMessage.toString();
Expand All @@ -56,6 +55,6 @@ public String getTarget() {
* @return The {@link TextAnalyticsErrorCode} for this exception.
*/
public TextAnalyticsErrorCode getErrorCode() {
return TextAnalyticsErrorCode.fromString(code);
return TextAnalyticsErrorCode.fromString(errorCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public final class TextAnalyticsWarning {
/*
* Warning error code.
*/
private final WarningCode code;
private final WarningCode warningCode;

/*
* Warning message.
Expand All @@ -23,11 +23,11 @@ public final class TextAnalyticsWarning {
/**
* Creates a {@link TextAnalyticsWarning} model that describes text analytics warning.
*
* @param code The warning code value
* @param warningCode The warning code value
* @param message The warning message.
*/
public TextAnalyticsWarning(WarningCode code, String message) {
this.code = code;
public TextAnalyticsWarning(String warningCode, String message) {
this.warningCode = WarningCode.fromString(warningCode);
this.message = message;
}

Expand All @@ -37,7 +37,7 @@ public TextAnalyticsWarning(WarningCode code, String message) {
* @return the warning code value.
*/
public WarningCode getWarningCode() {
return this.code;
return this.warningCode;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class TextSentiment extends ExpandableStringEnum<TextSentiment> {
* @return The corresponding {@link TextSentiment}.
*/
@JsonCreator
public static TextSentiment fromString(String name) {
static TextSentiment fromString(String name) {
return fromString(name, TextSentiment.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public final class WarningCode extends ExpandableStringEnum<WarningCode> {
* @return The corresponding {@link WarningCode}.
*/
@JsonCreator

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

don't need this>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

WarningCode is not a generated model.

if you take a look at what JsonCreator annotation's description:
https://fasterxml.github.io/jackson-annotations/javadoc/2.7/com/fasterxml/jackson/annotation/JsonCreator.html

Speaking of what other SDK's behavior of including this annotation. They included it as well.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It should be included on the deserialization layer i.e on the service side equivalent model. Don't need it here.

public static WarningCode fromString(String name) {
static WarningCode fromString(String name) {
return fromString(name, WarningCode.class);
}
}
Loading