Esempio n. 1
0
		/// <summary>
		/// Build a WordCount object from a text file.
		/// Source: http://stackoverflow.com/questions/2161895/reading-large-text-files-with-streams-in-c-sharp
		/// </summary>
		/// <param name="path"></param>
		/// <returns></returns>
		public static WordCount LoadFromFile(string path)
		{
			WordCount wc = new WordCount();

			using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
			using (BufferedStream bs = new BufferedStream(fs))
			using (StreamReader sr = new StreamReader(bs))
			{
				string line;
				while ((line = sr.ReadLine()) != null)
				{
					string[] words = line.Split(new[] { " ", "," }, StringSplitOptions.RemoveEmptyEntries);

					words.ToList().ForEach(x => wc.Add(x));
				}
			}

			return wc;
		}
Esempio n. 2
0
		public void NegativeWordFrequency()
		{
			WordCount wc = new WordCount();
			WordOccurences results = wc.MostFrequent(-1);
		}
Esempio n. 3
0
		public void ZeroWordFrequency()
		{
			WordCount wc = new WordCount();
			WordOccurences results = wc.MostFrequent(0);
		}
Esempio n. 4
0
		/// <summary>
		/// Run the test and compare the output with the test data set.
		/// </summary>
		/// <param name="testSet"></param>
		/// <param name="nRequest">n most frequently occuring words</param>
		/// <param name="nExpected">the expected number of frequently occuring words, i.e. when nRequest is greater than the number of words</param>
		private void RunTest(TestData[] testSet, int nRequest, int nExpected)
		{
			WordCount wc = new WordCount();
			AddTestData(wc, testSet);
			WordOccurences results = wc.MostFrequent(nRequest);
			TestResult(results, nExpected, testSet);
		}
Esempio n. 5
0
		/// <summary>
		/// Add a single world X times to the WordCount object.
		/// </summary>
		/// <param name="wc"></param>
		/// <param name="word"></param>
		/// <param name="repeat"></param>
		private void AddWord(WordCount wc, string word, int repeat)
		{
			for (int i = 0; i < repeat; i++)
			{
				wc.Add(word);
			}
		}
Esempio n. 6
0
		/// <summary>
		/// Add the test data to the WordCount object.
		/// </summary>
		/// <param name="wc"></param>
		/// <param name="testSet"></param>
		private void AddTestData(WordCount wc, TestData[] testSet)
		{
			for (int i = 0; i < testSet.Length; i++)
			{
				TestData testData = testSet[i];
				for (int k = 0; k < testData.words.Length; k++)
				{
					AddWord(wc, testData.words[k], testData.frequency);
				}
			}
		}
Esempio n. 7
0
		public void NoWords()
		{
			WordCount wc = new WordCount();
			WordOccurences results = wc.MostFrequent(10);
			Assert.IsTrue(results.Count == 0);
		}