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);
        }
Example #2
0
        public async Task AnalyzeOperationKeyPhrasesWithDisableServiceLogs()
        {
            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."
            };

            var options = new ExtractKeyPhrasesOptions()
            {
                DisableServiceLogs = true
            };

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

            await client.StartAnalyzeBatchActionsAsync(documents, batchActions);

            var content = mockTransport.Requests.Single().Content;

            using var stream = new MemoryStream();
            await content.WriteToAsync(stream, default);

            stream.Position        = 0;
            using var streamReader = new StreamReader(stream);
            string contentString = streamReader.ReadToEnd();
            string logging       = contentString.Substring(contentString.IndexOf("loggingOptOut"), 19);

            var expectedContent = "loggingOptOut\":true";

            Assert.AreEqual(expectedContent, logging);
        }
        public void ExtractKeyPhrasesBatch()
        {
            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));

            #region Snippet:TextAnalyticsSample3ExtractKeyPhrasesBatch
            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 ExtractKeyPhrasesOptions {
                IncludeStatistics = true
            };
            Response <ExtractKeyPhrasesResultCollection> response = client.ExtractKeyPhrasesBatch(documents, options);
            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)
            {
                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("");
            #endregion
        }