maksymdolgikh commited on
Commit
473b250
1 Parent(s): 552ef3e

initial commit

Browse files
Files changed (4) hide show
  1. README.md +165 -6
  2. app.py +11 -0
  3. requirements.txt +2 -0
  4. seqeval_with_fbetal.py +179 -0
README.md CHANGED
@@ -1,13 +1,172 @@
1
  ---
2
- title: Seqeval With Fbeta
3
- emoji: 🦀
4
- colorFrom: yellow
5
  colorTo: red
6
  sdk: gradio
7
- sdk_version: 4.19.1
8
  app_file: app.py
9
  pinned: false
10
- license: apache-2.0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: seqeval
3
+ emoji: 🤗
4
+ colorFrom: blue
5
  colorTo: red
6
  sdk: gradio
7
+ sdk_version: 3.19.1
8
  app_file: app.py
9
  pinned: false
10
+ tags:
11
+ - evaluate
12
+ - metric
13
+ description: >-
14
+ seqeval is a Python framework for sequence labeling evaluation.
15
+ seqeval can evaluate the performance of chunking tasks such as named-entity recognition, part-of-speech tagging, semantic role labeling and so on.
16
+
17
+ This is well-tested by using the Perl script conlleval, which can be used for
18
+ measuring the performance of a system that has processed the CoNLL-2000 shared task data.
19
+
20
+ seqeval supports following formats:
21
+ IOB1
22
+ IOB2
23
+ IOE1
24
+ IOE2
25
+ IOBES
26
+
27
+ See the [README.md] file at https://github.com/chakki-works/seqeval for more information.
28
  ---
29
 
30
+ # Metric Card for seqeval
31
+
32
+ Modified version of [seqeval](https://huggingface.co/spaces/evaluate-metric/seqeval) metric that include optional Fβ score.
33
+
34
+ ## Metric description
35
+
36
+ seqeval is a Python framework for sequence labeling evaluation. seqeval can evaluate the performance of chunking tasks such as named-entity recognition, part-of-speech tagging, semantic role labeling and so on.
37
+
38
+
39
+ ## How to use
40
+
41
+ Seqeval produces labelling scores along with its sufficient statistics from a source against one or more references.
42
+
43
+ It takes two mandatory arguments:
44
+
45
+ `predictions`: a list of lists of predicted labels, i.e. estimated targets as returned by a tagger.
46
+
47
+ `references`: a list of lists of reference labels, i.e. the ground truth/target values.
48
+
49
+ It can also take several optional arguments:
50
+
51
+ `beta`: the weight beta of micro Fβ-score
52
+
53
+ `suffix` (boolean): `True` if the IOB tag is a suffix (after type) instead of a prefix (before type), `False` otherwise. The default value is `False`, i.e. the IOB tag is a prefix (before type).
54
+
55
+ `scheme`: the target tagging scheme, which can be one of [`IOB1`, `IOB2`, `IOE1`, `IOE2`, `IOBES`, `BILOU`]. The default value is `None`.
56
+
57
+ `mode`: whether to count correct entity labels with incorrect I/B tags as true positives or not. If you want to only count exact matches, pass `mode="strict"` and a specific `scheme` value. The default is `None`.
58
+
59
+ `sample_weight`: An array-like of shape (n_samples,) that provides weights for individual samples. The default is `None`.
60
+
61
+ `zero_division`: Which value to substitute as a metric value when encountering zero division. Should be one of [`0`,`1`,`"warn"`]. `"warn"` acts as `0`, but the warning is raised.
62
+
63
+
64
+ ```python
65
+ >>> seqeval = evaluate.load('seqeval')
66
+ >>> predictions = [['O', 'O', 'B-MISC', 'I-MISC', 'I-MISC', 'I-MISC', 'O'], ['B-PER', 'I-PER', 'O']]
67
+ >>> references = [['O', 'O', 'O', 'B-MISC', 'I-MISC', 'I-MISC', 'O'], ['B-PER', 'I-PER', 'O']]
68
+ >>> results = seqeval.compute(predictions=predictions, references=references)
69
+ ```
70
+
71
+ ## Output values
72
+
73
+ This metric returns a dictionary with a summary of scores for overall and per type:
74
+
75
+ Overall:
76
+
77
+ `accuracy`: the average [accuracy](https://huggingface.co/metrics/accuracy), on a scale between 0.0 and 1.0.
78
+
79
+ `precision`: the average [precision](https://huggingface.co/metrics/precision), on a scale between 0.0 and 1.0.
80
+
81
+ `recall`: the average [recall](https://huggingface.co/metrics/recall), on a scale between 0.0 and 1.0.
82
+
83
+ `f1`: the average [F1 score](https://huggingface.co/metrics/f1), which is the harmonic mean of the precision and recall. It also has a scale of 0.0 to 1.0.
84
+
85
+ `fbeta`: the micro Fβ score.
86
+
87
+ Per type (e.g. `MISC`, `PER`, `LOC`,...):
88
+
89
+ `precision`: the average [precision](https://huggingface.co/metrics/precision), on a scale between 0.0 and 1.0.
90
+
91
+ `recall`: the average [recall](https://huggingface.co/metrics/recall), on a scale between 0.0 and 1.0.
92
+
93
+ `f1`: the average [F1 score](https://huggingface.co/metrics/f1), on a scale between 0.0 and 1.0.
94
+
95
+ `fbeta`: the micro Fβ score.
96
+
97
+
98
+ ### Values from popular papers
99
+ The 1995 "Text Chunking using Transformation-Based Learning" [paper](https://aclanthology.org/W95-0107) reported a baseline recall of 81.9% and a precision of 78.2% using non Deep Learning-based methods.
100
+
101
+ More recently, seqeval continues being used for reporting performance on tasks such as [named entity detection](https://www.mdpi.com/2306-5729/6/8/84/htm) and [information extraction](https://ieeexplore.ieee.org/abstract/document/9697942/).
102
+
103
+
104
+ ## Examples
105
+
106
+ Maximal values (full match) :
107
+
108
+ ```python
109
+ >>> seqeval = evaluate.load('seqeval')
110
+ >>> predictions = [['O', 'O', 'B-MISC', 'I-MISC', 'I-MISC', 'I-MISC', 'O'], ['B-PER', 'I-PER', 'O']]
111
+ >>> references = [['O', 'O', 'B-MISC', 'I-MISC', 'I-MISC', 'I-MISC', 'O'], ['B-PER', 'I-PER', 'O']]
112
+ >>> results = seqeval.compute(predictions=predictions, references=references)
113
+ >>> print(results)
114
+ {'MISC': {'precision': 1.0, 'recall': 1.0, 'f1': 1.0, 'number': 1}, 'PER': {'precision': 1.0, 'recall': 1.0, 'f1': 1.0, 'number': 1}, 'overall_precision': 1.0, 'overall_recall': 1.0, 'overall_f1': 1.0, 'overall_accuracy': 1.0}
115
+
116
+ ```
117
+
118
+ Minimal values (no match):
119
+
120
+ ```python
121
+ >>> seqeval = evaluate.load('seqeval')
122
+ >>> predictions = [['O', 'B-MISC', 'I-MISC'], ['B-PER', 'I-PER', 'O']]
123
+ >>> references = [['B-MISC', 'O', 'O'], ['I-PER', '0', 'I-PER']]
124
+ >>> results = seqeval.compute(predictions=predictions, references=references)
125
+ >>> print(results)
126
+ {'MISC': {'precision': 0.0, 'recall': 0.0, 'f1': 0.0, 'number': 1}, 'PER': {'precision': 0.0, 'recall': 0.0, 'f1': 0.0, 'number': 2}, '_': {'precision': 0.0, 'recall': 0.0, 'f1': 0.0, 'number': 1}, 'overall_precision': 0.0, 'overall_recall': 0.0, 'overall_f1': 0.0, 'overall_accuracy': 0.0}
127
+ ```
128
+
129
+ Partial match:
130
+
131
+ ```python
132
+ >>> seqeval = evaluate.load('seqeval')
133
+ >>> predictions = [['O', 'O', 'B-MISC', 'I-MISC', 'I-MISC', 'I-MISC', 'O'], ['B-PER', 'I-PER', 'O']]
134
+ >>> references = [['O', 'O', 'O', 'B-MISC', 'I-MISC', 'I-MISC', 'O'], ['B-PER', 'I-PER', 'O']]
135
+ >>> results = seqeval.compute(predictions=predictions, references=references)
136
+ >>> print(results)
137
+ {'MISC': {'precision': 0.0, 'recall': 0.0, 'f1': 0.0, 'number': 1}, 'PER': {'precision': 1.0, 'recall': 1.0, 'f1': 1.0, 'number': 1}, 'overall_precision': 0.5, 'overall_recall': 0.5, 'overall_f1': 0.5, 'overall_accuracy': 0.8}
138
+ ```
139
+
140
+ ## Limitations and bias
141
+
142
+ seqeval supports following IOB formats (short for inside, outside, beginning) : `IOB1`, `IOB2`, `IOE1`, `IOE2`, `IOBES`, `IOBES` (only in strict mode) and `BILOU` (only in strict mode).
143
+
144
+ For more information about IOB formats, refer to the [Wikipedia page](https://en.wikipedia.org/wiki/Inside%E2%80%93outside%E2%80%93beginning_(tagging)) and the description of the [CoNLL-2000 shared task](https://aclanthology.org/W02-2024).
145
+
146
+
147
+ ## Citation
148
+
149
+ ```bibtex
150
+ @inproceedings{ramshaw-marcus-1995-text,
151
+ title = "Text Chunking using Transformation-Based Learning",
152
+ author = "Ramshaw, Lance and
153
+ Marcus, Mitch",
154
+ booktitle = "Third Workshop on Very Large Corpora",
155
+ year = "1995",
156
+ url = "https://www.aclweb.org/anthology/W95-0107",
157
+ }
158
+ ```
159
+
160
+ ```bibtex
161
+ @misc{seqeval,
162
+ title={{seqeval}: A Python framework for sequence labeling evaluation},
163
+ url={https://github.com/chakki-works/seqeval},
164
+ note={Software available from https://github.com/chakki-works/seqeval},
165
+ author={Hiroki Nakayama},
166
+ year={2018},
167
+ }
168
+ ```
169
+
170
+ ## Further References
171
+ - [README for seqeval at GitHub](https://github.com/chakki-works/seqeval)
172
+ - [CoNLL-2000 shared task](https://www.clips.uantwerpen.be/conll2002/ner/bin/conlleval.txt)
app.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+
3
+ import evaluate
4
+ from evaluate.utils import launch_gradio_widget
5
+
6
+
7
+ sys.path = [p for p in sys.path if p != "/home/user/app"]
8
+ module = evaluate.load("seqeval_with_fbetal")
9
+ sys.path = ["/home/user/app"] + sys.path
10
+
11
+ launch_gradio_widget(module)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ git+https://github.com/huggingface/evaluate@8dfe05784099fb9af55b8e77793205a3b7c86465
2
+ seqeval
seqeval_with_fbetal.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Evaluate Authors.
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
+ """ seqeval metric. """
15
+
16
+ import importlib
17
+ from typing import List, Optional, Union
18
+
19
+ import datasets
20
+ from seqeval_with_fbetal.metrics import accuracy_score, classification_report
21
+
22
+ import evaluate
23
+
24
+
25
+ _CITATION = """\
26
+ @inproceedings{ramshaw-marcus-1995-text,
27
+ title = "Text Chunking using Transformation-Based Learning",
28
+ author = "Ramshaw, Lance and
29
+ Marcus, Mitch",
30
+ booktitle = "Third Workshop on Very Large Corpora",
31
+ year = "1995",
32
+ url = "https://www.aclweb.org/anthology/W95-0107",
33
+ }
34
+ @misc{seqeval,
35
+ title={{seqeval}: A Python framework for sequence labeling evaluation},
36
+ url={https://github.com/chakki-works/seqeval},
37
+ note={Software available from https://github.com/chakki-works/seqeval},
38
+ author={Hiroki Nakayama},
39
+ year={2018},
40
+ }
41
+ """
42
+
43
+ _DESCRIPTION = """\
44
+ seqeval is a Python framework for sequence labeling evaluation.
45
+ seqeval can evaluate the performance of chunking tasks such as named-entity recognition, part-of-speech tagging, semantic role labeling and so on.
46
+
47
+ This is well-tested by using the Perl script conlleval, which can be used for
48
+ measuring the performance of a system that has processed the CoNLL-2000 shared task data.
49
+
50
+ seqeval supports following formats:
51
+ IOB1
52
+ IOB2
53
+ IOE1
54
+ IOE2
55
+ IOBES
56
+
57
+ See the [README.md] file at https://github.com/chakki-works/seqeval for more information.
58
+ """
59
+
60
+ _KWARGS_DESCRIPTION = """
61
+ Produces labelling scores along with its sufficient statistics
62
+ from a source against one or more references.
63
+
64
+ Args:
65
+ predictions: List of List of predicted labels (Estimated targets as returned by a tagger)
66
+ references: List of List of reference labels (Ground truth (correct) target values)
67
+ beta: Weight for the F-score
68
+ suffix: True if the IOB prefix is after type, False otherwise. default: False
69
+ scheme: Specify target tagging scheme. Should be one of ["IOB1", "IOB2", "IOE1", "IOE2", "IOBES", "BILOU"].
70
+ default: None
71
+ mode: Whether to count correct entity labels with incorrect I/B tags as true positives or not.
72
+ If you want to only count exact matches, pass mode="strict". default: None.
73
+ sample_weight: Array-like of shape (n_samples,), weights for individual samples. default: None
74
+ zero_division: Which value to substitute as a metric value when encountering zero division. Should be on of 0, 1,
75
+ "warn". "warn" acts as 0, but the warning is raised.
76
+
77
+ Returns:
78
+ 'scores': dict. Summary of the scores for overall and per type
79
+ Overall:
80
+ 'accuracy': accuracy,
81
+ 'precision': precision,
82
+ 'recall': recall,
83
+ 'f1': F1 score, also known as balanced F-score or F-measure,
84
+ 'fbeta': F-score with weight beta
85
+ Per type:
86
+ 'precision': precision,
87
+ 'recall': recall,
88
+ 'f1': F1 score, also known as balanced F-score or F-measure,
89
+ 'fbeta': F-score with weight beta
90
+ Examples:
91
+
92
+ >>> predictions = [['O', 'O', 'B-MISC', 'I-MISC', 'I-MISC', 'I-MISC', 'O'], ['B-PER', 'I-PER', 'O']]
93
+ >>> references = [['O', 'O', 'O', 'B-MISC', 'I-MISC', 'I-MISC', 'O'], ['B-PER', 'I-PER', 'O']]
94
+ >>> seqeval = evaluate.load("seqeval")
95
+ >>> results = seqeval.compute(predictions=predictions, references=references, beta=1.0)
96
+ >>> print(list(results.keys()))
97
+ ['MISC', 'PER', 'overall_precision', 'overall_recall', 'overall_f1', 'overall_accuracy']
98
+ >>> print(results["overall_f1"])
99
+ 0.5
100
+ >>> print(results["PER"]["f1"])
101
+ 1.0
102
+ """
103
+
104
+
105
+ @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
106
+ class Seqeval(evaluate.Metric):
107
+ def _info(self):
108
+ return evaluate.MetricInfo(
109
+ description=_DESCRIPTION,
110
+ citation=_CITATION,
111
+ homepage="https://github.com/chakki-works/seqeval",
112
+ inputs_description=_KWARGS_DESCRIPTION,
113
+ features=datasets.Features(
114
+ {
115
+ "predictions": datasets.Sequence(datasets.Value("string", id="label"), id="sequence"),
116
+ "references": datasets.Sequence(datasets.Value("string", id="label"), id="sequence"),
117
+ }
118
+ ),
119
+ codebase_urls=["https://github.com/chakki-works/seqeval"],
120
+ reference_urls=["https://github.com/chakki-works/seqeval"],
121
+ )
122
+
123
+ def _compute(
124
+ self,
125
+ predictions,
126
+ references,
127
+ beta: float = 1.0,
128
+ suffix: bool = False,
129
+ scheme: Optional[str] = None,
130
+ mode: Optional[str] = None,
131
+ sample_weight: Optional[List[int]] = None,
132
+ zero_division: Union[str, int] = "warn",
133
+ ):
134
+ if scheme is not None:
135
+ try:
136
+ scheme_module = importlib.import_module("seqeval.scheme")
137
+ scheme = getattr(scheme_module, scheme)
138
+ except AttributeError:
139
+ raise ValueError(f"Scheme should be one of [IOB1, IOB2, IOE1, IOE2, IOBES, BILOU], got {scheme}")
140
+ report = classification_report(
141
+ y_true=references,
142
+ y_pred=predictions,
143
+ suffix=suffix,
144
+ output_dict=True,
145
+ scheme=scheme,
146
+ mode=mode,
147
+ sample_weight=sample_weight,
148
+ zero_division=zero_division,
149
+ )
150
+ report.pop("macro avg")
151
+ report.pop("weighted avg")
152
+
153
+ if beta != 1.0:
154
+ beta2 = beta ** 2
155
+ for k, v in report.items():
156
+ denom = beta2 * v["precision"] + v["recall"]
157
+ if denom == 0:
158
+ denom += 1
159
+ v[f"f{beta}-score"] = (1 + beta2) * v["precision"] * v["recall"] / denom
160
+
161
+ overall_score = report.pop("micro avg")
162
+
163
+ scores = {
164
+ type_name: {
165
+ "precision": score["precision"],
166
+ "recall": score["recall"],
167
+ "f1": score["f1-score"],
168
+ f"f{beta}": score[f"f{beta}-score"],
169
+ "number": score["support"],
170
+ }
171
+ for type_name, score in report.items()
172
+ }
173
+ scores["overall_precision"] = overall_score["precision"]
174
+ scores["overall_recall"] = overall_score["recall"]
175
+ scores["overall_f1"] = overall_score["f1-score"]
176
+ scores[f"overall_f{beta}"] = overall_score[f"f{beta}-score"]
177
+ scores["overall_accuracy"] = accuracy_score(y_true=references, y_pred=predictions)
178
+
179
+ return scores