edu.stanford.nlp.trees.tregex.tsurgeon.MoveNode 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.Trees;
import edu.stanford.nlp.trees.tregex.TregexMatcher;
import edu.stanford.nlp.util.Pair;
/** Does a delete (NOT prune!) + insert operation
* @author Roger Levy ([email protected])
*/
class MoveNode extends TsurgeonPattern {
TreeLocation location;
public MoveNode(TsurgeonPattern child, TreeLocation l) {
super("move", new TsurgeonPattern[] { child });
this.location = l;
}
@Override
protected void setRoot(TsurgeonPatternRoot root) {
super.setRoot(root);
location.setRoot(root);
}
@Override
public TsurgeonMatcher matcher(Map newNodeNames, CoindexationGenerator coindexer) {
return new Matcher(newNodeNames, coindexer);
}
private class Matcher extends TsurgeonMatcher {
TreeLocation.LocationMatcher locationMatcher;
public Matcher(Map newNodeNames, CoindexationGenerator coindexer) {
super(MoveNode.this, newNodeNames, coindexer);
locationMatcher = location.matcher(newNodeNames, coindexer);
}
@Override
public Tree evaluate(Tree tree, TregexMatcher tregex) {
Tree nodeToMove = childMatcher[0].evaluate(tree, tregex);
Tree oldParent = nodeToMove.parent(tree);
oldParent.removeChild(Trees.objectEqualityIndexOf(oldParent,nodeToMove));
Pair position = locationMatcher.evaluate(tree, tregex);
position.first().insertDtr(nodeToMove,position.second());
return tree;
}
}
@Override
public String toString() {
return label + "(" + children[0] + " " + location + ")";
}
}