From 5dd5612b4e6e88dd5b2698eb28d52d4243932be0 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Fri, 24 Apr 2020 17:31:12 -0400 Subject: [PATCH 1/4] adding text to SentenceSentiment --- sdk/textanalytics/azure-ai-textanalytics/CHANGELOG.md | 2 ++ .../azure/ai/textanalytics/_models.py | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/sdk/textanalytics/azure-ai-textanalytics/CHANGELOG.md b/sdk/textanalytics/azure-ai-textanalytics/CHANGELOG.md index 5a584eb99a21..fc8ec1aa36e8 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/CHANGELOG.md +++ b/sdk/textanalytics/azure-ai-textanalytics/CHANGELOG.md @@ -2,6 +2,8 @@ ## 1.0.0b5 (Unreleased) +**New features** +- Added `text` property to `SentenceSentiment` ## 1.0.0b4 (2020-04-07) 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 8d3ec48da743..c7e33fdc5ce1 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,8 @@ 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] From f6bddec3a449962d8825d0a423efd1de05317724 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Fri, 24 Apr 2020 17:31:20 -0400 Subject: [PATCH 2/4] added tests --- .../tests/test_analyze_sentiment.py | 18 ++++++++++++++++++ .../tests/test_analyze_sentiment_async.py | 18 ++++++++++++++++++ .../tests/test_text_analytics.py | 11 +++++------ 3 files changed, 41 insertions(+), 6 deletions(-) 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 256eaae17a51..f51b9acf6a54 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,16 +142,14 @@ 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(" - "positive=0.99, neutral=0.05, negative=0.02), grapheme_offset=0, grapheme_length=10, warnings=" - "['sentence was too short to find sentiment'])", repr(sentence_sentiment)) + 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, " - "warnings=['sentence was too short to find sentiment'])], is_error=False)", + "grapheme_offset=0, grapheme_length=10)], is_error=False)", repr(analyze_sentiment_result)) self.assertEqual("DocumentError(id=1, error=TextAnalyticsError(code=invalidRequest, " "message=The request is invalid, target=request), is_error=True)", repr(document_error)) From bae70b7ab23b3305f2bcd23e7499ccc885d72071 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Fri, 24 Apr 2020 17:31:27 -0400 Subject: [PATCH 3/4] added samples --- .../samples/async_samples/sample_analyze_sentiment_async.py | 4 ++-- .../samples/sample_analyze_sentiment.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) 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, From 3ca5dd64ee5239a955c8956364da13a4f7c07a56 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Fri, 24 Apr 2020 17:34:37 -0400 Subject: [PATCH 4/4] fixed pylint --- .../azure-ai-textanalytics/azure/ai/textanalytics/_models.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 c7e33fdc5ce1..a66e7e3f7192 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/_models.py +++ b/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/_models.py @@ -631,8 +631,9 @@ def _from_generated(cls, sentence): ) def __repr__(self): - 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 + 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]