Advertisement
Guest User

Untitled

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