Esempio n. 1
0
        /// <summary>
        ///  You are provided with a set of reviews in multiple languages. (reviews_mixed.json).
        ///  Use text analytics API to detect the language for each review and see if the review contains any Personal Identifiable Information. Print review numbers that contains PII.
        /// </summary>
        /// <remarks>
        ///  Seems like this endpoint also returns information about things which aren't PII from time to time? Anyway, the behavior of the SDK matches that of a hand authored REST request
        /// </remarks>
        static async Task Task4()
        {
            // The body we can share across our two calls.
            var body      = new JsonData();
            var documents = body.SetEmptyArray("documents");

            int i = 0;

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

            // Get languages.
            var response = await client.GetLanguagesAsync(RequestContent.Create(body));

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

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

            // Build a map of DocumentID -> Language Name from the response.
            var languageMap = new Dictionary <JsonData, JsonData>(responseBody["documents"].Items.Select(e =>
            {
                return(new KeyValuePair <JsonData, JsonData>(e["id"], e["detectedLanguage"]["iso6391Name"]));
            }));

            // Go back over the body object and augment each document with a language
            foreach (JsonData document in body["documents"].Items)
            {
                document["language"] = languageMap[document["id"]];
            }

            response = await client.GetEntitiesPiiAsync(RequestContent.Create(body));

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

            if (response.Status == 200)
            {
                foreach (var document in responseBody["documents"].Items)
                {
                    foreach (var entity in document["entities"].Items)
                    {
                        Console.WriteLine($"Document {document["id"]} has PII of type: {entity["category"]}");
                    }
                }
            }
            else
            {
                Console.Error.WriteLine(responseBody["error"]);
            }
        }