Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text analytics sentiment #25

Merged
merged 2 commits into from
Dec 16, 2019
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 @@ -944,13 +944,16 @@ private TextSentimentResult convertToTextSentimentResult(final DocumentSentiment
return null;
}
//TODO (shawn): calculate max length
documentSentimentText.setLength("MAX_LENGTH").setOffset(0).setTextSentimentClass(documentSentimentClass);
documentSentimentText.setTextSentimentClass(documentSentimentClass);
setTextSentimentScore(documentSentiment.getDocumentScores(), documentSentimentClass, documentSentimentText);

// Sentence text sentiment
final List<TextSentiment> sentenceSentimentTexts =
convertToSentenceSentiments(documentSentiment.getSentences());

documentSentimentText.setLength(sentenceSentimentTexts.stream().mapToInt(TextSentiment::getLength).sum());
documentSentimentText.setOffset(0);

return new TextSentimentResult(documentSentiment.getId(), documentSentiment.getStatistics(), null,
documentSentimentText, sentenceSentimentTexts);
}
Expand All @@ -959,8 +962,8 @@ private List<TextSentiment> convertToSentenceSentiments(final List<SentenceSenti
final List<TextSentiment> sentenceSentimentCollection = new ArrayList<>();
sentenceSentiments.stream().forEach(sentenceSentiment -> {
final TextSentiment singleSentenceSentiment = new TextSentiment();
singleSentenceSentiment.setLength(Integer.toString(sentenceSentiment.getLength()));
singleSentenceSentiment.setLength(Integer.toString(sentenceSentiment.getOffset()));
singleSentenceSentiment.setLength(sentenceSentiment.getLength());
singleSentenceSentiment.setOffset(sentenceSentiment.getOffset());
final TextSentimentClass sentimentClass = convertToTextSentimentClass(sentenceSentiment.getSentiment());
setTextSentimentScore(sentenceSentiment.getSentenceScores(), sentimentClass, singleSentenceSentiment);
singleSentenceSentiment.setTextSentimentClass(sentimentClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ public class TextSentiment {
private double positiveScore;

// sentence
private String length;
private int length;

private int offset;

// sentiment string
private TextSentimentClass textSentimentClass;

public String getLength() {
public int getLength() {
return length;
}

public TextSentiment setLength(String length) {
public TextSentiment setLength(int length) {
this.length = length;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,36 @@ public final class TextSentimentResult extends DocumentResult {
private final TextSentiment documentSentiment;
private final List<TextSentiment> sentenceSentiments;

/**
* TextSentimentResult model constructor
*
* @param id document id
* @param textDocumentStatistics text document statistics
* @param error the document error
* @param documentSentiment the document sentiment
* @param sentenceSentiments a list of sentence sentiments
*/
public TextSentimentResult(String id, TextDocumentStatistics textDocumentStatistics, Error error,
TextSentiment documentSentiment, List<TextSentiment> sentenceSentiments) {
super(id, textDocumentStatistics, error);
this.documentSentiment = documentSentiment;
this.sentenceSentiments = sentenceSentiments;
}

/**
* Get the document sentiment.
*
* @return the document sentiment
*/
public TextSentiment getDocumentSentiment() {
return documentSentiment;
}

/**
* Get a list of sentence sentiments.
*
* @return a list of sentence sentiments
*/
public List<TextSentiment> getSentenceSentiments() {
return sentenceSentiments;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.azure.ai.textanalytics.models.TextDocumentInput;
import com.azure.ai.textanalytics.models.TextSentiment;
import com.azure.ai.textanalytics.models.TextSentimentResult;
import com.azure.core.util.Configuration;
import com.azure.core.util.Context;

import java.util.Arrays;
Expand All @@ -21,14 +22,16 @@ public class AnalyzeSentimentBatchDocuments {
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsClient client = new TextAnalyticsClientBuilder()
.subscriptionKey("subscription-key")
.endpoint("https://servicename.cognitiveservices.azure.com/")
// .subscriptionKey("subscription-key")
// .endpoint("https://servicename.cognitiveservices.azure.com/")
.subscriptionKey(Configuration.getGlobalConfiguration().get("AZURE_TEXT_ANALYTICS_SUBSCRIPTION_KEY"))
.endpoint(Configuration.getGlobalConfiguration().get("AZURE_TEXT_ANALYTICS_ENDPOINT"))
.buildClient();

// The texts that need be analysed.
List<TextDocumentInput> inputs = Arrays.asList(
new TextDocumentInput("1", "The hotel was dark and unclean.", "en"),
new TextDocumentInput("2", "The restaurant had amazing gnocchi.", "en")
new TextDocumentInput("1", "The hotel was dark and unclean. The restaurant had amazing gnocchi.", "en"),
new TextDocumentInput("2", "The restaurant had amazing gnocchi. The hotel was dark and unclean.", "en")
);

final TextAnalyticsRequestOptions requestOptions = new TextAnalyticsRequestOptions().setShowStatistics(true);
Expand Down Expand Up @@ -60,8 +63,8 @@ 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 @@ -57,9 +57,9 @@ public static void main(String[] args) {
System.out.printf("Other detected Languages: %s, ISO 6391 Name: %s, Score: %s.%n",
detectedLanguage.getName(),
detectedLanguage.getIso6391Name(),
detectedLanguage.getScore());
}
}
detectedLanguage.getScore()));

});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.azure.ai.textanalytics.models.LinkedEntityResult;
import com.azure.ai.textanalytics.models.NamedEntity;
import com.azure.ai.textanalytics.models.NamedEntityResult;
import com.azure.ai.textanalytics.models.TextSentiment;
import com.azure.ai.textanalytics.models.TextSentimentClass;
import com.azure.core.exception.HttpResponseException;
import com.azure.core.util.Context;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -352,5 +354,57 @@ public void recognizePiiEntitiesForListLanguageHint() {
});
}


// Key Phrases



// Sentiment
@Test
public void analyseSentimentForTextInput() {

final TextSentiment expectedDocumentSentiment = new TextSentiment()
.setTextSentimentClass(TextSentimentClass.MIXED).setLength(66).setOffset(0);
final List<TextSentiment> expectedSentiments = Arrays.asList(
new TextSentiment().setTextSentimentClass(TextSentimentClass.NEGATIVE).setLength(31).setOffset(0),
new TextSentiment().setTextSentimentClass(TextSentimentClass.POSITIVE).setLength(35).setOffset(32)
);

StepVerifier.create(client.analyzeSentiment("The hotel was dark and unclean. The restaurant had amazing gnocchi."))
.assertNext(response -> {
validateAnalysedSentiment(expectedDocumentSentiment, response.getDocumentSentiment());
validateAnalysedSentenceSentiment(expectedSentiments, response.getSentenceSentiments());
})
.verifyComplete();
}

@Test
public void analyseSentimentForEmptyText() {

}

@Test
public void analyseSentimentForFaultyText() {

}

@Test
public void analyseSentimentForBatchInput() {

}

@Test
public void analyseSentimentForBatchInputShowStatistics() {

}

@Test
public void analyseSentimentForBatchStringInput() {

}

@Test
public void analyseSentimentForListLanguageHint() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,40 @@ public void recognizeKeyPhrasesForBatchStringInput() {
public void recognizeKeyPhrasesForListLanguageHint() {

}

// Sentiment
@Test
public void analyseSentimentForTextInput() {

}

@Test
public void analyseSentimentForEmptyText() {

}

@Test
public void analyseSentimentForFaultyText() {

}

@Test
public void analyseSentimentForBatchInput() {

}

@Test
public void analyseSentimentForBatchInputShowStatistics() {

}

@Test
public void analyseSentimentForBatchStringInput() {

}

@Test
public void analyseSentimentForListLanguageHint() {

}
}
Loading