public async Task AnalyzeOperationWithTopParameter() { TextAnalyticsClient client = GetClient(); AnalyzeOperationOptions operationOptions = new AnalyzeOperationOptions() { KeyPhrasesTaskParameters = new KeyPhrasesTaskParameters(), DisplayName = "AnalyzeOperationWithSkipParameter", //Top = 1 }; AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(batchConvenienceDocuments, operationOptions, "en"); await operation.WaitForCompletionAsync(); AnalyzeOperationResult resultCollection = operation.Value; ExtractKeyPhrasesResultCollection result = resultCollection.Tasks.KeyPhraseExtractionTasks[0].Results; Assert.IsNotNull(result); Assert.AreEqual(1, result.Count); var keyPhrasesListId1 = new List <string> { "CEO of SpaceX", "Elon Musk", "Tesla" }; foreach (string keyphrase in result[0].KeyPhrases) { Assert.IsTrue(keyPhrasesListId1.Contains(keyphrase)); } }
public async Task AnalyzeOperationBatchWithErrorTest() { TextAnalyticsClient client = GetClient(); var documents = new List <string> { "Subject is taking 100mg of ibuprofen twice daily", "Can cause rapid or irregular heartbeat, delirium, panic, psychosis, and heart failure.", "", }; AnalyzeOperationOptions operationOptions = new AnalyzeOperationOptions() { KeyPhrasesTaskParameters = new KeyPhrasesTaskParameters(), DisplayName = "AnalyzeOperationBatchWithErrorTest", //Skip = 1 }; await Task.Run(() => { RequestFailedException ex = Assert.ThrowsAsync <RequestFailedException>(async() => { AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(documents, operationOptions, "en"); }); Assert.IsTrue(ex.ErrorCode.Equals("InvalidArgument")); Assert.IsTrue(ex.Status.Equals(400)); }); }
public async Task AnalyzeOperationWithAADTest() { TextAnalyticsClient client = GetClient(useTokenCredential: true); AnalyzeOperationOptions operationOptions = new AnalyzeOperationOptions() { KeyPhrasesTaskParameters = new KeyPhrasesTaskParameters(), }; AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(batchConvenienceDocuments, operationOptions); await operation.WaitForCompletionAsync(PollingInterval); AnalyzeOperationResult resultCollection = operation.Value; IReadOnlyList <EntityRecognitionTasksItem> entityRecognitionTasksItemCollection = resultCollection.Tasks.EntityRecognitionTasks; IReadOnlyList <EntityRecognitionPiiTasksItem> entityRecognitionPiiTasksItemCollection = resultCollection.Tasks.EntityRecognitionPiiTasks; ExtractKeyPhrasesResultCollection keyPhrasesResult = resultCollection.Tasks.KeyPhraseExtractionTasks[0].Results; Assert.IsNotNull(keyPhrasesResult); Assert.IsNotNull(entityRecognitionTasksItemCollection); Assert.IsNotNull(entityRecognitionPiiTasksItemCollection); Assert.AreEqual(2, keyPhrasesResult.Count); }
public async Task AnalyzeOperationWithPagination() { TextAnalyticsClient client = GetClient(); List <string> documents = new (); for (int i = 0; i < 23; i++) { documents.Add("Elon Musk is the CEO of SpaceX and Tesla."); } AnalyzeOperationOptions operationOptions = new AnalyzeOperationOptions() { KeyPhrasesTaskParameters = new KeyPhrasesTaskParameters(), DisplayName = "AnalyzeOperationWithSkipParameter", }; AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(documents, operationOptions); Assert.IsFalse(operation.HasCompleted); Assert.IsFalse(operation.HasValue); Assert.ThrowsAsync <InvalidOperationException>(async() => await Task.Run(() => operation.Value)); Assert.Throws <InvalidOperationException>(() => operation.GetValues()); await operation.WaitForCompletionAsync(PollingInterval); Assert.IsTrue(operation.HasCompleted); Assert.IsTrue(operation.HasValue); await operation.WaitForCompletionAsync(PollingInterval); // try async //There most be 2 pages as service limit is 20 documents per page List <AnalyzeOperationResult> asyncPages = operation.Value.ToEnumerableAsync().Result; Assert.AreEqual(2, asyncPages.Count); // First page should have 20 results Assert.AreEqual(20, asyncPages[0].Tasks.KeyPhraseExtractionTasks.FirstOrDefault().Results.Count); // Second page should have remaining 3 results Assert.AreEqual(3, asyncPages[1].Tasks.KeyPhraseExtractionTasks.FirstOrDefault().Results.Count); // try sync //There most be 2 pages as service limit is 20 documents per page List <AnalyzeOperationResult> pages = operation.GetValues().AsEnumerable().ToList(); Assert.AreEqual(2, pages.Count); // First page should have 20 results Assert.AreEqual(20, pages[0].Tasks.KeyPhraseExtractionTasks.FirstOrDefault().Results.Count); // Second page should have remaining 3 results Assert.AreEqual(3, pages[1].Tasks.KeyPhraseExtractionTasks.FirstOrDefault().Results.Count); }
public async Task AnalyzeOperationWithLanguageTest() { TextAnalyticsClient client = GetClient(); var batchDocuments = new List <TextDocumentInput> { new TextDocumentInput("1", "Microsoft was founded by Bill Gates and Paul Allen.") { Language = "en", }, new TextDocumentInput("2", "Mi perro y mi gato tienen que ir al veterinario.") { Language = "es", } }; AnalyzeOperationOptions operationOptions = new AnalyzeOperationOptions() { KeyPhrasesTaskParameters = new KeyPhrasesTaskParameters(), DisplayName = "AnalyzeOperationWithLanguageTest" }; AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(batchDocuments, operationOptions); await operation.WaitForCompletionAsync(PollingInterval); //Take the first page AnalyzeOperationResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); ExtractKeyPhrasesResultCollection keyPhrasesResult = resultCollection.Tasks.KeyPhraseExtractionTasks[0].Results; Assert.IsNotNull(keyPhrasesResult); Assert.AreEqual(2, keyPhrasesResult.Count); Assert.AreEqual("AnalyzeOperationWithLanguageTest", resultCollection.DisplayName); var keyPhrasesListId1 = new List <string> { "Bill Gates", "Paul Allen", "Microsoft" }; var keyPhrasesListId2 = new List <string> { "gato", "perro", "veterinario" }; foreach (string keyphrase in keyPhrasesResult[0].KeyPhrases) { Assert.IsTrue(keyPhrasesListId1.Contains(keyphrase)); } foreach (string keyphrase in keyPhrasesResult[1].KeyPhrases) { Assert.IsTrue(keyPhrasesListId2.Contains(keyphrase)); } }
public async Task AnalyzeOperationTest() { TextAnalyticsClient client = GetClient(); AnalyzeOperationOptions operationOptions = new AnalyzeOperationOptions() { KeyPhrasesTaskParameters = new KeyPhrasesTaskParameters(), }; AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(batchConvenienceDocuments, operationOptions, "en"); await operation.WaitForCompletionAsync(PollingInterval); //Take the first page AnalyzeOperationResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); IReadOnlyList <EntityRecognitionTasksItem> entityRecognitionTasksItemCollection = resultCollection.Tasks.EntityRecognitionTasks; IReadOnlyList <EntityRecognitionPiiTasksItem> entityRecognitionPiiTasksItemCollection = resultCollection.Tasks.EntityRecognitionPiiTasks; ExtractKeyPhrasesResultCollection keyPhrasesResult = resultCollection.Tasks.KeyPhraseExtractionTasks[0].Results; Assert.IsNotNull(keyPhrasesResult); Assert.IsNotNull(entityRecognitionTasksItemCollection); Assert.IsNotNull(entityRecognitionPiiTasksItemCollection); Assert.AreEqual(2, keyPhrasesResult.Count); var keyPhrasesListId1 = new List <string> { "CEO of SpaceX", "Elon Musk", "Tesla" }; var keyPhrasesListId2 = new List <string> { "Tesla stock", "year" }; foreach (string keyphrase in keyPhrasesResult[0].KeyPhrases) { Assert.IsTrue(keyPhrasesListId1.Contains(keyphrase)); } foreach (string keyphrase in keyPhrasesResult[1].KeyPhrases) { Assert.IsTrue(keyPhrasesListId2.Contains(keyphrase)); } }
public async Task AnalyzeOperationBatchWithStatisticsTest() { TextAnalyticsClient client = GetClient(); var batchDocuments = new List <TextDocumentInput> { new TextDocumentInput("1", "Microsoft was founded by Bill Gates and Paul Allen.") { Language = "en", }, new TextDocumentInput("2", "Mi perro y mi gato tienen que ir al veterinario.") { Language = "es", } }; AnalyzeOperationOptions operationOptions = new AnalyzeOperationOptions() { KeyPhrasesTaskParameters = new KeyPhrasesTaskParameters(), DisplayName = "AnalyzeOperationTest", IncludeStatistics = true }; AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(batchDocuments, operationOptions); await operation.WaitForCompletionAsync(PollingInterval); //Take the first page AnalyzeOperationResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); ExtractKeyPhrasesResultCollection result = resultCollection.Tasks.KeyPhraseExtractionTasks[0].Results; Assert.IsNotNull(result); Assert.AreEqual(2, result.Count); // TODO - Update this once service start returning statistics. // TODO - Add Other request level statistics. Assert.AreEqual(0, result[0].Statistics.CharacterCount); Assert.AreEqual(0, result[0].Statistics.TransactionCount); }
public async Task AnalyzeOperationBatchWithPHIDomain() { TextAnalyticsClient client = GetClient(); var documents = new List <string> { "A patient with medical id 12345678 whose phone number is 800-102-1100 is going under heart surgery", }; AnalyzeOperationOptions operationOptions = new AnalyzeOperationOptions() { PiiTaskParameters = new PiiTaskParameters() { Domain = PiiTaskParametersDomain.Phi }, DisplayName = "AnalyzeOperationWithPHIDomain" }; AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(documents, operationOptions, "en"); await operation.WaitForCompletionAsync(PollingInterval); //Take the first page AnalyzeOperationResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); RecognizePiiEntitiesResultCollection result = resultCollection.Tasks.EntityRecognitionPiiTasks[0].Results;; Assert.IsNotNull(result); Assert.AreEqual(1, result.Count); // TODO - Update this once the service starts returning RedactedText //var redactedText = string.Empty; //Assert.AreEqual(redactedText, result[0].Entities.RedactedText); Assert.IsFalse(result[0].HasError); Assert.AreEqual(2, result[0].Entities.Count); }
public async Task AnalyzeOperationAsync_AutomaticPollingWithSetInterval() { string endpoint = TestEnvironment.Endpoint; string apiKey = TestEnvironment.ApiKey; var client = new TextAnalyticsClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); #region Snippet:AnalyzeOperationBatchAsyncAutomaticPolling string document = @"We went to Contoso Steakhouse located at midtown NYC last week for a dinner party, and we adore the spot! They provide marvelous food and they have a great menu. The chief cook happens to be the owner (I think his name is John Doe) and he is super nice, coming out of the kitchen and greeted us all. We enjoyed very much dining in the place! The Sirloin steak I ordered was tender and juicy, and the place was impeccably clean. You can even pre-order from their online menu at www.contososteakhouse.com, call 312-555-0176 or send email to [email protected]! The only complaint I have is the food didn't come fast enough. Overall I highly recommend it!"; var batchDocuments = new List <TextDocumentInput> { new TextDocumentInput("1", document) { Language = "en", } }; AnalyzeOperationOptions operationOptions = new AnalyzeOperationOptions() { KeyPhrasesTaskParameters = new KeyPhrasesTaskParameters(), EntitiesTaskParameters = new EntitiesTaskParameters(), PiiTaskParameters = new PiiTaskParameters(), DisplayName = "AnalyzeOperationSample" }; AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(batchDocuments, operationOptions); TimeSpan pollingInterval = new TimeSpan(1000); await operation.WaitForCompletionAsync(pollingInterval); await foreach (AnalyzeOperationResult documentsInPage in operation.Value) { RecognizeEntitiesResultCollection entitiesResult = documentsInPage.Tasks.EntityRecognitionTasks[0].Results; ExtractKeyPhrasesResultCollection keyPhrasesResult = documentsInPage.Tasks.KeyPhraseExtractionTasks[0].Results; RecognizePiiEntitiesResultCollection piiResult = documentsInPage.Tasks.EntityRecognitionPiiTasks[0].Results; Console.WriteLine("Recognized Entities"); foreach (RecognizeEntitiesResult result in entitiesResult) { Console.WriteLine($" Recognized the following {result.Entities.Count} entities:"); foreach (CategorizedEntity entity in result.Entities) { Console.WriteLine($" Entity: {entity.Text}"); Console.WriteLine($" Category: {entity.Category}"); Console.WriteLine($" Offset: {entity.Offset}"); Console.WriteLine($" ConfidenceScore: {entity.ConfidenceScore}"); Console.WriteLine($" SubCategory: {entity.SubCategory}"); } Console.WriteLine(""); } Console.WriteLine("Recognized PII Entities"); foreach (RecognizePiiEntitiesResult result in piiResult) { Console.WriteLine($" Recognized the following {result.Entities.Count} PII entities:"); foreach (PiiEntity entity in result.Entities) { Console.WriteLine($" Entity: {entity.Text}"); Console.WriteLine($" Category: {entity.Category}"); Console.WriteLine($" Offset: {entity.Offset}"); Console.WriteLine($" ConfidenceScore: {entity.ConfidenceScore}"); Console.WriteLine($" SubCategory: {entity.SubCategory}"); } Console.WriteLine(""); } Console.WriteLine("Key Phrases"); foreach (ExtractKeyPhrasesResult result in keyPhrasesResult) { Console.WriteLine($" Recognized the following {result.KeyPhrases.Count} Keyphrases:"); foreach (string keyphrase in result.KeyPhrases) { Console.WriteLine($" {keyphrase}"); } Console.WriteLine(""); } } }
public async Task AnalyzeOperationWithMultipleTasks() { TextAnalyticsClient client = GetClient(); var batchDocuments = new List <TextDocumentInput> { new TextDocumentInput("1", "Microsoft was founded by Bill Gates and Paul Allen.") { Language = "en", }, new TextDocumentInput("2", "Mi perro y mi gato tienen que ir al veterinario.") { Language = "es", } }; AnalyzeOperationOptions operationOptions = new AnalyzeOperationOptions() { KeyPhrasesTaskParameters = new KeyPhrasesTaskParameters(), EntitiesTaskParameters = new EntitiesTaskParameters(), PiiTaskParameters = new PiiTaskParameters(), DisplayName = "AnalyzeOperationWithMultipleTasks" }; AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(batchDocuments, operationOptions); await operation.WaitForCompletionAsync(); AnalyzeOperationResult resultCollection = operation.Value; RecognizeEntitiesResultCollection entitiesResult = resultCollection.Tasks.EntityRecognitionTasks[0].Results; ExtractKeyPhrasesResultCollection keyPhrasesResult = resultCollection.Tasks.KeyPhraseExtractionTasks[0].Results; RecognizePiiEntitiesResultCollection piiResult = resultCollection.Tasks.EntityRecognitionPiiTasks[0].Results; Assert.IsNotNull(keyPhrasesResult); Assert.IsNotNull(entitiesResult); Assert.IsNotNull(piiResult); Assert.AreEqual("AnalyzeOperationWithMultipleTasks", resultCollection.DisplayName); // Keyphrases Assert.AreEqual(2, keyPhrasesResult.Count); var keyPhrasesListId1 = new List <string> { "Bill Gates", "Paul Allen", "Microsoft" }; var keyPhrasesListId2 = new List <string> { "gato", "perro", "veterinario" }; foreach (string keyphrase in keyPhrasesResult[0].KeyPhrases) { Assert.IsTrue(keyPhrasesListId1.Contains(keyphrase)); } foreach (string keyphrase in keyPhrasesResult[1].KeyPhrases) { Assert.IsTrue(keyPhrasesListId2.Contains(keyphrase)); } // Entities Assert.AreEqual(2, entitiesResult.Count); Assert.AreEqual(3, entitiesResult[0].Entities.Count); var entitiesList = new List <string> { "Bill Gates", "Microsoft", "Paul Allen" }; foreach (CategorizedEntity entity in entitiesResult[0].Entities) { Assert.IsTrue(entitiesList.Contains(entity.Text)); Assert.IsNotNull(entity.Category); Assert.IsNotNull(entity.Offset); Assert.IsNotNull(entity.ConfidenceScore); } // PII Assert.AreEqual(2, entitiesResult.Count); Assert.AreEqual(3, entitiesResult[0].Entities.Count); Assert.IsNotNull(entitiesResult[0].Id); Assert.IsNotNull(entitiesResult[0].Entities); Assert.IsNotNull(entitiesResult[0].Error); }