Ejemplo n.º 1
0
        public async Task AnalyzeOperationRecognizeLinkedEntitiesWithDisableServiceLogs()
        {
            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 RecognizeLinkedEntitiesOptions()
            {
                DisableServiceLogs = true
            };

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                RecognizeLinkedEntitiesOptions = new List <RecognizeLinkedEntitiesOptions>()
                {
                    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 async Task RecognizeLinkedEntitiesBatchWithRecognizeLinkedEntitiesOptionsStatisticsTest()
        {
            RecognizeLinkedEntitiesOptions options = new RecognizeLinkedEntitiesOptions {
                IncludeStatistics = true
            };
            TextAnalyticsClient client = GetClient();
            RecognizeLinkedEntitiesResultCollection results = await client.RecognizeLinkedEntitiesBatchAsync(s_batchDocuments, options);

            var expectedOutput = new Dictionary <string, List <string> >()
            {
                { "1", s_document1ExpectedOutput },
                { "3", s_document1ExpectedOutput },
            };

            ValidateBatchDocumentsResult(results, expectedOutput, includeStatistics: true);

            // Assert the options classes since overloads were added and the original now instantiates a RecognizeLinkedEntitiesOptions.
            Assert.IsTrue(options.IncludeStatistics);
            Assert.IsNull(options.ModelVersion);
        }
        public async Task ExtractEntityLinkingBatchAsync()
        {
            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));

            string documentA = @"Microsoft was founded by Bill Gates with some friends he met at Harvard. One of his friends,
                                Steve Ballmer, eventually became CEO after Bill Gates as well.Steve Ballmer eventually stepped
                                down as CEO of Microsoft, and was succeeded by Satya Nadella.
                                Microsoft originally moved its headquarters to Bellevue, Washington in Januaray 1979, but is now
                                headquartered in Redmond";

            string documentB = @"El CEO de Microsoft es Satya Nadella, quien asumió esta posición en Febrero de 2014. Él
                                empezó como Ingeniero de Software en el año 1992.";

            string documentC = @"Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975, to develop and 
                                sell BASIC interpreters for the Altair 8800. During his career at Microsoft, Gates held
                                the positions of chairman chief executive officer, president and chief software architect
                                while also being the largest individual shareholder until May 2014.";

            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 RecognizeLinkedEntitiesOptions {
                IncludeStatistics = true
            };
            Response <RecognizeLinkedEntitiesResultCollection> response = await client.RecognizeLinkedEntitiesBatchAsync(documents, options);

            RecognizeLinkedEntitiesResultCollection entitiesInDocuments = response.Value;

            int i = 0;

            Console.WriteLine($"Results of Azure Text Analytics \"Entity Linking\", version: \"{entitiesInDocuments.ModelVersion}\"");
            Console.WriteLine("");

            foreach (RecognizeLinkedEntitiesResult entitiesInDocument in entitiesInDocuments)
            {
                TextDocumentInput document = documents[i++];

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

                if (entitiesInDocument.HasError)
                {
                    Console.WriteLine("  Error!");
                    Console.WriteLine($"  Document error code: {entitiesInDocument.Error.ErrorCode}.");
                    Console.WriteLine($"  Message: {entitiesInDocument.Error.Message}");
                }
                else
                {
                    Console.WriteLine($"Recognized {entitiesInDocument.Entities.Count} entities:");
                    foreach (LinkedEntity linkedEntity in entitiesInDocument.Entities)
                    {
                        Console.WriteLine($"  Name: {linkedEntity.Name}");
                        Console.WriteLine($"  Language: {linkedEntity.Language}");
                        Console.WriteLine($"  Data Source: {linkedEntity.DataSource}");
                        Console.WriteLine($"  URL: {linkedEntity.Url}");
                        Console.WriteLine($"  Entity Id in Data Source: {linkedEntity.DataSourceEntityId}");
                        foreach (LinkedEntityMatch match in linkedEntity.Matches)
                        {
                            Console.WriteLine($"    Match Text: {match.Text}");
                            Console.WriteLine($"    Offset: {match.Offset}");
                            Console.WriteLine($"    Length: {match.Length}");
                            Console.WriteLine($"    Confidence score: {match.ConfidenceScore}");
                        }
                        Console.WriteLine("");
                    }

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

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