Пример #1
0
        public SentimentSeries GetSentimentByKeyword(string keyword, TimeLibrary.TimeInterval groupingInterval, DateTime startDate, DateTime endDate)
        {
            var tweets = _TweetRepo.GetTweetsByKeyword(keyword, startDate, endDate);

            var sentiment = _SentimentAnalyzer.GetSentiment(tweets, groupingInterval);

            var sentimentSeries = new SentimentSeries()
            {
                Keywords  = new[] { keyword },
                Sentiment = sentiment,
            };

            return(sentimentSeries);
        }
Пример #2
0
        public SentimentSeries GetSentimentByConfigId(long configId, TimeLibrary.TimeInterval groupingInterval, DateTime startDate, DateTime endDate)
        {
            var tweets = _TweetRepo.GetTweetsByConfigId(configId, startDate, endDate);

            var sentiment = _SentimentAnalyzer.GetSentiment(tweets, groupingInterval);

            var config = _ConfigRepository.GetConfigurationById(configId);

            var sentimentSeries = new SentimentSeries()
            {
                Keywords  = new[] { config.Text },
                Sentiment = sentiment,
            };

            return(sentimentSeries);
        }
Пример #3
0
        /// <summary>
        /// Gets the sentiment for a set of tweets. Uses the Naive Bayes method.
        /// </summary>
        /// <param name="tweets"></param>
        /// <returns></returns>
        public Dictionary <DateTime, SentimentInfo> GetSentiment(IEnumerable <Tweet> tweets, TimeLibrary.TimeInterval groupingInterval)
        {
            var sentimentScores = new Dictionary <DateTime, SentimentInfo>();

            //// This will group the tweets by hour
            //var tweetsGroupedByHour = tweets
            //    .GroupBy(t => new DateTime(t.CreatedAt.Year, t.CreatedAt.Month, t.CreatedAt.Day, t.CreatedAt.Hour, 0, 0));

            // This will group the tweets by month
            var tweetsGroupedByHour = tweets
                                      .GroupBy(t => new DateTime(t.CreatedAt.Year, t.CreatedAt.Month, 1, 0, 0, 0));

            // foreach group of tweets
            foreach (var tweetGroup in tweetsGroupedByHour)
            {
                var numberOfTweets = tweetGroup.Count();

                // calculate the sum of sentiment of each tweet in the group
                var sentiment = tweetGroup.Sum(t => CalculateSentiment(t)) / numberOfTweets;

                // add the datetime and the sentiment info to the sentiment dictionary
                sentimentScores
                .Add(
                    tweetGroup.Key,
                    new SentimentInfo()
                {
                    Sentiment = sentiment,
                    Weight    = numberOfTweets
                });
            }

            return(sentimentScores);
        }
Пример #4
0
        public Dictionary <DateTime, SentimentInfo> GetSentiment(IEnumerable <Tweet> tweets, TimeLibrary.TimeInterval groupingInterval)
        {
            // If there are no tweets, return empty Dictionary
            if (tweets.Any() == false)
            {
                return(new Dictionary <DateTime, SentimentInfo>());
            }

            var sentimentScores = new Dictionary <DateTime, SentimentInfo>();
            //List<string> groupTweets = new List<string>();
            //List<bool> groupSentiment = new List<bool>();
            //// This will group the tweets by hour
            //var tweetsGroupedByHour = tweets
            //    .GroupBy(t => new DateTime(t.CreatedAt.Year, t.CreatedAt.Month, t.CreatedAt.Day, t.CreatedAt.Hour, 0, 0));

            var scores = this.runPrediction(tweets.Select(t => t.Text).ToList());

            var tweetsWithScore = tweets.Zip(scores, (t, s) => new { Score = s, Tweet = t });


            // This will group the tweets by month
            var tweetsGrouped = tweetsWithScore.GroupByTime(t => t.Tweet.CreatedAt, groupingInterval);

            //    .GroupBy(t => new DateTime(t.Tweet.CreatedAt.Year, t.Tweet.CreatedAt.Month, 1, 0, 0, 0));


            // foreach group of tweets
            foreach (var tweetGroup in tweetsGrouped)
            {
                var numberOfTweets = tweetGroup.Count();

                //// calculate the sum of sentiment of each tweet in the group
                var sentiment = 0.00;

                var groupSentiment = tweetGroup.Select(g => g.Score);

                foreach (var prediction in groupSentiment)
                {
                    //prediction being set to true means that a positive prediction was made
                    if (prediction == true)
                    {
                        sentiment += 1;
                    }
                }

                sentiment /= numberOfTweets;

                // add the datetime and the sentiment info to the sentiment dictionary
                sentimentScores
                .Add(
                    tweetGroup.Key,
                    new SentimentInfo()
                {
                    Sentiment = sentiment,
                    Weight    = numberOfTweets
                });
            }

            return(sentimentScores);
        }
Пример #5
0
        public static IEnumerable <IGrouping <DateTime, TSource> > GroupByTime <TSource>(this IEnumerable <TSource> source,
                                                                                         Func <TSource, DateTime> selector, TimeLibrary.TimeInterval interval)
        {
            var firstTime = new DateTime(source.Min(e => selector(e).Ticks));

            //var t1 = firstTime.Floor(interval).Ticks;

            var groups = source.GroupBy(e =>
            {
                // group# = floor((t - t1) / interval)
                //int groupNumber = (int)((selector(e).Ticks - t1) / interval.Ticks);

                //DateTime dateGroup = new DateTime(t1 + (groupNumber * interval.Ticks));

                DateTime dateGroup = selector(e).Floor(interval);

                return(dateGroup);
            });

            return(groups);
        }