| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Globalization; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | | using ApplicationCore.Model; |
| | 7 | | using Microsoft.Azure.CognitiveServices.Language.TextAnalytics; |
| | 8 | | using Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models; |
| | 9 | |
|
| | 10 | | namespace ApplicationCore.Analytics |
| | 11 | | { |
| | 12 | | public class TextAnalyzer : ITextAnalyzer |
| | 13 | | { |
| | 14 | | private const int _splitTextLength = 500; |
| | 15 | |
|
| | 16 | | private readonly TextAnalyticsConfig _config; |
| | 17 | | private readonly ITextAnalyticsClientFactory _textAnalyticsClientFactory; |
| | 18 | |
|
| 5 | 19 | | public bool CanAnalyze => !string.IsNullOrEmpty(_config.SubscriptionKey); |
| | 20 | |
|
| 10 | 21 | | public TextAnalyzer(TextAnalyticsConfig config, ITextAnalyticsClientFactory textAnalyticsClientFactory) |
| 10 | 22 | | { |
| 10 | 23 | | _config = config ?? throw new ArgumentNullException(nameof(config)); |
| 9 | 24 | | _textAnalyticsClientFactory = textAnalyticsClientFactory |
| 9 | 25 | | ?? throw new ArgumentNullException(nameof(textAnalyticsClientFactory)); |
| 8 | 26 | | } |
| | 27 | |
|
| | 28 | | public async Task<CommentResult> AnalyzeAsync(Comment comment) |
| 4 | 29 | | { |
| 4 | 30 | | if (comment is null) |
| 1 | 31 | | { |
| 1 | 32 | | throw new ArgumentNullException(nameof(comment)); |
| | 33 | | } |
| 3 | 34 | | if (!CanAnalyze) |
| 1 | 35 | | { |
| 1 | 36 | | return new CommentResult(comment); |
| | 37 | | } |
| | 38 | |
|
| | 39 | | Comment analyzedComment; |
| | 40 | |
|
| 2 | 41 | | using (var client = _textAnalyticsClientFactory.CreateClient(_config.SubscriptionKey, _config.Region)) |
| 2 | 42 | | { |
| 2 | 43 | | var result = await client.SentimentBatchAsync( |
| 2 | 44 | | new MultiLanguageBatchInput(SplitFiveHundredChars(comment.Message).ToList())) |
| 2 | 45 | | .ConfigureAwait(false); |
| | 46 | |
|
| 2 | 47 | | var score = result.Documents?.Any() == true ? result.Documents[0].Score : 0; |
| 2 | 48 | | analyzedComment = comment.WithScore($"{score:0.00}"); |
| 2 | 49 | | } |
| | 50 | |
|
| 2 | 51 | | return new CommentResult(analyzedComment); |
| 3 | 52 | | } |
| | 53 | |
|
| | 54 | | private IEnumerable<MultiLanguageInput> SplitFiveHundredChars(string input) |
| 2 | 55 | | { |
| 2 | 56 | | var inputs = new List<MultiLanguageInput>(); |
| | 57 | |
|
| 2 | 58 | | if (string.IsNullOrEmpty(input)) |
| 1 | 59 | | { |
| 1 | 60 | | return inputs; |
| | 61 | | } |
| | 62 | |
|
| 1 | 63 | | var id = 0; |
| 6 | 64 | | for (var i = 0; i < input.Length; i += _splitTextLength) |
| 2 | 65 | | { |
| 2 | 66 | | var multiLanguageInput = new MultiLanguageInput |
| 2 | 67 | | { |
| 2 | 68 | | Id = id.ToString(CultureInfo.InvariantCulture), |
| 2 | 69 | | Language = _config.Language |
| 2 | 70 | | }; |
| | 71 | |
|
| 2 | 72 | | if((i + _splitTextLength) < input.Length) |
| 1 | 73 | | { |
| 1 | 74 | | multiLanguageInput.Text = input.Substring(i, Math.Min(500, input.Length - i)); |
| 1 | 75 | | id++; |
| 1 | 76 | | } |
| | 77 | | else |
| 1 | 78 | | { |
| 1 | 79 | | multiLanguageInput.Text = input; |
| 1 | 80 | | } |
| | 81 | |
|
| 2 | 82 | | inputs.Add(multiLanguageInput); |
| 2 | 83 | | } |
| | 84 | |
|
| 1 | 85 | | return inputs; |
| 2 | 86 | | } |
| | 87 | | } |
| | 88 | | } |