edu.stanford.nlp.trees.tregex.tsurgeon.AdjoinNode 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 java.util.Map;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.tregex.TregexMatcher;
/** Adjoin in a tree (like in TAG).
*
* @author Roger Levy ([email protected])
*/
class AdjoinNode extends TsurgeonPattern {
private final AuxiliaryTree adjunctionTree;
public AdjoinNode(AuxiliaryTree t, TsurgeonPattern p) {
this("adjoin", t, p);
}
public AdjoinNode(String name, AuxiliaryTree t, TsurgeonPattern p) {
super(name, new TsurgeonPattern[] {p});
if (t == null || p == null) {
throw new NullPointerException("AdjoinNode: illegal null argument, t=" + t + ", p=" + p);
}
adjunctionTree = t;
}
protected AuxiliaryTree adjunctionTree() {
return adjunctionTree;
}
@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(AdjoinNode.this, newNodeNames, coindexer);
}
@Override
public Tree evaluate(Tree tree, TregexMatcher tregex) {
// find match and get its parent
Tree targetNode = childMatcher[0].evaluate(tree, tregex);
Tree parent = targetNode.parent(tree);
// put children underneath target in foot of auxilary tree
AuxiliaryTree ft = adjunctionTree.copy(this, tree.treeFactory(), tree.label().labelFactory());
ft.foot.setChildren(targetNode.getChildrenAsList());
// replace match with root of auxiliary tree
if (parent==null) {
return ft.tree;
} else {
int i = parent.objectIndexOf(targetNode);
parent.setChild(i,ft.tree);
return tree;
}
}
}
@Override
public String toString() {
return super.toString() + "<-" + adjunctionTree.toString();
}
}