Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System;
- namespace TextAnalysis
- {
- static class FrequencyAnalysisTask
- {
- public static Dictionary<string, string> GetDictWithGramm(List<List<string>> text)
- {
- var result = new Dictionary<string, string>();
- for (int j = 0; j < text.Count; j++)
- {
- var sentence = text[j];
- for (int i = 0; i < sentence.Count - 1; i++)
- {
- var keyB = sentence[i];
- if (result.ContainsKey(keyB))
- {
- result[keyB] += " " + sentence[i + 1];
- }
- else
- {
- result[keyB] = sentence[i + 1];
- }
- }
- for (int i = 0; i < sentence.Count - 2; i++)
- {
- var keyT = sentence[i] + " " + sentence[i + 1];
- if (result.ContainsKey(keyT))
- {
- result[keyT] += " " + sentence[i + 2];
- }
- else
- result[keyT] = sentence[i + 2];
- }
- }
- return result;
- }
- public static Dictionary<string, string> GetMostFrequentNextWords(List<List<string>> text)
- {
- var result = GetDictWithGramm(text);
- var keys = new List<string>();
- foreach (var key in result)
- keys.Add(key.Key);
- for (int i = 0; i < keys.Count; i++)
- {
- string key = keys[i];
- var words = result[key].Split();
- var dictWAndF = new Dictionary<string, int>();
- for (int j = 0; j < words.Length; j++)
- {
- string word = words[j];
- if (!dictWAndF.ContainsKey(word)) dictWAndF[word] = 0;
- dictWAndF[word]= dictWAndF[word]+1;
- }
- result[key] = GetContinue(dictWAndF);
- }
- return result;
- }
- public static string GetContinue(Dictionary<string, int> dict)
- {
- var i = 0;
- string continueW = "";
- foreach (var item in dict)
- {
- if (item.Value > i)
- {
- i = item.Value;
- continueW = item.Key;
- }
- else
- {
- if ((item.Value == i) && (String.CompareOrdinal(continueW, item.Key) > 0))
- {
- continueW = item.Key;
- i = item.Value;
- }
- }
- }
- return continueW;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement