edu.stanford.nlp.trees.international.pennchinese.ChineseCollinizer 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.international.pennchinese;
import edu.stanford.nlp.util.logging.Redwood;
import edu.stanford.nlp.ling.StringLabel;
import edu.stanford.nlp.trees.LabeledScoredTreeFactory;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TreeFactory;
import edu.stanford.nlp.trees.TreeTransformer;
import java.util.ArrayList;
import java.util.List;
/**
* Performs collinization operations on Chinese trees similar to
* those for English Namely:
* - strips all functional & automatically-added tags
*
- strips all punctuation
*
- merges PRN and ADVP
*
- eliminates ROOT (note that there are a few non-unary ROOT nodes;
* these are not eliminated)
*
*
* @author Roger Levy
* @author Christopher Manning
*/
public class ChineseCollinizer implements TreeTransformer {
/** A logger for this class */
private static Redwood.RedwoodChannels log = Redwood.channels(ChineseCollinizer.class);
private final static boolean VERBOSE = false;
private final boolean deletePunct;
ChineseTreebankLanguagePack ctlp;
protected TreeFactory tf = new LabeledScoredTreeFactory();
public ChineseCollinizer(ChineseTreebankLanguagePack ctlp) {
this(ctlp, true);
}
public ChineseCollinizer(ChineseTreebankLanguagePack ctlp, boolean deletePunct) {
this.deletePunct = deletePunct;
this.ctlp = ctlp;
}
public Tree transformTree(Tree tree) {
return transformTree(tree, true);
}
private Tree transformTree(Tree tree, boolean isRoot) {
String label = tree.label().value();
// log.info("ChineseCollinizer: Node label is " + label);
if (tree.isLeaf()) {
if (deletePunct && ctlp.isPunctuationWord(label)) {
return null;
} else {
return tf.newLeaf(new StringLabel(label));
}
}
if (tree.isPreTerminal() && deletePunct && ctlp.isPunctuationTag(label)) {
// System.out.println("Deleting punctuation");
return null;
}
List children = new ArrayList<>();
if (label.matches("ROOT.*") && tree.numChildren() == 1) { // keep non-unary roots for now
return transformTree(tree.children()[0], true);
}
//System.out.println("Enhanced label is " + label);
// remove all functional and machine-generated annotations
label = label.replaceFirst("[^A-Z].*$", "");
// merge parentheticals with adverb phrases
label = label.replaceFirst("PRN", "ADVP");
//System.out.println("New label is " + label);
for (int cNum = 0; cNum < tree.children().length; cNum++) {
Tree child = tree.children()[cNum];
Tree newChild = transformTree(child, false);
if (newChild != null) {
children.add(newChild);
}
}
// We don't delete the root because there are trees in the
// Chinese treebank that only have punctuation in them!!!
if (children.isEmpty() && ! isRoot) {
if (VERBOSE) {
log.info("ChineseCollinizer: all children of " + label +
" deleted; returning null");
}
return null;
}
return tf.newTreeNode(new StringLabel(label), children);
}
}