< Summary

Class:ApplicationCore.Analytics.TextAnalyzer
Assembly:ApplicationCore
File(s):C:\Users\Teknikaali\Source\Repos\jekyll-blog-comments\ApplicationCore\Analytics\TextAnalyzer.cs
Covered lines:49
Uncovered lines:0
Coverable lines:49
Total lines:88
Line coverage:100% (49 of 49)
Covered branches:17
Total branches:20
Branch coverage:85% (17 of 20)

Coverage History

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
.ctor(...)40100%100%
AnalyzeAsync()100100%70%
SplitFiveHundredChars(...)60100%100%

File(s)

C:\Users\Teknikaali\Source\Repos\jekyll-blog-comments\ApplicationCore\Analytics\TextAnalyzer.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Globalization;
 4using System.Linq;
 5using System.Threading.Tasks;
 6using ApplicationCore.Model;
 7using Microsoft.Azure.CognitiveServices.Language.TextAnalytics;
 8using Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models;
 9
 10namespace 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
 519        public bool CanAnalyze => !string.IsNullOrEmpty(_config.SubscriptionKey);
 20
 1021        public TextAnalyzer(TextAnalyticsConfig config, ITextAnalyticsClientFactory textAnalyticsClientFactory)
 1022        {
 1023            _config = config ?? throw new ArgumentNullException(nameof(config));
 924            _textAnalyticsClientFactory = textAnalyticsClientFactory
 925                ?? throw new ArgumentNullException(nameof(textAnalyticsClientFactory));
 826        }
 27
 28        public async Task<CommentResult> AnalyzeAsync(Comment comment)
 429        {
 430            if (comment is null)
 131            {
 132                throw new ArgumentNullException(nameof(comment));
 33            }
 334            if (!CanAnalyze)
 135            {
 136                return new CommentResult(comment);
 37            }
 38
 39            Comment analyzedComment;
 40
 241            using (var client = _textAnalyticsClientFactory.CreateClient(_config.SubscriptionKey, _config.Region))
 242            {
 243                var result = await client.SentimentBatchAsync(
 244                    new MultiLanguageBatchInput(SplitFiveHundredChars(comment.Message).ToList()))
 245                    .ConfigureAwait(false);
 46
 247                var score = result.Documents?.Any() == true ? result.Documents[0].Score : 0;
 248                analyzedComment = comment.WithScore($"{score:0.00}");
 249            }
 50
 251            return new CommentResult(analyzedComment);
 352        }
 53
 54        private IEnumerable<MultiLanguageInput> SplitFiveHundredChars(string input)
 255        {
 256            var inputs = new List<MultiLanguageInput>();
 57
 258            if (string.IsNullOrEmpty(input))
 159            {
 160                return inputs;
 61            }
 62
 163            var id = 0;
 664            for (var i = 0; i < input.Length; i += _splitTextLength)
 265            {
 266                var multiLanguageInput = new MultiLanguageInput
 267                {
 268                    Id = id.ToString(CultureInfo.InvariantCulture),
 269                    Language = _config.Language
 270                };
 71
 272                if((i + _splitTextLength) < input.Length)
 173                {
 174                    multiLanguageInput.Text = input.Substring(i, Math.Min(500, input.Length - i));
 175                    id++;
 176                }
 77                else
 178                {
 179                    multiLanguageInput.Text = input;
 180                }
 81
 282                inputs.Add(multiLanguageInput);
 283            }
 84
 185            return inputs;
 286        }
 87    }
 88}