| 137 | return instance['text'], trigger_list, role_list |
| 138 | |
| 139 | class Metric: |
| 140 | def __init__(self): |
| 141 | self.tp = 0. |
| 142 | self.gold_num = 0. |
| 143 | self.pred_num = 0. |
| 144 | |
| 145 | @staticmethod |
| 146 | def safe_div(a, b): |
| 147 | if b == 0.: |
| 148 | return 0. |
| 149 | else: |
| 150 | return a / b |
| 151 | |
| 152 | def compute_f1(self, prefix=''): |
| 153 | tp = self.tp |
| 154 | pred_num = self.pred_num |
| 155 | gold_num = self.gold_num |
| 156 | p, r = self.safe_div(tp, pred_num), self.safe_div(tp, gold_num) |
| 157 | return {prefix + 'tp': tp, |
| 158 | prefix + 'gold': gold_num, |
| 159 | prefix + 'pred': pred_num, |
| 160 | prefix + 'P': p * 100, |
| 161 | prefix + 'R': r * 100, |
| 162 | prefix + 'F1': self.safe_div(2 * p * r, p + r) * 100 |
| 163 | } |
| 164 | |
| 165 | def count_instance(self, gold_list, pred_list, verbose=False, text=None): |
| 166 | if verbose: |
| 167 | print("Gold:", gold_list) |
| 168 | print("Pred:", pred_list) |
| 169 | self.gold_num += len(gold_list) |
| 170 | self.pred_num += len(pred_list) |
| 171 | |
| 172 | dup_gold_list = deepcopy(gold_list) |
| 173 | for pred in pred_list: |
| 174 | if pred in dup_gold_list: |
| 175 | self.tp += 1 |
| 176 | dup_gold_list.remove(pred) |
| 177 | else: |
| 178 | print("text: ", text) |
| 179 | print("gold_list: ", gold_list) |
| 180 | print("no tp pred:", pred) |
| 181 | pass |
| 182 | |
| 183 | def main(): |
| 184 | parser = argparse.ArgumentParser() |