Advertisement
enevlogiev

pr03

May 20th, 2015
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. class WordCount
  7. {
  8.     private static void Main()
  9.     {
  10.         var textWords = new List<string>();
  11.  
  12.         using (var rdr = new StreamReader("text.txt")) //located in bin/debug
  13.         {
  14.             string input;
  15.             while ((input = rdr.ReadLine()) != null)
  16.             {
  17.                 var wordPattern = @"\b[A-Za-z]+\b";
  18.                 var rgx = new Regex(wordPattern);
  19.                 MatchCollection matches = rgx.Matches(input);
  20.                 textWords.AddRange(matches.Cast<Match>()
  21.                     .Select(x => x.Value.ToLower())
  22.                     .ToList());
  23.             }
  24.         }
  25.  
  26.         var occurences = new Dictionary<string, int>();
  27.         using (var rdr = new StreamReader("words.txt")) //located in bin/debug
  28.         {
  29.             string input;
  30.             while ((input = rdr.ReadLine()) != null)
  31.             {
  32.                 var count = textWords.Count(x => x == input);
  33.                 occurences.Add(input, count);
  34.             }
  35.         }
  36.  
  37.         using (var wtr = new StreamWriter("result.txt")) // should be created in bin/debug
  38.         {
  39.             occurences.OrderByDescending(x => x.Value)
  40.                 .ToList()
  41.                 .ForEach(x => wtr.WriteLine("{0} - {1}", x.Key, x.Value));
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement