edu.stanford.nlp.parser.lexparser.AbstractTreeExtractor 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.parser.lexparser;
import java.util.Collection;
import java.util.Iterator;
import java.util.function.Function;
import edu.stanford.nlp.trees.Tree;
/**
* An abstract superclass for parser classes that extract counts from Trees.
* @author grenager
* @author Sarah Spikes ([email protected]) - cleanup and filling in types
*/
public abstract class AbstractTreeExtractor implements Extractor {
protected final Options op;
protected AbstractTreeExtractor(Options op) {
this.op = op;
}
@SuppressWarnings({"UnusedDeclaration"})
protected void tallyLeaf(Tree lt, double weight) {
}
protected void tallyPreTerminal(Tree lt, double weight) {
}
protected void tallyInternalNode(Tree lt, double weight) {
}
protected void tallyRoot(Tree lt, double weight) {
}
public T formResult() {
return null;
}
protected void tallyLocalTree(Tree lt, double weight) {
// printTrainTree(null, "Tallying local tree:", lt);
if (lt.isLeaf()) {
// System.out.println("it's a leaf");
tallyLeaf(lt, weight);
} else if (lt.isPreTerminal()) {
// System.out.println("it's a preterminal");
tallyPreTerminal(lt, weight);
} else {
// System.out.println("it's a internal node");
tallyInternalNode(lt, weight);
}
}
public void tallyTree(Tree t, double weight) {
tallyRoot(t, weight);
for (Tree localTree : t.subTreeList()) {
tallyLocalTree(localTree, weight);
}
}
protected void tallyTrees(Collection trees, double weight) {
for (Tree tree : trees) {
tallyTree(tree, weight);
}
}
protected void tallyTreeIterator(Iterator treeIterator,
Function f, double weight) {
while (treeIterator.hasNext()) {
Tree tree = treeIterator.next();
try {
tree = f.apply(tree);
} catch (Exception e) {
if (op.testOptions.verbose) {
e.printStackTrace();
}
}
tallyTree(tree, weight);
}
}
public T extract() {
return formResult();
}
public T extract(Collection treeList) {
tallyTrees(treeList, 1.0);
return formResult();
}
public T extract(Collection trees1, double weight1,
Collection trees2, double weight2) {
tallyTrees(trees1, weight1);
tallyTrees(trees2, weight2);
return formResult();
}
public T extract(Iterator treeIterator, Function f, double weight) {
tallyTreeIterator(treeIterator, f, weight);
return formResult();
}
public T extract(Iterator iterator, Function f) {
return extract(iterator, f, 1.0);
}
}