Exemple #1
0
        /// <summary>
        ///  Detect Person entities that may have entries in the Wikipedia and print all associated hyperlinks to the console
        /// </summary>
        /// <returns></returns>
        static async Task Task3()
        {
            // Build the body (shared across calls)
            var body      = new JsonData();
            var documents = body.SetEmptyArray("documents");

            int i = 0;

            foreach (string review in ReadReviewsFromJson().Take(5))
            {
                var document = documents.AddEmptyObject();
                document["id"]   = (++i).ToString();
                document["text"] = review;
            }

            // Get all the persons:
            var response = await client.GetEntitiesAsync(RequestContent.Create(body));

            var responseBody = JsonData.FromBytes(response.Content.ToMemory());

            if (response.Status != 200)
            {
                Console.Error.WriteLine(responseBody["error"]);
                return;
            }

            var people = responseBody["documents"].Items.SelectMany(x => x["entities"].Items.Where(e => e["category"] == "Person").Select(e => e["text"])).Distinct();

            // Now, get the links
            response = await client.GetLinkedEntitiesAsync(RequestContent.Create(body));

            responseBody = JsonData.FromBytes(response.Content.ToMemory());

            // For any links, if they are about people in our list, print them.
            if (response.Status == 200)
            {
                foreach (var document in responseBody["documents"].Items)
                {
                    foreach (var entity in document["entities"].Items)
                    {
                        if (people.Contains(entity["name"]) && entity["dataSource"] == "Wikipedia")
                        {
                            Console.WriteLine($"Learn more about {entity["name"]} on {entity["dataSource"]} ({entity["url"]})");
                        }
                    }
                }
            }
            else
            {
                Console.Error.WriteLine(responseBody["error"]);
            }
        }