Search in sources :

Example 1 with CollinsRelation

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

the class CollinsDepEval method display.

@Override
public void display(boolean verbose, PrintWriter pw) {
    final NumberFormat nf = new DecimalFormat("0.00");
    final Set<CollinsRelation> cats = Generics.newHashSet();
    final Random rand = new Random();
    cats.addAll(precisions.keySet());
    cats.addAll(recalls.keySet());
    Map<Double, CollinsRelation> f1Map = new TreeMap<>();
    for (CollinsRelation cat : cats) {
        double pnum2 = pnums2.getCount(cat);
        double rnum2 = rnums2.getCount(cat);
        //(num > 0.0 ? precision/num : 0.0);
        double prec = precisions2.getCount(cat) / pnum2;
        //(num > 0.0 ? recall/num : 0.0);
        double rec = recalls2.getCount(cat) / rnum2;
        //(num > 0.0 ? f1/num : 0.0);
        double f1 = 2.0 / (1.0 / prec + 1.0 / rec);
        if (new Double(f1).equals(Double.NaN))
            f1 = -1.0;
        if (f1Map.containsKey(f1))
            f1Map.put(f1 + (rand.nextDouble() / 1000.0), cat);
        else
            f1Map.put(f1, cat);
    }
    pw.println(" Abstract Collins Dependencies -- final statistics");
    pw.println("================================================================================");
    for (CollinsRelation cat : f1Map.values()) {
        double pnum2 = pnums2.getCount(cat);
        double rnum2 = rnums2.getCount(cat);
        //(num > 0.0 ? precision/num : 0.0);
        double prec = precisions2.getCount(cat) / pnum2;
        //(num > 0.0 ? recall/num : 0.0);
        double rec = recalls2.getCount(cat) / rnum2;
        //(num > 0.0 ? f1/num : 0.0);
        double f1 = 2.0 / (1.0 / prec + 1.0 / rec);
        pw.println(cat + "\tLP: " + ((pnum2 == 0.0) ? " N/A" : nf.format(prec)) + "\tguessed: " + (int) pnum2 + "\tLR: " + ((rnum2 == 0.0) ? " N/A" : nf.format(rec)) + "\tgold:  " + (int) rnum2 + "\tF1: " + ((pnum2 == 0.0 || rnum2 == 0.0) ? " N/A" : nf.format(f1)));
    }
    pw.println("================================================================================");
}
Also used : Random(java.util.Random) DecimalFormat(java.text.DecimalFormat) CollinsRelation(edu.stanford.nlp.trees.CollinsRelation) TreeMap(java.util.TreeMap) NumberFormat(java.text.NumberFormat)

Example 2 with CollinsRelation

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

the class CollinsDepEval method evaluate.

@Override
public void evaluate(Tree guess, Tree gold, PrintWriter pw) {
    if (gold == null || guess == null) {
        System.err.printf("%s: Cannot compare against a null gold or guess tree!\n", this.getClass().getName());
        return;
    }
    if (DEBUG)
        System.out.println("guess:");
    Map<CollinsRelation, Set<CollinsDependency>> guessDeps = makeCollinsObjects(guess);
    if (DEBUG)
        System.out.println("gold:");
    Map<CollinsRelation, Set<CollinsDependency>> goldDeps = makeCollinsObjects(gold);
    Set<CollinsRelation> relations = Generics.newHashSet();
    relations.addAll(guessDeps.keySet());
    relations.addAll(goldDeps.keySet());
    num += 1.0;
    for (CollinsRelation rel : relations) {
        Set<CollinsDependency> thisGuessDeps = guessDeps.get(rel);
        Set<CollinsDependency> thisGoldDeps = goldDeps.get(rel);
        if (thisGuessDeps == null)
            thisGuessDeps = Generics.newHashSet();
        if (thisGoldDeps == null)
            thisGoldDeps = Generics.newHashSet();
        double currentPrecision = precision(thisGuessDeps, thisGoldDeps);
        double currentRecall = precision(thisGoldDeps, thisGuessDeps);
        double currentF1 = (currentPrecision > 0.0 && currentRecall > 0.0 ? 2.0 / (1.0 / currentPrecision + 1.0 / currentRecall) : 0.0);
        precisions.incrementCount(rel, currentPrecision);
        recalls.incrementCount(rel, currentRecall);
        f1s.incrementCount(rel, currentF1);
        precisions2.incrementCount(rel, thisGuessDeps.size() * currentPrecision);
        pnums2.incrementCount(rel, thisGuessDeps.size());
        recalls2.incrementCount(rel, thisGoldDeps.size() * currentRecall);
        rnums2.incrementCount(rel, thisGoldDeps.size());
        if (pw != null && runningAverages) {
            pw.println(rel + "\tP: " + ((int) (currentPrecision * 10000)) / 100.0 + " (sent ave " + ((int) (precisions.getCount(rel) * 10000 / num)) / 100.0 + ") (evalb " + ((int) (precisions2.getCount(rel) * 10000 / pnums2.getCount(rel))) / 100.0 + ")");
            pw.println("\tR: " + ((int) (currentRecall * 10000)) / 100.0 + " (sent ave " + ((int) (recalls.getCount(rel) * 10000 / num)) / 100.0 + ") (evalb " + ((int) (recalls2.getCount(rel) * 10000 / rnums2.getCount(rel))) / 100.0 + ")");
            double cF1 = 2.0 / (rnums2.getCount(rel) / recalls2.getCount(rel) + pnums2.getCount(rel) / precisions2.getCount(rel));
            String emit = str + " F1: " + ((int) (currentF1 * 10000)) / 100.0 + " (sent ave " + ((int) (10000 * f1s.getCount(rel) / num)) / 100.0 + ", evalb " + ((int) (10000 * cF1)) / 100.0 + ")";
            pw.println(emit);
        }
    }
    if (pw != null && runningAverages) {
        pw.println("================================================================================");
    }
}
Also used : Set(java.util.Set) CollinsDependency(edu.stanford.nlp.trees.CollinsDependency) CollinsRelation(edu.stanford.nlp.trees.CollinsRelation)

Example 3 with CollinsRelation

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

the class CollinsDepEval method makeCollinsObjects.

private Map<CollinsRelation, Set<CollinsDependency>> makeCollinsObjects(Tree t) {
    final Map<CollinsRelation, Set<CollinsDependency>> relMap = Generics.newHashMap();
    final Set<CollinsDependency> deps = CollinsDependency.extractNormalizedFromTree(t, startSymbol, hf);
    for (CollinsDependency dep : deps) {
        if (DEBUG)
            System.out.println(dep.toString());
        if (relMap.get(dep.getRelation()) == null)
            relMap.put(dep.getRelation(), Generics.<CollinsDependency>newHashSet());
        relMap.get(dep.getRelation()).add(dep);
    }
    if (DEBUG)
        System.out.println();
    return relMap;
}
Also used : Set(java.util.Set) CollinsDependency(edu.stanford.nlp.trees.CollinsDependency) CollinsRelation(edu.stanford.nlp.trees.CollinsRelation)

Aggregations

CollinsRelation (edu.stanford.nlp.trees.CollinsRelation)3 CollinsDependency (edu.stanford.nlp.trees.CollinsDependency)2 Set (java.util.Set)2 DecimalFormat (java.text.DecimalFormat)1 NumberFormat (java.text.NumberFormat)1 Random (java.util.Random)1 TreeMap (java.util.TreeMap)1