Example #1
0
 /// <summary> Get the time period spanned by tweets. </summary>
 ///
 /// <param name="tweets">
 ///   list of tweets with distinct ids, not modified by this method.
 /// </param>
 ///
 /// <returns>
 ///   a minimum-length time interval that contains the timestamp of
 ///   every tweet in the list. If the list is empty, return a timespan
 ///   of size zero.
 /// </returns>
 ///
 public static Timespan GetTimespan(List <Tweet> tweets)
 {
     if (tweets.Count == 0)
     {
         DateTime fakeStart = DateTime.Parse("2016-02-17T10:00:00Z");
         DateTime fakeEnd   = DateTime.Parse("2016-02-17T10:00:00Z");
         Timespan empty     = new Timespan(fakeStart, fakeEnd);
         return(empty);
     }
     else
     {
         DateTime current  = tweets[0].timestamp;
         DateTime latest   = tweets[0].timestamp;
         DateTime earliest = tweets[0].timestamp;
         foreach (Tweet tweet in tweets)
         {
             current = tweet.timestamp;
             int earlierOrNot = DateTime.Compare(current, earliest);
             int laterOrNot   = DateTime.Compare(current, latest);
             if (earlierOrNot < 0)
             {
                 earliest = current;
             }
             if (laterOrNot > 0)
             {
                 latest = current;
             }
         }
         Timespan span = new Timespan(earliest, latest);
         return(span);
     }
     //throw new Exception("not implemented");
 }
Example #2
0
 /// <summary>
 /// Find tweets that were sent during a particular timespan.
 /// </summary>
 ///
 /// <param name="tweets">
 ///    a list of tweets with distinct ids, not modified by this method.
 /// </param>
 /// <param name="timespan">
 ///    timespan
 /// </param>
 /// <returns>
 ///    all and only the tweets in the list that were sent during the timespan,
 ///    in the same order as in the input list.
 /// </returns>
 public static List <Tweet> InTimespan(List <Tweet> tweets, Timespan timespan)
 {
     //throw new Exception("not implemented");
     if ((tweets.Count == 0) | (timespan.duration == 0))
     {
         List <Tweet> empty = new List <Tweet>();
         return(empty);
     }
     else
     {
         List <Tweet> ocurred = new List <Tweet>();
         DateTime     current;
         DateTime     latest   = timespan.end;
         DateTime     earliest = timespan.start;
         foreach (Tweet tweet in tweets)
         {
             current = tweet.timestamp;
             int earlierOrNot = DateTime.Compare(current, earliest);
             int laterOrNot   = DateTime.Compare(current, latest);
             if ((earlierOrNot >= 0) && (laterOrNot <= 0))
             {
                 ocurred.Add(tweet);
             }
         }
         return(ocurred);
     }
 }
Example #3
0
        public void TestGetTimespanEmptyTweets()
        {
            List <Tweet> list   = new List <Tweet>();
            double       noTime = 0;

            Timespan timespan = Extract.GetTimespan(list);

            Assert.AreEqual(timespan.duration, noTime, "expected 0 size timespan");
        }
Example #4
0
        public void TestGetTimespanTwoTweets()
        {
            List <Tweet> list = new List <Tweet>()
            {
                tweet1,
                tweet2
            };
            Timespan timespan = Extract.GetTimespan(list);

            Assert.AreEqual(d1, timespan.start, "expected start");
            Assert.AreEqual(d2, timespan.end, "expected end");
        }
Example #5
0
        /// <see cref="object.Equals">
        public override bool Equals(object thatObject)
        {
            Timespan that = thatObject as Timespan;

            if (that == null)
            {
                return(false);
            }

            return(_start.Equals(that._start) &&
                   _end.Equals(that._end));
        }
Example #6
0
        public void TestGetTimespanDecendingTweets()
        {
            DateTime     dt1   = DateTime.Parse("2016-02-17T10:00:06Z");
            DateTime     dt2   = DateTime.Parse("2016-02-17T10:00:03Z");
            DateTime     dt3   = DateTime.Parse("2016-02-17T10:00:00Z");
            Tweet        t1    = new Tweet(1, "alyssa", "is it @bbitdiddle who talks about rivest so much?", dt1);
            Tweet        t2    = new Tweet(2, "bbitdiddle", "@alyssa talks for minimum 30 minutes @Ellen #hype", dt2);
            Tweet        t3    = new Tweet(3, "bibimbop", "@alyssa talks for minimum 30 minutes @Ellen #NoHype", dt3);
            List <Tweet> listt = new List <Tweet>()
            {
                t1, t2, t3
            };
            Timespan timespan = Extract.GetTimespan(listt);
            double   span     = 6;

            Assert.AreEqual(timespan.duration, span, "expected 6 sec timespan");
        }
Example #7
0
        //
        // Main method of the program. Fetches a sample of tweets and prints some
        // facts about it.
        //
        public static void Main()
        {
            List <Tweet> tweets;

            try
            {
                tweets = TweetReader.ReadTweetsFromWeb(SAMPLE_SERVER);
            }
            catch (Exception e)
            {
                throw new Exception("Cannot read tweets", e);
            }

            // display some characteristics about the tweets
            Console.WriteLine("fetched " + tweets.Count + " tweets");

            HashSet <string> mentionedUsers = Extract.GetMentionedUsers(tweets);

            Console.WriteLine("covers " + mentionedUsers.Count + " Twitter users");

            Timespan span = Extract.GetTimespan(tweets);

            Console.WriteLine("ranging from " + span.start + " to " + span.end);

            // infer the follows graph
            Dictionary <string, HashSet <string> > followsGraph =
                SocialNetwork.GuessFollowsGraph(tweets);

            Console.WriteLine("follows graph has " + followsGraph.Count + " nodes");

            // print the top-N influencers
            int           count       = 10;
            List <string> influencers = SocialNetwork.Influencers(followsGraph);

            foreach (string username in
                     influencers.GetRange(0, Math.Min(count, influencers.Count)))
            {
                Console.WriteLine(username);
            }
        }