gasmichel's picture
Upload folder using huggingface_hub
1a89bb0 verified
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)