/// <summary> /// Analyses the sentiment of the provided content. /// </summary> /// <param name="content">The content.</param> /// <returns> /// The sentiment result for the content. /// </returns> public async Task <ISentimentAnalysisModel> AnalyseSentiment(string content) { DocumentSentiment documentSentiment = await this.textAnalyticsClient.AnalyzeSentimentAsync(content); var result = new SentimentAnalysisModel() { SentimentResult = documentSentiment.Sentiment.ToString(), ConfidenceScores = new Dictionary <string, double>() { { "Neutral", documentSentiment.ConfidenceScores.Neutral }, { "Positive", documentSentiment.ConfidenceScores.Positive }, { "Negative", documentSentiment.ConfidenceScores.Negative } } }; return(result); }
static void SentimentAnalysisExample(TextAnalyticsClient client) { string inputText = "The FitnessGram PACER Test is a multistage aerobic capacity test that progressively gets more difficult as it continues. The test is used to measure a students aerobic capacity as part of the FitnessGram assessment. Students run back and forth as many times as they can, each lap signaled by a beep sound. The test get progressively faster as it continues until the student reaches their max lap score."; DocumentSentiment documentSentiment = client.AnalyzeSentiment(inputText); Console.WriteLine($"Document sentiment: {documentSentiment.Sentiment}\n"); foreach (var sentence in documentSentiment.Sentences) { Console.WriteLine($"\tText: \"{sentence.Text}\""); Console.WriteLine($"\tSentence sentiment: {sentence.Sentiment}"); Console.WriteLine($"\tPositive score: {sentence.ConfidenceScores.Positive:0.00}"); Console.WriteLine($"\tNegative score: {sentence.ConfidenceScores.Negative:0.00}"); Console.WriteLine($"\tNeutral score: {sentence.ConfidenceScores.Neutral:0.00}\n"); } }
static void SentimentAnalysisExample(TextAnalyticsClient client) { //string inputText = "I had the best day of my life. I wish you were there with me."; string inputText = "I liked the food. The host was grumpy."; DocumentSentiment documentSentiment = client.AnalyzeSentiment(inputText); Console.WriteLine($"Document sentiment: {documentSentiment.Sentiment}\n"); foreach (var sentence in documentSentiment.Sentences) { Console.WriteLine($"\tText: \"{sentence.Text}\""); Console.WriteLine($"\tSentence sentiment: {sentence.Sentiment}"); Console.WriteLine($"\tPositive score: {sentence.ConfidenceScores.Positive:0.00}"); Console.WriteLine($"\tNegative score: {sentence.ConfidenceScores.Negative:0.00}"); Console.WriteLine($"\tNeutral score: {sentence.ConfidenceScores.Neutral:0.00}\n"); } }
public async Task DoSentimentAnalysis(string message) { DocumentSentiment documentSentiment = await _client.AnalyzeSentimentAsync(message); Console.WriteLine($"Document sentiment: { documentSentiment.Sentiment }\n"); var stringInfo = new StringInfo(message); foreach (var sentence in documentSentiment.Sentences) { Console.WriteLine($"\tSentence [length {sentence.GraphemeLength}]"); Console.WriteLine($"\tText: \"{stringInfo.SubstringByTextElements(sentence.GraphemeOffset, sentence.GraphemeLength)}\""); Console.WriteLine($"\tSentence sentiment: {sentence.Sentiment}"); Console.WriteLine($"\tPositive score: {sentence.ConfidenceScores.Positive:0.00}"); Console.WriteLine($"\tNegative score: {sentence.ConfidenceScores.Negative:0.00}"); Console.WriteLine($"\tNeutral score: {sentence.ConfidenceScores.Neutral:0.00}\n"); } }
public void Sentiment(String text, StreamWriter writer) { if (text != null && text.Length > 0) { DocumentSentiment result = AzureTextAnalyticsService.AnalyzeSentiment(text); foreach (var sentence in result.Sentences) { writer.Write($"{sentence.Sentiment}, {sentence.ConfidenceScores.Positive:0.00}, {sentence.ConfidenceScores.Negative:0.00}, {sentence.ConfidenceScores.Neutral:0.00}"); List <string> keyWords = KeyWords(sentence.Text); foreach (string word in keyWords) { writer.Write($", \"{word}\""); } writer.WriteLine(""); } writer.Flush(); } }
public async Task <CognitiveServicesModel> AccessCognitiveServices(string input) { Azure.Response <DocumentSentiment> response = await _textAnalyticsClient.AnalyzeSentimentAsync(input); DocumentSentiment sentiment = response.Value; return(new CognitiveServicesModel { Sentiment = sentiment.Sentiment.ToString(), ConfidenceScores = new Dictionary <string, double> { [TextSentiment.Positive.ToString()] = sentiment.ConfidenceScores.Positive, [TextSentiment.Negative.ToString()] = sentiment.ConfidenceScores.Negative, [TextSentiment.Neutral.ToString()] = sentiment.ConfidenceScores.Neutral, } }); }
private double GetSentimentScore(DocumentSentiment sentiment) { switch (sentiment.Sentiment) { case TextSentiment.Positive: return(sentiment.ConfidenceScores.Positive); case TextSentiment.Neutral: return(sentiment.ConfidenceScores.Neutral); case TextSentiment.Negative: return(1 - sentiment.ConfidenceScores.Negative); case TextSentiment.Mixed: default: return(0.5); } }
static void SentimentAnalysisExample(TextAnalyticsClient client, string inputText) { DocumentSentiment documentSentiment = client.AnalyzeSentiment(inputText); Console.WriteLine($"Document sentiment: {documentSentiment.Sentiment}\n"); var si = new StringInfo(inputText); foreach (var sentence in documentSentiment.Sentences) { Console.WriteLine($"\tSentence [length {sentence.GraphemeLength}]"); Console.WriteLine($"\tText: \"{si.SubstringByTextElements(sentence.GraphemeOffset, sentence.GraphemeLength)}\""); Console.WriteLine($"\tSentence sentiment: {sentence.Sentiment}"); Console.WriteLine($"\tPositive score: {sentence.ConfidenceScores.Positive:0.00}"); Console.WriteLine($"\tNegative score: {sentence.ConfidenceScores.Negative:0.00}"); Console.WriteLine($"\tNeutral score: {sentence.ConfidenceScores.Neutral:0.00}\n"); } }
public static DocumentSentiment GetSentiment(string input) { var client = new TextAnalyticsClient(endpoint, credentials); DocumentSentiment documentSentiment = client.AnalyzeSentiment(input); Console.WriteLine($"Document sentiment: {documentSentiment.Sentiment}\n"); var si = new StringInfo(input); foreach (var sentence in documentSentiment.Sentences) { Console.WriteLine($"\tSentence [length {sentence.GraphemeLength}]"); Console.WriteLine($"\tText: \"{si.SubstringByTextElements(sentence.GraphemeOffset, sentence.GraphemeLength)}\""); Console.WriteLine($"\tSentence sentiment: {sentence.Sentiment}"); Console.WriteLine($"\tPositive score: {sentence.ConfidenceScores.Positive:0.00}"); Console.WriteLine($"\tNegative score: {sentence.ConfidenceScores.Negative:0.00}"); Console.WriteLine($"\tNeutral score: {sentence.ConfidenceScores.Neutral:0.00}\n"); } return documentSentiment; }
public async Task AnalyzeSentimentMixedSentenceSentiment() { using var stream = new MemoryStream(Encoding.UTF8.GetBytes(@" { ""documents"": [ { ""id"": ""1"", ""sentiment"": ""neutral"", ""confidenceScores"": { ""positive"": 0.1, ""neutral"": 0.88, ""negative"": 0.02 }, ""sentences"": [ { ""sentiment"": ""mixed"", ""confidenceScores"": { ""positive"": 0.1, ""neutral"": 0.88, ""negative"": 0.02 }, ""offset"": 0, ""length"": 18, ""text"": ""today is a hot day"" } ], ""warnings"": [] } ], ""errors"": [], ""modelVersion"": ""2020 -04-01"" }")); var mockResponse = new MockResponse(200); mockResponse.ContentStream = stream; var mockTransport = new MockTransport(new[] { mockResponse }); var client = CreateTestClient(mockTransport); DocumentSentiment response = await client.AnalyzeSentimentAsync("today is a hot day"); Assert.AreEqual(TextSentiment.Mixed, response.Sentences.FirstOrDefault().Sentiment); }
static void SentimentAnalysisExample(TextAnalyticsClient client) { string inputText = "I had the best day of my life. I wish you were there with me."; DocumentSentiment documentSentiment = client.AnalyzeSentiment(inputText); Console.WriteLine($"Document sentiment: {documentSentiment.Sentiment}\n"); var si = new StringInfo(inputText); foreach (var sentence in documentSentiment.Sentences) { Console.WriteLine($"\tSentence [offset {sentence.Offset}, length {sentence.Length}]"); Console.WriteLine($"\tText: \"{si.SubstringByTextElements(sentence.Offset, sentence.Length)}\""); Console.WriteLine($"\tSentence sentiment: {sentence.Sentiment}"); Console.WriteLine($"\tPositive score: {sentence.SentimentScores.Positive:0.00}"); Console.WriteLine($"\tNegative score: {sentence.SentimentScores.Negative:0.00}"); Console.WriteLine($"\tNeutral score: {sentence.SentimentScores.Neutral:0.00}\n"); } }
public async Task AnalyzeSentimentWithOpinionMiningNegated() { TextAnalyticsClient client = GetClient(); string document = "The bathrooms are not clean."; DocumentSentiment docSentiment = await client.AnalyzeSentimentAsync(document, options : new AnalyzeSentimentOptions() { AdditionalSentimentAnalyses = AdditionalSentimentAnalyses.OpinionMining }); CheckAnalyzeSentimentProperties(docSentiment, opinionMining: true); MinedOpinion minedOpinion = docSentiment.Sentences.FirstOrDefault().MinedOpinions.FirstOrDefault(); Assert.AreEqual("bathrooms", minedOpinion.Aspect.Text); Assert.AreEqual(TextSentiment.Negative, minedOpinion.Aspect.Sentiment); Assert.AreEqual("clean", minedOpinion.Opinions.FirstOrDefault().Text); Assert.AreEqual(TextSentiment.Negative, minedOpinion.Opinions.FirstOrDefault().Sentiment); Assert.IsTrue(minedOpinion.Opinions.FirstOrDefault().IsNegated); }
public async Task AnalyzeSentimentWithOpinionMiningNegated() { TextAnalyticsClient client = GetClient(); string document = "The bathrooms are not clean."; DocumentSentiment docSentiment = await client.AnalyzeSentimentAsync(document, options : new AnalyzeSentimentOptions() { IncludeOpinionMining = true }); CheckAnalyzeSentimentProperties(docSentiment, opinionMining: true); SentenceOpinion opinion = docSentiment.Sentences.FirstOrDefault().Opinions.FirstOrDefault(); Assert.AreEqual("bathrooms", opinion.Target.Text); Assert.AreEqual(TextSentiment.Negative, opinion.Target.Sentiment); Assert.AreEqual("clean", opinion.Assessments.FirstOrDefault().Text); Assert.AreEqual(TextSentiment.Negative, opinion.Assessments.FirstOrDefault().Sentiment); Assert.IsTrue(opinion.Assessments.FirstOrDefault().IsNegated); }
// build a overall email sentiment, some scoring could be done also static void SentimentAnalysis(TextAnalyticsClient client, string textSource) { DocumentSentiment documentSentiment = client.AnalyzeSentiment(textSource); Console.WriteLine($"\n\tDocument sentiment: {documentSentiment.Sentiment}\n"); _overallSentiment = documentSentiment.Sentiment.ToString(); foreach (var sentence in documentSentiment.Sentences) { double _positive = sentence.ConfidenceScores.Positive; double _negative = sentence.ConfidenceScores.Negative; double _neutral = sentence.ConfidenceScores.Neutral; double[] sentiments = new double[] { _positive, _negative, _neutral }; double maxValue = sentiments.Max(); double maxIndex = sentiments.ToList().IndexOf(maxValue); Console.Write($"\tResult: \"{maxIndex + " "+ maxValue}\" "); } Console.WriteLine(); }
public void AnalyzeSentiment() { string endpoint = TestEnvironment.Endpoint; string apiKey = TestEnvironment.ApiKey; #region Snippet:TextAnalyticsSample2CreateClient var client = new TextAnalyticsClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); #endregion #region Snippet:AnalyzeSentiment string document = "That was the best day of my life!"; DocumentSentiment docSentiment = client.AnalyzeSentiment(document); Console.WriteLine($"Sentiment was {docSentiment.Sentiment}, with confidence scores: "); Console.WriteLine($" Positive confidence score: {docSentiment.ConfidenceScores.Positive}."); Console.WriteLine($" Neutral confidence score: {docSentiment.ConfidenceScores.Neutral}."); Console.WriteLine($" Negative confidence score: {docSentiment.ConfidenceScores.Negative}."); #endregion }
/// <summary> /// Iterates through all the provided <see cref="CrawlAction"/> objects and uses /// Microsoft Azure Cognitive Services Text Analytics API to extract key phrases and sentiments for each action. /// </summary> public async Task FeaturizeActionsAsync(IEnumerable <CrawlAction> actions) { await Task.WhenAll(actions.Select(async a => { Metadata metadata = a.Metadata.ToObject <Metadata>(); string content = $"{metadata.Title ?? string.Empty} {metadata.Description ?? string.Empty}"; // Get key phrases from the article title and description Response <KeyPhraseCollection> keyPhrases = await textAnalyticsClient.ExtractKeyPhrasesAsync(content); // Create a dictionary of key phrases (with a constant values) since at this time we do not support list of strings features. var keyPhrasesWithConstValues = keyPhrases.Value.ToDictionary(x => x, x => 1); a.Features.Add(new { keyPhrases = keyPhrasesWithConstValues }); // Get sentiment score for the article DocumentSentiment sentiment = await textAnalyticsClient.AnalyzeSentimentAsync(content); a.Features.Add(new { sentiment.ConfidenceScores }); } )); }
public void AnalyzeSentiment() { string endpoint = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_ENDPOINT"); string apiKey = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_API_KEY"); #region Snippet:TextAnalyticsSample2CreateClient var client = new TextAnalyticsClient(new Uri(endpoint), new TextAnalyticsApiKeyCredential(apiKey)); #endregion #region Snippet:AnalyzeSentiment string input = "That was the best day of my life!"; DocumentSentiment docSentiment = client.AnalyzeSentiment(input); Console.WriteLine($"Sentiment was {docSentiment.Sentiment}, with scores: "); Console.WriteLine($" Positive score: {docSentiment.SentimentScores.Positive:0.00}."); Console.WriteLine($" Neutral score: {docSentiment.SentimentScores.Neutral:0.00}."); Console.WriteLine($" Negative score: {docSentiment.SentimentScores.Negative:0.00}."); #endregion }
public static void Run([ServiceBusTrigger("newproduct", "Sentiment", Connection = "ServiceBusConnectionString")] string message, [CosmosDB(databaseName: "ProductStore", collectionName: "Product", ConnectionStringSetting = "CosmosDb")] out dynamic result, ILogger log, ExecutionContext context) { var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); log.LogInformation($"C# ServiceBus topic trigger function processed message: {message}"); var product = JsonConvert.DeserializeObject <Product>(message); if (product != null && product.Reviews != null) { AzureKeyCredential credentials = new AzureKeyCredential(config["AzureKeyCrededntial"]); Uri endpoint = new Uri(config["SentimentEndpoint"]); var client = new TextAnalyticsClient(endpoint, credentials); var reviews = product.Reviews.ToList(); product.Reviews = new List <ProductReview>(); foreach (var review in reviews) { DocumentSentiment documentSentiment = client.AnalyzeSentiment(review.Title + "; " + review.Content); review.Type = documentSentiment.Sentiment.ToString(); } product.Reviews = reviews;; result = product; } else { result = null; } }
private static async Task SentimentAnalysisExampleAsync(TextAnalyticsClient client) { WriteLine("****** Sentiment analysis ******"); WriteLine(); var document = "I had the best day of my life. I wish you were there with me."; DocumentSentiment documentSentiment = await client.AnalyzeSentimentAsync(document); WriteLine($"Document: {document}"); WriteLine($"Document sentiment: {documentSentiment.Sentiment}\n"); foreach (var sentence in documentSentiment.Sentences) { WriteLine($"\tText: \"{sentence.Text}\""); WriteLine($"\tSentence sentiment: {sentence.Sentiment}"); WriteLine($"\tPositive score: {sentence.ConfidenceScores.Positive:0.00}"); WriteLine($"\tNegative score: {sentence.ConfidenceScores.Negative:0.00}"); WriteLine($"\tNeutral score: {sentence.ConfidenceScores.Neutral:0.00}\n"); } WriteLine(); }
public async Task AnalyzeSentimentTest() { TextAnalyticsClient client = GetClient(); string document = "That was the best day of my life!"; DocumentSentiment docSentiment = await client.AnalyzeSentimentAsync(document); Assert.AreEqual("Positive", docSentiment.Sentiment.ToString()); Assert.IsNotNull(docSentiment.ConfidenceScores.Positive); Assert.IsNotNull(docSentiment.ConfidenceScores.Neutral); Assert.IsNotNull(docSentiment.ConfidenceScores.Negative); foreach (var sentence in docSentiment.Sentences) { Assert.AreEqual("Positive", sentence.Sentiment.ToString()); Assert.IsNotNull(sentence.Text); Assert.AreEqual(document, sentence.Text); Assert.IsNotNull(sentence.ConfidenceScores.Positive); Assert.IsNotNull(sentence.ConfidenceScores.Neutral); Assert.IsNotNull(sentence.ConfidenceScores.Negative); } }
public float GetSentiment(string s) { DocumentSentiment documentSentiment = Client.AnalyzeSentiment(s); float score = 0; foreach (var sentence in documentSentiment.Sentences) { if (sentence.Sentiment == TextSentiment.Negative) { score--; } if (sentence.Sentiment == TextSentiment.Mixed) { score -= 0.5f; } if (sentence.Sentiment == TextSentiment.Positive) { score++; } } return(score / documentSentiment.Sentences.Count); }
public async Task AnalyzeSentimentBatchTest() { TextAnalyticsClient client = GetClient(); var inputs = new List <TextDocumentInput> { new TextDocumentInput("1", "Pike Place Market is my favorite Seattle attraction. We had so much fun there.") { Language = "en", }, new TextDocumentInput("2", "Esta comida no me gusta. Siempre que la como me enfermo.") { Language = "es", } }; AnalyzeSentimentResultCollection results = await client.AnalyzeSentimentBatchAsync(inputs); Assert.AreEqual("Positive", results[0].DocumentSentiment.Sentiment.ToString()); Assert.AreEqual("Negative", results[1].DocumentSentiment.Sentiment.ToString()); foreach (AnalyzeSentimentResult docs in results) { DocumentSentiment docSentiment = docs.DocumentSentiment; Assert.IsNotNull(docSentiment.ConfidenceScores.Positive); Assert.IsNotNull(docSentiment.ConfidenceScores.Neutral); Assert.IsNotNull(docSentiment.ConfidenceScores.Negative); foreach (var sentence in docSentiment.Sentences) { Assert.IsNotNull(sentence.ConfidenceScores.Positive); Assert.IsNotNull(sentence.ConfidenceScores.Neutral); Assert.IsNotNull(sentence.ConfidenceScores.Negative); Assert.IsNotNull(sentence.Offset); Assert.IsNotNull(sentence.Length); } } }
private async Task AnalyzeTextAsync() { try { if (!string.IsNullOrEmpty(this.speechRecognitionTextBox.Text)) { DocumentSentiment textAnalysisResult = await TextAnalyticsHelper.AnalyzeSentimentAsync(this.speechRecognitionTextBox.Text); this.sentimentControl.Sentiment = GetSentimentScore(textAnalysisResult); } else { this.sentimentControl.Sentiment = 0.5; } this.OnSpeechRecognitionAndSentimentProcessed(new SpeechRecognitionAndSentimentResult { SpeechRecognitionText = this.speechRecognitionTextBox.Text, TextAnalysisSentiment = this.sentimentControl.Sentiment }); } catch (Exception ex) { await Util.GenericApiCallExceptionHandler(ex, "Error during Text Analytics call."); } }
public void AnalyzeSentimentBatchConvenience() { string endpoint = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_ENDPOINT"); string apiKey = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_API_KEY"); // Instantiate a client that will be used to call the service. var client = new TextAnalyticsClient(new Uri(endpoint), new TextAnalyticsApiKeyCredential(apiKey)); var inputs = new List <string> { "That was the best day of my life!", "This food is very bad.", "I'm not sure how I feel about this product.", "Pike place market is my favorite Seattle attraction.", }; Debug.WriteLine($"Analyzing sentiment for inputs:"); foreach (string input in inputs) { Debug.WriteLine($" {input}"); } #region Snippet:TextAnalyticsSample2AnalyzeSentimentConvenience AnalyzeSentimentResultCollection results = client.AnalyzeSentimentBatch(inputs); #endregion Debug.WriteLine($"Predicted sentiments are:"); foreach (AnalyzeSentimentResult result in results) { DocumentSentiment docSentiment = result.DocumentSentiment; Debug.WriteLine($"Document sentiment is {docSentiment.Sentiment}, with confidence scores: "); Debug.WriteLine($" Positive confidence score: {docSentiment.ConfidenceScores.Positive}."); Debug.WriteLine($" Neutral confidence score: {docSentiment.ConfidenceScores.Neutral}."); Debug.WriteLine($" Negative confidence score: {docSentiment.ConfidenceScores.Negative}."); } }
public async Task AnalyzeSentimentAssessmentInOtherSentence() { using var stream = new MemoryStream(Encoding.UTF8.GetBytes(@" { ""documents"": [ { ""id"": ""1"", ""sentiment"": ""positive"", ""confidenceScores"": { ""positive"": 0.5, ""neutral"": 0.0, ""negative"": 0.5 }, ""sentences"": [ { ""sentiment"": ""positive"", ""confidenceScores"": { ""positive"": 1.0, ""neutral"": 0.0, ""negative"": 0.0 }, ""offset"": 0, ""length"": 30, ""text"": ""The park was clean."", ""targets"": [ { ""sentiment"": ""positive"", ""confidenceScores"": { ""positive"": 1.0, ""negative"": 0.0 }, ""offset"": 4, ""length"": 4, ""text"": ""park"", ""relations"": [ { ""relationType"": ""assessment"", ""ref"": ""#/documents/0/sentences/0/assessments/0"" } ] } ], ""assessments"": [ { ""sentiment"": ""positive"", ""confidenceScores"": { ""positive"": 1.0, ""negative"": 0.0 }, ""offset"": 13, ""length"": 5, ""text"": ""clean"", ""isNegated"": false } ] }, { ""sentiment"": ""positive"", ""confidenceScores"": { ""positive"": 0.0, ""neutral"": 0.0, ""negative"": 1.0 }, ""offset"": 31, ""length"": 23, ""text"": ""It was clean."", ""targets"": [ { ""sentiment"": ""positive"", ""confidenceScores"": { ""positive"": 0.0, ""negative"": 1.0 }, ""offset"": 35, ""length"": 4, ""text"": ""park"", ""relations"": [ { ""relationType"": ""assessment"", ""ref"": ""#/documents/0/sentences/0/assessments/0"" } ] } ], ""assessments"": [] } ], ""warnings"": [] } ], ""errors"": [], ""modelVersion"": ""2020-04-01"" }")); var mockResponse = new MockResponse(200); mockResponse.ContentStream = stream; var mockTransport = new MockTransport(new[] { mockResponse }); var client = CreateTestClient(mockTransport); DocumentSentiment response = await client.AnalyzeSentimentAsync("The park was clean. It was clean."); SentenceOpinion opinionS1 = response.Sentences.ElementAt(0).Opinions.FirstOrDefault(); Assert.AreEqual("park", opinionS1.Target.Text); Assert.AreEqual(TextSentiment.Positive, opinionS1.Target.Sentiment); Assert.AreEqual("clean", opinionS1.Assessments.FirstOrDefault().Text); SentenceOpinion opinionS2 = response.Sentences.ElementAt(1).Opinions.FirstOrDefault(); Assert.AreEqual("park", opinionS2.Target.Text); Assert.AreEqual(TextSentiment.Positive, opinionS2.Target.Sentiment); Assert.AreEqual("clean", opinionS2.Assessments.FirstOrDefault().Text); }
static DocumentSentiment ClassicSentimentAnalysis(TextAnalyticsClient client, string inputText) { DocumentSentiment documentSentiment = client.AnalyzeSentiment(inputText); return(documentSentiment); }
public static string GetFeedbackSentiment(string feedback) { DocumentSentiment documentSentiment = client.AnalyzeSentiment(feedback); return(documentSentiment.Sentiment.ToString()); }
public TextAnalysisSentiment SentimentAnalysisExampleAsync(TextAnalyticsClient client, string language, string text) { //string inputText = "I had the best day of my life. I wish you were there with me. I keep forgetting things. I am frustrated."; string document = text.Replace("\r", "").Replace("\n", ""); DocumentSentiment documentSentiment = client.AnalyzeSentiment(document, language); Console.WriteLine($"Document sentiment: {documentSentiment.Sentiment}\n"); double sumPostitive = 0; double sumNegative = 0; double sumNeutral = 0; int countPositive = 0, countNegative = 0, countNeutral = 0; double negSumScore = 0, posSumScore = 0, neuSumScore = 0; var noOfSentences = documentSentiment.Sentences.Count; foreach (var sentence in documentSentiment.Sentences) { Console.WriteLine($"\tText: \"{sentence.Text}\""); Console.WriteLine($"\tSentence sentiment: {sentence.Sentiment}"); Console.WriteLine($"\tPositive score: {sentence.ConfidenceScores.Positive:0.00}"); Console.WriteLine($"\tNegative score: {sentence.ConfidenceScores.Negative:0.00}"); Console.WriteLine($"\tNeutral score: {sentence.ConfidenceScores.Neutral:0.00}\n"); switch (sentence.Sentiment.ToString()) { case "Negative": countNegative += 1; negSumScore += sentence.ConfidenceScores.Negative; break; case "Positive": countPositive += 1; posSumScore += sentence.ConfidenceScores.Positive; break; case "Neutral": countNeutral += 1; neuSumScore += sentence.ConfidenceScores.Neutral; break; } sumPostitive += sentence.ConfidenceScores.Positive; sumNegative += sentence.ConfidenceScores.Negative; sumNeutral += sentence.ConfidenceScores.Neutral; } double avePos = sumPostitive / noOfSentences; Console.WriteLine($"\tAverage score Positive: {avePos}"); double aveNeg = sumNegative / noOfSentences; Console.WriteLine($"\tAverage score Negative: {aveNeg}"); double aveNeu = sumNeutral / noOfSentences; Console.WriteLine($"\tAverage score Neutral: {aveNeu}"); double v = posSumScore / countPositive; Console.WriteLine($"\tAverage score: {v}"); double v1 = negSumScore / countNegative; Console.WriteLine($"\tAverage score: {v1}"); double v2 = neuSumScore / countNeutral; Console.WriteLine($"\tAverage score: {v2}"); TextAnalysisSentiment output = new TextAnalysisSentiment(); output.DocumentSentiment = documentSentiment.Sentiment.ToString(); output.Negative = aveNeg; output.Positive = avePos; output.Neutral = aveNeu; return(output); }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { try { var logEntry = new LogEntry(); var options = new JsonSerializerOptions { WriteIndented = false }; options.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)); //var configurationBuilder = new ConfigurationBuilder().AddUserSecrets(); // var configuration = configurationBuilder.Build(); var hashTags = _configuration.GetValue <string>("HashTags").Split(",").ToList(); var users = _configuration.GetValue <string>("Users").Split(",").ToList(); var blockedWords = _configuration.GetValue <string>("BlockedWords").Split(",").ToList(); var blockedUsers = _configuration.GetValue <string>("BlockedUsers").Split(",").ToList(); var userCredentials = new TwitterCredentials( _configuration.GetValue <string>("Twitter:ApiKey"), _configuration.GetValue <string>("Twitter:ApiSecret"), _configuration.GetValue <string>("Twitter:AccessToken"), _configuration.GetValue <string>("Twitter:AccessSecret") ); var textAnalyticsClient = new TextAnalyticsClient( new Uri(_configuration.GetValue <string>("Azure:TextAnalyticsURI")), new AzureKeyCredential(_configuration.GetValue <string>("Azure:TextAnalyticsKey")) ); var userClient = new TwitterClient(userCredentials); var stream = userClient.Streams.CreateFilteredStream(); stream.AddLanguageFilter(LanguageFilter.English); stream.FilterLevel = StreamFilterLevel.Low; foreach (var hashTag in hashTags) { stream.AddTrack(hashTag); } foreach (var user in users) { var twitterUser = await userClient.Users.GetUserAsync(user); stream.AddFollow(twitterUser); } stream.MatchingTweetReceived += (sender, eventReceived) => { ITweet tweet = eventReceived.Tweet; string textToAnalyze = tweet.FullText ?? tweet.Text; foreach (var blockedWord in blockedWords) { if (textToAnalyze.ToLower().Contains(blockedWord.ToLower())) { return; } } if (blockedUsers.Contains(tweet.CreatedBy.ScreenName)) { return; } foreach (var blockedWord in blockedWords) { if (textToAnalyze.ToLower().Contains(blockedWord.ToLower())) { return; } } if (eventReceived.Tweet.IsRetweet) { return; } if (eventReceived.Tweet.CreatedBy.CreatedAt > DateTime.Now.AddMonths(-1)) { return; } if (eventReceived.Tweet.CreatedBy.FollowersCount < 100) { return; } if (eventReceived.MatchingFollowers.Length > 0 && eventReceived.MatchingFollowers.Contains(tweet.CreatedBy.Id) == false) { return; } //_logger.LogInformation("Matching tweet: {time}, {text}", DateTimeOffset.Now, textToAnalyze.Replace(Environment.NewLine,"")); var connStr = _configuration.GetConnectionString("DefaultConnection"); var optionsBuilder = new DbContextOptionsBuilder <ApplicationDbContext>(); optionsBuilder.UseSqlServer(connStr); hashTags = _configuration.GetValue <string>("HashTags").Split(",").ToList(); foreach (var hashTag in hashTags) { textToAnalyze = textToAnalyze.Replace(hashTag, ""); } DocumentSentiment documentSentiment = null; TweetSentiment tweetSentiment = new TweetSentiment(); List <SentimentDetail> sentimentDetails = new List <SentimentDetail>(); List <TweetEntity> listEntities = new List <TweetEntity>(); List <TweetKeyPhrase> listKeyPhrases = new List <TweetKeyPhrase>(); //_logger.LogInformation("Analyzing sentiment: {time}", DateTimeOffset.Now); documentSentiment = textAnalyticsClient.AnalyzeSentiment(textToAnalyze); logEntry.Sentiment = JsonSerializer.Serialize(documentSentiment, options); //_logger.LogInformation("Sentiment: {time}", documentSentiment.Sentiment); tweetSentiment = new TweetSentiment { IsPositive = (eventReceived.MatchingFollowers.Contains(tweet.CreatedBy.Id) && documentSentiment.Sentiment != TextSentiment.Negative) || (documentSentiment.Sentiment == TextSentiment.Positive) && (!documentSentiment.Sentences.Where(s => s.Sentiment == TextSentiment.Mixed || s.Sentiment == TextSentiment.Negative).Any()), TweetContent = textToAnalyze, TweetedBy = 0, TweetedOn = DateTime.Now, TweetID = tweet.Id }; foreach (var sentence in documentSentiment.Sentences) { var sentimentDetail = new SentimentDetail() { Sentence = sentence.Text, Positive = sentence.ConfidenceScores.Positive, Negative = sentence.ConfidenceScores.Negative, Neutral = sentence.ConfidenceScores.Neutral, TweetID = tweet.Id, Sentiment = sentence.Sentiment.ToString() }; sentimentDetails.Add(sentimentDetail); } logEntry.Details = JsonSerializer.Serialize(sentimentDetails, options); var responseEntities = textAnalyticsClient.RecognizeEntities(textToAnalyze); foreach (var entity in responseEntities.Value) { var tweetEntity = new TweetEntity { EntityText = entity.Text, Category = entity.Category.ToString(), SubCategory = entity.SubCategory, Confidence = entity.ConfidenceScore, TweetID = tweet.Id }; listEntities.Add(tweetEntity); } logEntry.Entities = JsonSerializer.Serialize(listEntities); var responseKeyPhrases = textAnalyticsClient.ExtractKeyPhrases(textToAnalyze); foreach (string keyphrase in responseKeyPhrases.Value) { var tweetKeyPhrase = new TweetKeyPhrase { TweetID = tweet.Id, KeyPhrase = keyphrase }; listKeyPhrases.Add(tweetKeyPhrase); } logEntry.Phrases = JsonSerializer.Serialize(listKeyPhrases, options); using (ApplicationDbContext db = new ApplicationDbContext(optionsBuilder.Options)) { //_logger.LogWarning("Saving tweet: {time}", DateTimeOffset.Now); db.TweetSentiments.Add(tweetSentiment); db.SentimentDetails.AddRange(sentimentDetails); db.TweetEntities.AddRange(listEntities); db.TweetKeyPhrases.AddRange(listKeyPhrases); db.SaveChanges(); } if (tweetSentiment.IsPositive) { eventReceived.Tweet.FavoriteAsync(); eventReceived.Tweet.PublishRetweetAsync(); } _logger.LogInformation(@$ "{logEntry.Sentiment} {logEntry.Details} {logEntry.Entities} {logEntry.Phrases}"); }; stream.StreamStopped += (sender, eventReceived) => { stream.StartMatchingAnyConditionAsync(); }; _ = stream.StartMatchingAnyConditionAsync(); while (!stoppingToken.IsCancellationRequested) { await Task.Delay(1000, stoppingToken); } } catch (OperationCanceledException) { _logger.LogWarning("Worker process was cancelled"); Environment.ExitCode = 0; } catch (Exception ex) { _logger.LogError(ex, "Worker process caught exception"); Environment.ExitCode = 0; } finally { // No matter what happens (success or exception), we need to indicate that it's time to stop the application. applicationLifetime.StopApplication(); } }
protected internal void SetResults(DocumentSentiment value) { SetConfidenceScores(value.ConfidenceScores); Sentiment = value.Sentiment; }