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 Dictionary<string, string> GetMostFrequentNextWords(List<List<string>> text)
- {
- var result = new Dictionary<string, string>();
- var test = new Dictionary<string, Dictionary<string, int>>();
- var test2 = new Dictionary<string, Dictionary<string, int>>();
- foreach (var line in text)
- {
- string firstWord = null;
- string secondWord = null;
- string firstTwoWords = null;
- foreach (var thirdWord in line)
- {
- if (firstWord == null)
- {
- firstWord = thirdWord;
- continue;
- }
- else if (secondWord == null)
- {
- secondWord = thirdWord;
- firstTwoWords = firstWord + " " + secondWord;
- if (!test.ContainsKey(firstWord)) test[firstWord] = new Dictionary<string, int>();
- if (!test[firstWord].ContainsKey(secondWord)) test[firstWord][secondWord] = 0;
- test[firstWord][secondWord]++;
- continue;
- }
- firstWord = secondWord;
- secondWord = thirdWord;
- if (!test.ContainsKey(firstWord)) test[firstWord] = new Dictionary<string, int>();
- if (!test[firstWord].ContainsKey(secondWord)) test[firstWord][secondWord] = 0;
- test[firstWord][secondWord]++;
- if (!test2.ContainsKey(firstTwoWords)) test2[firstTwoWords] = new Dictionary<string, int>();
- if (!test2[firstTwoWords].ContainsKey(thirdWord)) test2[firstTwoWords][thirdWord] = 0;
- test2[firstTwoWords][thirdWord]++;
- firstTwoWords = firstWord + " " + secondWord;
- }
- }
- foreach (var firstWord in test.Keys)
- {
- int maxWordCount = 0;
- string maxWord = "";
- foreach (var secondWord in test[firstWord].Keys)
- {
- if (test[firstWord][secondWord] > maxWordCount)
- {
- maxWordCount = test[firstWord][secondWord];
- maxWord = secondWord;
- }
- else if (test[firstWord][secondWord] == maxWordCount)
- {
- if (string.CompareOrdinal(maxWord, secondWord) > 0) maxWord = secondWord;
- }
- }
- result[firstWord] = maxWord;
- }
- foreach (var firstTwoWords in test2.Keys)
- {
- int maxWordCount = 0;
- string maxWord = "";
- foreach (var thirdWord in test2[firstTwoWords].Keys)
- {
- if (test2[firstTwoWords][thirdWord] > maxWordCount)
- {
- maxWordCount = test2[firstTwoWords][thirdWord];
- maxWord = thirdWord;
- }
- else if (test2[firstTwoWords][thirdWord] == maxWordCount)
- {
- if (string.CompareOrdinal(maxWord, thirdWord) > 0) maxWord = thirdWord;
- }
- }
- result[firstTwoWords] = maxWord;
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement