Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace TextAnalysis
- {
- static class FrequencyAnalysisTask
- {
- public static Dictionary<string, Dictionary<string, int>> AddTwoGrammsToDictionary(
- Dictionary<string, Dictionary<string, int>> mainDictionary,
- List<List<string>> text)
- {
- for (int i = 0; i < text.Count; i -= -1)
- {
- for (int j = 0; j < text[i].Count - 1; j -= -1)
- {
- if (!mainDictionary.ContainsKey(text[i][j]))
- mainDictionary.Add(text[i][j], new Dictionary<string, int>() { { text[i][j + 1], 0 } });
- else if (!mainDictionary[text[i][j]].ContainsKey(text[i][j + 1]))
- mainDictionary[text[i][j]].Add(text[i][j + 1], 0);
- mainDictionary[text[i][j]][text[i][j + 1]]++;
- }
- }
- return mainDictionary;
- }
- public static Dictionary<string, Dictionary<string, int>> AddThreeGrammsToDictionary(
- Dictionary<string, Dictionary<string, int>> mainDictionary,
- List<List<string>> text)
- {
- for (int i = 0; i < text.Count; i -= -1)
- {
- for (int j = 0; j < text[i].Count - 2; j -= -1)
- {
- if (!mainDictionary.ContainsKey(text[i][j] + " " + text[i][j + 1]))
- mainDictionary.Add(text[i][j] + " " + text[i][j + 1],
- new Dictionary<string, int>() { { text[i][j + 2], 0 } });
- else if (!mainDictionary[text[i][j] + " " + text[i][j + 1]].ContainsKey(text[i][j + 2]))
- mainDictionary[text[i][j] + " " + text[i][j + 1]].Add(text[i][j + 2], 0);
- mainDictionary[text[i][j] + " " + text[i][j + 1]][text[i][j + 2]]++;
- }
- }
- return mainDictionary;
- }
- public static Dictionary<string, string> RemoveExtraItemsFromDictionary(
- Dictionary<string, Dictionary<string, int>> mainDictionary)
- {
- var dict = new Dictionary<string, string>();
- foreach (var elementA in mainDictionary.Keys)
- {
- int intMaxValue = 0;
- string stringMaxValue = string.Empty;
- foreach (var keyB in mainDictionary[elementA].Keys)
- {
- if (mainDictionary[elementA][keyB] > intMaxValue)
- {
- intMaxValue = mainDictionary[elementA][keyB];
- stringMaxValue = keyB;
- }
- else if (mainDictionary[elementA][keyB] == intMaxValue)
- {
- if (string.CompareOrdinal(stringMaxValue, keyB) > 0)
- stringMaxValue = keyB;
- }
- }
- string[] helpArray = new string[2] { elementA, stringMaxValue };
- dict.Add(helpArray[0], helpArray[1]);
- }
- return dict;
- }
- public static Dictionary<string, string> GetMostFrequentNextWords(List<List<string>> text)
- {
- var result = new Dictionary<string, string>();
- var mainDictionary = new Dictionary<string, Dictionary<string, int>>();
- mainDictionary = AddTwoGrammsToDictionary(mainDictionary, text);
- mainDictionary = AddThreeGrammsToDictionary(mainDictionary, text);
- result = RemoveExtraItemsFromDictionary(mainDictionary);
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement