예제 #1
0
        public async Task CheckPendingTweets()
        {
            var pendingTweets = await _twitterRepository.GetPendingTweetsToAnalyzeAsync();

            // Only call to Cognitive API if pending tweets are more than 50
            // This is to reduce the number of calls to the API
            if (pendingTweets.Count > 5)
            {
                var requestToAnalyze = pendingTweets
                                       .Select(i => i.Text)
                                       .ToList();

                var analysisResults = await _textAnalyzerService.AnalyzeAsync(requestToAnalyze);

                foreach (var analysisResult in analysisResults)
                {
                    var currentTweet = pendingTweets
                                       .Where(i => i.Text == analysisResult.OriginalText)
                                       .First();

                    currentTweet.Sentiment  = analysisResult.Sentiment;
                    currentTweet.KeyPhrases = string.Join(",", analysisResult.KeyPhrases);
                }

                if (analysisResults.Any())
                {
                    await _twitterRepository.UpdateAsync(pendingTweets);
                }
            }
        }