// METHODS public KeyValuePair <char, float>[] Analyze(string text) { ResetAnalyzer(); text = Alphabet.TextAdapter(text); // count letter amount in text foreach (char letter in text) { if (letterAmount.ContainsKey(letter)) { ++letterAmount[letter]; } } // calculate frequency of each letter and fill in result array int index = 0; KeyValuePair <char, float>[] res = new KeyValuePair <char, float> [letterAmount.Count]; foreach (KeyValuePair <char, int> la in letterAmount) { res[index++] = new KeyValuePair <char, float>(la.Key, (float)System.Math.Round((double)la.Value / text.Length, 3)); } return(res); }
// CONSTRUCTORS public FrequencyAnalyzer(Alphabet alphabet) { letterAmount = new SortedDictionary <char, int>(); this.alphabet = alphabet; }