Skip to content

Commit c70a7bb

Browse files
committed
Added the !sentiment command as suggested by Hugo in #312
1 parent d3899ba commit c70a7bb

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using Fritz.StreamLib.Core;
6+
7+
namespace Fritz.Chatbot.Commands
8+
{
9+
public class SentimentCommand : IBasicCommand
10+
{
11+
public string Trigger => "sentiment";
12+
public string Description => "Information about the sentiment analysis features of the chat room";
13+
public TimeSpan? Cooldown => TimeSpan.FromSeconds(30);
14+
15+
public async Task Execute(IChatService chatService, string userName, ReadOnlyMemory<char> rhs)
16+
{
17+
18+
var sb = new StringBuilder();
19+
sb.Append($"The sentiment over the last minute is {SentimentSink.Sentiment1Minute.ToString("00.0%")} percent positive ");
20+
sb.Append((SentimentSink.Sentiment1Minute < SentimentSink.Sentiment5Minute) ? "and trending upward (up arrow) " : "and trending downward (down arrow) ");
21+
sb.Append("compared to the last five minutes ");
22+
sb.Append($"The most recent messages were {((SentimentSink.SentimentInstant >=0.5) ? "positive" : "negative")} (smiley face)");
23+
24+
await chatService.SendMessageAsync(sb.ToString());
25+
26+
}
27+
}
28+
}

Fritz.Chatbot/Commands/SentimentSink.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ public class SentimentSink
1010

1111
public static Queue<string> RecentChatMessages { get; } = new Queue<string>();
1212

13+
public static double SentimentInstant { get; set; }
14+
15+
public static double Sentiment1Minute { get; set; }
16+
17+
public static double Sentiment5Minute { get; set; }
18+
1319
}
1420

1521
}

Fritz.StreamTools/Services/SentimentService.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,14 @@ public async Task Run()
8585

8686
var now = DateTime.Now;
8787
_Observations.Add(now, (results.Documents.Count, avgScore));
88-
_followerClient.UpdateSentiment(avgScore,
89-
CalculateSentimentOverLastMinutes(1),
90-
CalculateSentimentOverLastMinutes(5),
88+
89+
SentimentSink.SentimentInstant = avgScore;
90+
SentimentSink.Sentiment1Minute = CalculateSentimentOverLastMinutes(1);
91+
SentimentSink.Sentiment5Minute = CalculateSentimentOverLastMinutes(5);
92+
93+
_followerClient.UpdateSentiment(SentimentSink.SentimentInstant,
94+
SentimentSink.Sentiment1Minute,
95+
SentimentSink.Sentiment5Minute,
9196
CalculateSentimentOverLastMinutes());
9297

9398
}

0 commit comments

Comments
 (0)