Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace TextAnalysis
  5. {
  6. static class FrequencyAnalysisTask
  7. {
  8. public static Dictionary<string, Dictionary<string, int>> AddTwoGrammsToDictionary(
  9. Dictionary<string, Dictionary<string, int>> mainDictionary,
  10. List<List<string>> text)
  11. {
  12. for (int i = 0; i < text.Count; i -= -1)
  13. {
  14. for (int j = 0; j < text[i].Count - 1; j -= -1)
  15. {
  16. if (!mainDictionary.ContainsKey(text[i][j]))
  17. mainDictionary.Add(text[i][j], new Dictionary<string, int>() { { text[i][j + 1], 0 } });
  18. else if (!mainDictionary[text[i][j]].ContainsKey(text[i][j + 1]))
  19. mainDictionary[text[i][j]].Add(text[i][j + 1], 0);
  20. mainDictionary[text[i][j]][text[i][j + 1]]++;
  21. }
  22. }
  23. return mainDictionary;
  24. }
  25.  
  26. public static Dictionary<string, Dictionary<string, int>> AddThreeGrammsToDictionary(
  27. Dictionary<string, Dictionary<string, int>> mainDictionary,
  28. List<List<string>> text)
  29. {
  30. for (int i = 0; i < text.Count; i -= -1)
  31. {
  32. for (int j = 0; j < text[i].Count - 2; j -= -1)
  33. {
  34. if (!mainDictionary.ContainsKey(text[i][j] + " " + text[i][j + 1]))
  35. mainDictionary.Add(text[i][j] + " " + text[i][j + 1],
  36. new Dictionary<string, int>() { { text[i][j + 2], 0 } });
  37.  
  38. else if (!mainDictionary[text[i][j] + " " + text[i][j + 1]].ContainsKey(text[i][j + 2]))
  39. mainDictionary[text[i][j] + " " + text[i][j + 1]].Add(text[i][j + 2], 0);
  40. mainDictionary[text[i][j] + " " + text[i][j + 1]][text[i][j + 2]]++;
  41. }
  42. }
  43. return mainDictionary;
  44. }
  45.  
  46. public static Dictionary<string, string> RemoveExtraItemsFromDictionary(
  47. Dictionary<string, Dictionary<string, int>> mainDictionary)
  48. {
  49. var dict = new Dictionary<string, string>();
  50. foreach (var elementA in mainDictionary.Keys)
  51. {
  52. int intMaxValue = 0;
  53. string stringMaxValue = string.Empty;
  54. foreach (var keyB in mainDictionary[elementA].Keys)
  55. {
  56. if (mainDictionary[elementA][keyB] > intMaxValue)
  57. {
  58. intMaxValue = mainDictionary[elementA][keyB];
  59. stringMaxValue = keyB;
  60. }
  61. else if (mainDictionary[elementA][keyB] == intMaxValue)
  62. {
  63. if (string.CompareOrdinal(stringMaxValue, keyB) > 0)
  64. stringMaxValue = keyB;
  65. }
  66. }
  67. string[] helpArray = new string[2] { elementA, stringMaxValue };
  68. dict.Add(helpArray[0], helpArray[1]);
  69. }
  70. return dict;
  71. }
  72.  
  73. public static Dictionary<string, string> GetMostFrequentNextWords(List<List<string>> text)
  74. {
  75. var result = new Dictionary<string, string>();
  76.  
  77. var mainDictionary = new Dictionary<string, Dictionary<string, int>>();
  78. mainDictionary = AddTwoGrammsToDictionary(mainDictionary, text);
  79. mainDictionary = AddThreeGrammsToDictionary(mainDictionary, text);
  80. result = RemoveExtraItemsFromDictionary(mainDictionary);
  81.  
  82. return result;
  83. }
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement