Exemple #1
0
        /// <summary>
        /// For words or phrases in those reviews that can be categorized as a Person, Location, or Organization, identify whether they are a person, location, or organization.
        /// Print the phrase found in text and its category, e.g.Kurt Russell is a Person.
        /// </summary>
        /// <returns></returns>
        static async Task Task2()
        {
            var requestBody = new JsonData();
            var documents   = requestBody.SetEmptyArray("documents");

            int i = 0;

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

            Response response = await client.GetEntitiesAsync(RequestContent.Create(requestBody));

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

            if (response.Status == 200)
            {
                foreach (var document in responseBody["documents"].Items)
                {
                    foreach (var entity in document["entities"].Items)
                    {
                        switch (entity["category"].ToString())
                        {
                        case "Person":
                        case "Location":
                        case "Organization":
                            Console.WriteLine($"Entity {entity["text"]} is a {entity["category"]}");
                            break;
                        }
                    }
                }
            }
            else
            {
                Console.Error.WriteLine(responseBody["error"]);
            }
        }
        /// <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(body);

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

            var people = response.Body["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(body);

            // For any links, if they are about people in our list, print them.
            if (response.Status == 200)
            {
                foreach (var document in response.Body["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(response.Body["error"]);
            }
        }