public Dictionary<string, double> GeneratePossibleWords(string word) { Dictionary<string, double> result = new Dictionary<string, double>(); // substitution for (int i = 0; i < word.Length; i++) { foreach (string charItem in alphabet) { string edited = String.Copy(word).Remove(i, 1).Insert(i, charItem); if (dictionary.FindWord(edited)) { double prop = this.CalculateProbability(EditOperation.Substitution, word[i], charItem[0]); if (!result.ContainsKey(edited)) { result.Add(edited, prop); } else if (prop > result[edited]) { result[edited] = prop; } } } } // deletions for (int i = 0; i < word.Length; i++) { string edited = String.Copy(word).Remove(i, 1); if (dictionary.FindWord(edited)) { char prev = (i - 1) < 0 ? ' ' : word[i]; double prop = this.CalculateProbability(EditOperation.Deletion,prev,word[i]); if (!result.ContainsKey(edited)) { result.Add(edited, prop); } else if (prop > result[edited]) { result[edited] = prop; } } } bool found = false; // insertions for (int i = 0; i <= word.Length; i++) { foreach (string item in alphabetWithSpace) { string edited = String.Copy(word).Insert(i, item); if (item == " ") { string tr = edited.Trim(); if (tr != word) { string[] parts = tr.Split(space); foreach (string part in parts) { if (dictionary.FindWord(part)) { found = true; } else { found = false; break; } } } } if (found || dictionary.FindWord(edited)) { char prev = (i - 1) < 0 ? ' ' : word[i-1]; double prop = this.CalculateProbability(EditOperation.Insertion, prev, item[0]); if (!result.ContainsKey(edited)) { result.Add(edited, prop); } else if (prop > result[edited]) { result[edited] = prop; } found = false; } } } // transposition for (int i = 0; i < word.Length - 1; i++) { string newString = String.Copy(word); string charItem = newString[i].ToString(); string edited = newString.Remove(i, 1).Insert(i + 1, charItem); if (dictionary.FindWord(edited)) { double prop = this.CalculateProbability(EditOperation.Transposition, word[i], word[i + 1]); if (!result.ContainsKey(edited)) { result.Add(edited, prop); } else if(prop > result[edited]) { result[edited] = prop; } } } return result; }
protected Dictionary<char, List<char>> ParsePairs(string[] pairs) { Dictionary<char, List<char>> result = new Dictionary<char, List<char>>(); foreach (string pair in pairs) { string[] data = pair.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries); if (result.ContainsKey(data[0][0])) { result[data[0][0]].Add(data[1][0]); } else { result.Add(data[0][0], new List<char>() { data[1][0] }); } } return result; }