| 45 | |
| 46 | |
| 47 | class Metric: |
| 48 | def __init__(self): |
| 49 | self.tp = 0. |
| 50 | self.gold_num = 0. |
| 51 | self.pred_num = 0. |
| 52 | |
| 53 | @staticmethod |
| 54 | def safe_div(a, b): |
| 55 | if b == 0.: |
| 56 | return 0. |
| 57 | else: |
| 58 | return a / b |
| 59 | |
| 60 | def compute_f1(self, prefix=''): |
| 61 | tp = self.tp |
| 62 | pred_num = self.pred_num |
| 63 | gold_num = self.gold_num |
| 64 | p, r = self.safe_div(tp, pred_num), self.safe_div(tp, gold_num) |
| 65 | return {prefix + 'tp': tp, |
| 66 | prefix + 'gold': gold_num, |
| 67 | prefix + 'pred': pred_num, |
| 68 | prefix + 'P': p * 100, |
| 69 | prefix + 'R': r * 100, |
| 70 | prefix + 'F1': self.safe_div(2 * p * r, p + r) * 100 |
| 71 | } |
| 72 | |
| 73 | def count_instance(self, gold_list, pred_list, verbose=False): |
| 74 | if verbose: |
| 75 | print("Gold:", gold_list) |
| 76 | print("Pred:", pred_list) |
| 77 | self.gold_num += len(gold_list) |
| 78 | self.pred_num += len(pred_list) |
| 79 | |
| 80 | dup_gold_list = deepcopy(gold_list) |
| 81 | for pred in pred_list: |
| 82 | if pred in dup_gold_list: |
| 83 | self.tp += 1 |
| 84 | dup_gold_list.remove(pred) |