private static async Task <AnalysedDocument> AnalyzeDocument(TextApiRequest sentimentDocument, string textAnalysisType)
        {
            // This example app does not yet have support for entities in v3.
            string version = _useLatestVersion && textAnalysisType != "entities" ? "3.0" : "2.1";

            TextApiResponse textApiResponse;

            using (var client = new HttpClient())
            {
                var textApiUrl = Constants.TextApiBaseUrl;
                if (!textApiUrl.StartsWith("http"))
                {
                    // Make sure we support custom URL as well as from earlier versions.
                    textApiUrl = $"https://{textApiUrl}.cognitiveservices.azure.com";
                }

                client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", Constants.TextApiToken);

                string json     = JsonConvert.SerializeObject(sentimentDocument);
                var    content  = new StringContent(json, Encoding.UTF8, "application/json");
                var    response = await client.PostAsync($"{textApiUrl.TrimEnd('/')}/text/analytics/v{version}/{textAnalysisType}", content);

                string responseJson = await response.Content.ReadAsStringAsync();

                textApiResponse = JsonConvert.DeserializeObject <TextApiResponse>(responseJson);
            }

            return(textApiResponse?.documents?.FirstOrDefault());
        }
        private static async Task AnalyzeText(SpeechRecognitionResult speechToTextResult)
        {
            WriteLineInColor(speechToTextResult.Text, ConsoleColor.Cyan);

            Console.WriteLine();
            WriteLineInColor("Text analysis...", ConsoleColor.DarkGray);

            // Prepare document for different text analysis APIs.
            // All of the requests will take in exactly the same request body.
            var documentRequest = new TextApiRequest
            {
                documents = new Document[]
                {
                    new Document
                    {
                        language = "en",
                        id       = "1",
                        text     = speechToTextResult.Text
                    }
                }
            };

            // Get sentiment analysis via Cognitive Services Text Analysis APIs.
            AnalysedDocument sentimentResult = await AnalyzeDocument(documentRequest, "sentiment");

            if (sentimentResult != null)
            {
                // We get back score representing sentiment.
                if (_useLatestVersion)
                {
                    // We are getting a more accurate representation of how positive, negative and neutral the text is.
                    Console.Write("  Sentiment is ");
                    WriteInColor(sentimentResult.sentiment, _sentimentToColor[sentimentResult.sentiment]);
                    Console.WriteLine($" with scores:");

                    WriteValuesInColor("   - Positive:  ", $"{Math.Round(sentimentResult.confidenceScores.positive * 100, 2)}%", _sentimentToColor["positive"]);
                    WriteValuesInColor("   - Neutral:   ", $"{Math.Round(sentimentResult.confidenceScores.neutral * 100, 2)}%", _sentimentToColor["neutral"]);
                    WriteValuesInColor("   - Negative:  ", $"{Math.Round(sentimentResult.confidenceScores.negative * 100, 2)}%", _sentimentToColor["negative"]);
                }
                else
                {
                    // We only get how potentially positive the text is in Sentiment analysis v2.
                    double score = sentimentResult.score;

                    // Try to determine if message is positive, negative or neutral.
                    string sentiment = score >= 0.75 ? "positive" : (score < 0.25 ? "negative" : "neutral");
                    Console.WriteLine($"  Sentiment is {sentiment} ({Math.Round(score * 100)}%)");
                }
            }
            else
            {
                WriteLineInColor("  No sentiment found", ConsoleColor.DarkYellow);
            }

            Console.WriteLine();

            AnalysedDocument keyPhrasesResult = await AnalyzeDocument(documentRequest, "keyPhrases");

            if (keyPhrasesResult?.keyPhrases?.Any() == true)
            {
                Console.WriteLine($"  Key phrases:");
                foreach (var keyPhrase in keyPhrasesResult.keyPhrases)
                {
                    Console.WriteLine($"   - {keyPhrase}");
                }
            }
            else
            {
                WriteLineInColor("  No key phrases found", ConsoleColor.DarkYellow);
            }

            Console.WriteLine();

            AnalysedDocument namedEntitiesResult = await AnalyzeDocument(documentRequest, "entities");

            if (namedEntitiesResult?.entities?.Any() == true)
            {
                Console.WriteLine("  Entities:");
                foreach (var entity in namedEntitiesResult.entities)
                {
                    Console.WriteLine($"   - {entity.name} ({entity.type})");
                    if (!string.IsNullOrWhiteSpace(entity.wikipediaUrl))
                    {
                        WriteLineInColor($"       {entity.wikipediaUrl}", ConsoleColor.Blue);
                    }
                }
            }
            else
            {
                WriteLineInColor("  No entities found", ConsoleColor.DarkYellow);
            }
        }