Esempio n. 1
0
        public static Sentiment GetSentiment(string input)
        {
            // 1
            // Split parameter into words
            var       words         = input.Split(Constants.DELIMITERS, StringSplitOptions.RemoveEmptyEntries);
            int       positiveCount = 0;
            int       negativeCount = 0;
            int       neutralCount  = 0;
            Sentiment sentiment     = new Sentiment();

            // Loop through all words
            foreach (string currentWord in words)
            {
                if (Constants.NEGATIVE_WORDS.Contains(currentWord.ToLower()))
                {
                    negativeCount++;
                }

                if (Constants.POSITIVE_WORDS.Contains(currentWord.ToLower()))
                {
                    positiveCount++;
                }
            }

            if (positiveCount == neutralCount)
            {
                sentiment.Type  = "Neutral";
                sentiment.Count = 0;
            }

            if (positiveCount > neutralCount)
            {
                sentiment.Type  = "Positive";
                sentiment.Count = positiveCount;
            }

            if (positiveCount < negativeCount)
            {
                sentiment.Type  = "Negative";
                sentiment.Count = negativeCount;
            }

            return(sentiment);
        }
        private static void SaveTweets(List <Status> tweets)
        {
            SqlConnection connection = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Twitter;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");

            connection.Open();

            List <TweetClassification> tweetClassifications = new List <TweetClassification>();

            for (int i = 0; i < 100; i++)
            {
                var tweet = tweets.ElementAt(i);

                using (SqlCommand com = new SqlCommand("insert into Tweets(TwitterID,FullText,SentimentType,SentimentCount,CreatedDate,Language,IsReweet,Location) values(@TwitterID,@FullText,@SentimentType,@SentimentCount,@CreatedDate,@Language,@IsReweet,@Location)", connection))
                {
                    Sentiment sentiment     = SentimentIdentifier.GetSentiment(tweet.Text);
                    string    sentimentType = i <= 90 ? sentiment.Type : TweetClassifer.FindSentimentType(sentiment.Count, tweet.retweeted, tweet.lang,
                                                                                                          tweetClassifications);

                    com.Parameters.AddWithValue("@TwitterID", tweet.id.ToString());
                    com.Parameters.AddWithValue("@FullText", StopWordRemover.RemoveStopwords(tweet.Text));
                    com.Parameters.AddWithValue("@SentimentType", sentimentType);
                    com.Parameters.AddWithValue("@SentimentCount", sentiment.Count);
                    com.Parameters.AddWithValue("@CreatedDate", tweet.CreatedAt);
                    com.Parameters.AddWithValue("@Language", tweet.lang);
                    com.Parameters.AddWithValue("@Location", tweet.User.Location);
                    com.Parameters.AddWithValue("@IsReweet", tweet.retweeted.ToString());

                    tweetClassifications.Add(new TweetClassification {
                        SentimentType = sentiment.Type, SentimentCount = sentiment.Count, IsRetweet = tweet.retweeted, Language = tweet.lang.ToString()
                    });

                    com.ExecuteNonQuery();
                }
            }

            connection.Close();
        }