File size: 2,329 Bytes
1a89bb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import json 
import sys 
from collections import defaultdict

if __name__ == "__main__":
    path = sys.argv[1]
    use_cf = int(sys.argv[2])
    TRSHLD = int(sys.argv[3])
    annotations = json.load(open('verbs/annotations2_gmichel.json'))
    out = json.load(open(path))

    corr = defaultdict(lambda :0)
    misstype  = corr.copy()
    fp = corr.copy()
    fps = []
    tot = corr.copy()
    print(use_cf) 
    if use_cf : 
        vals = []       
        for i in out : 
            v = list(i.values())
            if len(v) > 0 :
                vals.extend(list(v[0].keys()))
        keys = set(vals)
        if 'confidence' in keys : 
            new_out = [] 
            for i in out : 
                o = {}
                if len(i) > 0 :
                    for k,v in i.items() : 
                        if "confidence" in v :
                            if v["confidence"] >= TRSHLD: 
                                o[k] = v
                new_out.append(o)
            out = new_out
    from collections import defaultdict
    corr = defaultdict(lambda :0)
    misstype  = corr.copy()
    fp = corr.copy()
    fps = []
    tot = corr.copy()
    for idx in range(len(annotations)) : 
        pred = out[idx]
        
        gt  = annotations[idx]
        for gt_word, info in gt.items() : 
            if gt_word in pred : 
                if "type" not in pred[gt_word] : 
                    continue
                if pred[gt_word]["type"] == info["type"] : 
                    corr[info["type"]] += 1
                else : 
                    misstype[info["type"]] += 1
                    # corr[info["type"]] += 1
                    # print(idx, gt_word, pred[gt_word]["type"],info["type"])
            tot[info["type"]] += 1
        for pred_word, info in pred.items() : 
            if "type" not in info : 
                continue 
            if pred_word not in gt : 
                fp[info["type"]] += 1
                fps.append((idx, pred_word, info["type"]))

    for tp in tot : 
        prec = corr[tp] / max(1, (corr[tp] + fp[tp])) 
        recall = corr[tp] / tot[tp] 
        f1 = 2 * (prec * recall) / max(1, prec + recall)
        print(f"[{tp}: {tot[tp]}]")
        print(f"\tRecall: {recall:0.2f}\n\tPrec: {prec:0.2f}")
        print(f"\tF1: {f1:0.2f}")
    print(misstype)