Search in sources :

Example 16 with Tree

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();
}
Also used : Tree(edu.stanford.nlp.trees.Tree)

Example 17 with Tree

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;
}
Also used : TaggedWord(edu.stanford.nlp.ling.TaggedWord) Tree(edu.stanford.nlp.trees.Tree)

Example 18 with Tree

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);
}
Also used : Tree(edu.stanford.nlp.trees.Tree)

Example 19 with Tree

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();
    }
}
Also used : NoSuchParseException(edu.stanford.nlp.parser.common.NoSuchParseException) Tree(edu.stanford.nlp.trees.Tree)

Example 20 with Tree

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();
    }
}
Also used : NoSuchParseException(edu.stanford.nlp.parser.common.NoSuchParseException) ScoredObject(edu.stanford.nlp.util.ScoredObject) ArrayList(java.util.ArrayList) Tree(edu.stanford.nlp.trees.Tree)

Aggregations

Tree (edu.stanford.nlp.trees.Tree)329 CoreLabel (edu.stanford.nlp.ling.CoreLabel)99 ArrayList (java.util.ArrayList)59 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)55 TreeCoreAnnotations (edu.stanford.nlp.trees.TreeCoreAnnotations)43 SemanticGraphCoreAnnotations (edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations)32 ParserConstraint (edu.stanford.nlp.parser.common.ParserConstraint)30 CoreMap (edu.stanford.nlp.util.CoreMap)27 List (java.util.List)27 Label (edu.stanford.nlp.ling.Label)24 SemanticGraph (edu.stanford.nlp.semgraph.SemanticGraph)21 TreeReaderFactory (edu.stanford.nlp.trees.TreeReaderFactory)20 TreeReader (edu.stanford.nlp.trees.TreeReader)19 PrintWriter (java.io.PrintWriter)19 Language (edu.stanford.nlp.international.Language)17 TreeTransformer (edu.stanford.nlp.trees.TreeTransformer)16 Treebank (edu.stanford.nlp.trees.Treebank)16 IOException (java.io.IOException)16 Mention (edu.stanford.nlp.coref.data.Mention)15 TreebankLangParserParams (edu.stanford.nlp.parser.lexparser.TreebankLangParserParams)15