/// <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; }
/// <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); } }