示例#1
0
        /// <summary>
        /// Return the node containing the head tag for this node (or
        /// <code>null</code> if none), as recorded in this node's {@link
        /// CoreLabel <code>CoreLabel</code>}.  (In contrast to {@link
        /// edu.stanford.nlp.ling.CategoryWordTag
        /// <code>CategoryWordTag</code>}, we store head words and head
        /// tags as references to nodes, not merely as <code>string</code>s.)
        /// </summary>
        /// <returns>the node containing the head tag for this node</returns>
        public TreeGraphNode HeadTagNode()
        {
            TreeGraphNode htn = SafeCast(_label.Get(typeof(TreeCoreAnnotations.HeadTagAnnotation)));

            if (htn == null || (htn.TreeGraph() != null && !(htn.TreeGraph().Equals(this.TreeGraph()))))
            {
                return(null);
            }
            return(htn);
        }
示例#2
0
        /// <summary>
        /// Checks the node's ancestors to find the highest ancestor with the
        /// same <code>headWordNode</code> as this node
        /// </summary>
        public TreeGraphNode HighestNodeWithSameHead()
        {
            TreeGraphNode node = this;

            while (true)
            {
                TreeGraphNode parent = SafeCast(node.Parent());
                if (parent == null || parent.HeadWordNode() != node.HeadWordNode())
                {
                    return(node);
                }
                node = parent;
            }
        }
示例#3
0
        /// <summary>
        /// Create a new <code>TreeGraphNode</code> 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 TreeGraphNode(Tree t, TreeGraphNode parent)
        {
            this._parent = parent;
            Tree[] tKids   = t.Children();
            int    numKids = tKids.Length;

            _children = new TreeGraphNode[numKids];
            for (int i = 0; i < numKids; i++)
            {
                _children[i] = new 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());
        }
示例#4
0
        /// <summary>
        /// Uses the specified {@link HeadFinder <code>HeadFinder</code>}
        /// 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 {@link CoreLabel <code>CoreLabel</code>} and the
        /// <code>CoreLabel</code>s of all its descendants.
        ///
        /// Note that, in contrast to {@link Tree#percolateHeads
        /// <code>Tree.percolateHeads()</code>}, which assumes {@link
        /// edu.stanford.nlp.ling.CategoryWordTag
        /// <code>CategoryWordTag</code>} labels and therefore stores head
        /// words and head tags merely as <code>string</code>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())
            {
                TreeGraphNode hwn = HeadWordNode();
                if (hwn == null)
                {
                    SetHeadWordNode(this);
                }
            }
            else
            {
                foreach (Tree child in Children())
                {
                    child.PercolateHeads(hf);
                }
                TreeGraphNode head = SafeCast(hf.DetermineHead(this, _parent));
                if (head != null)
                {
                    TreeGraphNode hwn = head.HeadWordNode();
                    if (hwn == null && head.IsLeaf())
                    {
                        // below us is a leaf
                        SetHeadWordNode(head);
                    }
                    else
                    {
                        SetHeadWordNode(hwn);
                    }

                    TreeGraphNode htn = head.HeadTagNode();
                    if (htn == null && head.IsLeaf())
                    {
                        // below us is a leaf
                        SetHeadTagNode(this);
                    }
                    else
                    {
                        SetHeadTagNode(htn);
                    }
                }
            }
        }
        /// <summary>
        /// Given a {@code Tree} node {@code t}, attempts to
        /// return a list of nodes to which node {@code t} has this
        /// grammatical relation, with {@code t} as the governor.
        /// </summary>
        /// <param name="t">Target for finding dependents of t related by this GR</param>
        /// <param name="root">The root of the Tree</param>
        /// <returns>A Collection of dependent nodes to which t bears this GR</returns>
        public ICollection <TreeGraphNode> GetRelatedNodes(TreeGraphNode t, TreeGraphNode root, IHeadFinder headFinder)
        {
            Set <TreeGraphNode> nodeList = new Util.HashSet <TreeGraphNode>();

            foreach (TregexPattern p in targetPatterns)
            {
                // cdm: I deleted: && nodeList.isEmpty()
                // Initialize the TregexMatcher with the HeadFinder so that we
                // can use the same HeadFinder through the entire process of
                // building the dependencies
                TregexMatcher m = p.Matcher(root, headFinder);
                while (m.FindAt(t))
                {
                    var target = (TreeGraphNode)m.GetNode("target");
                    if (target == null)
                    {
                        throw new InvalidDataException("Expression has no target: " + p);
                    }
                    nodeList.Add(target);
                }
            }
            return(nodeList);
        }
示例#6
0
 // TODO it's not really clear what graph the copy should be a part of
 public TreeGraphNode(TreeGraphNode t) :
     this(t, t._parent)
 {
     this.SetTreeGraph(t.TreeGraph());
 }
示例#7
0
 /// <summary>
 /// Store the node containing the head tag for this node by
 /// storing it in this node's {@link CoreLabel <code>CoreLabel</code>}.
 /// (In contrast to {@link edu.stanford.nlp.ling.CategoryWordTag
 /// <code>CategoryWordTag</code>}, we store head words and head
 /// tags as references to nodes, not merely as
 /// <code>string</code>s.)
 /// </summary>
 /// <param name="htn">the node containing the head tag for this node</param>
 private void SetHeadTagNode(TreeGraphNode htn)
 {
     _label.Set(typeof(TreeCoreAnnotations.HeadTagAnnotation), htn);
 }
示例#8
0
 /// <summary>
 /// Store the node containing the head word for this node by
 /// storing it in this node's {@link CoreLabel
 /// <code>CoreLabel</code>}.  (In contrast to {@link
 /// edu.stanford.nlp.ling.CategoryWordTag
 /// <code>CategoryWordTag</code>}, we store head words and head
 /// tags as references to nodes, not merely as <code>string</code>s.)
 /// </summary>
 /// <param name="hwn">the node containing the head word for this node</param>
 private void SetHeadWordNode(TreeGraphNode hwn)
 {
     _label.Set(typeof(TreeCoreAnnotations.HeadWordAnnotation), hwn);
 }
示例#9
0
 /// <summary>
 /// Set the parent for the current node
 /// </summary>
 public void SetParent(TreeGraphNode parent)
 {
     this._parent = parent;
 }
示例#10
0
 // TODO it's not really clear what graph the copy should be a part of
 public TreeGraphNode(TreeGraphNode t) :
     this(t, t._parent)
 {
     this.SetTreeGraph(t.TreeGraph());
 }
示例#11
0
 /// <summary>
 /// Store the node containing the head tag for this node by
 /// storing it in this node's {@link CoreLabel <code>CoreLabel</code>}.
 /// (In contrast to {@link edu.stanford.nlp.ling.CategoryWordTag
 /// <code>CategoryWordTag</code>}, we store head words and head
 /// tags as references to nodes, not merely as
 /// <code>string</code>s.)
 /// </summary>
 /// <param name="htn">the node containing the head tag for this node</param>
 private void SetHeadTagNode(TreeGraphNode htn)
 {
     _label.Set(typeof (TreeCoreAnnotations.HeadTagAnnotation), htn);
 }
示例#12
0
 /// <summary>
 /// Store the node containing the head word for this node by 
 /// storing it in this node's {@link CoreLabel
 /// <code>CoreLabel</code>}.  (In contrast to {@link
 /// edu.stanford.nlp.ling.CategoryWordTag
 /// <code>CategoryWordTag</code>}, we store head words and head
 /// tags as references to nodes, not merely as <code>string</code>s.)
 /// </summary>
 /// <param name="hwn">the node containing the head word for this node</param>
 private void SetHeadWordNode(TreeGraphNode hwn)
 {
     _label.Set(typeof (TreeCoreAnnotations.HeadWordAnnotation), hwn);
 }
示例#13
0
 /// <summary>
 /// Set the parent for the current node
 /// </summary>
 public void SetParent(TreeGraphNode parent)
 {
     this._parent = parent;
 }
示例#14
0
 /// <summary>
 /// Create a new <code>TreeGraphNode</code> 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 TreeGraphNode(Tree t, TreeGraphNode parent)
 {
     this._parent = parent;
     Tree[] tKids = t.Children();
     int numKids = tKids.Length;
     _children = new TreeGraphNode[numKids];
     for (int i = 0; i < numKids; i++)
     {
         _children[i] = new 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());
 }