예제 #1
0
        public async Task ExtractKeyPhrasesBatchWithSatisticsTest()
        {
            TextAnalyticsClient client = GetClient();
            var documents = 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(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);
        }
        public async Task ExtractKeyPhrasesBatchConvenienceAsync()
        {
            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.",
            };

            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}");
                }
            }
        }
예제 #3
0
        public async Task ExtractKeyPhrasesBatchConvenienceTest()
        {
            TextAnalyticsClient client = GetClient();
            var documents = batchConvenienceDocuments;

            ExtractKeyPhrasesResultCollection results = await client.ExtractKeyPhrasesBatchAsync(documents);

            ValidateBatchDocumentsResult(results, 3);
        }
예제 #4
0
        public async Task ExtractKeyPhrasesBatchTest()
        {
            TextAnalyticsClient      client    = GetClient();
            List <TextDocumentInput> documents = batchDocuments;

            ExtractKeyPhrasesResultCollection results = await client.ExtractKeyPhrasesBatchAsync(documents);

            ValidateBatchDocumentsResult(results, 3);
        }
예제 #5
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);
        }
        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());
            }
        }
        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());
            }
        }
        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);
        }
        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);
        }
예제 #10
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());
        }
        public async Task <List <ExtractKeyPhrasesResult> > PredictKeyphraseBatchAsync(List <string> queries)
        {
            // verify text analytics char limit
            VerifyCharLimit(queries);
            // paginate throw chunks to abide by "DocumentCountLimit" of the api call
            var listPaginator = new Paginator <string>(queries, Constants.TextAnaylticsApiCallDocumentLimit);
            var result        = new List <ExtractKeyPhrasesResult>();

            while (listPaginator.HasNext())
            {
                var subList  = (listPaginator.GetNextPage()).ToList();
                var response = await _textAnalyticsClient.ExtractKeyPhrasesBatchAsync(subList, language : _predictionLanguage);

                HandleError(response.Value);
                result.AddRange(response.Value);
            }
            return(result);
        }
        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);
        }
        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());
        }
예제 #14
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 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);
        }
        public async Task ExtractKeyPhrasesBatchTest()
        {
            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);

            foreach (ExtractKeyPhrasesResult result in results)
            {
                Assert.AreEqual(3, result.KeyPhrases.Count());
            }
        }
        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);

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

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

            Assert.AreEqual(exceptionMessage, ex.Message);
        }
        public async Task ExtractKeyPhrasesBatchConvenienceAsync()
        {
            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), CreateSampleOptions());

            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.
                                Not necessarily recommended for small children.
                                A hotel close to the trail offers services for childcare in case you want that.";

            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!";

            string documentC = @"That was the best day of my life! We went on a 4 day trip where we stayed at Hotel Foo.
                                They had great amenities that included an indoor pool, a spa, and a bar.
                                The spa offered couples massages which were really good. 
                                The spa was clean and felt very peaceful. Overall the whole experience was great.
                                We will definitely come back.";

            string documentD = string.Empty;

            var documents = new List <string>
            {
                documentA,
                documentB,
                documentC,
                documentD
            };

            Response <ExtractKeyPhrasesResultCollection> response = await client.ExtractKeyPhrasesBatchAsync(documents);

            ExtractKeyPhrasesResultCollection keyPhrasesInDocuments = response.Value;

            int i = 0;

            Console.WriteLine($"Results of Azure Text Analytics \"Extract Key Phrases\" Model, version: \"{keyPhrasesInDocuments.ModelVersion}\"");
            Console.WriteLine("");

            foreach (ExtractKeyPhrasesResult keyPhrases in keyPhrasesInDocuments)
            {
                Console.WriteLine($"On document with Text: \"{documents[i++]}\"");
                Console.WriteLine("");

                if (keyPhrases.HasError)
                {
                    Console.WriteLine("  Error!");
                    Console.WriteLine($"  Document error: {keyPhrases.Error.ErrorCode}.");
                    Console.WriteLine($"  Message: {keyPhrases.Error.Message}");
                }
                else
                {
                    Console.WriteLine($"  Extracted the following {keyPhrases.KeyPhrases.Count()} key phrases:");

                    foreach (string keyPhrase in keyPhrases.KeyPhrases)
                    {
                        Console.WriteLine($"    {keyPhrase}");
                    }
                }
                Console.WriteLine("");
            }
        }
        public void ExtractKeyPhrasesBatchWithNullIdTest()
        {
            TextAnalyticsClient client = GetClient();
            var documents = new List <TextDocumentInput> {
                new TextDocumentInput(null, "Hello world")
            };

            RequestFailedException ex = Assert.ThrowsAsync <RequestFailedException>(async() => await client.ExtractKeyPhrasesBatchAsync(documents));

            Assert.AreEqual(TextAnalyticsErrorCode.InvalidDocument, ex.ErrorCode);
        }
        public async Task ExtractKeyPhrasesBatchAsync()
        {
            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 <TextDocumentInput>
            {
                new TextDocumentInput("1", "Microsoft was founded by Bill Gates and Paul Allen.")
                {
                    Language = "en",
                },
                new TextDocumentInput("2", "Text Analytics is one of the Azure Cognitive Services.")
                {
                    Language = "en",
                },
                new TextDocumentInput("3", "My cat might need to see a veterinarian.")
                {
                    Language = "en",
                }
            };

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

            int i = 0;

            Console.WriteLine($"Results of Azure Text Analytics \"Extract Key Phrases\" Model, version: \"{results.ModelVersion}\"");
            Console.WriteLine("");

            foreach (ExtractKeyPhrasesResult result in results)
            {
                TextDocumentInput document = documents[i++];

                Console.WriteLine($"On document (Id={document.Id}, Language=\"{document.Language}\", Text=\"{document.Text}\"):");

                if (result.HasError)
                {
                    Console.WriteLine($"    Document error: {result.Error.Code}.");
                    Console.WriteLine($"    Message: {result.Error.Message}.");
                }
                else
                {
                    Console.WriteLine($"    Extracted the following {result.KeyPhrases.Count()} key phrases:");

                    foreach (string keyPhrase in result.KeyPhrases)
                    {
                        Console.WriteLine($"        {keyPhrase}");
                    }

                    Console.WriteLine($"    Document statistics:");
                    Console.WriteLine($"        Character count (in Unicode graphemes): {result.Statistics.GraphemeCount}");
                    Console.WriteLine($"        Transaction count: {result.Statistics.TransactionCount}");
                    Console.WriteLine("");
                }
            }

            Console.WriteLine($"Batch operation statistics:");
            Console.WriteLine($"    Document count: {results.Statistics.DocumentCount}");
            Console.WriteLine($"    Valid document count: {results.Statistics.ValidDocumentCount}");
            Console.WriteLine($"    Invalid document count: {results.Statistics.InvalidDocumentCount}");
            Console.WriteLine($"    Transaction count: {results.Statistics.TransactionCount}");
            Console.WriteLine("");
        }
        public async Task ExtractKeyPhrasesBatchAsync()
        {
            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), CreateSampleOptions());

            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.
                                Not necessarily recommended for small children.
                                A hotel close to the trail offers services for childcare in case you want that.";

            string documentB = @"Nos hospedamos en el Hotel Foo la semana pasada por nuestro aniversario. La gerencia
                                sabía de nuestra celebración y me ayudaron a tenerle una sorpresa a mi pareja.
                                La habitación estaba limpia y decorada como yo había pedido. Una gran experiencia.
                                El próximo año volveremos.";

            string documentC = @"That was the best day of my life! We went on a 4 day trip where we stayed at Hotel Foo.
                                They had great amenities that included an indoor pool, a spa, and a bar.
                                The spa offered couples massages which were really good. 
                                The spa was clean and felt very peaceful. Overall the whole experience was great.
                                We will definitely come back.";

            var documents = new List <TextDocumentInput>
            {
                new TextDocumentInput("1", documentA)
                {
                    Language = "en",
                },
                new TextDocumentInput("2", documentB)
                {
                    Language = "es",
                },
                new TextDocumentInput("3", documentC)
                {
                    Language = "en",
                },
                new TextDocumentInput("4", string.Empty)
            };

            var options = new TextAnalyticsRequestOptions {
                IncludeStatistics = true
            };
            Response <ExtractKeyPhrasesResultCollection> response = await client.ExtractKeyPhrasesBatchAsync(documents, options);

            ExtractKeyPhrasesResultCollection keyPhrasesInDocuments = response.Value;

            int i = 0;

            Console.WriteLine($"Results of \"Extract Key Phrases\" Model, version: \"{keyPhrasesInDocuments.ModelVersion}\"");
            Console.WriteLine("");

            foreach (ExtractKeyPhrasesResult keyPhrases in keyPhrasesInDocuments)
            {
                TextDocumentInput document = documents[i++];

                Console.WriteLine($"On document (Id={document.Id}, Language=\"{document.Language}\"):");

                if (keyPhrases.HasError)
                {
                    Console.WriteLine("  Error!");
                    Console.WriteLine($"  Document error: {keyPhrases.Error.ErrorCode}.");
                    Console.WriteLine($"  Message: {keyPhrases.Error.Message}");
                }
                else
                {
                    Console.WriteLine($"  Extracted the following {keyPhrases.KeyPhrases.Count()} key phrases:");

                    foreach (string keyPhrase in keyPhrases.KeyPhrases)
                    {
                        Console.WriteLine($"    {keyPhrase}");
                    }

                    Console.WriteLine($"  Document statistics:");
                    Console.WriteLine($"    Character count: {keyPhrases.Statistics.CharacterCount}");
                    Console.WriteLine($"    Transaction count: {keyPhrases.Statistics.TransactionCount}");
                }
                Console.WriteLine("");
            }

            Console.WriteLine($"Batch operation statistics:");
            Console.WriteLine($"  Document count: {keyPhrasesInDocuments.Statistics.DocumentCount}");
            Console.WriteLine($"  Valid document count: {keyPhrasesInDocuments.Statistics.ValidDocumentCount}");
            Console.WriteLine($"  Invalid document count: {keyPhrasesInDocuments.Statistics.InvalidDocumentCount}");
            Console.WriteLine($"  Transaction count: {keyPhrasesInDocuments.Statistics.TransactionCount}");
            Console.WriteLine("");
        }