edu.stanford.nlp.trees.tregex.tsurgeon.TsurgeonPatternRoot Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of stanford-corenlp Show documentation
Show all versions of stanford-corenlp Show documentation
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.
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;
}
}
}