Search in sources :

Example 1 with ConstituentFactory

use of edu.stanford.nlp.trees.ConstituentFactory in project CoreNLP by stanfordnlp.

the class Evalb method emitSortedTrees.

private static void emitSortedTrees(PriorityQueue<Triple<Double, Tree, Tree>> queue, int worstKTreesToEmit, String filePrefix) {
    if (queue == null)
        log.info("Queue was not initialized properly");
    try {
        final PrintWriter guessPw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePrefix + ".kworst.guess"), "UTF-8")));
        final PrintWriter goldPw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePrefix + ".kworst.gold"), "UTF-8")));
        final ConstituentFactory cFact = new LabeledScoredConstituentFactory();
        final PrintWriter guessDepPw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePrefix + ".kworst.guess.deps"), "UTF-8")));
        final PrintWriter goldDepPw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePrefix + ".kworst.gold.deps"), "UTF-8")));
        System.out.printf("F1s of %d worst trees:\n", worstKTreesToEmit);
        for (int i = 0; queue.peek() != null && i < worstKTreesToEmit; i++) {
            final Triple<Double, Tree, Tree> trees = queue.poll();
            System.out.println(trees.first());
            //Output the trees
            goldPw.println(trees.second().toString());
            guessPw.println(trees.third().toString());
            //Output the set differences
            Set<Constituent> goldDeps = Generics.newHashSet();
            goldDeps.addAll(trees.second().constituents(cFact));
            goldDeps.removeAll(trees.third().constituents(cFact));
            for (Constituent c : goldDeps) goldDepPw.print(c.toString() + "  ");
            goldDepPw.println();
            Set<Constituent> guessDeps = Generics.newHashSet();
            guessDeps.addAll(trees.third().constituents(cFact));
            guessDeps.removeAll(trees.second().constituents(cFact));
            for (Constituent c : guessDeps) guessDepPw.print(c.toString() + "  ");
            guessDepPw.println();
        }
        guessPw.close();
        goldPw.close();
        goldDepPw.close();
        guessDepPw.close();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}
Also used : ConstituentFactory(edu.stanford.nlp.trees.ConstituentFactory) LabeledScoredConstituentFactory(edu.stanford.nlp.trees.LabeledScoredConstituentFactory) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BufferedWriter(java.io.BufferedWriter) LabeledScoredConstituentFactory(edu.stanford.nlp.trees.LabeledScoredConstituentFactory) FileOutputStream(java.io.FileOutputStream) Tree(edu.stanford.nlp.trees.Tree) OutputStreamWriter(java.io.OutputStreamWriter) Constituent(edu.stanford.nlp.trees.Constituent) PrintWriter(java.io.PrintWriter)

Aggregations

Constituent (edu.stanford.nlp.trees.Constituent)1 ConstituentFactory (edu.stanford.nlp.trees.ConstituentFactory)1 LabeledScoredConstituentFactory (edu.stanford.nlp.trees.LabeledScoredConstituentFactory)1 Tree (edu.stanford.nlp.trees.Tree)1 BufferedWriter (java.io.BufferedWriter)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1