public SentimentScores ElaborateSentence(string sentence)
        {
            var client = new TextAnalyticsClient(endpoint, credentials);

            DocumentSentiment documentSentiment = client.AnalyzeSentiment(sentence, "it");

            Console.WriteLine($"Sentence sentiment: {documentSentiment.Sentiment}\n");

            var score = new SentimentScores();

            score.SetSentiment((SentimentScores.TextSentiment)documentSentiment.Sentiment);
            score.Positive = documentSentiment.ConfidenceScores.Positive;
            score.Negative = documentSentiment.ConfidenceScores.Negative;
            score.Neutral  = documentSentiment.ConfidenceScores.Neutral;

            return(score);
        }
Exemplo n.º 2
0
        private async Task ProcessSentiment(OnMessageReceivedArgs e)
        {
            // to remove noise
            if (e.ChatMessage.Message.Length < 10)
            {
                return;
            }

            // limit number of stored SentimentScores to react quickly to changes
            if (sentimentScores.Count > 10)
            {
                sentimentScores.RemoveAt(0);
            }

            var currentScore = _sentimentAnalysisService.ElaborateSentence(e.ChatMessage.Message);

            await connection.SendAsync("SendTask", "SendSentiment", currentScore.GetSentiment().ToString().ToLower());

            var currentUnrankedSentiment = new Dictionary <SentimentScores.TextSentiment, double>
            {
                { SentimentScores.TextSentiment.Positive, currentScore.Positive },
                { SentimentScores.TextSentiment.Neutral, currentScore.Neutral },
                { SentimentScores.TextSentiment.Negative, currentScore.Negative },
            };

            var currentRankedSentiment = currentUnrankedSentiment.OrderBy(item => item.Value);

            _logger.LogInformation(string.Concat(
                                       "currentScore:",
                                       Environment.NewLine,
                                       string.Join(
                                           Environment.NewLine,
                                           currentRankedSentiment.Select(a => $"{a.Key}: {a.Value}")
                                           )
                                       )
                                   );

            var processedSentiment = new SentimentScores();

            switch (currentRankedSentiment.Last().Key)
            {
            case SentimentScores.TextSentiment.Positive:
                processedSentiment.Positive = currentRankedSentiment.Last().Value;
                break;

            case SentimentScores.TextSentiment.Neutral:
                processedSentiment.Neutral = currentRankedSentiment.Last().Value;
                break;

            case SentimentScores.TextSentiment.Negative:
                processedSentiment.Negative = currentRankedSentiment.Last().Value;
                break;
            }

            sentimentScores.Add(processedSentiment);

            var chatUnrankedSentiment = new Dictionary <SentimentScores.TextSentiment, double>
            {
                { SentimentScores.TextSentiment.Positive, sentimentScores.Average(c => c.Positive) },
                { SentimentScores.TextSentiment.Neutral, sentimentScores.Average(c => c.Neutral) },
                { SentimentScores.TextSentiment.Negative, sentimentScores.Average(c => c.Negative) },
            };

            var chatRankedSentiment = chatUnrankedSentiment.OrderBy(item => item.Value);

            // set current chat sentiment with values
            currentChatSentiment.SetSentiment(chatRankedSentiment.Last().Key);
            currentChatSentiment.Positive = chatUnrankedSentiment[SentimentScores.TextSentiment.Positive];
            currentChatSentiment.Neutral  = chatUnrankedSentiment[SentimentScores.TextSentiment.Neutral];
            currentChatSentiment.Negative = chatUnrankedSentiment[SentimentScores.TextSentiment.Negative];

            _logger.LogInformation(string.Concat(
                                       "Chat sentiment:",
                                       Environment.NewLine,
                                       string.Join(
                                           Environment.NewLine,
                                           chatRankedSentiment.Select(a => $"{a.Key}: {a.Value}")
                                           )
                                       )
                                   );

            // interesting can be used with a gauge or a vertical meter that can go from -1 to 1
            double absoluteSentiment = (currentChatSentiment.Positive - currentChatSentiment.Neutral) -
                                       (currentChatSentiment.Negative - currentChatSentiment.Neutral);

            _logger.LogInformation(string.Concat("Absolute sentiment:", absoluteSentiment));

            await connection.SendAsync("SendTask", "SendGaugeSentiment", absoluteSentiment);
        }