|
| 1 | +using Fritz.Chatbot.Commands; |
| 2 | +using Microsoft.Azure.CognitiveServices.Language.TextAnalytics; |
| 3 | +using Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models; |
| 4 | +using Microsoft.Extensions.Configuration; |
| 5 | +using Microsoft.Extensions.Hosting; |
| 6 | +using Microsoft.Rest; |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Linq; |
| 10 | +using System.Net.Http; |
| 11 | +using System.Threading; |
| 12 | +using System.Threading.Tasks; |
| 13 | + |
| 14 | +namespace Fritz.StreamTools.Services |
| 15 | +{ |
| 16 | + public class SentimentService : IHostedService |
| 17 | + { |
| 18 | + |
| 19 | + private readonly FollowerClient _followerClient; |
| 20 | + private bool _StopProcess = false; |
| 21 | + private TextAnalyticsClient _client; |
| 22 | + private static string _SubscriptionKey; |
| 23 | + |
| 24 | + private class ApiKeyServiceClientCredentials : ServiceClientCredentials |
| 25 | + { |
| 26 | + public override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
| 27 | + { |
| 28 | + request.Headers.Add("Ocp-Apim-Subscription-Key", _SubscriptionKey); |
| 29 | + return base.ProcessHttpRequestAsync(request, cancellationToken); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public SentimentService(FollowerClient followerClient, IConfiguration config) |
| 34 | + { |
| 35 | + |
| 36 | + _SubscriptionKey = config["FritzBot:SentimentAnalysisKey"].ToString(); |
| 37 | + _followerClient = followerClient; |
| 38 | + _client = new TextAnalyticsClient(new ApiKeyServiceClientCredentials()) |
| 39 | + { |
| 40 | + Endpoint = "https://centralus.api.cognitive.microsoft.com" |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + public Task StartAsync(CancellationToken cancellationToken) |
| 46 | + { |
| 47 | + Task.Run(async () => await Run()); |
| 48 | + return Task.CompletedTask; |
| 49 | + } |
| 50 | + |
| 51 | + public Task StopAsync(CancellationToken cancellationToken) |
| 52 | + { |
| 53 | + |
| 54 | + _StopProcess = true; |
| 55 | + return Task.CompletedTask; |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + public async Task Run() |
| 60 | + { |
| 61 | + |
| 62 | + while (!_StopProcess) |
| 63 | + { |
| 64 | + |
| 65 | + if (SentimentSink.RecentChatMessages.Any()) |
| 66 | + { |
| 67 | + |
| 68 | + var messageList = SentimentSink.RecentChatMessages.Select((value, index) => new MultiLanguageInput { Text = value, Id = index.ToString(), Language = "en" }).ToList(); |
| 69 | + SentimentSink.RecentChatMessages.Clear(); |
| 70 | + |
| 71 | + SentimentBatchResult results = await _client.SentimentAsync( |
| 72 | + new MultiLanguageBatchInput(messageList) |
| 73 | + ); |
| 74 | + |
| 75 | + var avgScore = results.Documents |
| 76 | + .Where(d => d.Score.HasValue) |
| 77 | + .Average(d => d.Score).Value; |
| 78 | + _followerClient.UpdateSentiment(avgScore); |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + await Task.Delay(100); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | +} |
0 commit comments