Advertisement
cap7ainjack

WordCount_v2

Oct 4th, 2015
1,174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10.  
  11. namespace Hmwrk_6_3
  12. {
  13. class Program
  14. {
  15. static void Main(string[] args)
  16. {
  17. SortedDictionary<string,int> dictionary = new SortedDictionary<string, int>();
  18. string inputWords = File.ReadAllText("../../words.txt");
  19. string[] words = inputWords.Split(new string[] {Environment.NewLine}, StringSplitOptions.None);
  20.  
  21. String pattern = @"[a-zA-Z']+";
  22. Regex regex = new Regex(pattern);
  23.  
  24.  
  25. using (var reader = new StreamReader("../../text.txt"))
  26. {
  27. string currentSentcence = reader.ReadLine();
  28.  
  29. while (currentSentcence !=null)
  30. {
  31.  
  32. foreach (Match match in regex.Matches(currentSentcence))
  33. {
  34. for (int i = 0; i < words.Length; i++)
  35. {
  36.  
  37. if (match.Value.ToLower() == words[i] && !(dictionary.ContainsKey(words[i])))
  38. {
  39. dictionary.Add(words[i], 1);
  40. }
  41.  
  42.  
  43. else if (match.Value.ToLower() == words[i])
  44. {
  45. dictionary[words[i]]++;
  46. }
  47.  
  48. }
  49.  
  50. }
  51. currentSentcence = reader.ReadLine();
  52. }
  53.  
  54. foreach (var item in dictionary.OrderByDescending(key => key.Value))
  55. {
  56. Console.WriteLine("{0} - {1}", item.Key, item.Value);
  57. }
  58.  
  59. }
  60. }
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement