File size: 548 Bytes
2f044c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def safe_divide(num: float, den: float) -> float:
    if den == 0:
        return 0
    else:
        return num / den


def f1_measure(precision: float, recall: float) -> float:
    if precision == 0 or recall == 0:
        return 0.0
    return safe_divide(2 * precision * recall, (precision + recall))


def compute_metrics(total_correct, total_preds, total_gold):
    precision = safe_divide(total_correct, total_preds)
    recall = safe_divide(total_correct, total_gold)
    f1 = f1_measure(precision, recall)
    return precision, recall, f1