private static void SortArrayDescending(WordsCount[] words) { Array.Sort(words, (x, y) => { if (x.Count == y.Count) return 0; if (x.Count < y.Count) return 1; if (x.Count > y.Count) return -1; return 0; } ); }
private static void WriteArrayToFile( string resultFileName, WordsCount[] words) { StreamWriter writer = new StreamWriter(resultFileName, false, Encoding.GetEncoding("windows-1251")); using (writer) { writer.WriteLine("List of words with their count found in text"); writer.WriteLine("============================================"); writer.WriteLine(); foreach (var item in words) { writer.WriteLine(string.Format("{0,10} => {1,5}", item.Word, item.Count)); } } }
private static void CountWordsInFile( string targetFileName, WordsCount[] words, string[] wordsToCheck) { StreamReader reader = new StreamReader(targetFileName, Encoding.GetEncoding("windows-1251")); using (reader) { string line = reader.ReadLine(); while (line != null) { string delimiters = @" ?!.,<>/\""'|[]{}~`@#$%^&*()-_+=;:" + "\t" + "\n" + "\r"; string[] arrLine = line.Split(delimiters.ToCharArray()); foreach (var word in arrLine) { int index = Array.IndexOf(wordsToCheck, word); if (index > -1) { words[index].Count++; } } line = reader.ReadLine(); } } }
private static WordsCount[] ReadWordsFromFile( string wordsFileName) { List<WordsCount> wordsReaded = new List<WordsCount>(); StreamReader reader = new StreamReader(wordsFileName, Encoding.GetEncoding("windows-1251")); using (reader) { string word = reader.ReadLine(); while (word != null) { WordsCount record = new WordsCount(word); wordsReaded.Add(record); word = reader.ReadLine(); } } return wordsReaded.ToArray(); }