Exemplo n.º 1
0
        public void TestGetHashtagsFromRetweets()
        {
            // the hashtag #youjustpulledanobama was popular at the time so I used it, no political reson behind it
            String originalTweet;
            var    results = TwitterTextUtility.GetRetweets("truth #youjustpulledanobama#test RT @obama I am the president #testtwo #helloworld #youjustpulledanobama", out originalTweet);

            var hashtagResults = TwitterTextUtility.GetHashtags(originalTweet);

            Assert.IsTrue(hashtagResults.Contains("#youjustpulledanobama"));
            Assert.IsTrue(hashtagResults.Contains("#test"));
            Assert.IsTrue(hashtagResults.Count == 2);

            hashtagResults = TwitterTextUtility.GetHashtags(results[0]);
            Assert.IsTrue(hashtagResults.Contains("#testtwo"));
            Assert.IsTrue(hashtagResults.Contains("#helloworld"));
            Assert.IsTrue(hashtagResults.Contains("#youjustpulledanobama"));
            Assert.IsTrue(hashtagResults.Count == 3);

            Assert.IsTrue(results.Count == 1);
        }
Exemplo n.º 2
0
        public void TestGetHashtags()
        {
            Assert.IsTrue(TwitterTextUtility.GetHashtags("hello world, no hashtags to see here").Count == 0);

            var results = TwitterTextUtility.GetHashtags("#helloworld");

            Assert.IsTrue(results.Count == 1);
            Assert.AreEqual(results[0], "#helloworld");

            results = TwitterTextUtility.GetHashtags("#one #two#three");
            Assert.IsTrue(results.Count == 3);
            Assert.AreEqual(results[0], "#one");
            Assert.AreEqual(results[1], "#two");
            Assert.AreEqual(results[2], "#three");

            results = TwitterTextUtility.GetHashtags("#test this is a #one hi #two#three");
            Assert.IsTrue(results.Count == 4);
            Assert.AreEqual(results[0], "#test");
            Assert.AreEqual(results[1], "#one");
            Assert.AreEqual(results[2], "#two");
            Assert.AreEqual(results[3], "#three");
        }
Exemplo n.º 3
0
        public static void TweetClassifier()
        {
            var context         = new OclumenContext();
            var classifier      = new NaiveBayes();
            var smoothingFactor = 1;

            var unigrams   = new Dictionary <string, List <KeyValuePair <Sentiment, decimal> > >();
            var unigramsSt = new Dictionary <string, List <KeyValuePair <Sentiment, decimal> > >();
            var bigrams    = new Dictionary <string, List <KeyValuePair <Sentiment, decimal> > >();
            var bigramsSt  = new Dictionary <string, List <KeyValuePair <Sentiment, decimal> > >();

            foreach (var ngram in context.BasicNgrams.Where(x => x.Cardinality == 1))
            {
                var ngramProbabilityList = new List <KeyValuePair <Sentiment, decimal> >(3);
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Positive, ngram.PositiveCount + smoothingFactor));
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Neutral, ngram.NeutralCount + smoothingFactor));
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Negative, ngram.NegativeCount + smoothingFactor));

                unigrams.Add(ngram.Text.ToLower(), ngramProbabilityList);

                ngramProbabilityList = new List <KeyValuePair <Sentiment, decimal> >(3);
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Positive, ngram.RtPositiveCount + smoothingFactor));
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Neutral, ngram.RtNeutralCount + smoothingFactor));
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Negative, ngram.RtNegativeCount + smoothingFactor));

                unigrams.Add(ngram.Text.ToLower() + "_rt", ngramProbabilityList);
            }

            foreach (var ngram in context.StemmedNgrams.Where(x => x.Cardinality == 1))
            {
                var ngramProbabilityList = new List <KeyValuePair <Sentiment, decimal> >(3);
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Positive, ngram.PositiveCount + smoothingFactor));
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Neutral, ngram.NeutralCount + smoothingFactor));
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Negative, ngram.NegativeCount + smoothingFactor));

                unigramsSt.Add(ngram.Text.ToLower(), ngramProbabilityList);

                ngramProbabilityList = new List <KeyValuePair <Sentiment, decimal> >(3);
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Positive, ngram.RtPositiveCount + smoothingFactor));
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Neutral, ngram.RtNeutralCount + smoothingFactor));
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Negative, ngram.RtNegativeCount + smoothingFactor));

                unigramsSt.Add(ngram.Text.ToLower() + "_rt", ngramProbabilityList);
            }

            foreach (var ngram in context.BasicNgrams.Where(x => x.Cardinality == 2))
            {
                var ngramProbabilityList = new List <KeyValuePair <Sentiment, decimal> >(3);
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Positive, ngram.PositiveCount + smoothingFactor));
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Neutral, ngram.NeutralCount + smoothingFactor));
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Negative, ngram.NegativeCount + smoothingFactor));

                bigrams.Add(ngram.Text.ToLower(), ngramProbabilityList);

                ngramProbabilityList = new List <KeyValuePair <Sentiment, decimal> >(3);
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Positive, ngram.RtPositiveCount + smoothingFactor));
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Neutral, ngram.RtNeutralCount + smoothingFactor));
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Negative, ngram.RtNegativeCount + smoothingFactor));

                bigrams.Add(ngram.Text.ToLower() + "_rt", ngramProbabilityList);
            }

            foreach (var ngram in context.StemmedNgrams.Where(x => x.Cardinality == 2))
            {
                var ngramProbabilityList = new List <KeyValuePair <Sentiment, decimal> >(3);
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Positive, ngram.PositiveCount + smoothingFactor));
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Neutral, ngram.NeutralCount + smoothingFactor));
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Negative, ngram.NegativeCount + smoothingFactor));

                bigramsSt.Add(ngram.Text.ToLower(), ngramProbabilityList);

                ngramProbabilityList = new List <KeyValuePair <Sentiment, decimal> >(3);
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Positive, ngram.RtPositiveCount + smoothingFactor));
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Neutral, ngram.RtNeutralCount + smoothingFactor));
                ngramProbabilityList.Add(new KeyValuePair <Sentiment, decimal>(Sentiment.Negative, ngram.RtNegativeCount + smoothingFactor));

                bigramsSt.Add(ngram.Text.ToLower() + "_rt", ngramProbabilityList);
            }


            int sleepTime = AppConfigSettings.ProcessTweetThreadSleepTime;

            while (CrawlerStatus.KeepRunning)
            {
                var tweetToProcess = context.RawTweets.First(x => x.AutoSentimentTimestamp == DateTime.MinValue);

                if (tweetToProcess == null)
                {
                    Thread.Sleep(sleepTime);
                    continue;
                }

                // add the hashtags to process queue
                String originalTweet;
                var    hashtags = TwitterTextUtility.GetHashtags(tweetToProcess.text);
                var    retweets = TwitterTextUtility.GetRetweets(tweetToProcess.text, out originalTweet);

                lock (_syncRoot)
                {
                    foreach (var hashtag in hashtags)
                    {
                        UnprocessedHashtags.Enqueue(hashtag);
                    }

                    foreach (var retweet in retweets)
                    {
                        UnprocessedRetweets.Enqueue(retweet);
                    }
                }

                // auto classify the tweets
                tweetToProcess.AutoUnigram        = (int)classifier.GetTextSentiment(tweetToProcess.text, 1, 1, false, Dictionary, context.BasicNgrams, context, unigrams);
                tweetToProcess.AutoUnigramStemmed = (int)classifier.GetTextSentiment(tweetToProcess.text, 1, 1, true, Dictionary, context.StemmedNgrams, context, unigramsSt);

                tweetToProcess.AutoBigram        = (int)classifier.GetTextSentiment(tweetToProcess.text, 2, 1, false, Dictionary, context.BasicNgrams, context, bigrams);
                tweetToProcess.AutoBigramStemmed = (int)classifier.GetTextSentiment(tweetToProcess.text, 2, 1, true, Dictionary, context.StemmedNgrams, context, bigramsSt);

                tweetToProcess.AutoSentimentTimestamp = DateTime.UtcNow;

                context.SaveChanges();
            }
        }