Exemple #1
0
        private static async Task RunRecognizeEntities(TextAnalyticsClient client,
                                                       MultiLanguageBatchInput docs)
        {
            var res = await client.EntitiesBatchAsync(docs);

            foreach (var document in res.Documents)
            {
                Console.WriteLine($"Document ID: {document.Id} ");
                Console.WriteLine("\tEntities:");

                foreach (var entity in document.Entities)
                {
                    Console.WriteLine($"\t\t{entity.Name}");
                    Console.WriteLine($"\t\tType: {entity.Type ?? "N/A"}");
                    Console.WriteLine($"\t\tSubType: {entity.SubType ?? "N/A"}");

                    foreach (var match in entity.Matches)
                    {
                        Console.WriteLine($"\t\tScore: {match.EntityTypeScore:F3}");
                    }

                    Console.WriteLine($"\t");
                }
            }
        }
        public async Task RunRecognizeEntitiesAsync(CidadaoModel cidadao)
        {
            var credentials = new ApiKeyServiceClientCredentials(key);
            var client      = new TextAnalyticsClient(credentials)
            {
                Endpoint = endpoint
            };

            // The documents to be submitted for entity recognition. The ID can be any value.
            var inputDocuments = new MultiLanguageBatchInput(
                new List <MultiLanguageInput>
            {
                new MultiLanguageInput("1", cidadao.texto, "pt"),
            });

            var entitiesResult = await client.EntitiesBatchAsync(inputDocuments);


            foreach (var document in entitiesResult.Documents)
            {
                foreach (var entity in document.Entities)
                {
                    foreach (var match in entity.Matches)
                    {
                        // Console.WriteLine($"\t\t\tOffset: {match.Offset},\tLength: {match.Length},\tScore: {match.EntityTypeScore:F3}");
                    }
                }
            }
            // Console.WriteLine();
        }
        public static async Task RunAsync(string endpoint, string key)
        {
            var credentials = new ApiKeyServiceClientCredentials(key);
            var client      = new TextAnalyticsClient(credentials)
            {
                Endpoint = endpoint
            };

            // The documents to be submitted for entity recognition. The ID can be any value.
            var inputDocuments = new MultiLanguageBatchInput(
                new List <MultiLanguageInput>
            {
                new MultiLanguageInput("1", "Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975, to develop and sell BASIC interpreters for the Altair 8800.", "en"),
                new MultiLanguageInput("2", "La sede principal de Microsoft se encuentra en la ciudad de Redmond, a 21 kilómetros de Seattle.", "es")
            });

            var entitiesResult = await client.EntitiesBatchAsync(inputDocuments);

            // Printing recognized entities
            Console.WriteLine("===== Named Entity Recognition & Entity Linking =====\n");

            foreach (var document in entitiesResult.Documents)
            {
                Console.WriteLine($"Document ID: {document.Id} ");

                Console.WriteLine("\t Entities:");

                foreach (var entity in document.Entities)
                {
                    Console.WriteLine($"\t\tName: {entity.Name},\tType: {entity.Type ?? "N/A"},\tSub-Type: {entity.SubType ?? "N/A"}");
                    foreach (var match in entity.Matches)
                    {
                        Console.WriteLine($"\t\t\tOffset: {match.Offset},\tLength: {match.Length},\tScore: {match.EntityTypeScore:F3}");
                    }
                }
            }
            Console.WriteLine();
        }
Exemple #4
0
        public static async Task <StandardCVForm> RunAsync(string endpoint, string key, string text)
        {
            var credentials = new ApiKeyServiceClientCredentials(key);
            var client      = new TextAnalyticsClient(credentials)
            {
                Endpoint = endpoint
            };

            // The documents to be submitted for entity recognition. The ID can be any value.
            var inputDocument = new MultiLanguageBatchInput(
                new List <MultiLanguageInput>
            {
                new MultiLanguageInput("1", text, "en")
            });

            var entitiesResult = await client.EntitiesBatchAsync(inputDocument);

            if (entitiesResult.Errors.Count > 0)
            {
                throw new Exception(entitiesResult.Errors[0].Message);
            }

            var parsedCV = new StandardCVForm()
            {
                Experiences = new List <Experience>()
            };

            // Getting name
            var recognizedPersons = entitiesResult.Documents[0].Entities.FirstOrDefault(e => e.Type == "Person");

            if (recognizedPersons != null)
            {
                var person = recognizedPersons.Matches.FirstOrDefault(m => m.EntityTypeScore > 0.7);
                parsedCV.Name = person != null ? person.Text : "<not present>";
            }

            var recognizedEmails = entitiesResult.Documents[0].Entities.FirstOrDefault(e => e.Type == "Email");

            if (recognizedEmails != null)
            {
                var email = recognizedEmails.Matches.FirstOrDefault(m => m.EntityTypeScore > 0.7);
                parsedCV.Email = email != null ? email.Text : "<not present>";
            }

            var recognizedPhones = entitiesResult.Documents[0].Entities.FirstOrDefault(e => e.Type == "Phone_Number");

            if (recognizedPhones != null)
            {
                var phone = recognizedPhones.Matches.FirstOrDefault(m => m.EntityTypeScore > 0.7);
                parsedCV.Phone = phone != null ? phone.Text : "<not present>";
            }

            var recognizedLocations = entitiesResult.Documents[0].Entities.Where(e => e.Type == "Location");

            for (var i = 0; i < recognizedLocations.Count() && i < 5; i++)
            {
                parsedCV.Address += recognizedLocations.ElementAt(i).Matches[0].Text;
            }

            var companies = entitiesResult.Documents[0].Entities.Where(e => e.Type == "Organization");

            foreach (var range in entitiesResult.Documents[0].Entities.Where(e => e.SubType == "DateRange"))
            {
                var period = range.Matches.FirstOrDefault(m => m.EntityTypeScore > 0.6);
                if (period != null)
                {
                    var experience = new Experience()
                    {
                        Period = period.Text
                    };

                    if (companies.Any())
                    {
                        // getting the first occurance of a company entity after the period
                        var company = companies.FirstOrDefault(c => c.Matches.Any(m => m.Offset > period.Offset));
                        experience.Company = company.Matches[0].Text;
                    }


                    parsedCV.Experiences.Add(experience);
                }
            }


            return(parsedCV);
        }