Skip to content

[text analytics] Add text to sentence sentiment #11054

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

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
3 changes: 3 additions & 0 deletions sdk/textanalytics/azure-ai-textanalytics/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -620,15 +623,17 @@ 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,
grapheme_length=sentence.length
)

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]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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))
Expand Down