private static void LoadProperties(WordCompareResult item, DataRow row)
 {
     item.ID = row.Field <int>("ID");
     item.mostPopularWordSearchResultID  = row.Field <int>("MostPopularWordSearchResultID");
     item.leastPopularWordSearchResultID = row.Field <int>("LeastPopularWordSearchResultID");
     item.WordsAreEquallyPopular         = row.Field <bool>("WordsAreEquallyPopular");
     if (row["EquallyPopularWordSearchResult1ID"] != DBNull.Value)
     {
         item.equallyPopularWordSearchResult1ID = row.Field <int>("EquallyPopularWordSearchResult1ID");
     }
     else
     {
         item.equallyPopularWordSearchResult1ID = -1;
     }
     if (row["EquallyPopularWordSearchResult2ID"] != DBNull.Value)
     {
         item.equallyPopularWordSearchResult2ID = row.Field <int>("EquallyPopularWordSearchResult2ID");
     }
     else
     {
         item.equallyPopularWordSearchResult2ID = -1;
     }
     item.SearchElapsedSeconds = row.Field <double>("SearchElapsedSeconds");
     item.CompareDateTime      = row.Field <DateTime>("CompareDateTime");
 }
        public static WordCompareResult RetrieveWordCompareResult(int Id)
        {
            WordCompareResult wordCompareResult = null;

            DataTable data = RetrieveSingleRecord(Id);

            if (data.Rows.Count == 1)
            {
                wordCompareResult = new WordCompareResult();
                LoadProperties(wordCompareResult, data.Rows[0]);
            }

            return(wordCompareResult);
        }
        public static List <WordCompareResult> RetrieveWordCompareResult()
        {
            List <WordCompareResult> items = new List <WordCompareResult>();

            DataTable data = RetrieveRecords();

            if (data.Rows.Count > 0)
            {
                foreach (DataRow itemRow in data.Rows)
                {
                    WordCompareResult item = new WordCompareResult();
                    LoadProperties(item, itemRow);
                    items.Add(item);
                }
            }

            return(items);
        }
Beispiel #4
0
        public static List <WordCompareResult> RetrieveWordCompareHistory()
        {
            var cmd = Database.GenericDataAccess.CreateCommand(Database.Utility.ConnectionString);

            cmd.CommandText = @"SELECT * 
                                FROM WordCompareResult
                                ORDER BY CompareDateTime DESC";

            var results = Database.GenericDataAccess.ExecuteSelectCommand(cmd);

            List <WordCompareResult> WordCompareResultsToReturn = new List <WordCompareResult>();

            foreach (DataRow result in results.Rows)
            {
                WordCompareResult savedResult = WordCompareResult.RetrieveWordCompareResult(result.Field <int>("ID"));
                WordCompareResultsToReturn.Add(savedResult);
            }

            return(WordCompareResultsToReturn);
        }
Beispiel #5
0
        public static void SaveWordCompareResult(WordCompareResult wordCompareResult)
        {
            if (wordCompareResult.WordsAreEquallyPopular)
            {
                List <WordSearchResult> savedWordSearchResults = new List <WordSearchResult>();

                foreach (var item in wordCompareResult.EquallyPopularResults)
                {
                    WordSearchResult savedResult = item.Save();
                    savedWordSearchResults.Add(savedResult);
                }

                wordCompareResult.EquallyPopularResults = savedWordSearchResults;
            }
            else
            {
                wordCompareResult.MostPopularWordSearchResult  = wordCompareResult.MostPopularWordSearchResult.Save();
                wordCompareResult.LeastPopularWordSearchResult = wordCompareResult.LeastPopularWordSearchResult.Save();
            }

            wordCompareResult.Save();
        }
Beispiel #6
0
        /// <summary>
        /// Takes two words and determines which is more popular on Twitter by how many tweets contain the word.
        /// </summary>
        /// <param name="searchWord1">The first word to search for.</param>
        /// <param name="searchWord2">The second word to search for.</param>
        /// <returns><see cref="WordCompareResult"/> of the comparison results.</returns>
        /// <exception cref="ArgumentException">Thrown when <paramref name="searchWord1"/> or <paramref name="searchWord2"/> is null, is only whitespace or contains a space.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="searchWord1"/> or <paramref name="searchWord2"/> length is over 500 characters.</exception>
        /// <exception cref="Comparitter.Compare.Exception.CompareException">Thrown when an error occurrs while searching for one of the words.</exception>
        public static WordCompareResult CompareByAppearanceCount(string searchWord1, string searchWord2)
        {
            if (string.IsNullOrWhiteSpace(searchWord1))
            {
                throw new ArgumentException("Phrase cannot be null or only whitespace.", nameof(searchWord1));
            }

            if (searchWord1.Length > 500)
            {
                throw new ArgumentOutOfRangeException(nameof(searchWord1), "Phrase is limited to 500 characters.");
            }

            if (searchWord1.Contains(" "))
            {
                throw new ArgumentException("SearchByWord() does not support searching for phrases. Only single words are allowed.", nameof(searchWord1));
            }

            if (string.IsNullOrWhiteSpace(searchWord2))
            {
                throw new ArgumentException("Phrase cannot be null or only whitespace.", nameof(searchWord2));
            }

            if (searchWord2.Length > 500)
            {
                throw new ArgumentOutOfRangeException(nameof(searchWord2), "Phrase is limited to 500 characters.");
            }

            if (searchWord2.Contains(" "))
            {
                throw new ArgumentException("SearchByWord() does not support searching for phrases. Only single words are allowed.", nameof(searchWord2));
            }

            WordCompareResult compareResultsToReturn = new WordCompareResult
            {
                CompareDateTime = DateTime.Now
            };

            Stopwatch howLong = new Stopwatch();

            try
            {
                howLong.Start();

                WordSearchResult word1SearchResult = GetWordSearchResults(searchWord1);

                WordSearchResult word2SearchResult = GetWordSearchResults(searchWord2);

                howLong.Stop();

                if (word1SearchResult.AppearanceCount == word2SearchResult.AppearanceCount)
                {
                    compareResultsToReturn.WordsAreEquallyPopular = true;
                    compareResultsToReturn.EquallyPopularResults  = new List <WordSearchResult>()
                    {
                        word1SearchResult, word2SearchResult
                    };
                }
                else if (word1SearchResult.AppearanceCount > word2SearchResult.AppearanceCount)
                {
                    compareResultsToReturn.WordsAreEquallyPopular       = false;
                    compareResultsToReturn.MostPopularWordSearchResult  = word1SearchResult;
                    compareResultsToReturn.LeastPopularWordSearchResult = word2SearchResult;
                }
                else
                {
                    compareResultsToReturn.WordsAreEquallyPopular       = false;
                    compareResultsToReturn.MostPopularWordSearchResult  = word2SearchResult;
                    compareResultsToReturn.LeastPopularWordSearchResult = word1SearchResult;
                }

                compareResultsToReturn.SearchElapsedSeconds = howLong.Elapsed.TotalSeconds;
            }
            catch (Tweetinvi.Exceptions.TwitterException ex)
            {
                //TODO: Would normally log this

                string message = "Communication with Twitter failed for one of the word searches. This could be caused by a network issue, Twitter rate limit or a bad request.";

                throw new Comparitter.Compare.Exception.CompareException(message, ex);
            }
            catch (System.Exception ex)
            {
                //TODO: Log this

                throw;
            }

            CompareHistory.SaveWordCompareResult(compareResultsToReturn);

            return(compareResultsToReturn);
        }