private void setSuffixLink(StNode node, StNode suffixNode) { if ((node != null) && (node != root)) { if (node.SuffixNode == null) { StUtil.WriteLine(StVerbosityLevel.Verbose, String.Format( " New suffix link from N{0:d} to N{1:d}", node.Id, suffixNode.Id)); } else { if (node.SuffixNode.Id == suffixNode.Id) { StUtil.WriteLine(StVerbosityLevel.Verbose, String.Format( " Suffix link (N{0:d} to N{1:d}) retaining same value", node.Id, node.SuffixNode.Id)); } else { StUtil.WriteLine(StVerbosityLevel.Verbose, String.Format( " Suffix link (N{0:d} to N{1:d}) set to new value (N{0:d} to N{2:d})", node.Id, node.SuffixNode.Id, suffixNode.Id)); } } node.SuffixNode = suffixNode; } }
private void incrSuffix(ref StSuffix active) { int origNodeId, begin, end; origNodeId = active.OriginNode.Id; begin = active.BeginIndex; end = active.EndIndex; if (active.OriginNode.IsRoot()) { active.BeginIndex++; } else { active.OriginNode = active.OriginNode.SuffixNode; } active.Canonicalize(); if (origNodeId != active.OriginNode.Id || begin != active.BeginIndex || end != active.EndIndex) { StUtil.WriteLine(StVerbosityLevel.Verbose, String.Format( " incrSuffix: Active suffix changed from {0:s} to {1:s}", StSuffix.ToSuffixString(origNodeId, begin, end), StSuffix.ToSuffixString(active.OriginNode.Id, active.BeginIndex, active.EndIndex))); } }
// Rule 1: Try to find matching edge for the parent node. private ExtensionResult extendSuffixByRuleOne( ref StSuffix active, ref StNode parentNode, int endIndex) { if (active.IsExplicit) { StEdge edge = active.OriginNode.GetChildEdge(text[endIndex]); if (edge != null && edge.IsSet()) { return(ExtensionResult.Done); } } else // active suffix is implicit { StEdge edge = active.OriginNode.GetChildEdge(text[active.BeginIndex]); int span = active.EndIndex - active.BeginIndex; if (text[edge.BeginIndex + span + 1] == text[endIndex]) { return(ExtensionResult.Done); } StUtil.WriteLine(StVerbosityLevel.Verbose, String.Format( " Rule #1: About to split edge E{0:d} (\"{1:s}\") at suffix {2:s}", edge.Id, edge.GetText(), active.ToString())); parentNode = edge.Split(active); } return(ExtensionResult.NotDone); }
// Rule 2: Create a new edge and add it to the tree at the parent's position. // Part of this is inserting the new edge into the hash table, // and creating a suffix link to the new node from the last one visited. private void extendSuffixByRuleTwo( ref StSuffix active, StNode parentNode, ref StNode prevParentNode, int endIndex) { StEdge newEdge = new StEdge(this, parentNode, endIndex, this.text.Length - 1); newEdge.Add(); StUtil.WriteLine(StVerbosityLevel.Verbose, String.Format( " Rule #2: New edge E{0:d} (\"{1:s}\") connects N{2:d} (old parent) to N{3:d} (new child)", newEdge.Id, newEdge.GetText(), newEdge.ParentNode.Id, newEdge.ChildNode.Id )); setSuffixLink(prevParentNode, parentNode); prevParentNode = parentNode; }
public SuffixTree(string text) { this.text = text; this.root = new StNode(this, null); StUtil.WriteLine(StVerbosityLevel.Verbose, "Creating the active (longest proper) suffix pointer"); StSuffix active = new StSuffix(this, root, 0, InfiniteIndex); for (int endIndex = 0; endIndex < text.Length; endIndex++) { StUtil.WriteLine(StVerbosityLevel.Verbose, this.ToString()); StUtil.WriteLine(StVerbosityLevel.Verbose, String.Format( "Calling extendSuffixes() with endIndex = {0:d} ('{1:c}') and active suffix = {2:s}", endIndex, text[endIndex], active.ToString())); extendSuffixes(ref active, endIndex); } StUtil.Write(StVerbosityLevel.Normal, this.ToString()); }
public StNode Split(StSuffix s) { Remove(); StEdge newEdge = new StEdge(tree, s.OriginNode, BeginIndex, BeginIndex + s.Span); newEdge.Add(); newEdge.ChildNode.SuffixNode = s.OriginNode; BeginIndex += s.Span + 1; ParentNode = newEdge.ChildNode; Add(); StUtil.WriteLine(StVerbosityLevel.Normal, String.Format( " Split E{0:d} into E{1:d} + E{0:d} = \"{2:s}\" + \"{3:s}\"", Id, newEdge.Id, newEdge.GetText(), this.GetText() )); return(newEdge.ChildNode); }