private TreePredictableItem(TreePredictableItem <TNode> parent, TNode node) { _parent = parent; _node = node; _count = node.Count; _isReal = true; }
internal static IEnumerable <IEnumerable <TNode> > FindOrderedItems(IEnumerable <TNode> database) { var seedEnumerator = database.GetEnumerator(); if (seedEnumerator.MoveNext()) { var seedItem = new TreePredictableItem <TNode>(null, seedEnumerator); var queue = new List <TreePredictableItem <TNode> > { seedItem }; while (queue.Count != 0) { var item = queue[0]; queue.RemoveAt(0); if (item._isReal) { yield return(item); if (item._enumerator.MoveNext()) { var nextItem = new TreePredictableItem <TNode>(item._parent, item._enumerator); nextItem.Enqueue(queue); } else { item._enumerator.Dispose(); } if (1 < item._count) { item.MakePotential(); item.Enqueue(queue); } } else { item.ExpandPotentialParent(queue); } } } }
internal static void PrototypeTreePredictions(ScoredTokenPredictionMaker maker, StringTokens tokens, int maxCount) { string ToText(TokenPredictorInfo info) { var text = tokens[info.Token]; var nullIndex = text.IndexOf('\0'); if (nullIndex != -1) { text = text.Substring(0, nullIndex); } return(text); } var roots = maker.GetContextRoots(); Debug.WriteLine(string.Empty); Debug.WriteLine("Prototype tree prediction:"); var enumerable = TreePredictableItem <TokenPredictorInfo> .FindOrderedItems(roots.SortedEnumerable); using (var enumerator = enumerable.GetEnumerator()) { for (var count = 0; count < maxCount && enumerator.MoveNext(); count++) { var text = string.Empty; foreach (var info in enumerator.Current) { text += $"{ToText(info)} "; } Debug.WriteLine(text); } } Debug.WriteLine(string.Empty); }
private void ExpandPotentialParent(List <TreePredictableItem <TNode> > queue) { var item = this; var done = false; while (!done) { var node = item._node; var enumerator = node.GetChildren().GetEnumerator(); if (enumerator.MoveNext()) { var firstNode = enumerator.Current; if (enumerator.MoveNext()) { item = new TreePredictableItem <TNode>(item, enumerator); item.Enqueue(queue); done = true; } else { // Walking down single branch. enumerator.Dispose(); item = new TreePredictableItem <TNode>(item, firstNode); } } else { // No potential next node. enumerator.Dispose(); done = true; } } }
internal TreePredictableItem(TreePredictableItem <TNode> parent, IEnumerator <TNode> enumerator) : this(parent, enumerator.Current) { _enumerator = enumerator; }