/// <summary> /// Classifies a list of tweets using both of our approaches. /// </summary> /// <param name="tweetList">List of tweets</param> /// <returns>An AnalysisResultObj which contains both results</returns> private AnalysisResultObj ClassifyTweet(List <Tweet> tweetList) { Classifier c = new Classifier(); AnalysisResultObj result = TweetAnalyzer.Instance.AnalyzeAndDecorateTweetsThreaded(tweetList); result.MIResult = c.RunNaiveBayes(tweetList); return(result); }
/// <summary> /// Splits the tweets int sub lists and analyzes them using tasks /// </summary> /// <param name="tweetList"></param> /// <returns></returns> public AnalysisResultObj AnalyzeAndDecorateTweetsThreaded(List <Tweet> tweetList) { Log.Debug("Spliting " + tweetList.Count + " tweets"); int tweets = tweetList.Count; List <Task <AnalysisResultObj> > tasks = new List <Task <AnalysisResultObj> >(); List <Tweet> copyTweetList = new List <Tweet>(); copyTweetList.AddRange(tweetList); int e = tweetList.Count / Constants.TWEET_LIST_AMOUNT; List <List <Tweet> > splittedList = new List <List <Tweet> >(); int tweetsSplitted = 0; // Splits to the max number of lists for (int i = 0; i < Constants.TWEET_LIST_AMOUNT; i++) { tweetsSplitted += e; splittedList.Add(copyTweetList.Take(e).ToList()); copyTweetList = copyTweetList.Skip(e).ToList(); } //If some tweets are not covered, adds another list such that all tweets are analyzed if (tweetsSplitted < tweets) { splittedList.Add(copyTweetList); } //Starts the tasks foreach (var item in splittedList) { Task <AnalysisResultObj> t = new Task <AnalysisResultObj>(() => AnalyzeAndDecorateTweets(item)); t.Start(); tasks.Add(t); } Task.WaitAll(tasks.ToArray()); AnalysisResultObj res = new AnalysisResultObj(); //Combines the result foreach (var task in tasks) { res.Count += task.Result.Count; res.PolCount += task.Result.PolCount; res.KeywordBias += task.Result.KeywordBias; res.MediaBias += task.Result.MediaBias; res.PositiveSentiment += task.Result.PositiveSentiment; res.NegativeSentiment += task.Result.NegativeSentiment; res.PositiveTweetsCount += task.Result.PositiveTweetsCount; res.NegativeTweetsCount += task.Result.NegativeTweetsCount; } Log.Debug("Combining tweets"); Log.Debug("Result " + res.GetAlgorithmResult()); return(res); }
/// <summary> /// Post method to be used by the TweetRetriever. /// </summary> /// <param name="resultObj">The result object returned by algorithm</param> /// <param name="user">The user</param> /// <returns>Returns true if the post request succeeded</returns> private bool PostResultToDB(AnalysisResultObj resultObj, User user) { //Create the post request bool succes = webHandler.DatabaseSendDataRequest(Constants.DB_SERVER_IP + "twitter", "POST", "twitter_name=" + user.ScreenName, "twitter_id=" + user.Id, "analysis_val=" + resultObj.GetAlgorithmResult().ToString(CultureInfo.InvariantCulture), "media_val=" + resultObj.GetMediaResult().ToString(CultureInfo.InvariantCulture), "mi_val=" + resultObj.MIResult.ToString(CultureInfo.InvariantCulture), "sentiment_val=" + resultObj.GetSentiment().ToString(CultureInfo.InvariantCulture), "tweet_count=" + resultObj.Count, "protect=" + Convert.ToInt32(user.IsProtected)); if (!succes) { Log.Error("Could not post the user to the database"); return(false); } return(succes); }
/// <summary> /// Post method to be used in the analysis of the friends of a user in the TweetRetriever /// </summary> /// <param name="resultObj">The result object returned by algorithm</param> /// <param name="user">The user</param> /// <returns>Returns true if the post request succeeded</returns> private bool PostResultToDBAndLink(AnalysisResultObj resultObj, User user) { //Uses the post method bool succes = PostResultToDB(resultObj, user); //Then it tries to find the posted user and use that users record id to link using the add follower method long tempRecordId = -1; succes = GetUsersRecordIdOnDb(user, ref tempRecordId); if (!succes) { Log.Error("Could not find the posted user"); return(false); } AddFollower(tempRecordId); return(succes); }
static void Main(string[] args) { List <Tweet> tweetList = new List <Tweet>(); List <Tweet> tweetList2 = new List <Tweet>(); tweetList = FileHelper.ReadObjectFromFile <List <Tweet> >("\\TestData\\MaggieNYT-Left.json"); tweetList2 = FileHelper.ReadObjectFromFile <List <Tweet> >("\\TestData\\MaggieNYT-Left.json"); AnalysisResultObj result = TweetAnalyzer.Instance.AnalyzeAndDecorateTweetsThreaded(tweetList); AnalysisResultObj result2 = TweetAnalyzer.Instance.AnalyzeAndDecorateTweets(tweetList2); Console.WriteLine(tweetList[10].Text); Console.WriteLine(result.GetPolPercent() + " " + result.GetAlgorithmResult() + " " + result.GetMediaResult() + " " + result.Count + " " + result.PolCount + " " + result.KeywordBias + " " + result.MediaBias); Console.WriteLine(result2.GetPolPercent() + " " + result2.GetAlgorithmResult() + " " + result2.GetMediaResult() + " " + result2.Count + " " + result2.PolCount + " " + result2.KeywordBias + " " + result2.MediaBias); Console.WriteLine("?"); Console.ReadLine(); }
/// <summary> /// Analyzes a list of Tweets, and returns the following values as a double-array: /// Keyword-Bias: Determined political value of the words used in the tweets /// Media-Bias: Determined political value of news media linked to in the tweets /// Count: Number of tweets analtyzed /// Pos: Number of positive words found /// Neg: Number of negative words found /// </summary> /// <param name="tweetList">The list of tweets</param> /// <returns>AnalysisResultObj</returns> public AnalysisResultObj AnalyzeAndDecorateTweets(List <Tweet> tweetList) { AnalysisResultObj output = new AnalysisResultObj(); foreach (Tweet tweet in tweetList) { tweet.HasQuotes = CheckForQuotationMarks(tweet); if (!tweet.HasQuotes) { var puncturation = tweet.Text.Where(Char.IsPunctuation).Distinct().ToArray(); List <String> wordList = tweet.Text.Split(' ').Select(x => x.Trim(puncturation)).ToList <String>(); SentimentAnalysis(wordList, tweet); KeywordAnalysis(wordList, tweet); MediaAnalysis(tweet); if (tweet.GetSentiment() > 0) { output.PositiveTweetsCount++; } else if (tweet.GetSentiment() < 0) { output.NegativeTweetsCount++; } output.NegativeSentiment += tweet.NegativeValue; output.PositiveSentiment += tweet.PositiveValue; if (tweet.KeywordBias != 0 || tweet.MediaBias != 0) { output.PolCount++; output.KeywordBias += tweet.KeywordBias; output.MediaBias += tweet.MediaBias; } } } output.Count = tweetList.Count; return(output); }
/// <summary> /// The threaded tweet retrieve method which is used in the task /// </summary> /// <param name="user">The user</param> /// <param name="auth">The auth object</param> /// <param name="checkDB">The method to check the database for previous results</param> /// <param name="classifyMethod">The method to classify tweets</param> /// <param name="postToDB">The method to post the result to the database</param> /// <returns>A list of tweets </returns> private List <Tweet> TweetThreadMethod(User user, AuthObj auth, Func <User, bool> checkDB = null, Func <List <Tweet>, AnalysisResultObj> classifyMethod = null, Func <AnalysisResultObj, User, bool> postToDB = null) { bool alreadyExist = false; AnalysisResultObj tempResult = new AnalysisResultObj(); List <Tweet> temp = new List <Tweet>(); if (checkDB != null) { alreadyExist = checkDB(user); //Uses the supplied method to find out if the result already exist on the database if (alreadyExist) { Log.Debug(String.Format("{0,-30} {1,-20} {2,-11}", user.Name, user.Id, "Already exist in db")); } } if (!alreadyExist) { temp = RetrieveTweets(user, auth); if (user.IsProtected) { Log.Debug(String.Format("{0,-30} {1,-20} {2,-11}", user.Name, user.Id, "Protected")); } else { Log.Debug(String.Format("{0,-30} {1,-20} {2,-11}", user.Name, user.Id, temp.Count)); } if (classifyMethod != null && postToDB != null) { tempResult = classifyMethod(temp); //Use the supplied method to classify the list of tweets postToDB(tempResult, user); //Use the supplied method to post the result to the database temp = new List <Tweet>(); } } return(temp); }