use of edu.stanford.nlp.trees.Tree in project CoreNLP by stanfordnlp.
the class SplittingGrammarExtractor method initialBetasAndLexicon.
private void initialBetasAndLexicon() {
wordIndex = new HashIndex<>();
tagIndex = new HashIndex<>();
lex = op.tlpParams.lex(op, wordIndex, tagIndex);
lex.initializeTraining(trainSize);
for (Tree tree : trees) {
double weight = treeWeights.getCount(tree);
lex.incrementTreesRead(weight);
initialBetasAndLexicon(tree, 0, weight);
}
lex.finishTraining();
}
use of edu.stanford.nlp.trees.Tree in project CoreNLP by stanfordnlp.
the class SplittingGrammarExtractor method initialBetasAndLexicon.
private int initialBetasAndLexicon(Tree tree, int position, double weight) {
if (tree.isLeaf()) {
// should never get here, unless a training tree is just one leaf
return position;
}
if (tree.isPreTerminal()) {
// fill in initial lexicon here
String tag = tree.label().value();
String word = tree.children()[0].label().value();
TaggedWord tw = new TaggedWord(word, state(tag, 0));
lex.train(tw, position, weight);
return (position + 1);
}
if (tree.children().length == 2) {
String label = tree.label().value();
String leftLabel = tree.getChild(0).label().value();
String rightLabel = tree.getChild(1).label().value();
if (!binaryBetas.contains(label, leftLabel, rightLabel)) {
double[][][] map = new double[1][1][1];
map[0][0][0] = 0.0;
binaryBetas.put(label, leftLabel, rightLabel, map);
}
} else if (tree.children().length == 1) {
String label = tree.label().value();
String childLabel = tree.getChild(0).label().value();
if (!unaryBetas.contains(label, childLabel)) {
double[][] map = new double[1][1];
map[0][0] = 0.0;
unaryBetas.put(label, childLabel, map);
}
} else {
// should have been binarized
throw new RuntimeException("Trees should have been binarized, expected 1 or 2 children");
}
for (Tree child : tree.children()) {
position = initialBetasAndLexicon(child, position, weight);
}
return position;
}
use of edu.stanford.nlp.trees.Tree in project CoreNLP by stanfordnlp.
the class BestOfTopKEval method evaluate.
public void evaluate(List<Tree> guesses, Tree gold, PrintWriter pw) {
double bestF1 = Double.NEGATIVE_INFINITY;
Tree bestTree = null;
for (Tree tree : guesses) {
comparisonEval.evaluate(tree, gold, null);
double f1 = comparisonEval.getLastF1();
if (bestTree == null || f1 > bestF1) {
bestTree = tree;
bestF1 = f1;
}
}
countingEval.evaluate(bestTree, gold, pw);
}
use of edu.stanford.nlp.trees.Tree in project CoreNLP by stanfordnlp.
the class LexicalizedParserQuery method getBestParse.
Tree getBestParse(boolean stripSubcat) {
if (parseSkipped) {
return null;
}
if (bparser != null && parseSucceeded) {
Tree binaryTree = bparser.getBestParse();
Tree tree = debinarizer.transformTree(binaryTree);
if (op.nodePrune) {
NodePruner np = new NodePruner(pparser, debinarizer);
tree = np.prune(tree);
}
if (stripSubcat) {
tree = subcategoryStripper.transformTree(tree);
}
restoreOriginalWords(tree);
return tree;
} else if (pparser != null && pparser.hasParse() && fallbackToPCFG) {
return getBestPCFGParse();
} else if (dparser != null && dparser.hasParse()) {
// return subcategoryStripper.transformTree(getBestDependencyParse(true));
return getBestDependencyParse(true);
} else {
throw new NoSuchParseException();
}
}
use of edu.stanford.nlp.trees.Tree in project CoreNLP by stanfordnlp.
the class LexicalizedParserQuery method getKBestParses.
/**
* Return the k best parses of the sentence most recently parsed.
*
* NB: The dependency parser does not implement a k-best method
* and the factored parser's method seems to be broken and therefore
* this method always returns a list of size 1 if either of these
* two parsers was used.
*
* @return A list of scored trees
* @throws NoSuchParseException If no previously successfully parsed
* sentence */
@Override
public List<ScoredObject<Tree>> getKBestParses(int k) {
if (parseSkipped) {
return null;
}
if (bparser != null && parseSucceeded) {
//The getKGoodParses seems to be broken, so just return the best parse
Tree binaryTree = bparser.getBestParse();
Tree tree = debinarizer.transformTree(binaryTree);
if (op.nodePrune) {
NodePruner np = new NodePruner(pparser, debinarizer);
tree = np.prune(tree);
}
tree = subcategoryStripper.transformTree(tree);
restoreOriginalWords(tree);
double score = dparser.getBestScore();
ScoredObject<Tree> so = new ScoredObject<>(tree, score);
List<ScoredObject<Tree>> trees = new ArrayList<>(1);
trees.add(so);
return trees;
} else if (pparser != null && pparser.hasParse() && fallbackToPCFG) {
return this.getKBestPCFGParses(k);
} else if (dparser != null && dparser.hasParse()) {
// && fallbackToDG
// The dependency parser doesn't support k-best parse extraction, so just
// return the best parse
Tree tree = this.getBestDependencyParse(true);
double score = dparser.getBestScore();
ScoredObject<Tree> so = new ScoredObject<>(tree, score);
List<ScoredObject<Tree>> trees = new ArrayList<>(1);
trees.add(so);
return trees;
} else {
throw new NoSuchParseException();
}
}
Aggregations