|
| 1 | +# coding=utf-8 |
| 2 | +# -------------------------------------------------------------------------- |
| 3 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | +# Licensed under the MIT License. See License.txt in the project root for |
| 5 | +# license information. |
| 6 | +# -------------------------------------------------------------------------- |
| 7 | +import pytest |
| 8 | +from azure.ai.textanalytics._models import ( |
| 9 | + AnalyzeSentimentResult, |
| 10 | + AspectSentiment, |
| 11 | + OpinionSentiment, |
| 12 | + SentenceSentiment, |
| 13 | + _get_indices, |
| 14 | +) |
| 15 | + |
| 16 | +from azure.ai.textanalytics._response_handlers import sentiment_result |
| 17 | + |
| 18 | +from azure.ai.textanalytics._generated.v3_1_preview_1 import models as _generated_models |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def generated_aspect_opinion_confidence_scores(): |
| 23 | + return _generated_models.AspectConfidenceScoreLabel( |
| 24 | + positive=1.0, |
| 25 | + neutral=0.0, |
| 26 | + negative=0.0, |
| 27 | + ) |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def generated_sentiment_confidence_score(): |
| 31 | + return _generated_models.SentimentConfidenceScorePerLabel( |
| 32 | + positive=1.0, |
| 33 | + neutral=0.0, |
| 34 | + negative=0.0, |
| 35 | + ) |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def generated_aspect_relation(): |
| 39 | + return _generated_models.AspectRelation( |
| 40 | + relation_type="opinion", |
| 41 | + ref="#/documents/0/sentences/1/opinions/0" |
| 42 | + ) |
| 43 | + |
| 44 | +@pytest.fixture |
| 45 | +def generated_aspect(generated_aspect_opinion_confidence_scores, generated_aspect_relation): |
| 46 | + return _generated_models.SentenceAspect( |
| 47 | + text="aspect", |
| 48 | + sentiment="positive", |
| 49 | + confidence_scores=generated_aspect_opinion_confidence_scores, |
| 50 | + offset=0, |
| 51 | + length=6, |
| 52 | + relations=[generated_aspect_relation], |
| 53 | + ) |
| 54 | + |
| 55 | +@pytest.fixture |
| 56 | +def generated_opinion(generated_aspect_opinion_confidence_scores): |
| 57 | + return _generated_models.SentenceOpinion( |
| 58 | + text="good", |
| 59 | + sentiment="positive", |
| 60 | + confidence_scores=generated_aspect_opinion_confidence_scores, |
| 61 | + offset=0, |
| 62 | + length=4, |
| 63 | + is_negated=False, |
| 64 | + ) |
| 65 | + |
| 66 | +def generated_sentence_sentiment(generated_sentiment_confidence_score, index, aspects=[], opinions=[]): |
| 67 | + return _generated_models.SentenceSentiment( |
| 68 | + text="not relevant", |
| 69 | + sentiment="positive", |
| 70 | + confidence_scores=generated_sentiment_confidence_score, |
| 71 | + offset=0, |
| 72 | + length=12, |
| 73 | + aspects=aspects, |
| 74 | + opinions=opinions, |
| 75 | + ) |
| 76 | + |
| 77 | +@pytest.fixture |
| 78 | +def generated_document_sentiment(generated_aspect, generated_opinion, generated_sentiment_confidence_score): |
| 79 | + aspect_sentence = generated_sentence_sentiment(generated_sentiment_confidence_score, index=0, aspects=[generated_aspect]) |
| 80 | + opinion_sentence = generated_sentence_sentiment(generated_sentiment_confidence_score, index=1, opinions=[generated_opinion]) |
| 81 | + |
| 82 | + return _generated_models.DocumentSentiment( |
| 83 | + id=1, |
| 84 | + sentiment="positive", |
| 85 | + confidence_scores=generated_sentiment_confidence_score, |
| 86 | + sentences=[aspect_sentence, opinion_sentence], |
| 87 | + warnings=[], |
| 88 | + ) |
| 89 | + |
| 90 | +@pytest.fixture |
| 91 | +def generated_sentiment_response(generated_document_sentiment): |
| 92 | + return _generated_models.SentimentResponse( |
| 93 | + documents=[generated_document_sentiment], |
| 94 | + errors=[], |
| 95 | + model_version="0000-00-00", |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +class TestJsonPointer(): |
| 100 | + |
| 101 | + def test_json_pointer_parsing(self): |
| 102 | + assert [1, 0, 15] == _get_indices("#/documents/1/sentences/0/opinions/15") |
| 103 | + |
| 104 | + def test_opinion_different_sentence_aspect(self, generated_sentiment_response): |
| 105 | + # the first sentence has the aspect, and the second sentence has the opinion |
| 106 | + # the desired behavior is the first wrapped sentence object has an aspect, and it's opinion |
| 107 | + # is in the second sentence. |
| 108 | + # the second sentence will have no mined opinions, since we define that as an aspect and opinion duo |
| 109 | + wrapped_sentiment = sentiment_result(response="not relevant", obj=generated_sentiment_response, response_headers={})[0] |
| 110 | + assert wrapped_sentiment.sentences[0].mined_opinions[0].opinions[0].text == "good" |
| 111 | + assert not wrapped_sentiment.sentences[1].mined_opinions |
0 commit comments