Example #1
0
        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);

            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);

            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 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);
        }
Example #5
0
        public async Task AnalyzeOperation()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

            var client = new TextAnalyticsClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

            #region Snippet:TextAnalyticsAnalyzeOperation

            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 = client.StartAnalyzeOperationBatch(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;

            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("");
            }
        }