Search in sources :

Example 1 with ArraySet

use of edu.stanford.nlp.util.ArraySet in project CoreNLP by stanfordnlp.

the class GrammaticalRelation method getRelatedNodes.

/** 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.
   *
   *  @param t Target for finding dependents of t related by this GR
   *  @param root The root of the Tree
   *  @return A Collection of dependent nodes to which t bears this GR
   */
public Collection<TreeGraphNode> getRelatedNodes(TreeGraphNode t, TreeGraphNode root, HeadFinder headFinder) {
    Set<TreeGraphNode> nodeList = new ArraySet<>();
    for (TregexPattern p : 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)) {
            TreeGraphNode target = (TreeGraphNode) m.getNode("target");
            if (target == null) {
                throw new AssertionError("Expression has no target: " + p);
            }
            nodeList.add(target);
            if (DEBUG) {
                log.info("found " + this + "(" + t + "-" + t.headWordNode() + ", " + m.getNode("target") + "-" + ((TreeGraphNode) m.getNode("target")).headWordNode() + ") using pattern " + p);
                for (String nodeName : m.getNodeNames()) {
                    if (nodeName.equals("target"))
                        continue;
                    log.info("  node " + nodeName + ": " + m.getNode(nodeName));
                }
            }
        }
    }
    return nodeList;
}
Also used : TregexPattern(edu.stanford.nlp.trees.tregex.TregexPattern) ArraySet(edu.stanford.nlp.util.ArraySet) TregexMatcher(edu.stanford.nlp.trees.tregex.TregexMatcher)

Aggregations

TregexMatcher (edu.stanford.nlp.trees.tregex.TregexMatcher)1 TregexPattern (edu.stanford.nlp.trees.tregex.TregexPattern)1 ArraySet (edu.stanford.nlp.util.ArraySet)1