File size: 840 Bytes
6680682
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from abc import ABC
from typing import *

from allennlp.training.metrics import Metric


class BaseF(Metric, ABC):
    def __init__(self, prefix: str):
        self.tp = self.fp = self.fn = 0
        self.prefix = prefix

    def reset(self) -> None:
        self.tp = self.fp = self.fn = 0

    def get_metric(
            self, reset: bool
    ) -> Union[float, Tuple[float, ...], Dict[str, float], Dict[str, List[float]]]:
        precision = self.tp * 100 / (self.tp + self.fp) if self.tp > 0 else 0.
        recall = self.tp * 100 / (self.tp + self.fn) if self.tp > 0 else 0.
        rst = {
            f'{self.prefix}_p': precision,
            f'{self.prefix}_r': recall,
            f'{self.prefix}_f': 2 / (1 / precision + 1 / recall) if self.tp > 0 else 0.
        }
        if reset:
            self.reset()
        return rst