private static bool validateSuffixStrings( GSuffixTree tree, out List <int> failedLeafNodeIds) { var edgeStringDicts = new Stack <EdgeStringDict>(); // Step 1: Populate edgeStrings with data from child edges of the root node. // Track any leaves that are immediate children of the root node. var leafEdgeStringDicts = new List <EdgeStringDict>(); foreach (GstEdge edge in tree.Root.ChildEdges()) { var edgeStringDict = new EdgeStringDict(edge, new Dictionary <int, string>()); foreach (int wordNum in edge.WordNums()) { edgeStringDict.Item2[wordNum] = edge.GetText(); edgeStringDicts.Push(edgeStringDict); } if (!edge.ChildNode.HasChildEdges()) { Console.WriteLine(String.Format( "SuffixTreeTest: Found a leaf edge adjacent to the root: E{0:d}", edge.Id)); leafEdgeStringDicts.Add(edgeStringDict); } } // Step 2: Walk the tree, adding the remaining edges. Keep track of leaf edges. // Also keep a running record of accumulated text for each edge. while (edgeStringDicts.Count > 0) { EdgeStringDict edgeStringDict = edgeStringDicts.Pop(); foreach (GstEdge childEdge in edgeStringDict.Item1.ChildNode.ChildEdges()) { EdgeStringDict newEdgeStringDict = new EdgeStringDict(childEdge, new Dictionary <int, string>()); foreach (int wordNum in childEdge.WordNums()) { newEdgeStringDict.Item2[wordNum] = edgeStringDict.Item2[wordNum] + childEdge.GetText(); } edgeStringDicts.Push(newEdgeStringDict); if (!childEdge.ChildNode.HasChildEdges()) { Console.WriteLine(String.Format( "SuffixTreeTest: Found a leaf not adjacent to the root: E{0:s}", newEdgeStringDict.Item1.Id)); leafEdgeStringDicts.Add(newEdgeStringDict); } } } // Step 3: Inspect the leaf edge content (i.e., strings). Keep track of failed leaf nodes failedLeafNodeIds = new List <int>(); foreach (var leafEdgeStringDict in leafEdgeStringDicts) { // Accumulated string should equal the corresponding substring of tree.Text. GstEdge edge = leafEdgeStringDict.Item1; foreach (int wordNum in leafEdgeStringDict.Item2.Keys) { int len = leafEdgeStringDict.Item2[wordNum].Length; string pathStr = leafEdgeStringDict.Item2[wordNum]; string textStr = tree.GetRangeString(wordNum, tree.GetWord(wordNum).Length - len, tree.GetWord(wordNum).Length - 1); string formatSpec2 = "{2" /* + "," + tree.GetWord(0).Length.ToString() */ + ":s}"; string formatSpec3 = "{3" /* + "," + tree.GetWord(0).Length.ToString() */ + ":s}"; string formatStr = "SuffixTreeTest: Leaf edge #{0:d}, word#{1:d}. " + String.Format("Comparing \"{0:s}\" with \"{1:s}\"", formatSpec2, formatSpec3); Console.WriteLine(formatStr, edge.Id, wordNum, pathStr, textStr); if (pathStr != textStr) { failedLeafNodeIds.Add(leafEdgeStringDict.Item1.ChildNode.Id); break; } } } return(failedLeafNodeIds.Count() == 0); }
private static bool validateSuffixStrings( GSuffixTree tree, out List<int> failedLeafNodeIds) { var edgeStringDicts = new Stack<EdgeStringDict>(); // Step 1: Populate edgeStrings with data from child edges of the root node. // Track any leaves that are immediate children of the root node. var leafEdgeStringDicts = new List<EdgeStringDict>(); foreach (GstEdge edge in tree.Root.ChildEdges()) { var edgeStringDict = new EdgeStringDict(edge, new Dictionary<int, string>()); foreach (int wordNum in edge.WordNums()) { edgeStringDict.Item2[wordNum] = edge.GetText(); edgeStringDicts.Push(edgeStringDict); } if (!edge.ChildNode.HasChildEdges()) { Console.WriteLine(String.Format( "SuffixTreeTest: Found a leaf edge adjacent to the root: E{0:d}", edge.Id)); leafEdgeStringDicts.Add(edgeStringDict); } } // Step 2: Walk the tree, adding the remaining edges. Keep track of leaf edges. // Also keep a running record of accumulated text for each edge. while (edgeStringDicts.Count > 0) { EdgeStringDict edgeStringDict = edgeStringDicts.Pop(); foreach (GstEdge childEdge in edgeStringDict.Item1.ChildNode.ChildEdges()) { EdgeStringDict newEdgeStringDict = new EdgeStringDict(childEdge, new Dictionary<int, string>()); foreach (int wordNum in childEdge.WordNums()) { newEdgeStringDict.Item2[wordNum] = edgeStringDict.Item2[wordNum] + childEdge.GetText(); } edgeStringDicts.Push(newEdgeStringDict); if (!childEdge.ChildNode.HasChildEdges()) { Console.WriteLine(String.Format( "SuffixTreeTest: Found a leaf not adjacent to the root: E{0:s}", newEdgeStringDict.Item1.Id)); leafEdgeStringDicts.Add(newEdgeStringDict); } } } // Step 3: Inspect the leaf edge content (i.e., strings). Keep track of failed leaf nodes failedLeafNodeIds = new List<int>(); foreach (var leafEdgeStringDict in leafEdgeStringDicts) { // Accumulated string should equal the corresponding substring of tree.Text. GstEdge edge = leafEdgeStringDict.Item1; foreach (int wordNum in leafEdgeStringDict.Item2.Keys) { int len = leafEdgeStringDict.Item2[wordNum].Length; string pathStr = leafEdgeStringDict.Item2[wordNum]; string textStr = tree.GetRangeString(wordNum, tree.GetWord(wordNum).Length - len, tree.GetWord(wordNum).Length - 1); string formatSpec2 = "{2" /* + "," + tree.GetWord(0).Length.ToString() */ + ":s}"; string formatSpec3 = "{3" /* + "," + tree.GetWord(0).Length.ToString() */ + ":s}"; string formatStr = "SuffixTreeTest: Leaf edge #{0:d}, word#{1:d}. " + String.Format("Comparing \"{0:s}\" with \"{1:s}\"", formatSpec2, formatSpec3); Console.WriteLine(formatStr, edge.Id, wordNum, pathStr, textStr); if (pathStr != textStr) { failedLeafNodeIds.Add(leafEdgeStringDict.Item1.ChildNode.Id); break; } } } return (failedLeafNodeIds.Count() == 0); }