All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.stanford.nlp.trees.tregex.tsurgeon.TsurgeonPatternRoot Maven / Gradle / Ivy

Go to download

Stanford CoreNLP provides a set of natural language analysis tools which can take raw English language text input and give the base forms of words, their parts of speech, whether they are names of companies, people, etc., normalize dates, times, and numeric quantities, mark up the structure of sentences in terms of phrases and word dependencies, and indicate which noun phrases refer to the same entities. It provides the foundational building blocks for higher level text understanding applications.

There is a newer version: 4.5.7
Show newest version
package edu.stanford.nlp.trees.tregex.tsurgeon;

import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.tregex.TregexMatcher;
import edu.stanford.nlp.util.Generics;

import java.util.Map;

/**
 * @author Roger Levy ([email protected])
 */
class TsurgeonPatternRoot extends TsurgeonPattern {

  public TsurgeonPatternRoot(TsurgeonPattern child) {
    this(new TsurgeonPattern[] { child });
  }

  public TsurgeonPatternRoot(TsurgeonPattern[] children) {
    super("operations: ", children);
    setRoot(this);
  }

  boolean coindexes = false;

  /**
   * If one of the children is a CoindexNodes (or something else that
   * wants coindexing), it can call this at the time of setRoot()
   */
  void setCoindexes() {
    coindexes = true;
  }

  @Override
  public TsurgeonMatcher matcher() {
    CoindexationGenerator coindexer = null;
    if (coindexes) {
      coindexer = new CoindexationGenerator();
    }
    return matcher(Generics.newHashMap(), coindexer);
  }

  @Override
  public TsurgeonMatcher matcher(Map newNodeNames, CoindexationGenerator coindexer) {
    return new Matcher(newNodeNames, coindexer);
  }


  private class Matcher extends TsurgeonMatcher {
    public Matcher(Map newNodeNames, CoindexationGenerator coindexer) {
      super(TsurgeonPatternRoot.this, newNodeNames, coindexer);
    }

    /**
     * returns null if one of the surgeries eliminates the tree entirely.  The
     * operated-on tree is not to be trusted in this instance.
     */
    @Override
    public Tree evaluate(Tree tree, TregexMatcher tregex) {
      if (coindexer != null) {
        coindexer.setLastIndex(tree);
      }
      for (TsurgeonMatcher child : childMatcher) {
        tree = child.evaluate(tree, tregex);
        if (tree == null) {
          return null;
        }
      }
      return tree;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy