/// <summary>
 /// Checks the node's ancestors to find the highest ancestor with the
 /// same
 /// <c>headWordNode</c>
 /// as this node.
 /// </summary>
 public virtual Edu.Stanford.Nlp.Trees.TreeGraphNode HighestNodeWithSameHead()
 {
     Edu.Stanford.Nlp.Trees.TreeGraphNode node = this;
     while (true)
     {
         Edu.Stanford.Nlp.Trees.TreeGraphNode parent = SafeCast(((Edu.Stanford.Nlp.Trees.TreeGraphNode)node.Parent()));
         if (parent == null || parent.HeadWordNode() != node.HeadWordNode())
         {
             return(node);
         }
         node = parent;
     }
 }
 /// <summary>
 /// <inheritDoc/>
 ///
 /// </summary>
 public override void SetChildren <_T0>(IList <_T0> childTreesList)
 {
     if (childTreesList == null || childTreesList.IsEmpty())
     {
         SetChildren(ZeroTgnChildren);
     }
     else
     {
         int leng = childTreesList.Count;
         Edu.Stanford.Nlp.Trees.TreeGraphNode[] childTrees = new Edu.Stanford.Nlp.Trees.TreeGraphNode[leng];
         Sharpen.Collections.ToArray(childTreesList, childTrees);
         SetChildren(childTrees);
     }
 }
        /// <summary>
        /// Create a new
        /// <c>TreeGraphNode</c>
        /// having the same tree
        /// structure and label values as an existing tree (but no shared
        /// storage).  Operates recursively to construct an entire
        /// subtree.
        /// </summary>
        /// <param name="t">the tree to copy</param>
        /// <param name="parent">the parent node</param>
        protected internal TreeGraphNode(Tree t, Edu.Stanford.Nlp.Trees.TreeGraphNode parent)
        {
            this.parent = parent;
            Tree[] tKids   = t.Children();
            int    numKids = tKids.Length;

            children = new Edu.Stanford.Nlp.Trees.TreeGraphNode[numKids];
            for (int i = 0; i < numKids; i++)
            {
                children[i] = new Edu.Stanford.Nlp.Trees.TreeGraphNode(tKids[i], this);
                if (t.IsPreTerminal())
                {
                    // add the tags to the leaves
                    children[i].label.SetTag(t.Label().Value());
                }
            }
            this.label = (CoreLabel)mlf.NewLabel(t.Label());
        }
 /// <summary>Removes the ith child from the TreeGraphNode.</summary>
 /// <remarks>
 /// Removes the ith child from the TreeGraphNode.  Needs to override
 /// the parent removeChild so it can avoid setting the parent
 /// pointers on the remaining children.  This is useful if you want
 /// to add and remove children from one node to another node; this way,
 /// it won't matter what order you do the add and remove operations.
 /// </remarks>
 public override Tree RemoveChild(int i)
 {
     Edu.Stanford.Nlp.Trees.TreeGraphNode[] kids    = ((Edu.Stanford.Nlp.Trees.TreeGraphNode[])Children());
     Edu.Stanford.Nlp.Trees.TreeGraphNode   kid     = kids[i];
     Edu.Stanford.Nlp.Trees.TreeGraphNode[] newKids = new Edu.Stanford.Nlp.Trees.TreeGraphNode[kids.Length - 1];
     for (int j = 0; j < newKids.Length; j++)
     {
         if (j < i)
         {
             newKids[j] = kids[j];
         }
         else
         {
             newKids[j] = kids[j + 1];
         }
     }
     this.children = newKids;
     return(kid);
 }
 /// <summary>Adds a child in the ith location.</summary>
 /// <remarks>
 /// Adds a child in the ith location.  Does so without overwriting
 /// the parent pointers of the rest of the children, which might be
 /// relevant in case there are add and remove operations mixed
 /// together.
 /// </remarks>
 public override void AddChild(int i, Tree t)
 {
     if (!(t is Edu.Stanford.Nlp.Trees.TreeGraphNode))
     {
         throw new ArgumentException("Horrible error");
     }
     ((Edu.Stanford.Nlp.Trees.TreeGraphNode)t).SetParent(this);
     Edu.Stanford.Nlp.Trees.TreeGraphNode[] kids    = this.children;
     Edu.Stanford.Nlp.Trees.TreeGraphNode[] newKids = new Edu.Stanford.Nlp.Trees.TreeGraphNode[kids.Length + 1];
     if (i != 0)
     {
         System.Array.Copy(kids, 0, newKids, 0, i);
     }
     newKids[i] = (Edu.Stanford.Nlp.Trees.TreeGraphNode)t;
     if (i != kids.Length)
     {
         System.Array.Copy(kids, i, newKids, i + 1, kids.Length - i);
     }
     this.children = newKids;
 }
 /// <summary>
 /// Uses the specified
 /// <see cref="IHeadFinder"/>
 ///
 /// <c>HeadFinder</c>
 /// }
 /// to determine the heads for this node and all its descendants,
 /// and to store references to the head word node and head tag node
 /// in this node's
 /// <see cref="Edu.Stanford.Nlp.Ling.CoreLabel"/>
 ///
 /// <c>CoreLabel</c>
 /// } and the
 /// <c>CoreLabel</c>
 /// s of all its descendants.<p>
 /// <p/>
 /// Note that, in contrast to
 /// <see cref="Tree.PercolateHeads(IHeadFinder)"/>
 /// {
 /// <c>Tree.percolateHeads()</c>
 /// }, which assumes
 /// <see cref="Edu.Stanford.Nlp.Ling.CategoryWordTag"/>
 /// {
 /// <c>CategoryWordTag</c>
 /// } labels and therefore stores head
 /// words and head tags merely as
 /// <c>String</c>
 /// s, this
 /// method stores references to the actual nodes.  This mitigates
 /// potential problems in sentences which contain the same word
 /// more than once.
 /// </summary>
 /// <param name="hf">The headfinding algorithm to use</param>
 public override void PercolateHeads(IHeadFinder hf)
 {
     if (IsLeaf())
     {
         Edu.Stanford.Nlp.Trees.TreeGraphNode hwn = HeadWordNode();
         if (hwn == null)
         {
             SetHeadWordNode(this);
         }
     }
     else
     {
         foreach (Tree child in ((Edu.Stanford.Nlp.Trees.TreeGraphNode[])Children()))
         {
             child.PercolateHeads(hf);
         }
         Edu.Stanford.Nlp.Trees.TreeGraphNode head = SafeCast(hf.DetermineHead(this, parent));
         if (head != null)
         {
             Edu.Stanford.Nlp.Trees.TreeGraphNode hwn = head.HeadWordNode();
             if (hwn == null && head.IsLeaf())
             {
                 // below us is a leaf
                 SetHeadWordNode(head);
             }
             else
             {
                 SetHeadWordNode(hwn);
             }
         }
         else
         {
             log.Info("Head is null: " + this);
         }
     }
 }
 /// <summary>
 /// Store the node containing the head word for this node by
 /// storing it in this node's
 /// <see cref="Edu.Stanford.Nlp.Ling.CoreLabel"/>
 /// {
 /// <c>CoreLabel</c>
 /// }.  (In contrast to
 /// <see cref="Edu.Stanford.Nlp.Ling.CategoryWordTag"/>
 /// {
 /// <c>CategoryWordTag</c>
 /// }, we store head words and head
 /// tags as references to nodes, not merely as
 /// <c>String</c>
 /// s.)
 /// </summary>
 /// <param name="hwn">the node containing the head word for this node</param>
 private void SetHeadWordNode(Edu.Stanford.Nlp.Trees.TreeGraphNode hwn)
 {
     this.headWordNode = hwn;
 }
 /// <summary>Set the parent for the current node.</summary>
 public virtual void SetParent(Edu.Stanford.Nlp.Trees.TreeGraphNode parent)
 {
     this.parent = parent;
 }