Search in sources :

Example 11 with ParserQuery

use of edu.stanford.nlp.parser.common.ParserQuery in project CoreNLP by stanfordnlp.

the class LexicalizedParser method parse.

/**
 * Parses the list of HasWord.  If the parse fails for some reason,
 * an X tree is returned instead of barfing.
 */
@Override
public Tree parse(List<? extends HasWord> lst) {
    try {
        ParserQuery pq = parserQuery();
        if (pq.parse(lst)) {
            Tree bestparse = pq.getBestParse();
            // -10000 denotes unknown words
            bestparse.setScore(pq.getPCFGScore() % -10000.0);
            return bestparse;
        }
    } catch (Exception e) {
        log.info("Following exception caught during parsing:");
        e.printStackTrace();
        log.info("Recovering using fall through strategy: will construct an (X ...) tree.");
    }
    // if can't parse or exception, fall through
    return ParserUtils.xTree(lst);
}
Also used : RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParserQuery(edu.stanford.nlp.parser.common.ParserQuery)

Example 12 with ParserQuery

use of edu.stanford.nlp.parser.common.ParserQuery in project CoreNLP by stanfordnlp.

the class EvaluateTreebank method testOnTreebank.

public double testOnTreebank(EvaluationDataset testTreebank) {
    log.info("Testing on treebank");
    Timing treebankTotalTimer = new Timing();
    TreePrint treePrint = op.testOptions.treePrint(op.tlpParams);
    TreebankLangParserParams tlpParams = op.tlpParams;
    TreebankLanguagePack tlp = op.langpack();
    PrintWriter pwOut, pwErr;
    if (op.testOptions.quietEvaluation) {
        NullOutputStream quiet = new NullOutputStream();
        pwOut = tlpParams.pw(quiet);
        pwErr = tlpParams.pw(quiet);
    } else {
        pwOut = tlpParams.pw();
        pwErr = tlpParams.pw(System.err);
    }
    if (op.testOptions.verbose) {
        testTreebank.summarize(pwErr, tlp);
    }
    if (op.testOptions.evalb) {
        EvalbFormatWriter.initEVALBfiles(tlpParams);
    }
    PrintWriter pwFileOut = null;
    if (op.testOptions.writeOutputFiles) {
        String fname = op.testOptions.outputFilesPrefix + "." + op.testOptions.outputFilesExtension;
        try {
            pwFileOut = op.tlpParams.pw(new FileOutputStream(fname));
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    PrintWriter pwStats = null;
    if (op.testOptions.outputkBestEquivocation != null) {
        try {
            pwStats = op.tlpParams.pw(new FileOutputStream(op.testOptions.outputkBestEquivocation));
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    List<Pair<ParserQuery, Tree>> results = testTreebank.dataset(pwErr, pwOut, pwFileOut, pwStats, treePrint);
    for (Pair<ParserQuery, Tree> result : results) {
        ParserQuery pq = result.first;
        Tree goldTree = result.second;
        processResults(pq, goldTree, pwErr, pwOut, pwFileOut, pwStats, treePrint);
    }
    // Done parsing...print the results of the evaluations
    treebankTotalTimer.done("Testing on treebank");
    if (op.testOptions.quietEvaluation) {
        pwErr = tlpParams.pw(System.err);
    }
    if (saidMemMessage) {
        ParserUtils.printOutOfMemory(pwErr);
    }
    if (op.testOptions.evalb) {
        EvalbFormatWriter.closeEVALBfiles();
    }
    if (numSkippedEvals != 0) {
        pwErr.printf("Unable to evaluate %d parser hypotheses due to yield mismatch\n", numSkippedEvals);
    }
    // only created here so we know what parser types are supported...
    // TODO: pass in the various pcfgparser, dependencyparser, etc?
    ParserQuery pq = pqFactory != null ? pqFactory.parserQuery() : null;
    if (summary) {
        if (pcfgLB != null)
            pcfgLB.display(false, pwErr);
        if (pcfgChildSpecific != null)
            pcfgChildSpecific.display(false, pwErr);
        if (pcfgLA != null)
            pcfgLA.display(false, pwErr);
        if (pcfgCB != null)
            pcfgCB.display(false, pwErr);
        if (pcfgDA != null)
            pcfgDA.display(false, pwErr);
        if (pcfgTA != null)
            pcfgTA.display(false, pwErr);
        if (pcfgLL != null && pq != null && pq.getPCFGParser() != null)
            pcfgLL.display(false, pwErr);
        if (depDA != null)
            depDA.display(false, pwErr);
        if (depTA != null)
            depTA.display(false, pwErr);
        if (depLL != null && pq != null && pq.getDependencyParser() != null)
            depLL.display(false, pwErr);
        if (factLB != null)
            factLB.display(false, pwErr);
        if (factChildSpecific != null)
            factChildSpecific.display(false, pwErr);
        if (factLA != null)
            factLA.display(false, pwErr);
        if (factCB != null)
            factCB.display(false, pwErr);
        if (factDA != null)
            factDA.display(false, pwErr);
        if (factTA != null)
            factTA.display(false, pwErr);
        if (factLL != null && pq != null && pq.getFactoredParser() != null)
            factLL.display(false, pwErr);
        if (pcfgCatE != null)
            pcfgCatE.display(false, pwErr);
        for (Eval eval : evals) {
            eval.display(false, pwErr);
        }
        for (BestOfTopKEval eval : topKEvals) {
            eval.display(false, pwErr);
        }
    }
    // these ones only have a display mode, so display if turned on!!
    if (pcfgRUO != null)
        pcfgRUO.display(true, pwErr);
    if (pcfgCUO != null)
        pcfgCUO.display(true, pwErr);
    if (tsv) {
        NumberFormat nf = new DecimalFormat("0.00");
        pwErr.println("factF1\tfactDA\tfactEx\tpcfgF1\tdepDA\tfactTA\tnum");
        if (factLB != null)
            pwErr.print(nf.format(factLB.getEvalbF1Percent()));
        pwErr.print("\t");
        if (pq != null && pq.getDependencyParser() != null && factDA != null)
            pwErr.print(nf.format(factDA.getEvalbF1Percent()));
        pwErr.print("\t");
        if (factLB != null)
            pwErr.print(nf.format(factLB.getExactPercent()));
        pwErr.print("\t");
        if (pcfgLB != null)
            pwErr.print(nf.format(pcfgLB.getEvalbF1Percent()));
        pwErr.print("\t");
        if (pq != null && pq.getDependencyParser() != null && depDA != null)
            pwErr.print(nf.format(depDA.getEvalbF1Percent()));
        pwErr.print("\t");
        if (pq != null && pq.getPCFGParser() != null && factTA != null)
            pwErr.print(nf.format(factTA.getEvalbF1Percent()));
        pwErr.print("\t");
        if (factLB != null)
            pwErr.print(factLB.getNum());
        pwErr.println();
    }
    double f1 = 0.0;
    if (factLB != null) {
        f1 = factLB.getEvalbF1();
    }
    // Close files (if necessary)
    if (pwFileOut != null)
        pwFileOut.close();
    if (pwStats != null)
        pwStats.close();
    for (ParserQueryEval parserQueryEval : parserQueryEvals) {
        parserQueryEval.display(false, pwErr);
    }
    return f1;
}
Also used : DecimalFormat(java.text.DecimalFormat) TreePrint(edu.stanford.nlp.trees.TreePrint) TreebankLangParserParams(edu.stanford.nlp.parser.lexparser.TreebankLangParserParams) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) Tree(edu.stanford.nlp.trees.Tree) TreebankLanguagePack(edu.stanford.nlp.trees.TreebankLanguagePack) Timing(edu.stanford.nlp.util.Timing) PrintWriter(java.io.PrintWriter) NullOutputStream(edu.stanford.nlp.io.NullOutputStream) Pair(edu.stanford.nlp.util.Pair) ParserQuery(edu.stanford.nlp.parser.common.ParserQuery) NumberFormat(java.text.NumberFormat)

Aggregations

ParserQuery (edu.stanford.nlp.parser.common.ParserQuery)12 Tree (edu.stanford.nlp.trees.Tree)6 Pair (edu.stanford.nlp.util.Pair)4 PrintWriter (java.io.PrintWriter)4 ArrayList (java.util.ArrayList)4 RuntimeIOException (edu.stanford.nlp.io.RuntimeIOException)3 CoreLabel (edu.stanford.nlp.ling.CoreLabel)3 HasWord (edu.stanford.nlp.ling.HasWord)3 Timing (edu.stanford.nlp.util.Timing)3 FileOutputStream (java.io.FileOutputStream)3 IOException (java.io.IOException)3 DecimalFormat (java.text.DecimalFormat)3 NumberFormat (java.text.NumberFormat)3 Test (org.junit.Test)3 NullOutputStream (edu.stanford.nlp.io.NullOutputStream)2 Word (edu.stanford.nlp.ling.Word)2 ParserConstraint (edu.stanford.nlp.parser.common.ParserConstraint)2 ParsingThreadsafeProcessor (edu.stanford.nlp.parser.common.ParsingThreadsafeProcessor)2 LexicalizedParser (edu.stanford.nlp.parser.lexparser.LexicalizedParser)2 RerankingParserQuery (edu.stanford.nlp.parser.lexparser.RerankingParserQuery)2