Exemplo n.º 1
0
        public async Task <IDictionary <string, SentimentResult> > GetBatchSentimentAsync(Dictionary <string, string> textBatch)
        {
            this.ValidateBatchRequest(textBatch);

            if (!textBatch.Any())
            {
                return(new Dictionary <string, SentimentResult>());
            }

            string content;

            using (var response = await this._requestor.PostAsync(Constants.SentimentBatchRequest, BuildInputString(textBatch)))
            {
                content = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode)
                {
                    return(textBatch.ToDictionary(r => r.Key, r => SentimentResult.Build(this._errorMessageGenerator.GenerateError(response.StatusCode, content))));
                }
            }

            var result = JsonConvert.DeserializeObject <AzureSentimentBatchResult>(content);

            var parsedResults = result.SentimentBatch.ToDictionary(sr => sr.Id, sr => SentimentResult.Build(sr.Score));

            foreach (var error in result.Errors)
            {
                parsedResults.Add(error.Id, SentimentResult.Build(error.Message));
            }

            return(parsedResults);
        }
Exemplo n.º 2
0
        public async Task DecodeResponse()
        {
            var expected = SentimentResult.Build(1M);
            var sut      = TextAnalyticsTestHelper.BuildSut(GetMessage());

            var result = await sut.GetSentimentAsync(Input);

            Assert.AreEqual(expected, result);
        }
Exemplo n.º 3
0
        public async Task ReturnBadRequestIfEmptyInput()
        {
            var expected = SentimentResult.Build(Constants.SentimentNullInputErrorText);
            var sut      = TextAnalyticsTestHelper.BuildSut(GetMessage());

            var result = await sut.GetSentimentAsync(null);

            Assert.AreEqual(expected, result);
        }
        public async Task GetResultFromAzure()
        {
            var          expected = SentimentResult.Build(0.9742637M);
            const string Input    = "This is very positive text because I love this service";

            var sut    = ServiceFactory.Build();
            var result = await sut.GetSentimentAsync(Input);

            Assert.AreEqual(expected, result);
        }
Exemplo n.º 5
0
        public async Task ReturnFailureOnBadResult()
        {
            var error    = new ErrorMessageGenerator().GenerateError(HttpStatusCode.BadRequest, Error);
            var expected = SentimentResult.Build(error);

            var sut    = TextAnalyticsTestHelper.BuildSut(TextAnalyticsTestHelper.GetErrorMessage(Error));
            var result = await sut.GetSentimentAsync(Input);

            Assert.AreEqual(expected, result);
        }
Exemplo n.º 6
0
        public async Task DecodeResponse()
        {
            var expected = new Dictionary <string, SentimentResult>
            {
                { "1", SentimentResult.Build(0.9549767M) },
                { "2", SentimentResult.Build(0.7767222M) },
                { "3", SentimentResult.Build(0.8988889M) },
                { "4", SentimentResult.Build("Record cannot be null/empty") }
            };
            var sut = TextAnalyticsTestHelper.BuildSut(GetMessage());

            var result = await sut.GetBatchSentimentAsync(this._input);

            CollectionAssert.AreEquivalent(expected, result.ToList());
        }
Exemplo n.º 7
0
        public async Task ReturnAllFailureOnBadResult()
        {
            var err      = new ErrorMessageGenerator().GenerateError(HttpStatusCode.BadRequest, Error);
            var expected = new Dictionary <string, SentimentResult>
            {
                { "1", SentimentResult.Build(err) },
                { "2", SentimentResult.Build(err) },
                { "3", SentimentResult.Build(err) },
                { "4", SentimentResult.Build(err) }
            };

            var sut    = TextAnalyticsTestHelper.BuildSut(TextAnalyticsTestHelper.GetErrorMessage(Error));
            var result = await sut.GetBatchSentimentAsync(this._input);

            CollectionAssert.AreEqual(expected, result.ToList());
        }
Exemplo n.º 8
0
        public async Task GetResultFromAzure()
        {
            var input = new Dictionary <string, string>
            {
                { "1", "This is very positive text because I love this service" },
                { "2", "Test is very bad because I hate this service" },
                { "3", "The service was OK, nothing special, I've had better" },
                { "4", "" }
            };

            var expected = new Dictionary <string, SentimentResult>
            {
                { "1", SentimentResult.Build(0.9742637M) },
                { "2", SentimentResult.Build(0.03089833M) },
                { "3", SentimentResult.Build(0.4267564M) },
                { "4", SentimentResult.Build("Record cannot be null/empty") }
            };

            var sut    = ServiceFactory.Build();
            var result = await sut.GetBatchSentimentAsync(input);

            CollectionAssert.AreEquivalent(expected, result.ToList());
        }
Exemplo n.º 9
0
        public async Task <SentimentResult> GetSentimentAsync(string text)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return(SentimentResult.Build(Constants.SentimentNullInputErrorText));
            }

            var    request = $"{Constants.SentimentRequest}{HttpUtility.UrlEncode(text)}";
            string content;

            using (var response = await this._requestor.GetAsync(request))
            {
                content = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode)
                {
                    return(SentimentResult.Build(this._errorMessageGenerator.GenerateError(response.StatusCode, content)));
                }
            }

            var result = JsonConvert.DeserializeObject <AzureSentimentResult>(content);

            return(SentimentResult.Build(result.Score));
        }