public async Task <IDictionary <string, KeyPhraseResult> > GetBatchKeyPhrasesAsync(Dictionary <string, string> textBatch) { this.ValidateBatchRequest(textBatch); if (!textBatch.Any()) { return(new Dictionary <string, KeyPhraseResult>()); } string content; using (var response = await this._requestor.PostAsync(Constants.KeyPhraseBatchRequest, BuildInputString(textBatch))) { content = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { return(textBatch.ToDictionary(r => r.Key, r => KeyPhraseResult.Build(this._errorMessageGenerator.GenerateError(response.StatusCode, content)))); } } var result = JsonConvert.DeserializeObject <AzureKeyPhrasesBatchResult>(content); var parsedResults = result.KeyPhrasesBatch.ToDictionary(sr => sr.Id, sr => KeyPhraseResult.Build(sr.KeyPhrases)); foreach (var error in result.Errors) { parsedResults.Add(error.Id, KeyPhraseResult.Build(error.Message)); } return(parsedResults); }
public async Task <KeyPhraseResult> GetKeyPhrasesAsync(string text) { if (string.IsNullOrWhiteSpace(text)) { return(KeyPhraseResult.Build(Constants.KeyPhraseNullInputErrorText)); } var request = $"{Constants.KeyPhraseRequest}{HttpUtility.UrlEncode(text)}"; string content; using (var response = await this._requestor.GetAsync(request)) { content = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { return(KeyPhraseResult.Build(this._errorMessageGenerator.GenerateError(response.StatusCode, content))); } } var result = JsonConvert.DeserializeObject <AzureKeyPhraseResult>(content); return(KeyPhraseResult.Build(result.KeyPhrases)); }
public async Task DecodeResponse() { var expected = KeyPhraseResult.Build(new[] { "wonderful hotel", "unique decor", "friendly staff" }); var sut = TextAnalyticsTestHelper.BuildSut(GetMessage()); var result = await sut.GetKeyPhrasesAsync(Input); Assert.AreEqual(expected, result); }
public async Task ReturnBadRequestIfEmptyInput() { var expected = KeyPhraseResult.Build(Constants.KeyPhraseNullInputErrorText); var sut = TextAnalyticsTestHelper.BuildSut(TextAnalyticsTestHelper.GetErrorMessage(Error)); var result = await sut.GetKeyPhrasesAsync(null); Assert.AreEqual(expected, result); }
public async Task ReturnFailureOnBadResult() { var error = new ErrorMessageGenerator().GenerateError(HttpStatusCode.BadRequest, Error); var expected = KeyPhraseResult.Build(error); var sut = TextAnalyticsTestHelper.BuildSut(TextAnalyticsTestHelper.GetErrorMessage(Error)); var result = await sut.GetKeyPhrasesAsync(Input); Assert.AreEqual(expected, result); }
public async Task GetResultFromAzure() { var expected = KeyPhraseResult.Build(new[] { "bunch of phrases", "wonderful hotel", "great service", "text" }); const string Input = "I need some text that can extract a bunch of phrases from. This was a wonderful hotel with great service but really overpriced."; var sut = ServiceFactory.Build(); var result = await sut.GetKeyPhrasesAsync(Input); Assert.AreEqual(expected, result, string.Join(",", result.Phrases)); }
public async Task DecodeResponse() { var expected = new Dictionary <string, KeyPhraseResult> { { "1", KeyPhraseResult.Build(new[] { "unique decor", "friendly staff", "wonderful hotel" }) }, { "2", KeyPhraseResult.Build(new[] { "amazing build conference", "interesting talks" }) }, { "3", KeyPhraseResult.Build(new[] { "hours", "traffic", "airport" }) }, { "4", KeyPhraseResult.Build("Record cannot be null/empty") } }; var sut = TextAnalyticsTestHelper.BuildSut(GetMessage()); var result = await sut.GetBatchKeyPhrasesAsync(this._input); CollectionAssert.AreEquivalent(expected, result.ToList()); }
public async Task ReturnAllFailureOnBadResult() { var err = new ErrorMessageGenerator().GenerateError(HttpStatusCode.BadRequest, Error); var expected = new Dictionary <string, KeyPhraseResult> { { "1", KeyPhraseResult.Build(err) }, { "2", KeyPhraseResult.Build(err) }, { "3", KeyPhraseResult.Build(err) }, { "4", KeyPhraseResult.Build(err) } }; var sut = TextAnalyticsTestHelper.BuildSut(TextAnalyticsTestHelper.GetErrorMessage(Error)); var result = await sut.GetBatchKeyPhrasesAsync(this._input); CollectionAssert.AreEqual(expected, result.ToList()); }
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, KeyPhraseResult> { { "1", KeyPhraseResult.Build(new[] { "service", "positive text" }) }, { "2", KeyPhraseResult.Build(new[] { "Test", "service" }) }, { "3", KeyPhraseResult.Build(new[] { "service" }) }, { "4", KeyPhraseResult.Build("Record cannot be null/empty") } }; var sut = ServiceFactory.Build(); var result = await sut.GetBatchKeyPhrasesAsync(input); CollectionAssert.AreEquivalent(expected, result.ToList()); }