public static async Task <List <TweetSentiment> > AnalyzeTweetSentiment(List <TweetRaw> tweets) { var tweetsSentiment = new List <TweetSentiment>(); int page = 0; int itemPerPage = 50; var temp = new List <TweetRaw>(); temp.AddRange(tweets.Skip(itemPerPage * page++).Take(itemPerPage)); while (temp.Count > 0) { var textBatchInput = new TextAnalyticsBatchInput(); foreach (var t in temp) { var textAnalysis = new TextAnalyticsInput { Id = t.Id.ToString(), Text = t.Text }; textBatchInput.Documents.Add(textAnalysis); } var sentimentResponse = await AzureSentiment.SentimentV3PreviewPredictAsync(textBatchInput); Console.WriteLine($"Tweets analyzed:{itemPerPage + itemPerPage * (page - 1)}"); foreach (var document in sentimentResponse.Documents) { var tweetSentiment = new TweetSentiment { TweetRawId = long.Parse(document.Id), PositiveScore = document.DocumentScores.Positive, NeutralScore = document.DocumentScores.Neutral, NegativeScore = document.DocumentScores.Negative, Sentiment = Enum.Parse <Degree.Models.DocumentSentimentLabel>(document.Sentiment.ToString()), }; if (document.Sentences != null && document.Sentences.Count() > 0) { foreach (var sentence in document.Sentences) { var s = new TweetSentenceSentiment() { TweetSentimentId = long.Parse(document.Id), Length = sentence.Length, PositiveScore = sentence.SentenceScores.Positive, NeutralScore = sentence.SentenceScores.Neutral, NegativeScore = sentence.SentenceScores.Negative, Offset = sentence.Offset, Sentiment = Enum.Parse <Degree.Models.SentenceSentimentLabel>(sentence.Sentiment.ToString()), Warnings = sentence.Warnings }; tweetSentiment.Sentences.Add(s); } } tweetsSentiment.Add(tweetSentiment); } temp.Clear(); temp.AddRange(tweets.Skip(itemPerPage * page++).Take(itemPerPage)); } return(tweetsSentiment); }
public static async Task <EntityPII> EntityRecognitionV3PreviewPredictAsync(TextAnalyticsBatchInput inputDocuments) { Keys.LoadKey(); using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", Keys.Azure.TEXT_ANALYSIS_KEY); var json = JsonConvert.SerializeObject(inputDocuments); var httpContent = new StringContent(json, Encoding.UTF8, "application/json"); var httpResponse = await httpClient.PostAsync(new Uri(Keys.Azure.TEXT_ANALYSIS_ENTITY_URL), httpContent); var responseContent = await httpResponse.Content.ReadAsStringAsync(); if (!httpResponse.StatusCode.Equals(HttpStatusCode.OK) || httpResponse.Content == null) { throw new Exception(responseContent); } return(JsonConvert.DeserializeObject <EntityPII>(responseContent, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore })); } }
public static void Main(string[] args) { var count = 1; Console.WriteLine("=============== Sentiment Analysis ================\n"); Console.WriteLine("The process of computationally identifying and categorizing opinions\nexpressed by the user, especially in order to determine whether the\nuser's attitude towards a particular topic, product, etc. is positive, negative, or neutral.\n"); Console.WriteLine("=============== Start of process ==================\n"); while (true) { Console.WriteLine("=============== Prediction Attempt:" + count++ + "==============="); //Console.WriteLine("Prediction Attempt : " + count++); Console.WriteLine("Enter the Sentiment: "); var inputSentiment = Console.ReadLine(); var inputDocuments = new TextAnalyticsBatchInput() { Documents = new List <TextAnalyticsInput>() { new TextAnalyticsInput() { Id = "1", Text = inputSentiment } } }; var sentimentPrediction = TextAnalyticsSentimentClient.SentimentPreviewPredictAsync(inputDocuments).Result; Console.WriteLine("===================================================================================="); Console.WriteLine("\nSentiment = " + sentimentPrediction.Documents[0].Sentiment + "\nScore = " + sentimentPrediction.Documents[0].Score); Console.WriteLine("\n====================================================================================\n\n"); } }
public static async Task <List <TweetEntityRecognized> > AnalyzeTweetEntity(List <TweetRaw> tweets) { var tweetsEntity = new List <TweetEntityRecognized>(); int page = 0; int itemPerPage = 50; var temp = new List <TweetRaw>(); temp.AddRange(tweets.Skip(itemPerPage * page++).Take(itemPerPage)); while (temp.Count > 0) { var textBatchInput = new TextAnalyticsBatchInput(); foreach (var t in temp) { var textAnalysis = new TextAnalyticsInput { Id = t.Id.ToString(), Text = t.Text }; textBatchInput.Documents.Add(textAnalysis); } var entityRecognitionResponse = await AzureEntityRecognition.EntityRecognitionV3PreviewPredictAsync(textBatchInput); Console.WriteLine($"Tweets analyzed:{itemPerPage + itemPerPage * (page - 1)}"); foreach (var document in entityRecognitionResponse.Documents) { foreach (var entity in document.Entities) { var tweetEntity = new TweetEntityRecognized { EntityName = entity.Text, EntityType = entity.Type, Length = entity.Length, Offset = entity.Offset, Score = entity.Score, TweetRawId = long.Parse(document.Id) }; tweetsEntity.Add(tweetEntity); } } temp.Clear(); temp.AddRange(tweets.Skip(itemPerPage * page++).Take(itemPerPage)); } return(tweetsEntity); }
public string analyze(string s) { var inputDocuments = new TextAnalyticsBatchInput() { Documents = new List <TextAnalyticsInput>() { new TextAnalyticsInput() { Id = "1", Text = s } } }; var sentimentV3Prediction = TextAnalyticsSentimentV3Client.SentimentV3PreviewPredictAsync(inputDocuments).Result; string res = "" + sentimentV3Prediction.Documents[0].Sentiment; return(res); }
public static void Main(string[] args) { var inputDocuments = new TextAnalyticsBatchInput() { Documents = new List <TextAnalyticsInput>() { new TextAnalyticsInput() { Id = "1", Text = "Hello world. This is some input text." }, new TextAnalyticsInput() { Id = "2", Text = "It's incredibly sunny outside! I'm so happy." }, new TextAnalyticsInput() { Id = "3", Text = "Pike place market is not my favorite Seattle attraction." } } }; //If you’re using C# 7.1 or greater, you can use an async main() method to await the function var sentimentV3Prediction = TextAnalyticsSentimentV3Client.SentimentV3PreviewPredictAsync(inputDocuments).Result; // Replace with whatever you wish to print or simply consume the sentiment v3 prediction Console.WriteLine("Document ID=" + sentimentV3Prediction.Documents[0].Id + " : Sentiment=" + sentimentV3Prediction.Documents[0].Sentiment); Console.WriteLine("Document ID=" + sentimentV3Prediction.Documents[1].Id + " : Sentiment=" + sentimentV3Prediction.Documents[1].Sentiment); Console.WriteLine("Document ID=" + sentimentV3Prediction.Documents[2].Id + " : Sentiment=" + sentimentV3Prediction.Documents[2].Sentiment); Console.ReadKey(); }
public static async Task <SentimentV3Response> SentimentV3PreviewPredictAsync(TextAnalyticsBatchInput inputDocuments) { using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", textAnalyticsKey); var httpContent = new StringContent(JsonConvert.SerializeObject(inputDocuments), Encoding.UTF8, "application/json"); var httpResponse = await httpClient.PostAsync(new Uri(textAnalyticsUrl), httpContent); var responseContent = await httpResponse.Content.ReadAsStringAsync(); if (!httpResponse.StatusCode.Equals(HttpStatusCode.OK) || httpResponse.Content == null) { throw new Exception(responseContent); } return(JsonConvert.DeserializeObject <SentimentV3Response>(responseContent, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore })); } }
public static async Task <SentimentResponse> SentimentPreviewPredictAsync(TextAnalyticsBatchInput inputDocuments) { //Uri newuri = new Uri(textAnalyticsUrl); //WebRequest objwebRequest = WebRequest.Create(newuri); //objwebRequest.Headers.Add("Ocp-Apim-Subscription-Key", textAnalyticsKey); //HttpWebResponse objwebResponse = (HttpWebResponse)objwebRequest.GetResponse(); //StreamReader objStreamReader = new StreamReader(objwebResponse.GetResponseStream()); //string sResponse = objStreamReader.ReadToEnd(); //List<SentimentResponse> dataList = JsonConvert.DeserializeObject<List<SentimentResponse>>(sResponse); using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", textAnalyticsKey); var httpContent = new StringContent(JsonConvert.SerializeObject(inputDocuments), Encoding.UTF8, "application/json"); var httpResponse = await httpClient.PostAsync(new Uri(textAnalyticsUrl), httpContent); var responseContent = await httpResponse.Content.ReadAsStringAsync(); if (!httpResponse.StatusCode.Equals(HttpStatusCode.OK) || httpResponse.Content == null) { throw new Exception(responseContent); } var test = JsonConvert.DeserializeObject(responseContent); JObject jObject = JObject.Parse(responseContent); string score = (string)jObject.SelectToken("documents[0].score"); //JArray signInNames = (JArray)jObject.SelectToken("documents"); //foreach (JToken signInName in signInNames) //{ // type = (string)signInName.SelectToken("score"); //} SentimentResponse sR = JsonConvert.DeserializeObject <SentimentResponse>(responseContent, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); double scoreCompare = Convert.ToDouble(score); if (scoreCompare > .5) { sR.Documents[0].Sentiment = "Positive"; } else { sR.Documents[0].Sentiment = "Negative"; } return(sR); } }