Advertisement
PavloSerg

Untitled

Feb 11th, 2023
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1.  
  2. using System.Text.RegularExpressions;
  3.  
  4. class TextParser
  5. {
  6. public static string[] GetRussianWords(string text)
  7. {
  8. Regex wordRegex = new Regex(@"[а-яА-Я]+");
  9. return wordRegex
  10. .Matches(text)
  11. .Select(x=> x.Value)
  12. .ToArray();
  13. }
  14. public static string[] GetWords(string text)
  15. {
  16.  
  17. return text.Split(new char[] { ',', '!', '.', '\"', '\'', ',', '-', ':', '\n', '\r', '-', ' ', ' ', '-', (char)56 },
  18. StringSplitOptions.RemoveEmptyEntries);
  19. }
  20. }
  21.  
  22. class TextUtils
  23. {
  24. public static int GetUnicueWordsCount(IDictionary<string, int> collection, IEnumerable<string> words)
  25. {
  26. foreach (var word in words)
  27. {
  28. if (collection.ContainsKey(word))
  29. {
  30. collection[word]++;
  31. }
  32. else
  33. {
  34. collection[word] = 1;
  35. }
  36. }
  37. var tenMostWords = collection
  38. .OrderByDescending(x => x.Value)
  39. .Take(10)
  40. .ToArray();
  41. //Console.WriteLine(String.Join(" ", tenMostWords));//.Select(x => (byte)x.Key[0])));
  42. return collection.Count;
  43. }
  44. }
  45. class Program
  46. {
  47.  
  48. static void Main(string[] args)
  49. {
  50. IDictionary<string, int>[] collections = new IDictionary<string, int>[]
  51. {
  52. new SortedList<string, int>(),
  53. new SortedDictionary<string, int>(),
  54. new Dictionary<string, int>(),
  55.  
  56. };
  57. var text = File.ReadAllText("Tolstoy_Lev_Voyna_i_mir_1-2.txt");
  58. var words = TextParser.GetWords(text);
  59. foreach (var collection in collections)
  60. {
  61. var startTime = DateTime.Now;
  62. var unicueWordsCount = TextUtils
  63. .GetUnicueWordsCount(new Dictionary<string, int>(), words);
  64. var endTime = DateTime.Now;
  65. Console.WriteLine($"Collection: {collection} | Time: {(endTime-startTime).Milliseconds}");
  66.  
  67. }
  68.  
  69.  
  70.  
  71.  
  72. //Console.WriteLine(String.Join(" ", words));
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement