yanchevilian

03. Word Count from Streams / C# Advanced

Jul 31st, 2021 (edited)
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _3._Word_Count
  8. {
  9.     class Program
  10.     {
  11.         static async Task Main(string[] args)
  12.         {
  13.             Dictionary<string, int> allCoincidences = new Dictionary<string, int>();
  14.  
  15.             using (StreamReader streamReaderWords = new StreamReader("words.txt"))
  16.             {
  17.                 string currentWord = await streamReaderWords.ReadLineAsync();
  18.  
  19.                 using (StreamReader streamReaderText = new StreamReader("text.txt"))
  20.                 {
  21.                     string currentString = await streamReaderText.ReadLineAsync();
  22.  
  23.                     using (StreamWriter streamWriter = new StreamWriter("Output.txt"))
  24.                     {
  25.                         while (currentString != null)
  26.                         {
  27.                             string[] arrayWords =
  28.                                 currentWord.Split(" ", StringSplitOptions.RemoveEmptyEntries).ToArray();
  29.                             string[] arrayText = currentString.Split(new char[] {' ','-','.','?','!',','},StringSplitOptions.RemoveEmptyEntries)
  30.                                 .ToArray();
  31.  
  32.                             for (int word = 0; word < arrayWords.Length; word++)
  33.                             {
  34.                                 string currentWordToCompare = arrayWords[word];
  35.                                 for (int wordFromString = 0; wordFromString < arrayText.Length; wordFromString++)
  36.                                 {
  37.                                     string currentWordFromString = arrayText[wordFromString].ToLower();
  38.                                     if (currentWordToCompare == currentWordFromString)
  39.                                     {
  40.                                         if (allCoincidences.ContainsKey(currentWordToCompare) == false)
  41.                                         {
  42.                                             allCoincidences.Add(currentWordToCompare, 1);
  43.                                         }
  44.                                         else
  45.                                         {
  46.                                             allCoincidences[currentWordToCompare]++;
  47.                                         }
  48.                                     }
  49.                                 }
  50.                             }
  51.                             currentString = await streamReaderText.ReadLineAsync();
  52.                         }
  53.  
  54.                         foreach (KeyValuePair<string, int> keyValuePair in allCoincidences.OrderByDescending(x => x.Value))
  55.                         {
  56.                             string textToWrite = $"{keyValuePair.Key} - {keyValuePair.Value}";
  57.  
  58.                             await streamWriter.WriteLineAsync(textToWrite);
  59.                         }
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.     }
  65. }
Add Comment
Please, Sign In to add comment