Esempio n. 1
0
        public async Task AnalyzeOperationConvenienceCanPollFromNewObject()
        {
            TextAnalyticsClient client = GetClient(out var nonInstrumentedClient);

            var documents = new List <string>
            {
                "Elon Musk is the CEO of SpaceX and Tesla.",
                "Tesla stock is up by 400% this year."
            };

            TextAnalyticsActions batchActions = new()
            {
                ExtractKeyPhrasesActions = new List <ExtractKeyPhrasesAction>()
                {
                    new ExtractKeyPhrasesAction()
                },
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(documents, batchActions);

            var sameOperation = InstrumentOperation(new AnalyzeActionsOperation(operation.Id, nonInstrumentedClient));
            await sameOperation.WaitForCompletionAsync();

            Assert.IsTrue(sameOperation.HasValue);
        }
Esempio n. 2
0
        public async Task ExtractSummaryBatchWithStatisticsTest()
        {
            TextAnalyticsClient client = GetClient();

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                ExtractSummaryActions = new List <ExtractSummaryAction>()
                {
                    new ExtractSummaryAction()
                    {
                        MaxSentenceCount = ExtractSummaryMaxSentenceCount
                    }
                }
            };

            AnalyzeActionsOptions options = new AnalyzeActionsOptions()
            {
                IncludeStatistics = true
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(s_extractSummaryBatchDocuments, batchActions, options);

            await operation.WaitForCompletionAsync();

            // Take the first page
            AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault();

            IReadOnlyCollection <ExtractSummaryActionResult> summaryActionsResults = resultCollection.ExtractSummaryResults;
            ExtractSummaryResultCollection summaryDocumentsResults = summaryActionsResults.FirstOrDefault().DocumentsResults;

            ValidateSummaryBatchResult(summaryDocumentsResults, includeStatistics: true);
        }
Esempio n. 3
0
        public async Task ExtractSummaryBatchWithErrorTest()
        {
            TextAnalyticsClient client = GetClient();

            var documents = new List <string>
            {
                "Subject is taking 100mg of ibuprofen twice daily",
                "",
            };
            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                ExtractSummaryActions = new List <ExtractSummaryAction>()
                {
                    new ExtractSummaryAction()
                }
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(documents, batchActions, "en");

            await operation.WaitForCompletionAsync();

            // Take the first page
            AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault();

            List <ExtractSummaryActionResult> summaryActions = resultCollection.ExtractSummaryResults.ToList();

            Assert.AreEqual(1, summaryActions.Count);

            ExtractSummaryResultCollection documentsResults = summaryActions[0].DocumentsResults;

            Assert.IsFalse(documentsResults[0].HasError);
            Assert.IsTrue(documentsResults[1].HasError);
            Assert.AreEqual(TextAnalyticsErrorCode.InvalidDocument, documentsResults[1].Error.ErrorCode.ToString());
        }
Esempio n. 4
0
        public async Task AnalyzeOperationTest()
        {
            TextAnalyticsClient client = GetClient();

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                ExtractKeyPhrasesActions = new List <ExtractKeyPhrasesAction>()
                {
                    new ExtractKeyPhrasesAction()
                },
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(batchConvenienceDocuments, batchActions, "en");

            await operation.WaitForCompletionAsync();

            //Take the first page
            AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault();

            IReadOnlyCollection <RecognizeEntitiesActionResult>       entitiesActionsResults               = resultCollection.RecognizeEntitiesResults;
            IReadOnlyCollection <ExtractKeyPhrasesActionResult>       keyPhrasesActionsResults             = resultCollection.ExtractKeyPhrasesResults;
            IReadOnlyCollection <RecognizePiiEntitiesActionResult>    piiActionsResults                    = resultCollection.RecognizePiiEntitiesResults;
            IReadOnlyCollection <RecognizeLinkedEntitiesActionResult> entityLinkingActionsResults          = resultCollection.RecognizeLinkedEntitiesResults;
            IReadOnlyCollection <AnalyzeSentimentActionResult>        analyzeSentimentActionsResults       = resultCollection.AnalyzeSentimentResults;
            IReadOnlyCollection <ExtractSummaryActionResult>          extractSummaryActionsResults         = resultCollection.ExtractSummaryResults;
            IReadOnlyCollection <RecognizeCustomEntitiesActionResult> recognizeCustomEntitiesActionResults = resultCollection.RecognizeCustomEntitiesResults;
            IReadOnlyCollection <SingleCategoryClassifyActionResult>  singleCategoryClassifyResults        = resultCollection.SingleCategoryClassifyResults;
            IReadOnlyCollection <MultiCategoryClassifyActionResult>   multiCategoryClassifyResults         = resultCollection.MultiCategoryClassifyResults;

            Assert.IsNotNull(keyPhrasesActionsResults);
            Assert.IsNotNull(entitiesActionsResults);
            Assert.IsNotNull(piiActionsResults);
            Assert.IsNotNull(entityLinkingActionsResults);
            Assert.IsNotNull(analyzeSentimentActionsResults);
            Assert.IsNotNull(extractSummaryActionsResults);
            Assert.IsNotNull(singleCategoryClassifyResults);
            Assert.IsNotNull(multiCategoryClassifyResults);
            Assert.IsNotNull(recognizeCustomEntitiesActionResults);

            var keyPhrasesListId1 = new List <string> {
                "CEO", "SpaceX", "Elon Musk", "Tesla"
            };
            var keyPhrasesListId2 = new List <string> {
                "Tesla stock"
            };

            ExtractKeyPhrasesResultCollection keyPhrasesDocumentsResults = keyPhrasesActionsResults.FirstOrDefault().DocumentsResults;

            Assert.AreEqual(2, keyPhrasesDocumentsResults.Count);

            foreach (string keyphrase in keyPhrasesDocumentsResults[0].KeyPhrases)
            {
                Assert.IsTrue(keyPhrasesListId1.Contains(keyphrase));
            }

            foreach (string keyphrase in keyPhrasesDocumentsResults[1].KeyPhrases)
            {
                Assert.IsTrue(keyPhrasesListId2.Contains(keyphrase));
            }
        }
Esempio n. 5
0
        public async Task ExtractSummaryWithDisableServiceLogs()
        {
            TextAnalyticsClient client = GetClient();

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                ExtractSummaryActions = new List <ExtractSummaryAction>()
                {
                    new ExtractSummaryAction()
                    {
                        DisableServiceLogs = true
                    }
                }
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(s_extractSummaryBatchConvenienceDocuments, batchActions);

            await operation.WaitForCompletionAsync();

            // Take the first page
            AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault();

            IReadOnlyCollection <ExtractSummaryActionResult> extractSummaryActionsResults = resultCollection.ExtractSummaryResults;

            Assert.IsNotNull(extractSummaryActionsResults);
            Assert.AreEqual(2, extractSummaryActionsResults.FirstOrDefault().DocumentsResults.Count);
        }
        public async Task CreateAnalyzeOperationConvenienceSetsOperationId()
        {
            var mockResponse = new MockResponse(202);

            mockResponse.AddHeader(new HttpHeader("Operation-Location", "something/jobs/2a96a91f-7edf-4931-a880-3fdee1d56f15"));

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

            var documents = new List <string>
            {
                "Elon Musk is the CEO of SpaceX and Tesla.",
                "Microsoft was founded by Bill Gates and Paul Allen.",
                "My cat and my dog might need to see a veterinarian."
            };

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                ExtractKeyPhrasesActions = new List <ExtractKeyPhrasesAction>()
                {
                    new ExtractKeyPhrasesAction()
                },
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(documents, batchActions);

            OperationContinuationToken continuationToken = OperationContinuationToken.Deserialize(operation.Id);

            Assert.IsNull(continuationToken.ShowStats);
            Assert.AreEqual("2a96a91f-7edf-4931-a880-3fdee1d56f15", continuationToken.JobId);
            Assert.AreEqual(3, continuationToken.InputDocumentOrder.Count);
            Assert.AreEqual(0, continuationToken.InputDocumentOrder["0"]);
            Assert.AreEqual(1, continuationToken.InputDocumentOrder["1"]);
            Assert.AreEqual(2, continuationToken.InputDocumentOrder["2"]);
        }
        public async Task SingleCategoryClassifyWithDisableServiceLogs()
        {
            TextAnalyticsClient client = GetClient();

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                SingleCategoryClassifyActions = new List <SingleCategoryClassifyAction>()
                {
                    new SingleCategoryClassifyAction(TestEnvironment.SingleClassificationProjectName, TestEnvironment.SingleClassificationDeploymentName)
                    {
                        DisableServiceLogs = true
                    }
                }
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(s_singleCategoryClassifyBatchConvenienceDocuments, batchActions);

            await PollUntilTimeout(operation);

            Assert.IsTrue(operation.HasCompleted);

            // Take the first page
            AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault();

            IReadOnlyCollection <SingleCategoryClassifyActionResult> singleCategoryClassifyActionsResults = resultCollection.SingleCategoryClassifyResults;

            Assert.IsNotNull(singleCategoryClassifyActionsResults);
            Assert.AreEqual(2, singleCategoryClassifyActionsResults.FirstOrDefault().DocumentsResults.Count);
        }
        public async Task SingleCategoryClassifyBatchWithStatisticsTest()
        {
            TextAnalyticsClient client = GetClient();

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                SingleCategoryClassifyActions = new List <SingleCategoryClassifyAction>()
                {
                    new SingleCategoryClassifyAction(TestEnvironment.SingleClassificationProjectName, TestEnvironment.SingleClassificationDeploymentName)
                }
            };

            AnalyzeActionsOptions options = new AnalyzeActionsOptions()
            {
                IncludeStatistics = true
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(s_singleCategoryClassifyBatchDocuments, batchActions, options);

            await PollUntilTimeout(operation);

            Assert.IsTrue(operation.HasCompleted);

            // Take the first page
            AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault();

            IReadOnlyCollection <SingleCategoryClassifyActionResult> singleCategoryClassifyActionsResults = resultCollection.SingleCategoryClassifyResults;
            SingleCategoryClassifyResultCollection singleCategoryClassifyResults = singleCategoryClassifyActionsResults.FirstOrDefault().DocumentsResults;

            ValidateSummaryBatchResult(singleCategoryClassifyResults, includeStatistics: true);
        }
Esempio n. 9
0
        public async Task MultiCategoryClassifyBatchTest()
        {
            TextAnalyticsClient client = GetClient();

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                MultiCategoryClassifyActions = new List <MultiCategoryClassifyAction>()
                {
                    new MultiCategoryClassifyAction(TestEnvironment.MultiClassificationProjectName, TestEnvironment.MultiClassificationDeploymentName)
                }
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(s_multiCategoryClassifyBatchDocuments, batchActions);

            await PollUntilTimeout(operation);

            Assert.IsTrue(operation.HasCompleted);

            // Take the first page
            AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault();

            IReadOnlyCollection <MultiCategoryClassifyActionResult> multiCategoryClassifyActionsResults = resultCollection.MultiCategoryClassifyResults;
            MultiCategoryClassifyResultCollection multiCategoryClassifyResults = multiCategoryClassifyActionsResults.FirstOrDefault().DocumentsResults;

            ValidateSummaryBatchResult(multiCategoryClassifyResults);
        }
Esempio n. 10
0
        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",
                }
            };

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                ExtractKeyPhrasesActions = new List <ExtractKeyPhrasesAction>()
                {
                    new ExtractKeyPhrasesAction()
                },
                DisplayName = "AnalyzeOperationWithLanguageTest"
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(batchDocuments, batchActions);

            await operation.WaitForCompletionAsync();

            //Take the first page
            AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault();

            IReadOnlyCollection <ExtractKeyPhrasesActionResult> keyPhrasesActionsResults = resultCollection.ExtractKeyPhrasesActionsResults;

            Assert.IsNotNull(keyPhrasesActionsResults);

            ExtractKeyPhrasesResultCollection keyPhrasesResult = keyPhrasesActionsResults.FirstOrDefault().Result;

            Assert.AreEqual(2, keyPhrasesResult.Count);

            Assert.AreEqual("AnalyzeOperationWithLanguageTest", operation.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));
            }
        }
Esempio n. 11
0
        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.");
            }

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                ExtractKeyPhrasesActions = new List <ExtractKeyPhrasesAction>()
                {
                    new ExtractKeyPhrasesAction()
                },
                DisplayName = "AnalyzeOperationWithPagination",
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(documents, batchActions);

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

            Assert.IsTrue(operation.HasCompleted);
            Assert.IsTrue(operation.HasValue);

            // try async
            //There most be 2 pages as service limit is 20 documents per page
            List <AnalyzeActionsResult> asyncPages = operation.Value.ToEnumerableAsync().Result;

            Assert.AreEqual(2, asyncPages.Count);

            // First page should have 20 results
            Assert.AreEqual(20, asyncPages[0].ExtractKeyPhrasesActionsResults.FirstOrDefault().Result.Count);

            // Second page should have remaining 3 results
            Assert.AreEqual(3, asyncPages[1].ExtractKeyPhrasesActionsResults.FirstOrDefault().Result.Count);

            // try sync
            //There most be 2 pages as service limit is 20 documents per page
            List <AnalyzeActionsResult> pages = operation.GetValues().AsEnumerable().ToList();

            Assert.AreEqual(2, pages.Count);

            // First page should have 20 results
            Assert.AreEqual(20, pages[0].ExtractKeyPhrasesActionsResults.FirstOrDefault().Result.Count);

            // Second page should have remaining 3 results
            Assert.AreEqual(3, pages[1].ExtractKeyPhrasesActionsResults.FirstOrDefault().Result.Count);
        }
        private async Task <(IEnumerable <AnalyzeSentimentResult> sentimentResults, IEnumerable <RecognizePiiEntitiesResult> piiResults, IEnumerable <string> errors)> GetOperationResults(int index, string operationId)
        {
            var errors           = new List <string>();
            var sentimentResults = new List <AnalyzeSentimentResult>();
            var piiResults       = new List <RecognizePiiEntitiesResult>();

            try
            {
                Log.LogInformation($"Sending text analytics request for document chunk with id {index}.");
                using var cts = new CancellationTokenSource();
                cts.CancelAfter(RequestTimeout);

                var textAnalyticsOperation = new AnalyzeActionsOperation(operationId, TextAnalyticsClient);

                await textAnalyticsOperation.UpdateStatusAsync().ConfigureAwait(false);

                if (!textAnalyticsOperation.HasCompleted)
                {
                    throw new InvalidOperationException("Tried to get result of not completed text analytics request.");
                }

                await foreach (var documentsInPage in textAnalyticsOperation.GetValuesAsync())
                {
                    foreach (var piiResult in documentsInPage.RecognizePiiEntitiesResults)
                    {
                        if (piiResult.HasError)
                        {
                            errors.Add($"PII recognition failed with error: {piiResult.Error.Message}");
                        }

                        piiResults.AddRange(piiResult.DocumentsResults);
                    }

                    foreach (var sentimentResult in documentsInPage.AnalyzeSentimentResults)
                    {
                        if (sentimentResult.HasError)
                        {
                            errors.Add($"Sentiment analysis failed with error: {sentimentResult.Error.Message}");
                        }

                        sentimentResults.AddRange(sentimentResult.DocumentsResults);
                    }
                }
            }
            catch (OperationCanceledException operationCanceledException)
            {
                throw new TransientFailureException($"Operation was canceled after {RequestTimeout.TotalSeconds} seconds.", operationCanceledException);
            }

            // do not catch throttling errors, rather throw and retry
            catch (RequestFailedException e) when(e.Status != 429)
            {
                errors.Add($"Text analytics request failed with error: {e.Message}");
            }

            return(sentimentResults, piiResults, errors);
        }
Esempio n. 13
0
        public async Task AnalyzeOperationWithStatisticsTest()
        {
            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",
                },
                new TextDocumentInput("3", "")
                {
                    Language = "es",
                }
            };

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                ExtractKeyPhrasesActions = new List <ExtractKeyPhrasesAction>()
                {
                    new ExtractKeyPhrasesAction()
                },
                DisplayName = "AnalyzeOperationTest",
            };

            AnalyzeActionsOptions options = new AnalyzeActionsOptions()
            {
                IncludeStatistics = true
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(batchDocuments, batchActions, options);

            await operation.WaitForCompletionAsync();

            //Take the first page
            AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault();

            ExtractKeyPhrasesResultCollection result = resultCollection.ExtractKeyPhrasesActionsResults.ElementAt(0).Result;

            Assert.IsNotNull(result);

            Assert.AreEqual(3, result.Count);

            Assert.AreEqual(3, result.Statistics.DocumentCount);
            Assert.AreEqual(2, result.Statistics.TransactionCount);
            Assert.AreEqual(2, result.Statistics.ValidDocumentCount);
            Assert.AreEqual(1, result.Statistics.InvalidDocumentCount);

            Assert.AreEqual(51, result[0].Statistics.CharacterCount);
            Assert.AreEqual(1, result[0].Statistics.TransactionCount);
        }
        // This has been added to stop the custom tests to run forever while we
        // get more reliable information on which scenarios cause timeouts.
        // Issue https://github.com/Azure/azure-sdk-for-net/issues/25152
        internal async Task PollUntilTimeout(AnalyzeActionsOperation operation, int timeoutInMinutes = 20)
        {
            TimeSpan pollingInterval = TimeSpan.FromSeconds(10);
            var      timeout         = TimeSpan.FromMinutes(timeoutInMinutes);

            using CancellationTokenSource cts = new CancellationTokenSource(timeout);
            try
            {
                await operation.WaitForCompletionAsync(pollingInterval, cts.Token);
            }
            catch (TaskCanceledException)
            {
                Debug.WriteLine("Test cancelled. Test timed out.");
            }
        }
        public void CreateAnalyzeOperationFromFakeValidOperationId()
        {
            var jobId = "2a96a91f-7edf-4931-a880-3fdee1d56f15";

            var mockResponse = new MockResponse(202);

            mockResponse.AddHeader(new HttpHeader("Operation-Location", $"something/jobs/{jobId}"));
            var mockTransport = new MockTransport(new[] { mockResponse, mockResponse });
            var client        = CreateTestClient(mockTransport);

            var inputOrder = new Dictionary <string, int>(1)
            {
                { "0", 0 }
            };
            string operationId = OperationContinuationToken.Serialize(jobId, inputOrder, true);

            var operation = new AnalyzeActionsOperation(operationId, client);

            Assert.AreEqual(operationId, operation.Id);
        }
Esempio n. 16
0
        public async Task AnalyzeOperationWithPiiCategories()
        {
            TextAnalyticsClient client = GetClient();

            var documents = new List <string>
            {
                "A developer with SSN 859-98-0987 whose phone number is 800-102-1100 is building tools with our APIs. They work at Microsoft.",
            };

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                RecognizePiiEntitiesActions = new List <RecognizePiiEntitiesAction>()
                {
                    new RecognizePiiEntitiesAction()
                    {
                        CategoriesFilter = { PiiEntityCategory.USSocialSecurityNumber }
                    }
                },
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(documents, batchActions, "en");

            await operation.WaitForCompletionAsync();

            //Take the first page
            AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault();

            IReadOnlyCollection <RecognizePiiEntitiesActionResult> piiActionsResults = resultCollection.RecognizePiiEntitiesResults;

            Assert.IsNotNull(piiActionsResults);

            RecognizePiiEntitiesResultCollection piiDocumentsResults = piiActionsResults.FirstOrDefault().DocumentsResults;

            Assert.AreEqual(1, piiDocumentsResults.Count);

            Assert.IsNotEmpty(piiDocumentsResults[0].Entities.RedactedText);

            Assert.IsFalse(piiDocumentsResults[0].HasError);
            Assert.AreEqual(1, piiDocumentsResults[0].Entities.Count);
            Assert.AreEqual(PiiEntityCategory.USSocialSecurityNumber, piiDocumentsResults[0].Entities.FirstOrDefault().Category);
        }
Esempio n. 17
0
        public async Task AnalyzeOperationWithPHIDomain()
        {
            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",
            };

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                RecognizePiiEntitiesActions = new List <RecognizePiiEntitiesAction>()
                {
                    new RecognizePiiEntitiesAction()
                    {
                        DomainFilter = PiiEntityDomainType.ProtectedHealthInformation
                    }
                },
                DisplayName = "AnalyzeOperationWithPHIDomain",
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(documents, batchActions, "en");

            await operation.WaitForCompletionAsync();

            //Take the first page
            AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault();

            IReadOnlyCollection <RecognizePiiEntitiesActionResult> piiActionsResults = resultCollection.RecognizePiiEntitiesActionsResults;

            Assert.IsNotNull(piiActionsResults);

            RecognizePiiEntitiesResultCollection piiResult = piiActionsResults.FirstOrDefault().Result;

            Assert.AreEqual(1, piiResult.Count);

            Assert.IsNotEmpty(piiResult[0].Entities.RedactedText);

            Assert.IsFalse(piiResult[0].HasError);
            Assert.AreEqual(2, piiResult[0].Entities.Count);
        }
        public async Task CreateAnalyzeOperationSetsOperationId()
        {
            var mockResponse = new MockResponse(202);

            mockResponse.AddHeader(new HttpHeader("Operation-Location", "something/jobs/2a96a91f-7edf-4931-a880-3fdee1d56f15"));

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

            var documents = new List <TextDocumentInput>
            {
                new TextDocumentInput("134234", "Elon Musk is the CEO of SpaceX and Tesla.")
                {
                    Language = "en",
                },
                new TextDocumentInput("324232", "Tesla stock is up by 400% this year.")
                {
                    Language = "en",
                }
            };

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                ExtractKeyPhrasesActions = new List <ExtractKeyPhrasesAction>()
                {
                    new ExtractKeyPhrasesAction()
                },
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(documents, batchActions);

            OperationContinuationToken continuationToken = OperationContinuationToken.Deserialize(operation.Id);

            Assert.IsNull(continuationToken.ShowStats);
            Assert.AreEqual("2a96a91f-7edf-4931-a880-3fdee1d56f15", continuationToken.JobId);
            Assert.AreEqual(2, continuationToken.InputDocumentOrder.Count);
            Assert.AreEqual(0, continuationToken.InputDocumentOrder["134234"]);
            Assert.AreEqual(1, continuationToken.InputDocumentOrder["324232"]);
        }
Esempio n. 19
0
        public async Task RecognizeCustomEntitiesWithMultipleActions()
        {
            TextAnalyticsClient client = GetClient();

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                RecognizeCustomEntitiesActions = new List <RecognizeCustomEntitiesAction>()
                {
                    new RecognizeCustomEntitiesAction(TestEnvironment.RecognizeCustomEntitiesProjectName, TestEnvironment.RecognizeCustomEntitiesDeploymentName)
                    {
                        DisableServiceLogs = true,
                        ActionName         = "RecognizeCustomEntitiesWithDisabledServiceLogs"
                    },
                    new RecognizeCustomEntitiesAction(TestEnvironment.RecognizeCustomEntitiesProjectName, TestEnvironment.RecognizeCustomEntitiesDeploymentName)
                    {
                        ActionName = "RecognizeCustomEntities"
                    }
                }
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(e_batchConvenienceDocuments, batchActions);

            await PollUntilTimeout(operation);

            Assert.IsTrue(operation.HasCompleted);

            // Take the first page
            AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault();

            IReadOnlyCollection <RecognizeCustomEntitiesActionResult> RecognizeCustomEntitiesActionsResults = resultCollection.RecognizeCustomEntitiesResults;

            Assert.IsNotNull(RecognizeCustomEntitiesActionsResults);

            IList <string> expected = new List <string> {
                "RecognizeCustomEntities", "RecognizeCustomEntitiesWithDisabledServiceLogs"
            };

            CollectionAssert.AreEquivalent(expected, RecognizeCustomEntitiesActionsResults.Select(result => result.ActionName));
        }
Esempio n. 20
0
        public async Task MultiCategoryClassifyWithMultipleActions()
        {
            TextAnalyticsClient client = GetClient();

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                MultiCategoryClassifyActions = new List <MultiCategoryClassifyAction>()
                {
                    new MultiCategoryClassifyAction(TestEnvironment.MultiClassificationProjectName, TestEnvironment.MultiClassificationDeploymentName)
                    {
                        DisableServiceLogs = true,
                        ActionName         = "MultiCategoryClassifyWithDisabledServiceLogs"
                    },
                    new MultiCategoryClassifyAction(TestEnvironment.MultiClassificationProjectName, TestEnvironment.MultiClassificationDeploymentName)
                    {
                        ActionName = "MultiCategoryClassify"
                    }
                }
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(s_multiCategoryClassifyBatchConvenienceDocuments, batchActions);

            await PollUntilTimeout(operation);

            Assert.IsTrue(operation.HasCompleted);

            // Take the first page
            AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault();

            IReadOnlyCollection <MultiCategoryClassifyActionResult> multiCategoryClassifyActionsResults = resultCollection.MultiCategoryClassifyResults;

            Assert.IsNotNull(multiCategoryClassifyActionsResults);

            IList <string> expected = new List <string> {
                "MultiCategoryClassify", "MultiCategoryClassifyWithDisabledServiceLogs"
            };

            CollectionAssert.AreEquivalent(expected, multiCategoryClassifyActionsResults.Select(result => result.ActionName));
        }
Esempio n. 21
0
        public async Task AnalyzeOperationAnalyzeSentimentWithOpinionMining()
        {
            TextAnalyticsClient client = GetClient();

            var documents = new List <string>
            {
                "The park was clean and pretty. The bathrooms and restaurant were not clean.",
            };

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                AnalyzeSentimentActions = new List <AnalyzeSentimentAction>()
                {
                    new AnalyzeSentimentAction()
                    {
                        IncludeOpinionMining = true
                    }
                },
                DisplayName = "AnalyzeOperationWithOpinionMining",
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(documents, batchActions);

            await operation.WaitForCompletionAsync();

            //Take the first page
            AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault();

            IReadOnlyCollection <AnalyzeSentimentActionResult> analyzeSentimentActionsResults = resultCollection.AnalyzeSentimentActionsResults;

            Assert.IsNotNull(analyzeSentimentActionsResults);

            AnalyzeSentimentResultCollection analyzeSentimentResult = analyzeSentimentActionsResults.FirstOrDefault().Result;

            Assert.AreEqual(1, analyzeSentimentResult.Count);

            Assert.AreEqual(TextSentiment.Mixed, analyzeSentimentResult[0].DocumentSentiment.Sentiment);
        }
        public async Task RecognizeLinkedEntitiesWithMultipleActions()
        {
            TextAnalyticsClient client = GetClient();

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                RecognizeLinkedEntitiesActions = new List <RecognizeLinkedEntitiesAction>()
                {
                    new RecognizeLinkedEntitiesAction()
                    {
                        DisableServiceLogs = true,
                        ActionName         = "RecognizeLinkedEntitiesWithDisabledServiceLogs"
                    },
                    new RecognizeLinkedEntitiesAction()
                    {
                        ActionName = "RecognizeLinkedEntities"
                    }
                }
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(s_batchDocuments, batchActions);

            await operation.WaitForCompletionAsync();

            // Take the first page
            AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault();

            IReadOnlyCollection <RecognizeLinkedEntitiesActionResult> RecognizeLinkedEntitiesActionsResults = resultCollection.RecognizeLinkedEntitiesResults;

            Assert.IsNotNull(RecognizeLinkedEntitiesActionsResults);

            IList <string> expected = new List <string> {
                "RecognizeLinkedEntities", "RecognizeLinkedEntitiesWithDisabledServiceLogs"
            };

            CollectionAssert.AreEquivalent(expected, RecognizeLinkedEntitiesActionsResults.Select(result => result.ActionName));
        }
Esempio n. 23
0
        public async Task AnalyzeSentimentWithMultipleActions()
        {
            TextAnalyticsClient client = GetClient();

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                AnalyzeSentimentActions = new List <AnalyzeSentimentAction>()
                {
                    new AnalyzeSentimentAction()
                    {
                        DisableServiceLogs = true,
                        ActionName         = "AnalyzeSentimentWithDisabledServiceLogs"
                    },
                    new AnalyzeSentimentAction()
                    {
                        ActionName = "AnalyzeSentiment"
                    }
                }
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(batchConvenienceDocuments, batchActions);

            await operation.WaitForCompletionAsync();

            // Take the first page
            AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault();

            IReadOnlyCollection <AnalyzeSentimentActionResult> AnalyzeSentimentActionsResults = resultCollection.AnalyzeSentimentResults;

            Assert.IsNotNull(AnalyzeSentimentActionsResults);

            IList <string> expected = new List <string> {
                "AnalyzeSentiment", "AnalyzeSentimentWithDisabledServiceLogs"
            };

            CollectionAssert.AreEquivalent(expected, AnalyzeSentimentActionsResults.Select(result => result.ActionName));
        }
Esempio n. 24
0
        public async Task ExtractSummaryWithMultipleActions()
        {
            TextAnalyticsClient client = GetClient();

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                ExtractSummaryActions = new List <ExtractSummaryAction>()
                {
                    new ExtractSummaryAction()
                    {
                        DisableServiceLogs = true,
                        ActionName         = "ExtractSummaryWithDisabledServiceLogs"
                    },
                    new ExtractSummaryAction()
                    {
                        ActionName = "ExtractSummary"
                    }
                }
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(s_extractSummaryBatchDocuments, batchActions);

            await operation.WaitForCompletionAsync();

            // Take the first page
            AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault();

            IReadOnlyCollection <ExtractSummaryActionResult> ExtractSummaryActionsResults = resultCollection.ExtractSummaryResults;

            Assert.IsNotNull(ExtractSummaryActionsResults);

            IList <string> expected = new List <string> {
                "ExtractSummary", "ExtractSummaryWithDisabledServiceLogs"
            };

            CollectionAssert.AreEquivalent(expected, ExtractSummaryActionsResults.Select(result => result.ActionName));
        }
        public async Task SingleCategoryClassifyBatchWithErrorTest()
        {
            TextAnalyticsClient client = GetClient();

            var documents = new List <string>
            {
                "Subject is taking 100mg of ibuprofen twice daily",
                "",
            };
            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                SingleCategoryClassifyActions = new List <SingleCategoryClassifyAction>()
                {
                    new SingleCategoryClassifyAction(TestEnvironment.SingleClassificationProjectName, TestEnvironment.SingleClassificationDeploymentName)
                }
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(documents, batchActions, "en");

            await PollUntilTimeout(operation);

            Assert.IsTrue(operation.HasCompleted);

            // Take the first page
            AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault();

            List <SingleCategoryClassifyActionResult> singleCategoryClassifyActions = resultCollection.SingleCategoryClassifyResults.ToList();

            Assert.AreEqual(1, singleCategoryClassifyActions.Count);

            SingleCategoryClassifyResultCollection documentsResults = singleCategoryClassifyActions[0].DocumentsResults;

            Assert.IsFalse(documentsResults[0].HasError);
            Assert.IsTrue(documentsResults[1].HasError);
            Assert.AreEqual(TextAnalyticsErrorCode.InvalidDocument, documentsResults[1].Error.ErrorCode.ToString());
        }
        /// <summary>
        /// Checks for all text analytics requests that were marked as running if they have completed and sets a new state accordingly.
        /// <param name="audioFileInfos"></param>
        /// <returns>True if all requests completed, else false.</returns>
        /// </summary>
        public async Task <bool> TextAnalyticsRequestsCompleted(IEnumerable <AudioFileInfo> audioFileInfos)
        {
            var runningTextAnalyticsRequests = new List <TextAnalyticsRequest>();

            if (audioFileInfos.Where(audioFileInfo => audioFileInfo.TextAnalyticsRequests.AudioLevelRequests != null).Any())
            {
                runningTextAnalyticsRequests.AddRange(audioFileInfos.SelectMany(audioFileInfo => audioFileInfo.TextAnalyticsRequests.AudioLevelRequests).Where(text => text.Status == TextAnalyticsRequestStatus.Running));
            }

            if (audioFileInfos.Where(audioFileInfo => audioFileInfo.TextAnalyticsRequests.UtteranceLevelRequests != null).Any())
            {
                runningTextAnalyticsRequests.AddRange(audioFileInfos.SelectMany(audioFileInfo => audioFileInfo.TextAnalyticsRequests.UtteranceLevelRequests).Where(text => text.Status == TextAnalyticsRequestStatus.Running));
            }

            var textAnalyticsRequestCompleted = true;

            foreach (var textAnalyticsJob in runningTextAnalyticsRequests)
            {
                var operation = new AnalyzeActionsOperation(textAnalyticsJob.Id, TextAnalyticsClient);

                using var cts = new CancellationTokenSource();
                cts.CancelAfter(RequestTimeout);
                await operation.UpdateStatusAsync(cts.Token).ConfigureAwait(false);

                if (operation.HasCompleted)
                {
                    textAnalyticsJob.Status = TextAnalyticsRequestStatus.Completed;
                }
                else
                {
                    textAnalyticsRequestCompleted = false;
                }
            }

            return(textAnalyticsRequestCompleted);
        }
Esempio n. 27
0
        public async Task AnalyzeOperationWithAADTest()
        {
            TextAnalyticsClient client = GetClient(useTokenCredential: true);

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                ExtractKeyPhrasesActions = new List <ExtractKeyPhrasesAction>()
                {
                    new ExtractKeyPhrasesAction()
                },
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(batchDocuments, batchActions);

            await operation.WaitForCompletionAsync();

            //Take the first page
            AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault();

            IReadOnlyCollection <ExtractKeyPhrasesActionResult> keyPhrasesActionsResults = resultCollection.ExtractKeyPhrasesActionsResults;

            Assert.IsNotNull(keyPhrasesActionsResults);
            Assert.AreEqual(2, keyPhrasesActionsResults.FirstOrDefault().Result.Count);
        }
Esempio n. 28
0
        public void MultiCategoryClassifyConvenience()
        {
            // Create a text analytics client.
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            // Get input document.
            string document = @"I need a reservation for an indoor restaurant in China. Please don't stop the music. Play music and add it to my playlist.";

            // Prepare analyze operation input. You can add multiple documents to this list and perform the same
            // operation to all of them.
            var batchInput = new List <string>
            {
                document
            };

            // Set project and deployment names of the target model
            // To train a model to classify your documents, see https://aka.ms/azsdk/textanalytics/customfunctionalities
            string projectName    = TestEnvironment.MultiClassificationProjectName;
            string deploymentName = TestEnvironment.MultiClassificationDeploymentName;

            var multiCategoryClassifyAction = new MultiCategoryClassifyAction(projectName, deploymentName);

            TextAnalyticsActions actions = new TextAnalyticsActions()
            {
                MultiCategoryClassifyActions = new List <MultiCategoryClassifyAction>()
                {
                    multiCategoryClassifyAction
                }
            };

            // Start analysis process.
            AnalyzeActionsOperation operation = client.StartAnalyzeActions(batchInput, actions);

            // Wait for completion with manual polling.
            TimeSpan pollingInterval = new TimeSpan(1000);

            while (true)
            {
                Console.WriteLine($"Status: {operation.Status}");
                operation.UpdateStatus();
                if (operation.HasCompleted)
                {
                    break;
                }

                Thread.Sleep(pollingInterval);
            }

            // View operation status.
            Console.WriteLine($"AnalyzeActions operation has completed");
            Console.WriteLine();

            Console.WriteLine($"Created On   : {operation.CreatedOn}");
            Console.WriteLine($"Expires On   : {operation.ExpiresOn}");
            Console.WriteLine($"Id           : {operation.Id}");
            Console.WriteLine($"Status       : {operation.Status}");
            Console.WriteLine($"Last Modified: {operation.LastModified}");
            Console.WriteLine();

            // View operation results.
            foreach (AnalyzeActionsResult documentsInPage in operation.GetValues())
            {
                IReadOnlyCollection <MultiCategoryClassifyActionResult> multiClassificationActionResults = documentsInPage.MultiCategoryClassifyResults;

                foreach (MultiCategoryClassifyActionResult classificationActionResults in multiClassificationActionResults)
                {
                    Console.WriteLine($" Action name: {classificationActionResults.ActionName}");
                    foreach (MultiCategoryClassifyResult documentResults in classificationActionResults.DocumentsResults)
                    {
                        if (documentResults.Classifications.Count > 0)
                        {
                            Console.WriteLine($"  The following classes were predicted for this document:");

                            foreach (ClassificationCategory classification in documentResults.Classifications)
                            {
                                Console.WriteLine($"  Class category \"{classification.Category}\" predicted with a confidence score of {classification.ConfidenceScore}.");
                            }

                            Console.WriteLine();
                        }
                    }
                }
            }
        }
        public async Task ExtractSummaryWithoutErrorHandling()
        {
            // Shorter than other Extractive Summarization samples. Used in README for simplicity.

            // Create a text analytics client.
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;
            var    client   = new TextAnalyticsClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

            #region Snippet:TextAnalyticsExtractSummaryWithoutErrorHandlingAsync
            // Get input document.
            string document = @"Windows 365 was in the works before COVID-19 sent companies around the world on a scramble to secure solutions to support employees suddenly forced to work from home, but “what really put the firecracker behind it was the pandemic, it accelerated everything,” McKelvey said. She explained that customers were asking, “’How do we create an experience for people that makes them still feel connected to the company without the physical presence of being there?”
                                In this new world of Windows 365, remote workers flip the lid on their laptop, bootup the family workstation or clip a keyboard onto a tablet, launch a native app or modern web browser and login to their Windows 365 account.From there, their Cloud PC appears with their background, apps, settings and content just as they left it when they last were last there – in the office, at home or a coffee shop.
                                And then, when you’re done, you’re done.You won’t have any issues around security because you’re not saving anything on your device,” McKelvey said, noting that all the data is stored in the cloud.
                                The ability to login to a Cloud PC from anywhere on any device is part of Microsoft’s larger strategy around tailoring products such as Microsoft Teams and Microsoft 365 for the post-pandemic hybrid workforce of the future, she added. It enables employees accustomed to working from home to continue working from home; it enables companies to hire interns from halfway around the world; it allows startups to scale without requiring IT expertise.
                                “I think this will be interesting for those organizations who, for whatever reason, have shied away from virtualization.This is giving them an opportunity to try it in a way that their regular, everyday endpoint admin could manage,” McKelvey said.
                                The simplicity of Windows 365 won over Dean Wells, the corporate chief information officer for the Government of Nunavut. His team previously attempted to deploy a traditional virtual desktop infrastructure and found it inefficient and unsustainable given the limitations of low-bandwidth satellite internet and the constant need for IT staff to manage the network and infrastructure.
                                We didn’t run it for very long,” he said. “It didn’t turn out the way we had hoped.So, we actually had terminated the project and rolled back out to just regular PCs.”
                                He re-evaluated this decision after the Government of Nunavut was hit by a ransomware attack in November 2019 that took down everything from the phone system to the government’s servers. Microsoft helped rebuild the system, moving the government to Teams, SharePoint, OneDrive and Microsoft 365. Manchester’s team recruited the Government of Nunavut to pilot Windows 365. Wells was intrigued, especially by the ability to manage the elastic workforce securely and seamlessly.
                                “The impact that I believe we are finding, and the impact that we’re going to find going forward, is being able to access specialists from outside the territory and organizations outside the territory to come in and help us with our projects, being able to get people on staff with us to help us deliver the day-to-day expertise that we need to run the government,” he said.
                                “Being able to improve healthcare, being able to improve education, economic development is going to improve the quality of life in the communities.”";

            // Prepare analyze operation input. You can add multiple documents to this list and perform the same
            // operation to all of them.
            var batchInput = new List <string>
            {
                document
            };

            TextAnalyticsActions actions = new TextAnalyticsActions()
            {
                ExtractSummaryActions = new List <ExtractSummaryAction>()
                {
                    new ExtractSummaryAction()
                }
            };

            // Start analysis process.
            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(batchInput, actions);

            await operation.WaitForCompletionAsync();

            // View operation status.
            Console.WriteLine($"AnalyzeActions operation has completed");
            Console.WriteLine();

            Console.WriteLine($"Created On   : {operation.CreatedOn}");
            Console.WriteLine($"Expires On   : {operation.ExpiresOn}");
            Console.WriteLine($"Id           : {operation.Id}");
            Console.WriteLine($"Status       : {operation.Status}");
            Console.WriteLine($"Last Modified: {operation.LastModified}");
            Console.WriteLine();

            // View operation results.
            await foreach (AnalyzeActionsResult documentsInPage in operation.Value)
            {
                IReadOnlyCollection <ExtractSummaryActionResult> summaryResults = documentsInPage.ExtractSummaryResults;

                foreach (ExtractSummaryActionResult summaryActionResults in summaryResults)
                {
                    foreach (ExtractSummaryResult documentResults in summaryActionResults.DocumentsResults)
                    {
                        Console.WriteLine($"  Extracted the following {documentResults.Sentences.Count} sentence(s):");
                        Console.WriteLine();

                        foreach (SummarySentence sentence in documentResults.Sentences)
                        {
                            Console.WriteLine($"  Sentence: {sentence.Text}");
                            Console.WriteLine($"  Rank Score: {sentence.RankScore}");
                            Console.WriteLine($"  Offset: {sentence.Offset}");
                            Console.WriteLine($"  Length: {sentence.Length}");
                            Console.WriteLine();
                        }
                    }
                }
            }
            #endregion Snippet:TextAnalyticsExtractSummaryWithoutErrorHandlingAsync
        }
        public async Task RecognizeCustomEntitiesConvenienceAsync()
        {
            // Create a text analytics client.
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            // Create input documents.
            string documentA = @"We love this trail and make the trip every year. The views are breathtaking and well
                                worth the hike! Yesterday was foggy though, so we missed the spectacular views.
                                We tried again today and it was amazing. Everyone in my family liked the trail although
                                it was too challenging for the less athletic among us.";

            string documentB = @"Last week we stayed at Hotel Foo to celebrate our anniversary. The staff knew about
                                our anniversary so they helped me organize a little surprise for my partner.
                                The room was clean and with the decoration I requested. It was perfect!";

            var batchDocuments = new List <string>
            {
                documentA,
                documentB
            };

            // Set project and deployment names of the target model
            // To train a model to recognize your custom entities, see https://aka.ms/azsdk/textanalytics/customentityrecognition
            string projectName    = TestEnvironment.RecognizeCustomEntitiesProjectName;
            string deploymentName = TestEnvironment.RecognizeCustomEntitiesDeploymentName;

            var recognizeCustomEntitiesAction = new RecognizeCustomEntitiesAction(projectName, deploymentName);

            // prepare actions.
            var actions = new TextAnalyticsActions()
            {
                RecognizeCustomEntitiesActions = new List <RecognizeCustomEntitiesAction>()
                {
                    recognizeCustomEntitiesAction
                }
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(batchDocuments, actions);

            await operation.WaitForCompletionAsync();

            await foreach (AnalyzeActionsResult documentsInPage in operation.Value)
            {
                IReadOnlyCollection <RecognizeCustomEntitiesActionResult> customEntitiesActionResults = documentsInPage.RecognizeCustomEntitiesResults;
                foreach (RecognizeCustomEntitiesActionResult customEntitiesActionResult in customEntitiesActionResults)
                {
                    Console.WriteLine($" Action name: {customEntitiesActionResult.ActionName}");
                    int docNumber = 1;
                    foreach (RecognizeEntitiesResult documentResults in customEntitiesActionResult.DocumentsResults)
                    {
                        Console.WriteLine($" Document #{docNumber++}");
                        Console.WriteLine($"  Recognized the following {documentResults.Entities.Count} entities:");

                        foreach (CategorizedEntity entity in documentResults.Entities)
                        {
                            Console.WriteLine($"  Entity: {entity.Text}");
                            Console.WriteLine($"  Category: {entity.Category}");
                            Console.WriteLine($"  Offset: {entity.Offset}");
                            Console.WriteLine($"  Length: {entity.Length}");
                            Console.WriteLine($"  ConfidenceScore: {entity.ConfidenceScore}");
                            Console.WriteLine($"  SubCategory: {entity.SubCategory}");
                        }
                        Console.WriteLine("");
                    }
                }
            }
        }