Beispiel #1
0
        public void TestCount()
        {
            WordCounter wordCounter = new WordCounter();

            Assert.AreEqual(1, wordCounter.CountWords(new StringDocument("mouse")));
            Assert.AreEqual(2, wordCounter.CountWords(new StringDocument(" \nblue\t  table\r")));
        }
Beispiel #2
0
        public void AggregatesMultipleCalls()
        {
            // Arrange
            var wc = new WordCounter();

            // Act
            wc.CountWords("one two one one");
            wc.CountWords("one two one one");

            // Assert
            Assert.That(wc.Counts.Count, Is.EqualTo(2));
            Assert.That(wc.Counts.Single(w => w.Key == "one").Value, Is.EqualTo(6));
            Assert.That(wc.Counts.Single(w => w.Key == "two").Value, Is.EqualTo(2));
        }
        public void CountWordsFindsAWordInASingleWordSentenceWithPunctuation()
        {
            var sentence = "Test!";
            var cut = new WordCounter(sentence);

            Assert.AreEqual(1, cut.CountWords().Count);
        }
        public void CountWordsProcessesFindsAWordInASingleWordSentence()
        {
            var sentence = "Test";
            var cut = new WordCounter(sentence);

            Assert.AreEqual(1, cut.CountWords().Count);
        }
        public void WordCounter_GetCount_CountWordFalse()
        {
            WordCounter testWords = new WordCounter("cat", "The dog is in the cathedral.");
            int         result    = testWords.CountWords(testWords.RemovePunctuation(testWords.Sentence), testWords.Word);

            Assert.AreEqual(result, 0);
        }
Beispiel #6
0
        void CountWordsTest(string str, Dictionary <string, int> d1)
        {
            var countedWords = WordCounter.CountWords(str);

            Assert.IsTrue(countedWords.OrderBy(i => i.Value)
                          .SequenceEqual(d1.OrderBy(i => i.Value)));
        }
        public void CountWordsReturnCorrectCountIgnoringCaseTest()
        {
            // Arrange
            var word1 = Guid.NewGuid().ToString();
            var word2 = Guid.NewGuid().ToString();
            var word3 = "ABCd";
            var word3AllLowercase = "abcd";

            var words = new List<string>
            {
                word1,
                word2,
                word3,
                word3AllLowercase,
                word2,
                word2
            };

            // Act
            var counter = new WordCounter();
            var result = counter.CountWords(words);

            // Assert
            Assert.AreEqual(3, result.Count);
            Assert.AreEqual(1, result[word1]);
            Assert.AreEqual(3, result[word2]);
            Assert.AreEqual(2, result[word3AllLowercase]);
            Assert.IsFalse(result.ContainsKey(word3));
        }
        public void CountWordsFindsTwoWordsInASimpleTwoWordSentence()
        {
            var sentence = "hello world";
            var cut = new WordCounter(sentence);

            Assert.AreEqual(2, cut.CountWords().Count);
        }
Beispiel #9
0
        public void CountWordsReturnCorrectCountIgnoringCaseTest()
        {
            // Arrange
            var word1             = Guid.NewGuid().ToString();
            var word2             = Guid.NewGuid().ToString();
            var word3             = "ABCd";
            var word3AllLowercase = "abcd";

            var words = new List <string>
            {
                word1,
                word2,
                word3,
                word3AllLowercase,
                word2,
                word2
            };

            // Act
            var counter = new WordCounter();
            var result  = counter.CountWords(words);

            // Assert
            Assert.AreEqual(3, result.Count);
            Assert.AreEqual(1, result[word1]);
            Assert.AreEqual(3, result[word2]);
            Assert.AreEqual(2, result[word3AllLowercase]);
            Assert.IsFalse(result.ContainsKey(word3));
        }
        public void CountWordsReturnsAnEmptyDictionaryForNullInput()
        {
            string nullSentence = null;
            var cut = new WordCounter(nullSentence);
            var expectedResult = new Dictionary<string, int>();

            Assert.AreEqual(expectedResult, cut.CountWords());
        }
        public void CountWordsReturnsAnEmptyDictionaryForEmptyInput()
        {
            var emptySentence = String.Empty;
            var cut = new WordCounter(emptySentence);
            var expectedResult = new Dictionary<string, int>();

            Assert.AreEqual(expectedResult, cut.CountWords());
        }
Beispiel #12
0
        public void Test()
        {
            string      file = @"D:\\201731107105\\test.txt";
            WordCounter w    = new WordCounter();

            w.WordFrequency(file);
            Assert.AreEqual(w.CountWords(file), 5);
        }
        public void Counter_CheckForInstanceOfWordInSentence_int()
        {
            string      word     = "dog";
            string      sentence = "My dog is a dog with a dog who is also a dog";
            WordCounter myWord   = new WordCounter(word, sentence);

            Assert.AreEqual(4, myWord.CountWords());
        }
        public void MatchSixLetterCapitalization_One()
        {
            string Word = "iNSidE";

            string[]    InputStringArray = new string[] { "a", "inside" };
            WordCounter testWordCounter  = new WordCounter(Word, InputStringArray);

            Assert.AreEqual(1, testWordCounter.CountWords(Word, InputStringArray));
        }
        public void CountWordsFindsADuplicateWordInATwoWordSentenceWithAmplePunctuation()
        {
            var sentence = "hello, !@#$%^&, hello";
            var cut = new WordCounter(sentence);
            var result = cut.CountWords();
            var expectedResult = new Dictionary<string, int> {{"hello", 2}};

            Assert.AreEqual(expectedResult, result);
        }
        public void NoInput_Zero()
        {
            string Word = "";

            string[]    InputStringArray = new string[] { "" };
            WordCounter testWordCounter  = new WordCounter(Word, InputStringArray);

            Assert.AreEqual(0, testWordCounter.CountWords(Word, InputStringArray));
        }
        public void MatchTwoLetters_One()
        {
            string Word = "in";

            string[]    InputStringArray = new string[] { "an", "in" };
            WordCounter testWordCounter  = new WordCounter(Word, InputStringArray);

            Assert.AreEqual(1, testWordCounter.CountWords(Word, InputStringArray));
        }
        public void CountWordsFindsADuplicateWordInATwoWordSentenceWithDifferentCases()
        {
            var sentence = "hello Hello";
            var cut = new WordCounter(sentence);
            var result = cut.CountWords();
            var expectedResult = new Dictionary<string, int> {{"hello", 2}};

            Assert.AreEqual(expectedResult, result);
        }
        public void NoMatches_Zero()
        {
            string Word = "outside";

            string[]    InputStringArray = new string[] { "a", "an", "in", "inside" };
            WordCounter testWordCounter  = new WordCounter(Word, InputStringArray);

            Assert.AreEqual(0, testWordCounter.CountWords(Word, InputStringArray));
        }
        public void MatchOneLetterLongerList_One()
        {
            string Word = "a";

            string[]    InputStringArray = new string[] { "a", "I" };
            WordCounter testWordCounter  = new WordCounter(Word, InputStringArray);

            Assert.AreEqual(1, testWordCounter.CountWords(Word, InputStringArray));
        }
        public void PartialMatch_Zero()
        {
            string Word = "a";

            string[]    InputStringArray = new string[] { "an" };
            WordCounter testWordCounter  = new WordCounter(Word, InputStringArray);

            Assert.AreEqual(0, testWordCounter.CountWords(Word, InputStringArray));
        }
        public void IncorrectInputNumber_Zero()
        {
            string Word = "5";

            string[]    InputStringArray = new string[] { "a" };
            WordCounter testWordCounter  = new WordCounter(Word, InputStringArray);

            Assert.AreEqual(0, testWordCounter.CountWords(Word, InputStringArray));
        }
Beispiel #23
0
        public void CounterTest()
        {
            var request = new WordCounterRequest();

            request.UserInput = "This is a test.";

            var result = new WordCounter();
            var output = result.CountWords(request);

            Assert.AreEqual(output.Counter, 4);
        }
Beispiel #24
0
        public void IsInvalidFilePath_ThrowException()
        {
            //Creating a MOQ object of ILogger interface to instantiate the WordCounter
            //Arrange
            var logger = new Mock <ILoggerFactory>().Object;
            //Act
            var wordCounter = new WordCounter(logger);

            //Assert
            wordCounter.CountWords(String.Empty);
        }
Beispiel #25
0
        public void ReturnsOneWhenOnlyOneWord()
        {
            // Arrange
            var wc = new WordCounter();

            // Act
            wc.CountWords("single");

            // Assert
            Assert.That(wc.Counts.Single(w => w.Key == "single").Value, Is.EqualTo(1));
        }
Beispiel #26
0
        public void IsCaseInsensitive()
        {
            // Arrange
            var wc = new WordCounter();

            // Act
            wc.CountWords("single SINGLE sinGLE");

            // Assert
            Assert.That(wc.Counts.Single(w => w.Key == "single").Value, Is.EqualTo(3));
        }
Beispiel #27
0
        static void Main(string[] args)
        {
            var counter = new WordCounter();

            var manager = new FileManager();

            var words = counter.CountWords(manager.GetLines("data.txt"));

            var sorter = new WordCollectionSorter();

            manager.SaveResult("results.txt", sorter.GetLines(words));
        }
Beispiel #28
0
        public void ReturnsCorrectCountsWhenDuplicatesWords()
        {
            // Arrange
            var wc = new WordCounter();

            // Act
            wc.CountWords("one two one one");

            // Assert
            Assert.That(wc.Counts.Count, Is.EqualTo(2));
            Assert.That(wc.Counts.Single(w => w.Key == "one").Value, Is.EqualTo(3));
            Assert.That(wc.Counts.Single(w => w.Key == "two").Value, Is.EqualTo(1));
        }
Beispiel #29
0
        public void ReturnsBothWordsWhenTwoDistinctWords()
        {
            // Arrange
            var wc = new WordCounter();

            // Act
            wc.CountWords("two words");

            // Assert
            Assert.That(wc.Counts.Count, Is.EqualTo(2));
            Assert.That(wc.Counts.Single(w => w.Key == "two").Value, Is.EqualTo(1));
            Assert.That(wc.Counts.Single(w => w.Key == "words").Value, Is.EqualTo(1));
        }
Beispiel #30
0
        public void DoesNotConsiderAHyphenToBeAWordBreakingCharacter()
        {
            // Arrange
            var wc = new WordCounter();

            // Act
            wc.CountWords("one-two one one");

            // Assert
            Assert.That(wc.Counts.Count, Is.EqualTo(2));
            Assert.That(wc.Counts.Single(w => w.Key == "one-two").Value, Is.EqualTo(1));
            Assert.That(wc.Counts.Single(w => w.Key == "one").Value, Is.EqualTo(2));
        }
Beispiel #31
0
        public void IsValidFilePath_Return_ConcurrentDictionary()
        {
            //Creating a MOQ object of ILogger interface to instantiate the WordCounter
            //Arrange
            var serviceProvider = new ServiceCollection()
                                  .AddLogging()
                                  .BuildServiceProvider();
            var factory = serviceProvider.GetService <ILoggerFactory>();
            //Act
            var wordCounter = new WordCounter(factory);
            var result      = wordCounter.CountWords(_filePath);

            //Assert
            Assert.IsInstanceOfType(result, typeof(ConcurrentDictionary <string, int>));
        }
Beispiel #32
0
        public void CountSingleWordWithTrailingSpace()
        {
            // Arrange
            int wordCount;
            int characterCount;
            int nonWhitespaceCount;

            // Act
            WordCounter.CountWords(
                "cheese  ", out wordCount, out characterCount, out nonWhitespaceCount);

            // Assert
            Assert.AreEqual(1, wordCount);
            Assert.AreEqual(8, characterCount);
            Assert.AreEqual(6, nonWhitespaceCount);
        }
Beispiel #33
0
        public void CountSplicedWords()
        {
            // Arrange
            int wordCount;
            int characterCount;
            int nonWhitespaceCount;

            // Act
            WordCounter.CountWords(
                "cheese-curds", out wordCount, out characterCount, out nonWhitespaceCount);

            // Assert
            Assert.AreEqual(1, wordCount);
            Assert.AreEqual(12, characterCount);
            Assert.AreEqual(12, nonWhitespaceCount);
        }
Beispiel #34
0
        public void CountBlankString()
        {
            // Arrange
            int wordCount;
            int characterCount;
            int nonWhitespaceCount;

            // Act
            WordCounter.CountWords(
                "", out wordCount, out characterCount, out nonWhitespaceCount);

            // Assert
            Assert.AreEqual(0, wordCount);
            Assert.AreEqual(0, characterCount);
            Assert.AreEqual(0, nonWhitespaceCount);
        }
        public ActionResult WordCounterInput(WordCounterModel request)
        {
            if (ModelState.IsValid)
            {
                var wordFinder = new WordCounter();
                var wordData   = new WordCounterRequest();
                wordData.UserInput = request.UserInput;


                var result = wordFinder.CountWords(wordData);
                return(View("WordCounterOutput", result));
            }
            else
            {
                return(View(request));
            }
        }
Beispiel #36
0
        public ActionResult Submit()
        {
            string word        = Request.Form["word-input"];
            string listOfWords = Request.Form["list-of-words"];

            string sanitizedSentence = listOfWords.Replace("0", "").Replace("1", "").Replace("2", "").Replace("3", "").Replace("4", "").Replace("5", "").Replace("6", "").Replace("7", "").Replace("8", "").Replace("9", "").Replace("!", "").Replace("@", "").Replace("#", "").Replace("$", "").Replace("%", "").Replace("^", "").Replace("&", "").Replace("*", "").Replace("(", "").Replace(")", "").Replace(",", "").Replace(".", "").Replace(";", "").Replace("[", "").Replace("]", "").Replace("-", "").Replace("=", "").Replace("<", "").Replace(">", "").Replace("?", "").Replace(":", "").Replace("{", "").Replace("}", "").Replace("_", "").Replace("+", "");

            Console.WriteLine(word);
            Console.WriteLine(sanitizedSentence);

            string[] sentence = sanitizedSentence.Split(' ');

            Console.WriteLine(sentence);

            WordCounter newWordCounter = new WordCounter(word, sentence);
            int         NumberOfWords  = newWordCounter.CountWords(word, sentence);

            return(View("Index", NumberOfWords));
        }
Beispiel #37
0
        public void TestCountWords()
        {
            var wordCounter = new WordCounter();

            string[] text1 = new string[] {
                "дом",
                "стол",
                "окно",
                "стена",
                "дом",
                "пол",
                "подоконник",
                "стена",
                "стена",
                "стена",
                "дом",
                "дом",
                "стул",
                "дом",
                "окно"
            };
            var expected1 = new Dictionary <string, int> ();

            expected1.Add("дом", 5);
            expected1.Add("стол", 1);
            expected1.Add("окно", 2);
            expected1.Add("стена", 4);
            expected1.Add("пол", 1);
            expected1.Add("подоконник", 1);
            expected1.Add("стул", 1);
            var result1 = wordCounter.CountWords(text1);

            Assert.AreEqual <int>(result1.Count, expected1.Count);
            foreach (string word in expected1.Keys)
            {
                Assert.AreEqual <int>(result1[word], expected1[word]);
            }
        }
        public void ProcessHandlesAReasonablyLargeSentenceOfManyCopiesOfASingleWordProperly()
        {
            // 15 in a line, 6 lines
            var sentence = @"
                hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello 
                hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello 
                hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello 
                hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello 
                hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello 
                hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello";
            var cut = new WordCounter(sentence);
            var result = cut.CountWords();
            var expectedResult = new Dictionary<string, int> {{"hello", 90}};

            Assert.AreEqual(expectedResult, result);
        }
        public void CountWordsFindsADuplicateWordInATwoWordSentenceWhereAWordHasAnUppercaseLetterInTheMiddle()
        {
            var sentence = "hello heLlo";
            var cut = new WordCounter(sentence);
            var result = cut.CountWords();
            var expectedResult = new Dictionary<string, int> {{"hello", 2}};

            Assert.AreEqual(expectedResult, result);
        }
        public void ClientProvidedExampleTest()
        {
            var sentence = "This is a statement, and so is this.";
            var cut = new WordCounter(sentence);
            var result = cut.CountWords();

            var expectedResult = new Dictionary<string, int>
            {
                {"this", 2},
                {"is", 2},
                {"a", 1},
                {"statement", 1},
                {"and", 1},
                {"so", 1}
            };


            Assert.AreEqual(expectedResult, result);
        }
Beispiel #41
0
        static void Main(string[] args)
        {
            #region Initializing Objects

            //Introducing Dependency Injection
            var serviceProvider = new ServiceCollection()
                                  .AddLogging(loggingBuilder =>
            {
                loggingBuilder.AddConsole();
            })
                                  .AddSingleton <IWordCounter, WordCounter>()
                                  .AddSingleton <IFileValidator, FileValidator>()
                                  .BuildServiceProvider();

            var logger = serviceProvider.GetService <ILoggerFactory>()
                         .CreateLogger <Program>();

            WordCounter   = serviceProvider.GetService <IWordCounter>();
            FileValidator = serviceProvider.GetService <IFileValidator>();

            //Defining the File Path from Solution Directory
            Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
            String root = Directory.GetCurrentDirectory();

            //File information has been hard coded to save time. But this could be made as user input.
            _filePath = root + @"\Files\mobydick.txt";

            #endregion

            //Read the display count from user.
            Console.Write("Enter the number of top words you would like to view (or press enter to skip and display top 20):");
            var userEntry = Console.ReadLine();

            #region Read User Input

            //Check if the user has entered any input
            if (!String.IsNullOrEmpty(userEntry))
            {
                //Check the user input is valid integer or not
                while (!Int32.TryParse(userEntry, out _displayCount))
                {
                    Console.WriteLine("Not a valid number, Please enter again.");
                    Console.Write("Enter the number of top words you would like to view:");

                    userEntry = Console.ReadLine();
                }
            }
            #endregion

            //Some Decoration for better UI
            Console.WriteLine("".PadRight(24, '-'));

            var isValidFile = Validator(FileValidator);

            if (isValidFile)
            {
                //Some Decoration for better UI
                Console.WriteLine("Output");
                Console.WriteLine("".PadRight(24, '-'));

                #region Counting the Top Words

                //Returns a dictionary of unique word and it's count
                var result = WordCounter.CountWords(_filePath);

                int rank = 1;

                //Sort the dictionary elements to display the top count.
                foreach (var word in result.OrderByDescending(x => x.Value).Take(_displayCount))
                {
                    //Print the result in diserable format.
                    Console.WriteLine("Rank: " + rank + " | " + "Word: " + word.Key + " | " + "Count: " + word.Value);
                    //increase the counter to display the rank.
                    rank++;
                }

                #endregion

                //Some Decoration for better UI
                Console.WriteLine("".PadRight(24, '-'));
                Console.WriteLine("Please enter key to exit the program.");
                logger.LogInformation("All done!");
                Console.ReadLine();
            }
        }
        public void ProcessHandlesASentenceWithWordsAndGarbledPunctuation()
        {
            var sentence = "hello, )(_!, world";
            var cut = new WordCounter(sentence);
            var result = cut.CountWords();
            var expectedResult = new Dictionary<string, int> {{"hello", 1}, {"world", 1}};

            Assert.AreEqual(expectedResult, result);
        }
        public void CountWordsFindsADuplicateWordSurroundedByPunctuation()
        {
            var sentence = "hello,world,hello!";
            var cut = new WordCounter(sentence);
            var result = cut.CountWords();
            var expectedResult = new Dictionary<string, int> {{"hello", 2}, {"world", 1}};

            Assert.AreEqual(expectedResult, result);
        }