public TextAnalyticsResponse <SentimentResponse> Get(TextMessages messages) { var requestContent = new StringContent(JsonConvert.SerializeObject(messages), Encoding.UTF8, JsonContentType); var httpResponse = HttpClient.PostAsync(SentimentEndpoint, requestContent).Result; httpResponse.EnsureSuccessStatusCode(); TextAnalyticsResponse <SentimentResponse> response = ReadResponseMessage <SentimentResponse>(httpResponse); return(response); }
private void MergeUpdatedSentimentsWithCacheValues(string resultJson) { JsonObject jsonObj = JsonObject.Parse(resultJson); var documentsArray = jsonObj.GetNamedArray("documents"); var processedObjects = documentsArray.Select(a => { var objNew = a.GetObject(); var objParsednew = new TextAnalyticsResponse { Id = long.Parse(objNew.GetNamedString("id")), Sentiment = objNew.GetNamedNumber("score") }; return(objParsednew); }).ToList(); processedObjects.ForEach(update => { var toBeUpdated = NewsCache.Where(c => c.DatePublished.Ticks == update.Id).ToList(); toBeUpdated.ForEach(oldValue => oldValue.Sentiment = update.Sentiment); }); }
static async Task ProcessTexts() { string msg = "Today is an excellent day! Every day is; you see, rain makes plants grow and sun makes us happy. It's all good, either way."; string apiUrl = "https://eastus.api.cognitive.microsoft.com/text/analytics/v2.1/"; string apiKey = "2b7db267d498432e80061c5c8146b7e7"; TextAnalyticsServiceClient svc = new TextAnalyticsServiceClient(apiUrl, apiKey); TextAnalyticsServiceResult svcResult = svc.ProcessAsync(msg).GetAwaiter().GetResult(); if (svcResult.Responses.Count > 0) { TextAnalyticsResponse response = svcResult.Responses.First(); Console.WriteLine("Sentiment Score: " + response.SentimentScore.ToString()); Console.WriteLine(); Console.WriteLine("Key Phrases: " + response.KeyPhrases.Count.ToString()); foreach (string keyPhrase in response.KeyPhrases) { Console.WriteLine(keyPhrase); } Console.WriteLine(); Console.WriteLine("Detected Languages: " + response.DetectedLanguages.Count.ToString()); foreach (TextAnalyticsLanguage detectedLanguage in response.DetectedLanguages) { Console.WriteLine(detectedLanguage.Name); } Console.WriteLine(); Console.WriteLine("Entities: " + response.Entities.Count.ToString()); foreach (TextAnalyticsEntity entity in response.Entities) { Console.WriteLine(entity.Name + " | " + entity.Type + " | " + entity.SubType); } Console.WriteLine(); Console.WriteLine("Errors: " + svcResult.Errors.Count.ToString()); foreach (TextAnalyticsError error in svcResult.Errors) { Console.WriteLine(error.Message); } Console.WriteLine(); } //Console.WriteLine("George Washington Farewell Address (partial)"); //string text2 = File.ReadAllText("texts/text2.txt"); //TextAnalyticsServiceResult result2 = await svc.ProcessAsync(text2); //string json2 = result2.ToJson(); //Console.WriteLine(json2); //Console.WriteLine(); //Console.WriteLine(); }