Advertisement
Guest User

Word Count

a guest
May 22nd, 2015
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6.  
  7. public class WordCount
  8. {
  9.     public static void Main()
  10.     {
  11.         StreamReader fileWords = new StreamReader("words.txt", Encoding.UTF8);
  12.         StreamReader fileText = new StreamReader("text.txt", Encoding.UTF8);
  13.         StreamWriter fileDestination = new StreamWriter("result.txt");
  14.         Dictionary<string, int> wordsDictionary = new Dictionary<string, int>();
  15.  
  16.         using (fileWords)
  17.         {
  18.             string line = fileWords.ReadLine();
  19.  
  20.             while (line != null)
  21.             {
  22.                 string[] lineWords = line.Split(new char[] { ' ', '\n', '.', '!', '?', '-', ','},
  23.                     StringSplitOptions.RemoveEmptyEntries);
  24.  
  25.                 for (int i = 0; i < lineWords.Length; i++)
  26.                 {
  27.                     lineWords[i] = lineWords[i].ToLower();
  28.  
  29.                     if (!(wordsDictionary.ContainsKey(lineWords[i])))
  30.                     {
  31.                         wordsDictionary.Add(lineWords[i], 0);
  32.                     }
  33.                 }
  34.  
  35.                 line = fileWords.ReadLine();
  36.             }
  37.  
  38.             using (fileText)
  39.             {
  40.                 using (fileDestination)
  41.                 {
  42.                     string lineText = fileText.ReadLine();
  43.  
  44.                     while (lineText != null)
  45.                     {
  46.                         string[] lineTextSplited = lineText.Split(new char[] {' ', '\n', '.', '!', '?', '-', ','},
  47.                             StringSplitOptions.RemoveEmptyEntries);
  48.  
  49.                         for (int j = 0; j < lineTextSplited.Length; j++)
  50.                         {
  51.                             lineTextSplited[j] = lineTextSplited[j].ToLower();
  52.  
  53.                             if (wordsDictionary.ContainsKey(lineTextSplited[j]))
  54.                             {
  55.                                 wordsDictionary[lineTextSplited[j]] += 1;
  56.                             }
  57.                         }
  58.  
  59.                         lineText = fileText.ReadLine();
  60.                     }
  61.  
  62.                     foreach (var wordCount in wordsDictionary.OrderByDescending(key => key.Value))
  63.                     {
  64.                         fileDestination.WriteLine("{0} - {1}", wordCount.Key, wordCount.Value);
  65.                     }
  66.  
  67.                     Console.WriteLine("All Done!Please check the file \"result.txt\" to see the result");
  68.                     Console.WriteLine();
  69.                 }
  70.             }
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement