Exemple #1
0
        public async Task AnalyzeSentimentWithOpinionMiningNegated()
        {
            TextAnalyticsClient client = GetClient();
            string document            = "The bathrooms are not clean.";

            DocumentSentiment docSentiment = await client.AnalyzeSentimentAsync(document, options : new AnalyzeSentimentOptions()
            {
                IncludeOpinionMining = true
            });

            CheckAnalyzeSentimentProperties(docSentiment, opinionMining: true);
            SentenceOpinion opinion = docSentiment.Sentences.FirstOrDefault().Opinions.FirstOrDefault();

            Assert.AreEqual("bathrooms", opinion.Target.Text);
            Assert.AreEqual(TextSentiment.Negative, opinion.Target.Sentiment);
            Assert.AreEqual("clean", opinion.Assessments.FirstOrDefault().Text);
            Assert.AreEqual(TextSentiment.Negative, opinion.Assessments.FirstOrDefault().Sentiment);
            Assert.IsTrue(opinion.Assessments.FirstOrDefault().IsNegated);
        }
        public async Task AnalyzeSentimentAssessmentInOtherSentence()
        {
            using var stream = new MemoryStream(Encoding.UTF8.GetBytes(@"
                {
                    ""documents"": [
                        {
                            ""id"": ""1"",
                            ""sentiment"": ""positive"",
                            ""confidenceScores"": {
                                ""positive"": 0.5,
                                ""neutral"": 0.0,
                                ""negative"": 0.5
                            },
                            ""sentences"": [
                                {
                                    ""sentiment"": ""positive"",
                                    ""confidenceScores"": {
                                        ""positive"": 1.0,
                                        ""neutral"": 0.0,
                                        ""negative"": 0.0
                                    },
                                    ""offset"": 0,
                                    ""length"": 30,
                                    ""text"": ""The park was clean."",
                                    ""targets"": [
                                        {
                                            ""sentiment"": ""positive"",
                                            ""confidenceScores"": {
                                                ""positive"": 1.0,
                                                ""negative"": 0.0
                                            },
                                            ""offset"": 4,
                                            ""length"": 4,
                                            ""text"": ""park"",
                                            ""relations"": [
                                                {
                                                    ""relationType"": ""assessment"",
                                                    ""ref"": ""#/documents/0/sentences/0/assessments/0""
                                                }
                                            ]
                                        }
                                    ],
                                    ""assessments"": [
                                        {
                                            ""sentiment"": ""positive"",
                                            ""confidenceScores"": {
                                                ""positive"": 1.0,
                                                ""negative"": 0.0
                                            },
                                            ""offset"": 13,
                                            ""length"": 5,
                                            ""text"": ""clean"",
                                            ""isNegated"": false
                                        }
                                    ]
                                },
                                {
                                    ""sentiment"": ""positive"",
                                    ""confidenceScores"": {
                                        ""positive"": 0.0,
                                        ""neutral"": 0.0,
                                        ""negative"": 1.0
                                    },
                                    ""offset"": 31,
                                    ""length"": 23,
                                    ""text"": ""It was clean."",
                                    ""targets"": [
                                        {
                                            ""sentiment"": ""positive"",
                                            ""confidenceScores"": {
                                                ""positive"": 0.0,
                                                ""negative"": 1.0
                                            },
                                            ""offset"": 35,
                                            ""length"": 4,
                                            ""text"": ""park"",
                                            ""relations"": [
                                                {
                                                    ""relationType"": ""assessment"",
                                                    ""ref"": ""#/documents/0/sentences/0/assessments/0""
                                                }
                                            ]
                                        }
                                    ],
                                    ""assessments"": []
                                }
                            ],
                            ""warnings"": []
                        }
                    ],
                    ""errors"": [],
                    ""modelVersion"": ""2020-04-01""
                }"));

            var mockResponse = new MockResponse(200);

            mockResponse.ContentStream = stream;

            var mockTransport = new MockTransport(new[] { mockResponse });
            var client        = CreateTestClient(mockTransport);

            DocumentSentiment response = await client.AnalyzeSentimentAsync("The park was clean. It was clean.");

            SentenceOpinion opinionS1 = response.Sentences.ElementAt(0).Opinions.FirstOrDefault();

            Assert.AreEqual("park", opinionS1.Target.Text);
            Assert.AreEqual(TextSentiment.Positive, opinionS1.Target.Sentiment);
            Assert.AreEqual("clean", opinionS1.Assessments.FirstOrDefault().Text);

            SentenceOpinion opinionS2 = response.Sentences.ElementAt(1).Opinions.FirstOrDefault();

            Assert.AreEqual("park", opinionS2.Target.Text);
            Assert.AreEqual(TextSentiment.Positive, opinionS2.Target.Sentiment);
            Assert.AreEqual("clean", opinionS2.Assessments.FirstOrDefault().Text);
        }