/// <summary> /// This function returns true if both elements /// 1. contain the same character /// 2. have the same children /// 3. have the same siblings /// </summary> /// <param name="Element1"></param> /// <param name="?"></param> /// <returns></returns> private Boolean CompareElements(WordListWorkEntry WLE1, WordListWorkEntry WLE2) { Boolean Result = true; int index; //compare character Result = (WLE1.Character == WLE2.Character /* && WLE1.Depth == WLE2.Depth*/); if (!Result) { return(false); } //compare children if (WLE1.Children.Count != WLE2.Children.Count) { return(false); } index = WLE1.Children.Count; while (index > 0 && Result) { index--; if (WLE1.Children[index] != WLE2.Children[index]) { Result = false; } } return(Result); }
/// <summary> /// This function builds the wordlist by processing the textbased file (word per row). /// </summary> /// <param name="Filename"></param> public void Build(string Filename) { StreamReader source; String word; WordListWorkEntry WLEroot; System.Diagnostics.Debug.WriteLine("Building WordList"); System.Diagnostics.Debug.Indent(); source = new StreamReader(Filename); //clear list m_WLEs.Clear(); //create the top-node WLEroot = new WordListWorkEntry(); WLEroot.Character = (Char)ASCII.SOT; //start of text WLEroot.Depth = 0; WLEroot.Parent = -1; WLEroot.ID = 0; m_WLEs.Add(WLEroot); //read the lines while ((word = source.ReadLine()) != null) { //check if the wordlist doens't already contains the given word if (!Contains(word)) { BuildAddWord(WLEroot, word, word, 0); } } source.Close(); System.Diagnostics.Debug.WriteLine("Number of elements: " + m_WLEs.Count); System.Diagnostics.Debug.Unindent(); //compress tree Compress(); }
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 add the word/sequence to the Wordlist /// </summary> /// <param name="WLE"></param> /// <param name="word"></param> private int BuildAddWord(WordListWorkEntry WLE, String word, String orgWord, int depth) { bool Continue; WordListWorkEntry newWLE; WordListWorkEntry childWLE; Char Character; int newDepth = 0; depth++; if (word.Length == 0) { Character = (char)ASCII.EOT; } else { Character = word[0]; } Continue = true; int index = WLE.Children.Count; while (index > 0 && Continue) { index--; childWLE = m_WLEs[WLE.Children[index]]; if (childWLE.Character == Character) { Continue = false; //Continue with the children of this item if (Character != (Char)ASCII.EOT && word.Length > 0) { if (word.Length > 0) { word = word.Substring(1); } newDepth = Math.Max(WLE.Depth, BuildAddWord(childWLE, word, orgWord, depth)); } else { newDepth = 1; } } } if (Continue) { //no corresponding item found, create one and continue newWLE = new WordListWorkEntry(); newWLE.Character = Character; newWLE.Depth = 0; newWLE.ID = m_WLEs.Count; newWLE.Parent = WLE.Parent; m_WLEs.Add(newWLE); WLE.AddChild(newWLE.ID); //Continue with the next Character if (Character != (Char)ASCII.EOT) { if (word.Length > 0) { word = word.Substring(1); } newDepth = BuildAddWord(newWLE, word, orgWord, depth); } else { newDepth = 1; } } WLE.Depth = Math.Max(WLE.Depth, newDepth); return(WLE.Depth + 1); }