dannashao commited on
Commit
c32baa5
β€’
1 Parent(s): bad1e68
Files changed (2) hide show
  1. README.md +7 -1
  2. span_metric.py +123 -0
README.md CHANGED
@@ -7,6 +7,12 @@ sdk: gradio
7
  sdk_version: 4.31.0
8
  app_file: app.py
9
  pinned: false
 
 
 
 
 
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
7
  sdk_version: 4.31.0
8
  app_file: app.py
9
  pinned: false
10
+ tags:
11
+ - evaluate
12
+ - metric
13
+ description: >-
14
+ This metric calculates both Token Overlap and Span Agreement precision, recall and f1 scores.
15
  ---
16
 
17
+ ## Description
18
+ This metric calculates both Token Overlap and Span Agreement precision, recall and f1 scores. This script is adapted from seqeval.
span_metric.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """This metric calculates both Token Overlap and Span Agreement precision, recall and f1 scores."""
15
+
16
+ import datasets
17
+
18
+
19
+
20
+ _CITATION = """\
21
+ @inproceedings{morante-blanco-2012-sem,
22
+ title = "*{SEM} 2012 Shared Task: Resolving the Scope and Focus of Negation",
23
+ author = "Morante, Roser and Blanco, Eduardo",
24
+ booktitle = "*{SEM} 2012: The First Joint Conference on Lexical and Computational Semantics {--} Volume 1: Proceedings of the main conference and the shared task, and Volume 2: Proceedings of the Sixth International Workshop on Semantic Evaluation ({S}em{E}val 2012)",
25
+ month = "7-8 " # jun,
26
+ year = "2012",
27
+ address = "Montr{\'e}al, Canada",
28
+ publisher = "Association for Computational Linguistics",
29
+ url = "https://aclanthology.org/S12-1035",
30
+ pages = "265--274",
31
+ }
32
+ """
33
+
34
+ # TODO: Add description of the metric here
35
+ _DESCRIPTION = """\
36
+ This metric calculates both Token Overlap and Span Agreement precision, recall and f1 scores. This script is adapted from seqeval.
37
+ """
38
+
39
+
40
+ # TODO: Add description of the arguments of the metric here
41
+ _KWARGS_DESCRIPTION = """
42
+ Calculates how good are predictions given some references, using certain scores
43
+ Args:
44
+ predictions: List of List of predicted labels.
45
+ references: List of List of reference labels.
46
+ Returns:
47
+ 'token_precision': precision,
48
+ 'token_recall': recall,
49
+ 'token_f1': F1 score for token overlap
50
+
51
+ 'span_precision': precision,
52
+ 'span_recall': recall,
53
+ 'span_f1': F1 score for span agreement
54
+
55
+ """
56
+
57
+
58
+ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
59
+ class SpanAgree(datasets.Metric):
60
+ """Calculates span agreement metric."""
61
+
62
+ def _info(self):
63
+ return datasets.MetricInfo(
64
+ description=_DESCRIPTION,
65
+ citation=_CITATION,
66
+ inputs_description=_KWARGS_DESCRIPTION,
67
+
68
+ features=datasets.Features({
69
+ 'predictions': datasets.Sequence(datasets.Value("int8", id="label"), id="sequence"),
70
+ "references": datasets.Sequence(datasets.Value("int8", id="label"), id="sequence"),
71
+ }),
72
+
73
+ homepage="https://github.com/dannashao",
74
+ codebase_urls=["https://github.com/dannashao"],
75
+ reference_urls=["https://github.com/dannashao"]
76
+ )
77
+
78
+
79
+ def _compute(self, predictions, references):
80
+ """Returns the scores"""
81
+ # TOKEN LEVEL
82
+ tn, fp, fn, tp = 0,0,0,0
83
+ for span_true, span_pred in zip(references, predictions):
84
+ for token_true, token_pred in zip(span_true, span_pred):
85
+ if token_true == 1:
86
+ if token_pred == 1:
87
+ tp += 1
88
+ else:
89
+ fn += 1
90
+ else:
91
+ if token_pred == 1:
92
+ fp += 1
93
+ else:
94
+ tn += 1
95
+ precision = tp / (tp + fp) if tp + fp > 0 else 0
96
+ recall = tp / (tp + fn) if tp + fn > 0 else 0
97
+ f1 = 2 * (precision * recall) / (precision + recall) if precision + recall > 0 else 0
98
+
99
+ # SPAN LEVEL
100
+ tn, fp, fn, tp = 0,0,0,0
101
+ for span_true, span_pred in zip(references, predictions):
102
+ if 1 in span_true:
103
+ if span_true == span_pred:
104
+ tp += 1
105
+ elif all([(yt == 0 or (yt == 1 and predictions[i] == 1)) for i, yt in enumerate(references)]):
106
+ fp += 1
107
+ else:
108
+ fp += 1
109
+ fn += 1
110
+ else:
111
+ if 1 in span_pred:
112
+ fp += 1
113
+ fn += 1
114
+ else:
115
+ tn += 1
116
+
117
+ span_precision = tp / (tp + fp) if tp + fp > 0 else 0
118
+ span_recall = tp / (tp + fn) if tp + fn > 0 else 0
119
+ span_f1 = 2 * (span_precision * span_recall) / (span_precision + span_recall) if span_precision + span_recall > 0 else 0
120
+
121
+ scores = {"token_precision":precision, "token_recall":recall, "token_f1":f1,
122
+ "span_precision":span_precision, "span_recall":span_recall, "span_f1":span_f1}
123
+ return scores