コード例 #1
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));
            }
        }
コード例 #2
0
        public async Task AnalyzeOperationWithErrorsInDocumentTest()
        {
            TextAnalyticsClient client = GetClient();

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

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

            await operation.WaitForCompletionAsync();

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

            //Key phrases
            List <ExtractKeyPhrasesActionResult> keyPhrasesActions = resultCollection.ExtractKeyPhrasesActionsResults.ToList();

            Assert.AreEqual(1, keyPhrasesActions.Count);

            ExtractKeyPhrasesResultCollection results = keyPhrasesActions[0].Result;

            Assert.IsFalse(results[0].HasError);
            Assert.IsTrue(results[1].HasError);
            Assert.AreEqual(TextAnalyticsErrorCode.InvalidDocument, results[1].Error.ErrorCode.ToString());
        }
        public async Task ExtractKeyPhrasesBatchConvenienceAsync()
        {
            string endpoint = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_ENDPOINT");
            string apiKey   = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_API_KEY");

            // Instantiate a client that will be used to call the service.
            var client = new TextAnalyticsClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

            var documents = new List <string>
            {
                "Microsoft was founded by Bill Gates and Paul Allen.",
                "Text Analytics is one of the Azure Cognitive Services.",
                "My cat might need to see a veterinarian.",
            };

            ExtractKeyPhrasesResultCollection results = await client.ExtractKeyPhrasesBatchAsync(documents);

            Console.WriteLine($"Extracted key phrases for each document are:");
            int i = 0;

            foreach (ExtractKeyPhrasesResult result in results)
            {
                Console.WriteLine($"For document: \"{documents[i++]}\",");
                Console.WriteLine($"the following {result.KeyPhrases.Count()} key phrases were found: ");

                foreach (string keyPhrase in result.KeyPhrases)
                {
                    Console.WriteLine($"    {keyPhrase}");
                }
            }
        }
コード例 #4
0
        public async Task ExtractKeyPhrasesBatchWithSatisticsTest()
        {
            TextAnalyticsClient client = GetClient();
            var inputs = 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",
                }
            };

            ExtractKeyPhrasesResultCollection results = await client.ExtractKeyPhrasesBatchAsync(inputs, new TextAnalyticsRequestOptions { IncludeStatistics = true });

            foreach (ExtractKeyPhrasesResult result in results)
            {
                Assert.AreEqual(3, result.KeyPhrases.Count());
            }

            Assert.IsNotNull(results.Statistics.DocumentCount);
            Assert.IsNotNull(results.Statistics.InvalidDocumentCount);
            Assert.IsNotNull(results.Statistics.TransactionCount);
            Assert.IsNotNull(results.Statistics.ValidDocumentCount);
        }
コード例 #5
0
        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));
            }
        }
コード例 #6
0
        public async Task AnalyzeOperationAllActionsAndDisableServiceLogs()
        {
            TextAnalyticsClient client = GetClient();

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                ExtractKeyPhrasesOptions = new List <ExtractKeyPhrasesOptions>()
                {
                    new ExtractKeyPhrasesOptions()
                    {
                        DisableServiceLogs = true
                    }
                },
                RecognizeEntitiesOptions = new List <RecognizeEntitiesOptions>()
                {
                    new RecognizeEntitiesOptions()
                    {
                        DisableServiceLogs = true
                    }
                },
                RecognizePiiEntitiesOptions = new List <RecognizePiiEntitiesOptions>()
                {
                    new RecognizePiiEntitiesOptions()
                    {
                        DisableServiceLogs = false
                    }
                },
                RecognizeLinkedEntitiesOptions = new List <RecognizeLinkedEntitiesOptions>()
                {
                    new RecognizeLinkedEntitiesOptions()
                    {
                        DisableServiceLogs = true
                    }
                },
            };

            AnalyzeBatchActionsOperation operation = await client.StartAnalyzeBatchActionsAsync(batchConvenienceDocuments, batchActions);

            await operation.WaitForCompletionAsync();

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

            ExtractKeyPhrasesResultCollection       keyPhrasesResult    = resultCollection.ExtractKeyPhrasesActionsResults.FirstOrDefault().Result;
            RecognizeEntitiesResultCollection       entitiesResult      = resultCollection.RecognizeEntitiesActionsResults.FirstOrDefault().Result;
            RecognizePiiEntitiesResultCollection    piiResult           = resultCollection.RecognizePiiEntitiesActionsResults.FirstOrDefault().Result;
            RecognizeLinkedEntitiesResultCollection entityLinkingResult = resultCollection.RecognizeLinkedEntitiesActionsResults.FirstOrDefault().Result;

            Assert.IsNotNull(keyPhrasesResult);
            Assert.AreEqual(2, keyPhrasesResult.Count);

            Assert.IsNotNull(entitiesResult);
            Assert.AreEqual(2, keyPhrasesResult.Count);

            Assert.IsNotNull(piiResult);
            Assert.AreEqual(2, keyPhrasesResult.Count);

            Assert.IsNotNull(entityLinkingResult);
            Assert.AreEqual(2, keyPhrasesResult.Count);
        }
コード例 #7
0
        public void ExtractKeyPhrasesBatchConvenience()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

            // Instantiate a client that will be used to call the service.
            var client = new TextAnalyticsClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

            var documents = new List <string>
            {
                "Microsoft was founded by Bill Gates and Paul Allen.",
                "Text Analytics is one of the Azure Cognitive Services.",
                "My cat might need to see a veterinarian.",
            };

            #region Snippet:TextAnalyticsSample3ExtractKeyPhrasesConvenience
            ExtractKeyPhrasesResultCollection results = client.ExtractKeyPhrasesBatch(documents);
            #endregion

            Debug.WriteLine($"Extracted key phrases for each document are:");
            int i = 0;
            foreach (ExtractKeyPhrasesResult result in results)
            {
                Debug.WriteLine($"For document: \"{documents[i++]}\",");
                Debug.WriteLine($"the following {result.KeyPhrases.Count()} key phrases were found: ");

                foreach (string keyPhrase in result.KeyPhrases)
                {
                    Debug.WriteLine($"    {keyPhrase}");
                }
            }
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        public void ExtractKeyPhrasesBatchConvenience()
        {
            string endpoint        = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_ENDPOINT");
            string subscriptionKey = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_SUBSCRIPTION_KEY");

            var client = new TextAnalyticsClient(new Uri(endpoint), subscriptionKey);

            var inputs = new List <string>
            {
                "Microsoft was founded by Bill Gates and Paul Allen.",
                "Text Analytics is one of the Azure Cognitive Services.",
                "My cat might need to see a veterinarian.",
            };

            #region Snippet:TextAnalyticsSample3ExtractKeyPhrasesConvenience
            ExtractKeyPhrasesResultCollection results = client.ExtractKeyPhrases(inputs);
            #endregion

            Debug.WriteLine($"Extracted key phrases for each input are:");
            int i = 0;
            foreach (ExtractKeyPhrasesResult result in results)
            {
                Debug.WriteLine($"For input: \"{inputs[i++]}\",");
                Debug.WriteLine($"the following {result.KeyPhrases.Count()} key phrases were found: ");

                foreach (string keyPhrase in result.KeyPhrases)
                {
                    Debug.WriteLine($"    {keyPhrase}");
                }
            }
        }
コード例 #10
0
        public async Task AnalyzeOperationWithAADTest()
        {
            TextAnalyticsClient client = GetClient(useTokenCredential: true);

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

            AnalyzeBatchActionsOperation operation = await client.StartAnalyzeBatchActionsAsync(batchDocuments, batchActions);

            await operation.WaitForCompletionAsync(PollingInterval);

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

            IReadOnlyCollection <RecognizeEntitiesActionResult> entitiesResult = resultCollection.RecognizeEntitiesActionsResults;

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

            IReadOnlyCollection <RecognizePiiEntitiesActionResult> piiResult = resultCollection.RecognizePiiEntitiesActionsResults;

            IReadOnlyCollection <RecognizeLinkedEntitiesActionResult> elResult = resultCollection.RecognizeLinkedEntitiesActionsResults;

            Assert.IsNotNull(keyPhrasesResult);
            Assert.IsNotNull(entitiesResult);
            Assert.IsNotNull(piiResult);
            Assert.IsNotNull(elResult);

            Assert.AreEqual(2, keyPhrasesResult.Count);
        }
コード例 #11
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));
            }
        }
コード例 #12
0
        public async Task ExtractKeyPhrasesBatchTest()
        {
            TextAnalyticsClient      client    = GetClient();
            List <TextDocumentInput> documents = batchDocuments;

            ExtractKeyPhrasesResultCollection results = await client.ExtractKeyPhrasesBatchAsync(documents);

            ValidateBatchDocumentsResult(results, 3);
        }
コード例 #13
0
        public async Task ExtractKeyPhrasesBatchConvenienceTest()
        {
            TextAnalyticsClient client = GetClient();
            var documents = batchConvenienceDocuments;

            ExtractKeyPhrasesResultCollection results = await client.ExtractKeyPhrasesBatchAsync(documents);

            ValidateBatchDocumentsResult(results, 3);
        }
コード例 #14
0
        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",
                },
                new TextDocumentInput("3", "")
                {
                    Language = "es",
                }
            };

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

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

            AnalyzeBatchActionsOperation operation = await client.StartAnalyzeBatchActionsAsync(batchDocuments, batchActions, options);

            await operation.WaitForCompletionAsync(PollingInterval);

            //Take the first page
            AnalyzeBatchActionsResult 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);
        }
コード例 #15
0
        public async Task ExtractKeyPhrasesBatchWithSatisticsTest()
        {
            TextAnalyticsClient      client    = GetClient();
            List <TextDocumentInput> documents = batchDocuments;

            ExtractKeyPhrasesResultCollection results = await client.ExtractKeyPhrasesBatchAsync(documents, new TextAnalyticsRequestOptions { IncludeStatistics = true });

            ValidateBatchDocumentsResult(results, 3, includeStatistics: true);

            Assert.AreEqual(documents.Count, results.Statistics.DocumentCount);
        }
コード例 #16
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",
                }
            };

            AnalyzeOperationOptions operationOptions = new AnalyzeOperationOptions()
            {
                KeyPhrasesTaskParameters = new KeyPhrasesTaskParameters(),
                DisplayName = "AnalyzeOperationWithLanguageTest"
            };

            AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(batchDocuments, operationOptions);

            await operation.WaitForCompletionAsync();

            AnalyzeOperationResult resultCollection = operation.Value;

            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));
            }
        }
コード例 #17
0
        public async Task ExtractKeyPhrasesBatchTest()
        {
            TextAnalyticsClient      client    = GetClient();
            List <TextDocumentInput> documents = batchDocuments;

            ExtractKeyPhrasesResultCollection results = await client.ExtractKeyPhrasesBatchAsync(documents);

            foreach (ExtractKeyPhrasesResult result in results)
            {
                Assert.AreEqual(3, result.KeyPhrases.Count());
            }
        }
コード例 #18
0
        public async Task DeserializeTextAnalyticsError()
        {
            using var stream = new MemoryStream(Encoding.UTF8.GetBytes(@"
                {
                    ""kind"": ""KeyPhraseExtractionResults"",
                    ""results"": {  
                        ""documents"": [
                            {
                                ""id"": ""0"",
                                ""keyPhrases"": [],
                                ""warnings"": []
                            }
                        ],
                        ""errors"": [
                            {
                                ""id"": ""1"",
                                ""error"": {
                                    ""code"": ""InvalidArgument"",
                                    ""message"": ""Invalid document in request."",
                                    ""innererror"": {
                                        ""code"": ""InvalidDocument"",
                                        ""message"": ""Document text is empty.""
                                    }
                                }
                            }
                        ],
                        ""modelVersion"": ""2020-07-01""
                    }
                }"));

            var mockResponse = new MockResponse(200);

            mockResponse.ContentStream = stream;

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

            var documents = new List <string>()
            {
                "Something smells",
                ""
            };

            ExtractKeyPhrasesResultCollection result = await client.ExtractKeyPhrasesBatchAsync(documents);

            var resultError = result[1];

            Assert.IsTrue(resultError.HasError);
            Assert.AreEqual(TextAnalyticsErrorCode.InvalidDocument, resultError.Error.ErrorCode.ToString());
            Assert.AreEqual("Document text is empty.", resultError.Error.Message);
        }
コード例 #19
0
        public async Task AnalyzeOperationTest()
        {
            TextAnalyticsClient client = GetClient();

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

            AnalyzeBatchActionsOperation operation = await client.StartAnalyzeBatchActionsAsync(batchConvenienceDocuments, batchActions, "en");

            await operation.WaitForCompletionAsync(PollingInterval);

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

            IReadOnlyCollection <RecognizeEntitiesActionResult> entitiesResult = resultCollection.RecognizeEntitiesActionsResults;

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

            IReadOnlyCollection <RecognizePiiEntitiesActionResult> piiResult = resultCollection.RecognizePiiEntitiesActionsResults;

            IReadOnlyCollection <RecognizeLinkedEntitiesActionResult> elResult = resultCollection.RecognizeLinkedEntitiesActionsResults;

            Assert.IsNotNull(keyPhrasesResult);
            Assert.IsNotNull(entitiesResult);
            Assert.IsNotNull(piiResult);
            Assert.IsNotNull(elResult);

            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));
            }
        }
コード例 #20
0
        public async Task ExtractKeyPhrasesBatchConvenienceTest()
        {
            TextAnalyticsClient client = GetClient();
            var inputs = new List <string>
            {
                "Microsoft was founded by Bill Gates and Paul Allen.",
                "My cat and my dog might need to see a veterinarian."
            };

            ExtractKeyPhrasesResultCollection results = await client.ExtractKeyPhrasesBatchAsync(inputs);

            foreach (ExtractKeyPhrasesResult result in results)
            {
                Assert.AreEqual(3, result.KeyPhrases.Count());
            }
        }
コード例 #21
0
        public async Task ExtractKeyPhrasesBatchWithNullTextTest()
        {
            TextAnalyticsClient client = GetClient();
            var documents = new List <TextDocumentInput> {
                new TextDocumentInput("1", null)
            };

            ExtractKeyPhrasesResultCollection results = await client.ExtractKeyPhrasesBatchAsync(documents);

            var exceptionMessage = "Cannot access result for document 1, due to error InvalidDocument: Document text is empty.";

            Assert.IsTrue(results[0].HasError);
            InvalidOperationException ex = Assert.Throws <InvalidOperationException>(() => results[0].KeyPhrases.Count());

            Assert.AreEqual(exceptionMessage, ex.Message);
        }
コード例 #22
0
        public async Task ExtractKeyPhrasesBatchWithSatisticsTest()
        {
            TextAnalyticsClient      client    = GetClient();
            List <TextDocumentInput> documents = batchDocuments;

            ExtractKeyPhrasesResultCollection results = await client.ExtractKeyPhrasesBatchAsync(documents, new TextAnalyticsRequestOptions { IncludeStatistics = true });

            foreach (ExtractKeyPhrasesResult result in results)
            {
                Assert.AreEqual(3, result.KeyPhrases.Count());
            }

            Assert.IsNotNull(results.Statistics.DocumentCount);
            Assert.IsNotNull(results.Statistics.InvalidDocumentCount);
            Assert.IsNotNull(results.Statistics.TransactionCount);
            Assert.IsNotNull(results.Statistics.ValidDocumentCount);
        }
コード例 #23
0
        public async Task ExtractKeyPhrasesBatchWithErrorTest()
        {
            TextAnalyticsClient client = GetClient();
            var inputs = new List <string>
            {
                "Microsoft was founded by Bill Gates and Paul Allen.",
                "",
                "My cat might need to see a veterinarian."
            };

            ExtractKeyPhrasesResultCollection results = await client.ExtractKeyPhrasesBatchAsync(inputs);

            Assert.IsTrue(!results[0].HasError);
            Assert.IsTrue(!results[2].HasError);

            Assert.IsTrue(results[1].HasError);
            Assert.Throws <InvalidOperationException>(() => results[1].KeyPhrases.GetType());
        }
コード例 #24
0
        public async Task ExtractKeyPhrasesBatchWithExtractKeyPhrasesOptionsSatisticsTest()
        {
            var options = new ExtractKeyPhrasesOptions {
                IncludeStatistics = true
            };
            TextAnalyticsClient      client    = GetClient();
            List <TextDocumentInput> documents = batchDocuments;

            ExtractKeyPhrasesResultCollection results = await client.ExtractKeyPhrasesBatchAsync(documents, options);

            ValidateBatchDocumentsResult(results, 3, includeStatistics: true);

            Assert.AreEqual(documents.Count, results.Statistics.DocumentCount);

            // Assert the options classes since overloads were added and the original now instantiates a RecognizeEntitiesOptions.
            Assert.IsTrue(options.IncludeStatistics);
            Assert.IsNull(options.ModelVersion);
        }
コード例 #25
0
        public async Task ExtractKeyPhrasesWithWarningTest()
        {
            TextAnalyticsClient client = GetClient();
            string document            = "Anthony runs his own personal training business so thisisaverylongtokenwhichwillbetruncatedtoshowushowwarningsareemittedintheapi";

            ExtractKeyPhrasesResultCollection keyPhrasesCollection = await client.ExtractKeyPhrasesBatchAsync(new List <string> {
                document
            }, "es", new ExtractKeyPhrasesOptions()
            {
                ModelVersion = "2020-07-01"
            });

            KeyPhraseCollection keyPhrases = keyPhrasesCollection.FirstOrDefault().KeyPhrases;

            ValidateInDocumenResult(keyPhrases, 1);

            Assert.AreEqual(TextAnalyticsWarningCode.LongWordsInDocument, keyPhrases.Warnings.FirstOrDefault().WarningCode.ToString());
        }
コード例 #26
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);

            //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));
            }
        }
コード例 #27
0
        public async Task ExtractKeyPhrasesBatchConvenienceWithStatisticsTest()
        {
            var options = new TextAnalyticsRequestOptions {
                IncludeStatistics = true
            };
            TextAnalyticsClient client = GetClient();
            var documents = batchConvenienceDocuments;

            ExtractKeyPhrasesResultCollection results = await client.ExtractKeyPhrasesBatchAsync(documents, "en", options);

            ValidateBatchDocumentsResult(results, 3, includeStatistics: true);

            Assert.AreEqual(documents.Count, results.Statistics.DocumentCount);

            // Assert the options classes since overloads were added and the original now instantiates a RecognizeEntitiesOptions.
            Assert.IsTrue(options.IncludeStatistics);
            Assert.IsNull(options.ModelVersion);
            Assert.AreEqual(StringIndexType.Utf16CodeUnit, options.StringIndexType);
        }
        public void ExtractKeyPhrasesWithWarnings()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            string document = @"Anthony runs his own personal training business so
                              thisisaverylongtokenwhichwillbetruncatedtoshowushowwarningsareemittedintheapi";

            try
            {
                Response <ExtractKeyPhrasesResultCollection> response = client.ExtractKeyPhrasesBatch(new List <string>()
                {
                    document
                }, options: new TextAnalyticsRequestOptions()
                {
                    ModelVersion = "2020-07-01"
                });
                ExtractKeyPhrasesResultCollection keyPhrasesInDocuments = response.Value;
                KeyPhraseCollection keyPhrases = keyPhrasesInDocuments.FirstOrDefault().KeyPhrases;

                if (keyPhrases.Warnings.Count > 0)
                {
                    Console.WriteLine("**Warnings:**");
                    foreach (TextAnalyticsWarning warning in keyPhrases.Warnings)
                    {
                        Console.WriteLine($"  Warning: Code: {warning.WarningCode}, Message: {warning.Message}");
                    }
                }

                Console.WriteLine($"Extracted {keyPhrases.Count} key phrases:");
                foreach (string keyPhrase in keyPhrases)
                {
                    Console.WriteLine($"  {keyPhrase}");
                }
            }
            catch (RequestFailedException exception)
            {
                Console.WriteLine($"Error Code: {exception.ErrorCode}");
                Console.WriteLine($"Message: {exception.Message}");
            }
        }
コード例 #29
0
        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);
        }
コード例 #30
0
        public async Task ExtractKeyPhrasesBatchConvenienceWithStatisticsTest()
        {
            TextAnalyticsClient client = GetClient();
            var inputs = new List <string>
            {
                "Microsoft was founded by Bill Gates and Paul Allen.",
                "My cat and my dog might need to see a veterinarian."
            };

            ExtractKeyPhrasesResultCollection results = await client.ExtractKeyPhrasesBatchAsync(inputs, "en", new TextAnalyticsRequestOptions { IncludeStatistics = true });

            foreach (ExtractKeyPhrasesResult result in results)
            {
                Assert.AreEqual(3, result.KeyPhrases.Count());
            }

            Assert.IsNotNull(results.Statistics.DocumentCount);
            Assert.IsNotNull(results.Statistics.InvalidDocumentCount);
            Assert.IsNotNull(results.Statistics.TransactionCount);
            Assert.IsNotNull(results.Statistics.ValidDocumentCount);
        }