Exemplo n.º 1
0
        public async Task <TweetScore> GetAverageSentimentScore()
        {
            if (this.cancellationToken.IsCancellationRequested)
            {
                return(null);
            }

            var tweetScore      = new TweetScore();
            var scoreList       = new List <KeyValuePair <string, decimal> >();
            var scoreDictionary =
                await this.StateManager.GetOrAddAsync <IReliableDictionary <string, decimal> >("scoreDictionary");

            using (var tx = this.StateManager.CreateTransaction())
            {
                tweetScore.TweetCount = await scoreDictionary.GetCountAsync(tx);

                var enumerable = await scoreDictionary.CreateEnumerableAsync(tx);

                using (var e = enumerable.GetAsyncEnumerator())
                {
                    while (await e.MoveNextAsync(this.cancellationToken).ConfigureAwait(false))
                    {
                        scoreList.Add(e.Current);
                    }
                }

                tweetScore.TweetSentimentAverageScore = tweetScore.TweetCount == 0 ? 0 : scoreList.Average(x => x.Value);
            }

            return(tweetScore);
        }
Exemplo n.º 2
0
        public FilterJsonRead Process(long cnt, string line)
        {
            var bytes = _encoding.GetBytes(line);

            using (var sf = new MemoryStream(bytes))
            {
                try
                {
                    var row = (UniTwitterRow)_ser.ReadObject(sf);

                    if (!Ids.Contains(row.Doc.User.IdStr))
                    {
                        return(this);
                    }

                    var tm = DateTime.ParseExact(row.Doc.CreatedAt,
                                                 "ddd MMM dd HH:mm:ss +0000 yyyy", null, DateTimeStyles.None);

                    var res = analyzer.PolarityScores(row.Doc.Text);

                    var post = new TweetScore
                    {
                        Location   = row.Key[0],
                        PostId     = row.Id,
                        CreateTime = tm,
                        Text       = row.Doc.Text,
                        UserIdStr  = row.Doc.User.IdStr,
                        UserName   = row.Doc.User.Name,
                        RecordId   = cnt,
                        Source     = row.Doc.Source,
                        Negative   = res.Negative,
                        Neutral    = res.Neutral,
                        Positive   = res.Positive,
                        Compound   = res.Compound
                    };

                    if (row.Doc.User.TimeZone != null)
                    {
                        post.TimeZone = row.Doc.User.TimeZone;
                    }

                    if (row.Doc.Coordinates != null)
                    {
                        post.GeoEnabled = true;

                        if (row.Doc.Coordinates.Coord[0].HasValue)
                        {
                            post.Xloc = row.Doc.Coordinates.Coord[0].Value;
                        }

                        if (row.Doc.Coordinates.Coord[1].HasValue)
                        {
                            post.Yloc = row.Doc.Coordinates.Coord[1].Value;
                        }
                    }

                    if (row.Doc.Entities.Hashtags != null)
                    {
                        post.HashTags = row.Doc.Entities.Hashtags.Select(x => x.Text).ToList();
                    }


                    Records.Add(post);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Problem {ex.Message}");
                }
            }

            return(this);
        }