diff --git a/sdk/textanalytics/azure-ai-textanalytics/CHANGELOG.md b/sdk/textanalytics/azure-ai-textanalytics/CHANGELOG.md index f774de88a97d..f8c48186f667 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/CHANGELOG.md +++ b/sdk/textanalytics/azure-ai-textanalytics/CHANGELOG.md @@ -2,6 +2,9 @@ ## 1.0.0b5 (Unreleased) +**New features** +- Added `text` property to `SentenceSentiment` + **Breaking changes** - `score` attribute of `DetectedLanguage` has been renamed to `confidence_score` diff --git a/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/_models.py b/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/_models.py index 13c62dbe9f11..2560d01d3bf1 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/_models.py +++ b/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/_models.py @@ -597,6 +597,8 @@ class SentenceSentiment(DictMixin): """SentenceSentiment contains the predicted sentiment and confidence scores for each individual sentence in the document. + :param text: The sentence text. + :type text: str :param sentiment: The predicted Sentiment for the sentence. Possible values include: 'positive', 'neutral', 'negative' :type sentiment: str @@ -612,6 +614,7 @@ class SentenceSentiment(DictMixin): """ def __init__(self, **kwargs): + self.text = kwargs.get("text", None) self.sentiment = kwargs.get("sentiment", None) self.confidence_scores = kwargs.get("confidence_scores", None) self.grapheme_offset = kwargs.get("grapheme_offset", None) @@ -620,6 +623,7 @@ def __init__(self, **kwargs): @classmethod def _from_generated(cls, sentence): return cls( + text=sentence.text, sentiment=sentence.sentiment, confidence_scores=SentimentConfidenceScores._from_generated(sentence.confidence_scores), # pylint: disable=protected-access grapheme_offset=sentence.offset, @@ -627,8 +631,9 @@ def _from_generated(cls, sentence): ) def __repr__(self): - return "SentenceSentiment(sentiment={}, confidence_scores={}, grapheme_offset={}, grapheme_length={})".format( - self.sentiment, repr(self.confidence_scores), self.grapheme_offset, self.grapheme_length + return "SentenceSentiment(text={}, sentiment={}, confidence_scores={}, grapheme_offset={}, "\ + "grapheme_length={})".format(self.text, self.sentiment, repr(self.confidence_scores), + self.grapheme_offset, self.grapheme_length )[:1024] diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_sentiment_async.py b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_sentiment_async.py index 70d50167c6a2..ec34fef5bf13 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_sentiment_async.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_sentiment_async.py @@ -56,8 +56,8 @@ async def analyze_sentiment_async(self): doc.confidence_scores.neutral, doc.confidence_scores.negative, )) - for idx, sentence in enumerate(doc.sentences): - print("Sentence {} sentiment: {}".format(idx+1, sentence.sentiment)) + for sentence in doc.sentences: + print("Sentence '{}' has sentiment: {}".format(sentence.text, sentence.sentiment)) print("Sentence confidence scores: positive={}; neutral={}; negative={}".format( sentence.confidence_scores.positive, sentence.confidence_scores.neutral, diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_sentiment.py b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_sentiment.py index 6a5829e6ab53..8b246c77f0ec 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_sentiment.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_sentiment.py @@ -53,8 +53,8 @@ def analyze_sentiment(self): doc.confidence_scores.neutral, doc.confidence_scores.negative, )) - for idx, sentence in enumerate(doc.sentences): - print("Sentence {} sentiment: {}".format(idx+1, sentence.sentiment)) + for sentence in doc.sentences: + print("Sentence '{}' has sentiment: {}".format(sentence.text, sentence.sentiment)) print("Sentence confidence scores: positive={}; neutral={}; negative={}".format( sentence.confidence_scores.positive, sentence.confidence_scores.neutral, diff --git a/sdk/textanalytics/azure-ai-textanalytics/tests/test_analyze_sentiment.py b/sdk/textanalytics/azure-ai-textanalytics/tests/test_analyze_sentiment.py index 4abea64b1a24..b19955fdb96a 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/tests/test_analyze_sentiment.py +++ b/sdk/textanalytics/azure-ai-textanalytics/tests/test_analyze_sentiment.py @@ -47,6 +47,15 @@ def test_all_successful_passing_dict(self, client): self.assertIsNotNone(doc.confidence_scores) self.assertIsNotNone(doc.sentences) + self.assertEqual(len(response[0].sentences), 1) + self.assertEqual(response[0].sentences[0].text, "Microsoft was founded by Bill Gates and Paul Allen.") + self.assertEqual(len(response[1].sentences), 2) + self.assertEqual(response[1].sentences[0].text, "I did not like the hotel we stayed at.") + self.assertEqual(response[1].sentences[1].text, "It was too expensive.") + self.assertEqual(len(response[2].sentences), 2) + self.assertEqual(response[2].sentences[0].text, "The restaurant had really good food.") + self.assertEqual(response[2].sentences[1].text, "I recommend you try it.") + @GlobalTextAnalyticsAccountPreparer() @TextAnalyticsClientPreparer() def test_all_successful_passing_text_document_input(self, client): @@ -65,6 +74,15 @@ def test_all_successful_passing_text_document_input(self, client): self.assertIsNotNone(doc.confidence_scores) self.assertIsNotNone(doc.sentences) + self.assertEqual(len(response[0].sentences), 1) + self.assertEqual(response[0].sentences[0].text, "Microsoft was founded by Bill Gates and Paul Allen.") + self.assertEqual(len(response[1].sentences), 2) + self.assertEqual(response[1].sentences[0].text, "I did not like the hotel we stayed at.") + self.assertEqual(response[1].sentences[1].text, "It was too expensive.") + self.assertEqual(len(response[2].sentences), 2) + self.assertEqual(response[2].sentences[0].text, "The restaurant had really good food.") + self.assertEqual(response[2].sentences[1].text, "I recommend you try it.") + @GlobalTextAnalyticsAccountPreparer() @TextAnalyticsClientPreparer() def test_passing_only_string(self, client): diff --git a/sdk/textanalytics/azure-ai-textanalytics/tests/test_analyze_sentiment_async.py b/sdk/textanalytics/azure-ai-textanalytics/tests/test_analyze_sentiment_async.py index 7cf100435b64..9ffc17ad41eb 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/tests/test_analyze_sentiment_async.py +++ b/sdk/textanalytics/azure-ai-textanalytics/tests/test_analyze_sentiment_async.py @@ -63,6 +63,15 @@ async def test_all_successful_passing_dict(self, client): self.assertIsNotNone(doc.confidence_scores) self.assertIsNotNone(doc.sentences) + self.assertEqual(len(response[0].sentences), 1) + self.assertEqual(response[0].sentences[0].text, "Microsoft was founded by Bill Gates and Paul Allen.") + self.assertEqual(len(response[1].sentences), 2) + self.assertEqual(response[1].sentences[0].text, "I did not like the hotel we stayed at.") + self.assertEqual(response[1].sentences[1].text, "It was too expensive.") + self.assertEqual(len(response[2].sentences), 2) + self.assertEqual(response[2].sentences[0].text, "The restaurant had really good food.") + self.assertEqual(response[2].sentences[1].text, "I recommend you try it.") + @GlobalTextAnalyticsAccountPreparer() @TextAnalyticsClientPreparer() async def test_all_successful_passing_text_document_input(self, client): @@ -81,6 +90,15 @@ async def test_all_successful_passing_text_document_input(self, client): self.assertIsNotNone(doc.confidence_scores) self.assertIsNotNone(doc.sentences) + self.assertEqual(len(response[0].sentences), 1) + self.assertEqual(response[0].sentences[0].text, "Microsoft was founded by Bill Gates and Paul Allen.") + self.assertEqual(len(response[1].sentences), 2) + self.assertEqual(response[1].sentences[0].text, "I did not like the hotel we stayed at.") + self.assertEqual(response[1].sentences[1].text, "It was too expensive.") + self.assertEqual(len(response[2].sentences), 2) + self.assertEqual(response[2].sentences[0].text, "The restaurant had really good food.") + self.assertEqual(response[2].sentences[1].text, "I recommend you try it.") + @GlobalTextAnalyticsAccountPreparer() @TextAnalyticsClientPreparer() async def test_passing_only_string(self, client): diff --git a/sdk/textanalytics/azure-ai-textanalytics/tests/test_text_analytics.py b/sdk/textanalytics/azure-ai-textanalytics/tests/test_text_analytics.py index 87dc75c58476..db854b7795fe 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/tests/test_text_analytics.py +++ b/sdk/textanalytics/azure-ai-textanalytics/tests/test_text_analytics.py @@ -79,6 +79,7 @@ def test_repr(self): _models.SentimentConfidenceScores(positive=0.99, neutral=0.05, negative=0.02) sentence_sentiment = _models.SentenceSentiment( + text="This is a sentence.", sentiment="neutral", confidence_scores=sentiment_confidence_score_per_label, grapheme_offset=0, @@ -141,12 +142,12 @@ def test_repr(self): "transaction_count=18), is_error=False)", repr(recognize_linked_entities_result)) self.assertEqual("SentimentConfidenceScores(positive=0.99, neutral=0.05, negative=0.02)", repr(sentiment_confidence_score_per_label)) - self.assertEqual("SentenceSentiment(sentiment=neutral, confidence_scores=SentimentConfidenceScores(" + self.assertEqual("SentenceSentiment(text=This is a sentence., sentiment=neutral, confidence_scores=SentimentConfidenceScores(" "positive=0.99, neutral=0.05, negative=0.02), grapheme_offset=0, grapheme_length=10)", repr(sentence_sentiment)) self.assertEqual("AnalyzeSentimentResult(id=1, sentiment=positive, statistics=TextDocumentStatistics(" "grapheme_count=14, transaction_count=18), confidence_scores=SentimentConfidenceScores" "(positive=0.99, neutral=0.05, negative=0.02), " - "sentences=[SentenceSentiment(sentiment=neutral, confidence_scores=" + "sentences=[SentenceSentiment(text=This is a sentence., sentiment=neutral, confidence_scores=" "SentimentConfidenceScores(positive=0.99, neutral=0.05, negative=0.02), " "grapheme_offset=0, grapheme_length=10)], is_error=False)", repr(analyze_sentiment_result))