/* * This method updates the {@code Key} when a word gets removed from the {@code Stack} * * Algorithm: * 1. Pop one {@code Index} off the {@code Stack} * 2. Re-input all other pairs still on the {@code Stack} to ensure that no older word had the same key/value pair * * @param stack the current {@code Stack} of words that have been solved * @param words {@code List} of all the encrypted words * @return the {@code Index} that got popped off the {@code Stack} */ public Index UpdateKey(IndexStack stack, List <string> words) { var removed = stack.Pop(); RemoveFromKey(words[removed.GetWi()]); foreach (var index in stack) { var word = words[index.GetWi()]; var eWord = MyDictionary.GetInstance().GetWordSubset(word.Length, StringPattern.GetPattern(word)).ElementAt(index.GetMi()); AddToKey(word, eWord); } return(removed); }
/* * This method adds a word to the dictionary * * @param word word to be added to the dictionary */ public void AddWord(string word) { String pattern = StringPattern.GetPattern(word); int key = word.Length; if (!_words.ContainsKey(key)) { _words.Add(key, new Dictionary <string, HashSet <string> >()); } if (!_words[key].ContainsKey(pattern)) { _words[key].Add(pattern, new HashSet <string>()); } _words[key][pattern].Add(word); }
/* * This method checks pairs of an encrypted word with its set of decryptions against the {@code Key} * * @param word encrypted word * @param index starting index in the list of decrypted matches * @return the index of a pair that was successfully inserted into the key */ private int PickMatch(string word, int index) { var mySet = _dictionary.GetWordSubset(word.Length, StringPattern.GetPattern(word)); if (!mySet.Any()) { throw new SystemException("No matches found for: " + word); } for (var i = index; i < mySet.Count; i++) { if (_key.AddToKey(word, mySet.ElementAt(i))) { return(i); } } return(-1); }