sashomaga

Linq, dictionary, regular expresions

Jan 27th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text.RegularExpressions;
  5. using System.Security;
  6. using System.Linq;
  7. //Write a program that reads a list of words from a file words.txt and finds how many times each of
  8. //the words is contained in another file test.txt. The result should be written in the file
  9. //result.txt and the words should be sorted by the number of their occurrences in descending order.
  10. //Handle all possible exceptions in your methods.
  11. class RemoveWords
  12. {
  13.     static void Main()
  14.     {
  15.         try
  16.         {
  17.             Dictionary<string, int> words = new Dictionary<string, int>();
  18.             using (StreamReader reader = new StreamReader("words.txt"))
  19.             {
  20.                 ReadWords(reader, words);
  21.             }
  22.             using (StreamReader reader = new StreamReader("test.txt"))
  23.             using (StreamWriter writer = new StreamWriter("result.txt"))
  24.             {
  25.                 CountMatches(reader, words);
  26.                 WriteWords(words, writer);
  27.             }
  28.         }
  29.         catch (FileNotFoundException err)
  30.         {
  31.             Console.WriteLine(err.Message);
  32.         }
  33.         catch (DirectoryNotFoundException err)
  34.         {
  35.             Console.WriteLine(err.Message);
  36.         }
  37.         catch (IOException err)
  38.         {
  39.             Console.WriteLine(err.Message);
  40.         }
  41.         catch (UnauthorizedAccessException err)
  42.         {
  43.             Console.WriteLine(err.Message);
  44.         }
  45.         catch (ArgumentNullException err)
  46.         {
  47.             Console.WriteLine(err.Message);
  48.         }
  49.         catch (ArgumentException err)
  50.         {
  51.             Console.WriteLine(err.Message);
  52.         }
  53.         catch (OutOfMemoryException err)
  54.         {
  55.             Console.WriteLine(err.Message);
  56.         }
  57.         catch (NotSupportedException err)
  58.         {
  59.             Console.WriteLine(err.Message);
  60.         }
  61.         catch (SecurityException err)
  62.         {
  63.             Console.WriteLine(err.Message);
  64.         }
  65.         catch (RegexMatchTimeoutException err)
  66.         {          
  67.             Console.WriteLine(err.Message);
  68.         }
  69.     }
  70.  
  71.     private static void WriteWords(Dictionary<string, int> words, StreamWriter writer)
  72.     {
  73.         var sortedWords = (from entry in words orderby entry.Value descending select entry).ToDictionary(pair => pair.Key, pair => pair.Value);
  74.         foreach (var item in sortedWords)
  75.         {
  76.             writer.WriteLine(item.Key + " " + item.Value);
  77.         }
  78.     }
  79.  
  80.     private static void CountMatches(StreamReader reader, Dictionary<string, int> words)
  81.     {
  82.         List<string> keys = new List<string>(words.Keys);
  83.         string line;
  84.  
  85.         while ((line = reader.ReadLine()) != null)
  86.         {
  87.             foreach (string key in keys)
  88.             {
  89.                 words[key] += Regex.Matches(line, "\\b" + key + "\\b").Count;
  90.             }
  91.         }
  92.     }
  93.  
  94.     private static void ReadWords(StreamReader reader, Dictionary<string, int> words)
  95.     {
  96.         string line;
  97.         while ((line = reader.ReadLine()) != null)
  98.         {
  99.             string[] word = line.Split();
  100.             foreach (string item in word)
  101.             {
  102.                 words.Add(item, 0);
  103.             }
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment