Spaces:
Running
Running
Commit ·
ab93454
1
Parent(s): 7bd87dc
fix precision bug
Browse files- chinesebleu.py +13 -14
chinesebleu.py
CHANGED
|
@@ -56,12 +56,12 @@ Examples:
|
|
| 56 |
>>> my_new_module = evaluate.load("chinesebleu")
|
| 57 |
>>> results = my_new_module.compute(references=["這裡就是香港都會大學"], predictions=["這裡是香港都會大學"])
|
| 58 |
>>> print(results)
|
| 59 |
-
{'score': 71.89393375176813, 'counts': [9, 7, 5, 4], 'totals': [9, 8, 7, 6], 'bp': 1.0, tokenizer: 'chinese'}
|
| 60 |
"""
|
| 61 |
|
| 62 |
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
| 63 |
class ChineseBLEU(evaluate.Metric):
|
| 64 |
-
"""
|
| 65 |
|
| 66 |
def _info(self):
|
| 67 |
return evaluate.MetricInfo(
|
|
@@ -134,19 +134,22 @@ class ChineseBLEU(evaluate.Metric):
|
|
| 134 |
raise ValueError("Predictions and references must have the same length.")
|
| 135 |
|
| 136 |
max_n = 4 # Default n-gram = 4
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
tokenizer = 'chinese'
|
| 138 |
|
| 139 |
pred_tokens = [self._tokenize_chinese(p, tokenizer) for p in predictions]
|
| 140 |
ref_tokens = [self._tokenize_chinese(r, tokenizer) for r in references]
|
| 141 |
|
| 142 |
-
# For total number of tokens < 4, fallback to
|
| 143 |
if len(pred_tokens[0]) < 4 or len(ref_tokens[0]) < 4:
|
| 144 |
tokenizer = 'char'
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
totals = [0] * max_n
|
| 150 |
|
| 151 |
for n in range(1, max_n + 1):
|
| 152 |
clipped_counts_n = 0
|
|
@@ -168,15 +171,11 @@ class ChineseBLEU(evaluate.Metric):
|
|
| 168 |
|
| 169 |
# Compute precisions
|
| 170 |
precisions = []
|
| 171 |
-
for
|
| 172 |
if t == 0:
|
| 173 |
precisions.append(0.0)
|
| 174 |
else:
|
| 175 |
-
|
| 176 |
-
p = float(c) / t
|
| 177 |
-
if i == 3 and c == 0 and t == 1: # i=3 is 4-gram (0-indexed)
|
| 178 |
-
p = 1.0 / 2.0 # 0.5, matching SacreBLEU's heuristic
|
| 179 |
-
precisions.append(p)
|
| 180 |
|
| 181 |
# Geometric mean of precisions
|
| 182 |
if any(p == 0 for p in precisions):
|
|
|
|
| 56 |
>>> my_new_module = evaluate.load("chinesebleu")
|
| 57 |
>>> results = my_new_module.compute(references=["這裡就是香港都會大學"], predictions=["這裡是香港都會大學"])
|
| 58 |
>>> print(results)
|
| 59 |
+
{'score': 71.89393375176813, 'counts': [9, 7, 5, 4], 'totals': [9, 8, 7, 6], 'bp': 1.0, 'sys_len': 9, 'ref_len': 10, tokenizer: 'chinese'}
|
| 60 |
"""
|
| 61 |
|
| 62 |
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
| 63 |
class ChineseBLEU(evaluate.Metric):
|
| 64 |
+
"""Chinese BLEU - a BLEU-based metric for Chinese sentences"""
|
| 65 |
|
| 66 |
def _info(self):
|
| 67 |
return evaluate.MetricInfo(
|
|
|
|
| 134 |
raise ValueError("Predictions and references must have the same length.")
|
| 135 |
|
| 136 |
max_n = 4 # Default n-gram = 4
|
| 137 |
+
|
| 138 |
+
counts = [0] * max_n
|
| 139 |
+
totals = [0] * max_n
|
| 140 |
+
|
| 141 |
tokenizer = 'chinese'
|
| 142 |
|
| 143 |
pred_tokens = [self._tokenize_chinese(p, tokenizer) for p in predictions]
|
| 144 |
ref_tokens = [self._tokenize_chinese(r, tokenizer) for r in references]
|
| 145 |
|
| 146 |
+
# For total number of tokens < 4, fallback to SacreBLEU
|
| 147 |
if len(pred_tokens[0]) < 4 or len(ref_tokens[0]) < 4:
|
| 148 |
tokenizer = 'char'
|
| 149 |
+
sacrebleu = evaluate.load('sacrebleu')
|
| 150 |
+
bleu_result = sacrebleu.compute(predictions=predictions, references=references, tokenize="zh")
|
| 151 |
+
bleu_result['tokenizer'] = tokenizer
|
| 152 |
+
return bleu_result
|
|
|
|
| 153 |
|
| 154 |
for n in range(1, max_n + 1):
|
| 155 |
clipped_counts_n = 0
|
|
|
|
| 171 |
|
| 172 |
# Compute precisions
|
| 173 |
precisions = []
|
| 174 |
+
for c, t in zip(counts, totals):
|
| 175 |
if t == 0:
|
| 176 |
precisions.append(0.0)
|
| 177 |
else:
|
| 178 |
+
precisions.append(float(c) / t)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
# Geometric mean of precisions
|
| 181 |
if any(p == 0 for p in precisions):
|