Spaces:
Running
Running
Update Space (evaluate main: e4a27243)
Browse files- google_bleu.py +28 -11
- requirements.txt +1 -1
google_bleu.py
CHANGED
@@ -13,7 +13,8 @@
|
|
13 |
# limitations under the License.
|
14 |
""" Google BLEU (aka GLEU) metric. """
|
15 |
|
16 |
-
from
|
|
|
17 |
|
18 |
import datasets
|
19 |
from nltk.translate import gleu_score
|
@@ -124,13 +125,28 @@ Examples:
|
|
124 |
"""
|
125 |
|
126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
128 |
class GoogleBleu(evaluate.Metric):
|
129 |
-
|
|
|
|
|
|
|
|
|
130 |
return evaluate.MetricInfo(
|
131 |
description=_DESCRIPTION,
|
132 |
citation=_CITATION,
|
133 |
inputs_description=_KWARGS_DESCRIPTION,
|
|
|
134 |
features=[
|
135 |
datasets.Features(
|
136 |
{
|
@@ -147,14 +163,12 @@ class GoogleBleu(evaluate.Metric):
|
|
147 |
],
|
148 |
)
|
149 |
|
150 |
-
def _compute(
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
max_len: int = 4,
|
157 |
-
) -> Dict[str, float]:
|
158 |
# if only one reference is provided make sure we still use list of lists
|
159 |
if isinstance(references[0], str):
|
160 |
references = [[ref] for ref in references]
|
@@ -163,6 +177,9 @@ class GoogleBleu(evaluate.Metric):
|
|
163 |
predictions = [tokenizer(p) for p in predictions]
|
164 |
return {
|
165 |
"google_bleu": gleu_score.corpus_gleu(
|
166 |
-
list_of_references=references,
|
|
|
|
|
|
|
167 |
)
|
168 |
}
|
|
|
13 |
# limitations under the License.
|
14 |
""" Google BLEU (aka GLEU) metric. """
|
15 |
|
16 |
+
from dataclasses import dataclass
|
17 |
+
from typing import Callable, Dict, List, Optional
|
18 |
|
19 |
import datasets
|
20 |
from nltk.translate import gleu_score
|
|
|
125 |
"""
|
126 |
|
127 |
|
128 |
+
@dataclass
|
129 |
+
class GoogleBleuConfig(evaluate.info.Config):
|
130 |
+
|
131 |
+
name: str = "default"
|
132 |
+
|
133 |
+
tokenizer: Optional[Callable] = None
|
134 |
+
min_len: int = 1
|
135 |
+
max_len: int = 4
|
136 |
+
|
137 |
+
|
138 |
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
139 |
class GoogleBleu(evaluate.Metric):
|
140 |
+
|
141 |
+
CONFIG_CLASS = GoogleBleuConfig
|
142 |
+
ALLOWED_CONFIG_NAMES = ["default"]
|
143 |
+
|
144 |
+
def _info(self, config) -> MetricInfo:
|
145 |
return evaluate.MetricInfo(
|
146 |
description=_DESCRIPTION,
|
147 |
citation=_CITATION,
|
148 |
inputs_description=_KWARGS_DESCRIPTION,
|
149 |
+
config=config,
|
150 |
features=[
|
151 |
datasets.Features(
|
152 |
{
|
|
|
163 |
],
|
164 |
)
|
165 |
|
166 |
+
def _compute(self, predictions, references):
|
167 |
+
|
168 |
+
if self.config.tokenizer is None:
|
169 |
+
tokenizer = Tokenizer13a()
|
170 |
+
else:
|
171 |
+
tokenizer = self.config.tokenizer
|
|
|
|
|
172 |
# if only one reference is provided make sure we still use list of lists
|
173 |
if isinstance(references[0], str):
|
174 |
references = [[ref] for ref in references]
|
|
|
177 |
predictions = [tokenizer(p) for p in predictions]
|
178 |
return {
|
179 |
"google_bleu": gleu_score.corpus_gleu(
|
180 |
+
list_of_references=references,
|
181 |
+
hypotheses=predictions,
|
182 |
+
min_len=self.config.min_len,
|
183 |
+
max_len=self.config.max_len,
|
184 |
)
|
185 |
}
|
requirements.txt
CHANGED
@@ -1,2 +1,2 @@
|
|
1 |
-
git+https://github.com/huggingface/evaluate@
|
2 |
nltk
|
|
|
1 |
+
git+https://github.com/huggingface/evaluate@e4a2724377909fe2aeb4357e3971e5a569673b39
|
2 |
nltk
|