/// <summary> /// This function checks if the first Character in the word matches one of the /// siblings. And if so it continues to search for the next Character in the word in /// the children of this matching sibling. /// </summary> /// <param name="WLE"></param> /// <param name="Word"></param> /// <returns></returns> private String Contains(WordListEntry WLE, String Word, String WordListWord, Boolean CaseSensitive) { bool Result; Char SearchCharacter; WordListEntry childWLE; Result = false; SearchCharacter = (Word.Length > 0 ? Word[0] : (Char)ASCII.EOT); if (!CaseSensitive && SearchCharacter != (Char)ASCII.EOT) { SearchCharacter = System.Char.ToLower(SearchCharacter); } //Iterate through the children to find the matching Character. int index = WLE.Children.Count; do { index--; childWLE = WLEs[WLE.Children[index]]; Char CompareChar = CaseSensitive ? childWLE.Character : System.Char.ToLower(childWLE.Character); if (CompareChar == SearchCharacter) { Result = true; WLE = childWLE; if (SearchCharacter != (Char)ASCII.EOT) { WordListWord += WLE.Character; } } }while (index > 0 && !Result); //Continue to search for the rest of the Characters in the word (if exist) if (Result && Word.Length > 0 && WLE.Children.Count > 0) { WordListWord = Contains(WLE, Word.Substring(1), WordListWord, CaseSensitive); } if (!Result) { WordListWord = ""; } return(WordListWord); }
private int CompressRebuildTree(WordListWorkEntry WLE) { WordListEntry newWLE; int newID; if (WLE.newID != -1) { return(WLE.newID); } newWLE = new WordListEntry(); newWLE.Character = WLE.Character; newID = WLEs.Count(); WLE.newID = newID; WLEs.Add(newWLE); foreach (int childID in WLE.Children) { newWLE.AddChild(CompressRebuildTree(m_WLEs[childID])); } return(newID); }
/// <summary> /// This function compares the two characters and explores the children if /// there is a match /// </summary> /// <param name="CompareChar"></param> /// <param name="SearchCharacter"></param> /// <param name="Suggestions"></param> /// <param name="WLE"></param> /// <param name="Word"></param> /// <param name="WordListWord"></param> /// <param name="CaseSensitive"></param> /// <param name="Alterations"></param> /// <returns></returns> private void SearchSuggestionCompareAndExplore(Char CompareChar, Char SearchCharacter, List <SuggestionEntry> Suggestions, WordListEntry WLE, String Word, String WordListWord, Boolean CaseSensitive, int Alterations, int MaxAlterations) { String newWordListWord = WordListWord; if (CompareChar == SearchCharacter && CompareChar == (Char)ASCII.EOT && Alterations <= MaxAlterations) { //The end of the word is reached, add it as a suggestion SearchSuggestionAdd(Suggestions, Alterations, WordListWord); } if (Word.Length > 0) { //The characters match. Continue without a penalty to alterations if (CompareChar == SearchCharacter || SearchCharacter == (Char)'?') { SearchSuggestion(Suggestions, WLE, Word.Substring(1), WordListWord + WLE.Character, CaseSensitive, Alterations, MaxAlterations, true); } else { //Explorer the option 'replace character at current position in the word' SearchSuggestion(Suggestions, WLE, Word.Substring(1), WordListWord + WLE.Character, CaseSensitive, Alterations + 1, MaxAlterations, true); } } //Explorer the option 'add character at current position in the word' SearchSuggestion(Suggestions, WLE, Word, WordListWord + WLE.Character, CaseSensitive, Alterations + 1, MaxAlterations, true); }
private bool SearchSuggestion(List <SuggestionEntry> Suggestions, WordListEntry WLE, String Word, String WordListWord, Boolean CaseSensitive, int Alterations, int MaxAlterations, Boolean CheckReplacements) { Char SearchCharacter; WordListEntry childWLE; //Do not continue if we used all our alterations if (Alterations > MaxAlterations) { return(false); } //Do not continue if this element is the End-Of-Text mark. We found a possible suggestion if (WLE.Character == (Char)ASCII.EOT) { Alterations += Word.Length; if (Alterations <= MaxAlterations) { SearchSuggestionAdd(Suggestions, Alterations, WordListWord.Substring(0, WordListWord.Length - 1)); } } //Do not continue if this element doesn't have children -> nothing to compare against. if (WLE.Children.Count == 0) { return(false); } //Use the editdistance of the first suggestion as the maxalterations value if (Suggestions.Count > 0) { MaxAlterations = Suggestions.First().EditDistance; } try { String ReplacementWord; //Check if the parameter Word starts with one of the replacements. If so, start //another comparison branche. if (CheckReplacements) { foreach (Replacement Replacement in m_CharacterReplacements.Replacements) { if (SearchSuggestionCompareReplacement(Word, Replacement.Original)) { ReplacementWord = Replacement.ReplacementString + Word.Substring(Replacement.Original.Length); SearchSuggestion(Suggestions, WLE, ReplacementWord, WordListWord, CaseSensitive, Alterations, MaxAlterations, false); } } } SearchCharacter = (Word.Length > 0 ? Word[0] : (Char)ASCII.EOT); if (!CaseSensitive && SearchCharacter != (Char)ASCII.EOT) { SearchCharacter = System.Char.ToLower(SearchCharacter); } //Iterate through the children int index = WLE.Children.Count; do { index--; childWLE = WLEs[WLE.Children[index]]; Char CompareChar = CaseSensitive ? childWLE.Character : System.Char.ToLower(childWLE.Character); SearchSuggestionCompareAndExplore(CompareChar, SearchCharacter, Suggestions, childWLE, Word, WordListWord, CaseSensitive, Alterations, MaxAlterations); }while (index > 0); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("{0} Exception caught." + e.StackTrace.ToString()); } return(true); }