/** Optimize this grammar node. */ public void Optimize() { for (int i = 0; i < _arcList.Count; i++) { GrammarArc arc = _arcList[i]; _arcList[i] = OptimizeArc(arc); } // now remove all self-looping empty arcs if (IsEmpty) { for (Int16 i = 0; i < _arcList.Count; i++) { if (i > _arcList.Count - 2) { break; } GrammarArc arc = _arcList[i + 1]; if (this == arc.GrammarNode) { _arcList.RemoveAt(i); } } } }
/** * /// Optimize the given arc. If an arc branches to an empty node that has only one exit, the node can be bypassed by * /// making a new arc that skips the nodes. This can happen multiple times. * * /// @param arc the arc to optimize * /// @return the optimized arc */ GrammarArc OptimizeArc(GrammarArc arc) { GrammarNode nextNode = arc.GrammarNode; while (nextNode.IsEmpty && nextNode._arcList.Count == 1) { GrammarArc nextArc = nextNode._arcList[0]; arc = new GrammarArc(nextArc.GrammarNode, arc.Probability + nextArc.Probability); nextNode = arc.GrammarNode; } return(arc); }