lvwerra HF staff commited on
Commit
57be236
1 Parent(s): 229d77b

Update Space (evaluate main: 828c6327)

Browse files
Files changed (4) hide show
  1. README.md +157 -5
  2. app.py +6 -0
  3. requirements.txt +4 -0
  4. ter.py +200 -0
README.md CHANGED
@@ -1,12 +1,164 @@
1
  ---
2
- title: Ter
3
- emoji: 🦀
4
- colorFrom: green
5
- colorTo: purple
6
  sdk: gradio
7
  sdk_version: 3.0.2
8
  app_file: app.py
9
  pinned: false
 
 
 
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: TER
3
+ emoji: 🤗
4
+ colorFrom: blue
5
+ colorTo: red
6
  sdk: gradio
7
  sdk_version: 3.0.2
8
  app_file: app.py
9
  pinned: false
10
+ tags:
11
+ - evaluate
12
+ - metric
13
  ---
14
 
15
+ # Metric Card for TER
16
+
17
+ ## Metric Description
18
+ TER (Translation Edit Rate, also called Translation Error Rate) is a metric to quantify the edit operations that a hypothesis requires to match a reference translation. We use the implementation that is already present in [sacrebleu](https://github.com/mjpost/sacreBLEU#ter), which in turn is inspired by the [TERCOM implementation](https://github.com/jhclark/tercom).
19
+
20
+ The implementation here is slightly different from sacrebleu in terms of the required input format. The length of the references and hypotheses lists need to be the same, so you may need to transpose your references compared to sacrebleu's required input format. See [this github issue](https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534).
21
+
22
+ See the README.md file at https://github.com/mjpost/sacreBLEU#ter for more information.
23
+
24
+
25
+ ## How to Use
26
+ This metric takes, at minimum, predicted sentences and reference sentences:
27
+ ```python
28
+ >>> predictions = ["does this sentence match??",
29
+ ... "what about this sentence?",
30
+ ... "What did the TER metric user say to the developer?"]
31
+ >>> references = [["does this sentence match", "does this sentence match!?!"],
32
+ ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"],
33
+ ... ["Your jokes are...", "...TERrible"]]
34
+ >>> ter = evaluate.load("ter")
35
+ >>> results = ter.compute(predictions=predictions,
36
+ ... references=references,
37
+ ... case_sensitive=True)
38
+ >>> print(results)
39
+ {'score': 150.0, 'num_edits': 15, 'ref_length': 10.0}
40
+ ```
41
+
42
+ ### Inputs
43
+ This metric takes the following as input:
44
+ - **`predictions`** (`list` of `str`): The system stream (a sequence of segments).
45
+ - **`references`** (`list` of `list` of `str`): A list of one or more reference streams (each a sequence of segments).
46
+ - **`normalized`** (`boolean`): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`.
47
+ - **`ignore_punct`** (`boolean`): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`.
48
+ - **`support_zh_ja_chars`** (`boolean`): If `True`, tokenization/normalization supports processing of Chinese characters, as well as Japanese Kanji, Hiragana, Katakana, and Phonetic Extensions of Katakana. Only applies if `normalized = True`. Defaults to `False`.
49
+ - **`case_sensitive`** (`boolean`): If `False`, makes all predictions and references lowercase to ignore differences in case. Defaults to `False`.
50
+
51
+ ### Output Values
52
+ This metric returns the following:
53
+ - **`score`** (`float`): TER score (num_edits / sum_ref_lengths * 100)
54
+ - **`num_edits`** (`int`): The cumulative number of edits
55
+ - **`ref_length`** (`float`): The cumulative average reference length
56
+
57
+ The output takes the following form:
58
+ ```python
59
+ {'score': ter_score, 'num_edits': num_edits, 'ref_length': ref_length}
60
+ ```
61
+
62
+ The metric can take on any value `0` and above. `0` is a perfect score, meaning the predictions exactly match the references and no edits were necessary. Higher scores are worse. Scores above 100 mean that the cumulative number of edits, `num_edits`, is higher than the cumulative length of the references, `ref_length`.
63
+
64
+ #### Values from Popular Papers
65
+
66
+
67
+ ### Examples
68
+ Basic example with only predictions and references as inputs:
69
+ ```python
70
+ >>> predictions = ["does this sentence match??",
71
+ ... "what about this sentence?"]
72
+ >>> references = [["does this sentence match", "does this sentence match!?!"],
73
+ ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]]
74
+ >>> ter = evaluate.load("ter")
75
+ >>> results = ter.compute(predictions=predictions,
76
+ ... references=references,
77
+ ... case_sensitive=True)
78
+ >>> print(results)
79
+ {'score': 62.5, 'num_edits': 5, 'ref_length': 8.0}
80
+ ```
81
+
82
+ Example with `normalization = True`:
83
+ ```python
84
+ >>> predictions = ["does this sentence match??",
85
+ ... "what about this sentence?"]
86
+ >>> references = [["does this sentence match", "does this sentence match!?!"],
87
+ ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]]
88
+ >>> ter = evaluate.load("ter")
89
+ >>> results = ter.compute(predictions=predictions,
90
+ ... references=references,
91
+ ... normalized=True,
92
+ ... case_sensitive=True)
93
+ >>> print(results)
94
+ {'score': 57.14285714285714, 'num_edits': 6, 'ref_length': 10.5}
95
+ ```
96
+
97
+ Example ignoring punctuation and capitalization, and everything matches:
98
+ ```python
99
+ >>> predictions = ["does this sentence match??",
100
+ ... "what about this sentence?"]
101
+ >>> references = [["does this sentence match", "does this sentence match!?!"],
102
+ ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]]
103
+ >>> ter = evaluate.load("ter")
104
+ >>> results = ter.compute(predictions=predictions,
105
+ ... references=references,
106
+ ... ignore_punct=True,
107
+ ... case_sensitive=False)
108
+ >>> print(results)
109
+ {'score': 0.0, 'num_edits': 0, 'ref_length': 8.0}
110
+ ```
111
+
112
+ Example ignoring punctuation and capitalization, but with an extra (incorrect) sample:
113
+ ```python
114
+ >>> predictions = ["does this sentence match??",
115
+ ... "what about this sentence?",
116
+ ... "What did the TER metric user say to the developer?"]
117
+ >>> references = [["does this sentence match", "does this sentence match!?!"],
118
+ ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"],
119
+ ... ["Your jokes are...", "...TERrible"]]
120
+ >>> ter = evaluate.load("ter")
121
+ >>> results = ter.compute(predictions=predictions,
122
+ ... references=references,
123
+ ... ignore_punct=True,
124
+ ... case_sensitive=False)
125
+ >>> print(results)
126
+ {'score': 100.0, 'num_edits': 10, 'ref_length': 10.0}
127
+ ```
128
+
129
+
130
+ ## Limitations and Bias
131
+
132
+
133
+ ## Citation
134
+ ```bibtex
135
+ @inproceedings{snover-etal-2006-study,
136
+ title = "A Study of Translation Edit Rate with Targeted Human Annotation",
137
+ author = "Snover, Matthew and
138
+ Dorr, Bonnie and
139
+ Schwartz, Rich and
140
+ Micciulla, Linnea and
141
+ Makhoul, John",
142
+ booktitle = "Proceedings of the 7th Conference of the Association for Machine Translation in the Americas: Technical Papers",
143
+ month = aug # " 8-12",
144
+ year = "2006",
145
+ address = "Cambridge, Massachusetts, USA",
146
+ publisher = "Association for Machine Translation in the Americas",
147
+ url = "https://aclanthology.org/2006.amta-papers.25",
148
+ pages = "223--231",
149
+ }
150
+ @inproceedings{post-2018-call,
151
+ title = "A Call for Clarity in Reporting {BLEU} Scores",
152
+ author = "Post, Matt",
153
+ booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers",
154
+ month = oct,
155
+ year = "2018",
156
+ address = "Belgium, Brussels",
157
+ publisher = "Association for Computational Linguistics",
158
+ url = "https://www.aclweb.org/anthology/W18-6319",
159
+ pages = "186--191",
160
+ }
161
+ ```
162
+
163
+ ## Further References
164
+ - See [the sacreBLEU github repo](https://github.com/mjpost/sacreBLEU#ter) for more information.
app.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
1
+ import evaluate
2
+ from evaluate.utils import launch_gradio_widget
3
+
4
+
5
+ module = evaluate.load("ter")
6
+ launch_gradio_widget(module)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
1
+ # TODO: fix github to release
2
+ git+https://github.com/huggingface/evaluate.git@b6e6ed7f3e6844b297bff1b43a1b4be0709b9671
3
+ datasets~=2.0
4
+ sacrebleu
ter.py ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2021 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
+ """ TER metric as available in sacrebleu. """
15
+ import datasets
16
+ import sacrebleu as scb
17
+ from packaging import version
18
+ from sacrebleu import TER
19
+
20
+ import evaluate
21
+
22
+
23
+ _CITATION = """\
24
+ @inproceedings{snover-etal-2006-study,
25
+ title = "A Study of Translation Edit Rate with Targeted Human Annotation",
26
+ author = "Snover, Matthew and
27
+ Dorr, Bonnie and
28
+ Schwartz, Rich and
29
+ Micciulla, Linnea and
30
+ Makhoul, John",
31
+ booktitle = "Proceedings of the 7th Conference of the Association for Machine Translation in the Americas: Technical Papers",
32
+ month = aug # " 8-12",
33
+ year = "2006",
34
+ address = "Cambridge, Massachusetts, USA",
35
+ publisher = "Association for Machine Translation in the Americas",
36
+ url = "https://aclanthology.org/2006.amta-papers.25",
37
+ pages = "223--231",
38
+ }
39
+ @inproceedings{post-2018-call,
40
+ title = "A Call for Clarity in Reporting {BLEU} Scores",
41
+ author = "Post, Matt",
42
+ booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers",
43
+ month = oct,
44
+ year = "2018",
45
+ address = "Belgium, Brussels",
46
+ publisher = "Association for Computational Linguistics",
47
+ url = "https://www.aclweb.org/anthology/W18-6319",
48
+ pages = "186--191",
49
+ }
50
+ """
51
+
52
+ _DESCRIPTION = """\
53
+ TER (Translation Edit Rate, also called Translation Error Rate) is a metric to quantify the edit operations that a
54
+ hypothesis requires to match a reference translation. We use the implementation that is already present in sacrebleu
55
+ (https://github.com/mjpost/sacreBLEU#ter), which in turn is inspired by the TERCOM implementation, which can be found
56
+ here: https://github.com/jhclark/tercom.
57
+
58
+ The implementation here is slightly different from sacrebleu in terms of the required input format. The length of
59
+ the references and hypotheses lists need to be the same, so you may need to transpose your references compared to
60
+ sacrebleu's required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534
61
+
62
+ See the README.md file at https://github.com/mjpost/sacreBLEU#ter for more information.
63
+ """
64
+
65
+ _KWARGS_DESCRIPTION = """
66
+ Produces TER scores alongside the number of edits and reference length.
67
+
68
+ Args:
69
+ predictions (list of str): The system stream (a sequence of segments).
70
+ references (list of list of str): A list of one or more reference streams (each a sequence of segments).
71
+ normalized (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`.
72
+ ignore_punct (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`.
73
+ support_zh_ja_chars (boolean): If `True`, tokenization/normalization supports processing of Chinese characters,
74
+ as well as Japanese Kanji, Hiragana, Katakana, and Phonetic Extensions of Katakana.
75
+ Only applies if `normalized = True`. Defaults to `False`.
76
+ case_sensitive (boolean): If `False`, makes all predictions and references lowercase to ignore differences in case. Defaults to `False`.
77
+
78
+ Returns:
79
+ 'score' (float): TER score (num_edits / sum_ref_lengths * 100)
80
+ 'num_edits' (int): The cumulative number of edits
81
+ 'ref_length' (float): The cumulative average reference length
82
+
83
+ Examples:
84
+ Example 1:
85
+ >>> predictions = ["does this sentence match??",
86
+ ... "what about this sentence?",
87
+ ... "What did the TER metric user say to the developer?"]
88
+ >>> references = [["does this sentence match", "does this sentence match!?!"],
89
+ ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"],
90
+ ... ["Your jokes are...", "...TERrible"]]
91
+ >>> ter = evaluate.load("ter")
92
+ >>> results = ter.compute(predictions=predictions,
93
+ ... references=references,
94
+ ... case_sensitive=True)
95
+ >>> print(results)
96
+ {'score': 150.0, 'num_edits': 15, 'ref_length': 10.0}
97
+
98
+ Example 2:
99
+ >>> predictions = ["does this sentence match??",
100
+ ... "what about this sentence?"]
101
+ >>> references = [["does this sentence match", "does this sentence match!?!"],
102
+ ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]]
103
+ >>> ter = evaluate.load("ter")
104
+ >>> results = ter.compute(predictions=predictions,
105
+ ... references=references,
106
+ ... case_sensitive=True)
107
+ >>> print(results)
108
+ {'score': 62.5, 'num_edits': 5, 'ref_length': 8.0}
109
+
110
+ Example 3:
111
+ >>> predictions = ["does this sentence match??",
112
+ ... "what about this sentence?"]
113
+ >>> references = [["does this sentence match", "does this sentence match!?!"],
114
+ ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]]
115
+ >>> ter = evaluate.load("ter")
116
+ >>> results = ter.compute(predictions=predictions,
117
+ ... references=references,
118
+ ... normalized=True,
119
+ ... case_sensitive=True)
120
+ >>> print(results)
121
+ {'score': 57.14285714285714, 'num_edits': 6, 'ref_length': 10.5}
122
+
123
+ Example 4:
124
+ >>> predictions = ["does this sentence match??",
125
+ ... "what about this sentence?"]
126
+ >>> references = [["does this sentence match", "does this sentence match!?!"],
127
+ ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]]
128
+ >>> ter = evaluate.load("ter")
129
+ >>> results = ter.compute(predictions=predictions,
130
+ ... references=references,
131
+ ... ignore_punct=True,
132
+ ... case_sensitive=False)
133
+ >>> print(results)
134
+ {'score': 0.0, 'num_edits': 0, 'ref_length': 8.0}
135
+
136
+ Example 5:
137
+ >>> predictions = ["does this sentence match??",
138
+ ... "what about this sentence?",
139
+ ... "What did the TER metric user say to the developer?"]
140
+ >>> references = [["does this sentence match", "does this sentence match!?!"],
141
+ ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"],
142
+ ... ["Your jokes are...", "...TERrible"]]
143
+ >>> ter = evaluate.load("ter")
144
+ >>> results = ter.compute(predictions=predictions,
145
+ ... references=references,
146
+ ... ignore_punct=True,
147
+ ... case_sensitive=False)
148
+ >>> print(results)
149
+ {'score': 100.0, 'num_edits': 10, 'ref_length': 10.0}
150
+ """
151
+
152
+
153
+ @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
154
+ class Ter(evaluate.EvaluationModule):
155
+ def _info(self):
156
+ if version.parse(scb.__version__) < version.parse("1.4.12"):
157
+ raise ImportWarning(
158
+ "To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn't match this condition.\n"
159
+ 'You can install it with `pip install "sacrebleu>=1.4.12"`.'
160
+ )
161
+ return evaluate.EvaluationModuleInfo(
162
+ description=_DESCRIPTION,
163
+ citation=_CITATION,
164
+ homepage="http://www.cs.umd.edu/~snover/tercom/",
165
+ inputs_description=_KWARGS_DESCRIPTION,
166
+ features=datasets.Features(
167
+ {
168
+ "predictions": datasets.Value("string", id="sequence"),
169
+ "references": datasets.Sequence(datasets.Value("string", id="sequence"), id="references"),
170
+ }
171
+ ),
172
+ codebase_urls=["https://github.com/mjpost/sacreBLEU#ter"],
173
+ reference_urls=[
174
+ "https://github.com/jhclark/tercom",
175
+ ],
176
+ )
177
+
178
+ def _compute(
179
+ self,
180
+ predictions,
181
+ references,
182
+ normalized: bool = False,
183
+ ignore_punct: bool = False,
184
+ support_zh_ja_chars: bool = False,
185
+ case_sensitive: bool = False,
186
+ ):
187
+ references_per_prediction = len(references[0])
188
+ if any(len(refs) != references_per_prediction for refs in references):
189
+ raise ValueError("Sacrebleu requires the same number of references for each prediction")
190
+ transformed_references = [[refs[i] for refs in references] for i in range(references_per_prediction)]
191
+
192
+ sb_ter = TER(
193
+ normalized=normalized,
194
+ no_punct=ignore_punct,
195
+ asian_support=support_zh_ja_chars,
196
+ case_sensitive=case_sensitive,
197
+ )
198
+ output = sb_ter.corpus_score(predictions, transformed_references)
199
+
200
+ return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}