Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- namespace TextAnalysis
- {
- static class FrequencyAnalysisTask
- {
- public static void GetBigrams(List<List<string>> text, Dictionary<string, Dictionary<string, int>> result)
- {
- foreach (var words in text)
- {
- for (int i = 0; i < words.Count - 1; i++)
- {
- if (!result.ContainsKey(words[i]))
- result.Add(words[i], new Dictionary<string, int>());
- if (!result[words[i]].ContainsKey(words[i + 1]))
- result[words[i]].Add(words[i + 1], 1);
- else
- result[words[i]][words[i + 1]]++;
- }
- }
- }
- public static void GetTrigrams(List<List<string>> text, Dictionary<string, Dictionary<string, int>> result)
- {
- foreach (var words in text)
- {
- for (int i = 0; i < words.Count - 2; i++)
- {
- if (!result.ContainsKey(words[i] + " " + words[i + 1]))
- result.Add(words[i] + " " + words[i + 1], new Dictionary<string, int>());
- if (!result[words[i] + " " + words[i + 1]].ContainsKey(words[i + 2]))
- result[words[i] + " " + words[i + 1]].Add(words[i + 2], 1);
- else
- result[words[i] + " " + words[i + 1]][words[i + 2]]++;
- }
- }
- }
- public static Dictionary<string, string> GetMostFrequentNextWords(List<List<string>> text)
- {
- var result = new Dictionary<string, Dictionary<string, int>>();
- var total = new Dictionary<string, string>();
- GetBigrams(text, result);
- GetTrigrams(text, result);
- return FrequencyDetermination(result, total);
- }
- private static Dictionary<string, string> FrequencyDetermination
- (Dictionary<string, Dictionary<string, int>> result, Dictionary<string, string> total)
- {
- foreach (var key in result.Keys)
- {
- string mostFrequentWord = null;
- int maxCount = 0;
- foreach (var innerKey in result[key])
- {
- if (innerKey.Value > maxCount || innerKey.Value == maxCount &&
- string.CompareOrdinal(innerKey.Key, mostFrequentWord) < 0)
- {
- maxCount = innerKey.Value;
- mostFrequentWord = innerKey.Key;
- }
- }
- total.Add(key, mostFrequentWord);
- }
- return total;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement