10
10
11
11
class Metric (object ):
12
12
13
- def __lt__ (self , other : ' Metric' ) -> bool :
13
+ def __lt__ (self , other : Metric ) -> bool :
14
14
return self .score < other
15
15
16
- def __le__ (self , other : ' Metric' ) -> bool :
16
+ def __le__ (self , other : Metric ) -> bool :
17
17
return self .score <= other
18
18
19
- def __ge__ (self , other : ' Metric' ) -> bool :
19
+ def __ge__ (self , other : Metric ) -> bool :
20
20
return self .score >= other
21
21
22
- def __gt__ (self , other : ' Metric' ) -> bool :
22
+ def __gt__ (self , other : Metric ) -> bool :
23
23
return self .score > other
24
24
25
+ def __add__ (self , other : Metric ) -> Metric :
26
+ raise NotImplementedError
27
+
25
28
@property
26
29
def score (self ):
27
30
return 0.
@@ -68,6 +71,16 @@ def __call__(
68
71
self .correct_rels += rel_mask_seq .sum ().item ()
69
72
return self
70
73
74
+ def __add__ (self , other : AttachmentMetric ) -> AttachmentMetric :
75
+ metric = AttachmentMetric (self .eps )
76
+ metric .n = self .n + other .n
77
+ metric .n_ucm = self .n_ucm + other .n_ucm
78
+ metric .n_lcm = self .n_lcm + other .n_lcm
79
+ metric .total = self .total + other .total
80
+ metric .correct_arcs = self .correct_arcs + other .correct_arcs
81
+ metric .correct_rels = self .correct_rels + other .correct_rels
82
+ return metric
83
+
71
84
@property
72
85
def score (self ):
73
86
return self .las
@@ -103,6 +116,13 @@ def __init__(self, eps: float = 1e-12) -> SpanMetric:
103
116
self .gold = 0.0
104
117
self .eps = eps
105
118
119
+ def __repr__ (self ):
120
+ s = f"UCM: { self .ucm :6.2%} LCM: { self .lcm :6.2%} "
121
+ s += f"UP: { self .up :6.2%} UR: { self .ur :6.2%} UF: { self .uf :6.2%} "
122
+ s += f"LP: { self .lp :6.2%} LR: { self .lr :6.2%} LF: { self .lf :6.2%} "
123
+
124
+ return s
125
+
106
126
def __call__ (self , preds : List [List [Tuple ]], golds : List [List [Tuple ]]) -> SpanMetric :
107
127
for pred , gold in zip (preds , golds ):
108
128
upred , ugold = Counter ([tuple (span [:- 1 ]) for span in pred ]), Counter ([tuple (span [:- 1 ]) for span in gold ])
@@ -117,12 +137,16 @@ def __call__(self, preds: List[List[Tuple]], golds: List[List[Tuple]]) -> SpanMe
117
137
self .gold += len (gold )
118
138
return self
119
139
120
- def __repr__ (self ):
121
- s = f"UCM: { self .ucm :6.2%} LCM: { self .lcm :6.2%} "
122
- s += f"UP: { self .up :6.2%} UR: { self .ur :6.2%} UF: { self .uf :6.2%} "
123
- s += f"LP: { self .lp :6.2%} LR: { self .lr :6.2%} LF: { self .lf :6.2%} "
124
-
125
- return s
140
+ def __add__ (self , other : SpanMetric ) -> SpanMetric :
141
+ metric = SpanMetric (self .eps )
142
+ metric .n = self .n + other .n
143
+ metric .n_ucm = self .n_ucm + other .n_ucm
144
+ metric .n_lcm = self .n_lcm + other .n_lcm
145
+ metric .utp = self .utp + other .utp
146
+ metric .ltp = self .ltp + other .ltp
147
+ metric .pred = self .pred + other .pred
148
+ metric .gold = self .gold + other .gold
149
+ return metric
126
150
127
151
@property
128
152
def score (self ):
@@ -172,6 +196,9 @@ def __init__(self, eps: float = 1e-12) -> ChartMetric:
172
196
self .gold = 0.0
173
197
self .eps = eps
174
198
199
+ def __repr__ (self ):
200
+ return f"UP: { self .up :6.2%} UR: { self .ur :6.2%} UF: { self .uf :6.2%} P: { self .p :6.2%} R: { self .r :6.2%} F: { self .f :6.2%} "
201
+
175
202
def __call__ (self , preds : torch .Tensor , golds : torch .Tensor ) -> ChartMetric :
176
203
pred_mask = preds .ge (0 )
177
204
gold_mask = golds .ge (0 )
@@ -182,8 +209,13 @@ def __call__(self, preds: torch.Tensor, golds: torch.Tensor) -> ChartMetric:
182
209
self .utp += span_mask .sum ().item ()
183
210
return self
184
211
185
- def __repr__ (self ):
186
- return f"UP: { self .up :6.2%} UR: { self .ur :6.2%} UF: { self .uf :6.2%} P: { self .p :6.2%} R: { self .r :6.2%} F: { self .f :6.2%} "
212
+ def __add__ (self , other : ChartMetric ) -> ChartMetric :
213
+ metric = ChartMetric (self .eps )
214
+ metric .tp = self .tp + other .tp
215
+ metric .utp = self .utp + other .utp
216
+ metric .pred = self .pred + other .pred
217
+ metric .gold = self .gold + other .gold
218
+ return metric
187
219
188
220
@property
189
221
def score (self ):
0 commit comments