private List <TextAnalyticsRequestsChunk> CreateAudioLevelRequests(SpeechTranscript speechTranscript, int documentRequestLimit)
        {
            var textAnalyticChunks = new List <TextAnalyticsRequestsChunk>();

            if (!speechTranscript.RecognizedPhrases.Any())
            {
                return(textAnalyticChunks);
            }

            var textAnalyticsDocumentList = new List <TextAnalyticsRequest>();

            foreach (var combinedRecognizedPhrase in speechTranscript.CombinedRecognizedPhrases)
            {
                var channel      = combinedRecognizedPhrase.Channel;
                var requestCount = 0;

                var displayForm = combinedRecognizedPhrase.Display;

                for (int i = 0; i < displayForm.Length; i += TextAnalyticsRequestCharacterLimit)
                {
                    var displayChunk          = displayForm.Substring(i, Math.Min(TextAnalyticsRequestCharacterLimit, displayForm.Length - i));
                    var textAnalyticsDocument = new TextAnalyticsRequest(Locale, $"{channel}_{requestCount}", displayChunk);
                    textAnalyticsDocumentList.Add(textAnalyticsDocument);
                    requestCount += 1;
                }
            }

            for (int i = 0; i < textAnalyticsDocumentList.Count; i += documentRequestLimit)
            {
                textAnalyticChunks.Add(new TextAnalyticsRequestsChunk(textAnalyticsDocumentList.GetRange(i, Math.Min(documentRequestLimit, textAnalyticsDocumentList.Count - i))));
            }

            Log.LogInformation($"Received {textAnalyticChunks.Count} text analytics chunks from {textAnalyticsDocumentList.Count} documents.");
            return(textAnalyticChunks);
        }
        private List <TextAnalyticsRequestsChunk> CreateUtteranceLevelRequests(SpeechTranscript speechTranscript, int documentRequestLimit)
        {
            var textAnalyticChunks = new List <TextAnalyticsRequestsChunk>();

            if (!speechTranscript.RecognizedPhrases.Any())
            {
                return(textAnalyticChunks);
            }

            var textAnalyticsDocumentList = new List <TextAnalyticsRequest>();

            foreach (var recognizedPhrase in speechTranscript.RecognizedPhrases)
            {
                var id   = $"{recognizedPhrase.Channel}_{recognizedPhrase.Offset}";
                var text = recognizedPhrase.NBest.FirstOrDefault().Display;
                text = text.Substring(0, Math.Min(text.Length, TextAnalyticsRequestCharacterLimit));
                var textAnalyticsDocument = new TextAnalyticsRequest(Locale, id, text);
                textAnalyticsDocumentList.Add(textAnalyticsDocument);
            }

            for (int i = 0; i < textAnalyticsDocumentList.Count; i += documentRequestLimit)
            {
                textAnalyticChunks.Add(new TextAnalyticsRequestsChunk(textAnalyticsDocumentList.GetRange(i, Math.Min(documentRequestLimit, textAnalyticsDocumentList.Count - i))));
            }

            Log.LogInformation($"Received {textAnalyticChunks.Count} text analytics chunks from {textAnalyticsDocumentList.Count} documents.");
            return(textAnalyticChunks);
        }
        private List <TextAnalyticsRequestsChunk> CreateRequestChunks(SpeechTranscript speechTranscript, int documentRequestLimit)
        {
            var textAnalyticsDocumentList = new List <TextAnalyticsRequest>();
            var textAnalyticChunks        = new List <TextAnalyticsRequestsChunk>();

            if (!speechTranscript.RecognizedPhrases.Any())
            {
                return(textAnalyticChunks);
            }

            foreach (var recognizedPhrase in speechTranscript.RecognizedPhrases)
            {
                var id = $"{recognizedPhrase.Channel}_{recognizedPhrase.Offset}";
                var textAnalyticsDocument = new TextAnalyticsRequest(Locale, id, recognizedPhrase.NBest.FirstOrDefault().Display);
                textAnalyticsDocumentList.Add(textAnalyticsDocument);
            }

            Log.LogInformation($"Total text analytics documents: {textAnalyticsDocumentList.Count}");

            for (int i = 0; i < textAnalyticsDocumentList.Count; i += documentRequestLimit)
            {
                textAnalyticChunks.Add(new TextAnalyticsRequestsChunk(textAnalyticsDocumentList.GetRange(i, Math.Min(documentRequestLimit, textAnalyticsDocumentList.Count - i))));
            }

            Log.LogInformation($"Total chunks: {textAnalyticChunks.Count}");
            return(textAnalyticChunks);
        }
        public static async Task <string> GetTextAnalyticsData(string apiUrl, string apiKey, string utterance, string apiOperation)
        {
            using (var client = new HttpClient())
            {
                //setup HttpClient
                var fullApiUrl = apiUrl + apiOperation;
                client.BaseAddress = new Uri(fullApiUrl);
                client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", apiKey);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                //setup data object
                var documents = new List <TextAnalyticsRequestDocument>();
                documents.Add(new TextAnalyticsRequestDocument()
                {
                    id       = Guid.NewGuid().ToString(),
                    language = "en",
                    text     = utterance
                });
                var dataObject = new TextAnalyticsRequest()
                {
                    documents = documents
                };

                //setup httpContent object
                var dataJson = JsonConvert.SerializeObject(dataObject);
                HttpResponseMessage response;
                using (HttpContent content = new StringContent(dataJson))
                {
                    content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
                    response = await client.PostAsync(fullApiUrl, content);
                }

                if (response.IsSuccessStatusCode)
                {
                    return(await response.Content.ReadAsStringAsync());
                }
                else
                {
                    return(string.Empty);
                }
            }
        }
        public async Task <IEnumerable <string> > GetImportantWords(string fullText)
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "181f82bbf51a4bf88921fe13ee1b2638");

                var request = new TextAnalyticsRequest()
                {
                    Documents = new List <TextAnalyticsDocumentRequest>()
                    {
                        new TextAnalyticsDocumentRequest()
                        {
                            Id       = "1",
                            Language = "en",
                            Text     = fullText
                        }
                    }
                };
                var requestJson = JsonConvert.SerializeObject(request);

                var content = new StringContent(requestJson, Encoding.UTF8, "application/json");

                var response =
                    await
                    client.PostAsync("https://text-analytics-bscox.cognitiveservices.azure.com/text/analytics/v2.1/keyPhrases",
                                     content);

                if (!response.IsSuccessStatusCode)
                {
                    return(null);
                }

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

                var responseObject = JsonConvert.DeserializeObject <TextAnalyticsResponse>(responseJson);
                IEnumerable <string> keyPhrases = responseObject.Documents.FirstOrDefault()?.KeyPhrases;

                return(keyPhrases);
            }
        }
Exemplo n.º 6
0
        private async Task AnalyzeText(IDialogContext context, string sentence)
        {
            // 57af6a7afd87438497231387d94767ba
            var client      = new HttpClient();
            var queryString = HttpUtility.ParseQueryString(String.Empty);

            // Request headers
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "694352e44987494dbd7cdc52fb65ba09");

            var uri = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment?" + queryString;

            HttpResponseMessage response;
            var body = new TextAnalyticsRequest
            {
                Documents = new List <RequestDocument>
                {
                    new RequestDocument
                    {
                        Language = "en",
                        Text     = sentence,
                        Id       = "primary"
                    }
                }
            };
            var jsonBody = JsonConvert.SerializeObject(body);

            // Request body
            byte[] byteData = Encoding.UTF8.GetBytes(jsonBody);
            TextAnalyticsResponse textAnalyticsResponse;

            using (var content = new ByteArrayContent(byteData))
            {
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                response = await client.PostAsync(uri, content);

                var json = await response.Content.ReadAsStringAsync();

                Debug.WriteLine(json);
                textAnalyticsResponse = JsonConvert.DeserializeObject <TextAnalyticsResponse>(json);
            }
            if (textAnalyticsResponse != null)
            {
                var message      = context.MakeMessage();
                var responseCard = new HeroCard();
                if (textAnalyticsResponse.documents[0].score > .80)
                {
                    responseCard.Images = new List <CardImage> {
                        new CardImage("http://localhost:3979/Assets/Star5.png")
                    }
                }
                ;
                else if (textAnalyticsResponse.documents[0].score > .60)
                {
                    responseCard.Images = new List <CardImage> {
                        new CardImage("http://localhost:3979/Assets/Star4.png")
                    }
                }
                ;
                else if (textAnalyticsResponse.documents[0].score > .40)
                {
                    responseCard.Images = new List <CardImage> {
                        new CardImage("http://localhost:3979/Assets/Star3.png")
                    }
                }
                ;
                else if (textAnalyticsResponse.documents[0].score > .20)
                {
                    responseCard.Images = new List <CardImage> {
                        new CardImage("http://localhost:3979/Assets/Star2.png")
                    }
                }
                ;
                else
                {
                    responseCard.Images = new List <CardImage> {
                        new CardImage("http://localhost:3979/Assets/Star1.png")
                    }
                };

                responseCard.Title = "Text Analytics";
                responseCard.Text  = $"Your entered sentence is {Math.Truncate(textAnalyticsResponse.documents[0].score * 100)}% positive.";
                message.Attachments.Add(responseCard.ToAttachment());
                await context.PostAsync(message);

                context.Wait(this.MessageReceived);
            }
        }
    }