Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace TextAnalysis
  6. {
  7. static class FrequencyAnalysisTask
  8. {
  9. public static Dictionary<string, string> GetMostFrequentNextWords(List<List<string>> text)
  10. {
  11. var temp = new List<Tuple<string, string>>();
  12.  
  13. foreach (var sentence in text)
  14. {
  15. if (sentence.Count == 2)
  16. {
  17. temp.Add(Tuple.Create(sentence[0], sentence[1]));
  18. }
  19. else
  20. {
  21. for (int i = 0; i < sentence.Count - 2; i++)
  22. {
  23. var ngrams = sentence.Skip(i).Take(3).ToArray();
  24. temp.Add(Tuple.Create(ngrams[0], ngrams[1]));
  25. temp.Add(Tuple.Create(string.Join(" ", ngrams.Take(2)), ngrams[2]));
  26. if (i == sentence.Count - 3) temp.Add(Tuple.Create(ngrams[1], ngrams[2]));
  27. }
  28. }
  29. }
  30. var result = temp.GroupBy(tuple => tuple.Item1)
  31. .Select(tuple => new
  32. {
  33. tuple.Key,
  34. Value = tuple.Select(item => item.Item2)
  35. .GroupBy(x => x).ToList().
  36. Select(y => new { y.Key, Count = y.Count() })
  37. .OrderByDescending(x => x.Count)
  38. .ThenBy(x => x.Key,StringComparer.Ordinal)
  39. .Select(p => p.Key)
  40. .First()
  41. }).ToDictionary(x => x.Key, x => x.Value);
  42.  
  43. return result;
  44. }
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement