raptorkwok commited on
Commit
a21b8c3
·
1 Parent(s): 22984fb

add SacreBLEU like soothing precision

Browse files
Files changed (1) hide show
  1. chinesebleu.py +13 -8
chinesebleu.py CHANGED
@@ -46,16 +46,17 @@ Returns:
46
  score: the Chinese BLEU score,
47
  counts: Counts in n-gram (1-4 grams),
48
  totals: Totals in n-gram,
49
- bp: Brevity Penalty
 
50
 
51
  Examples:
52
  Examples should be written in doctest format, and should illustrate how
53
  to use the function.
54
 
55
  >>> my_new_module = evaluate.load("chinesebleu")
56
- >>> results = my_new_module.compute(references=["Reference Sentence in Chinese"], predictions=["Predicted Sentence in Chinese"])
57
  >>> print(results)
58
- {'score': 71.89393375176813, 'counts': [9, 7, 5, 4], 'totals': [9, 8, 7, 6], 'bp': 1.0}
59
  """
60
 
61
  @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
@@ -72,8 +73,8 @@ class ChineseBLEU(evaluate.Metric):
72
  'predictions': datasets.Value('string'),
73
  'references': datasets.Value('string'),
74
  }),
75
- homepage="https://github.com/shivanraptor/chinesebleu",
76
- codebase_urls=["https://github.com/shivanraptor/chinesebleu"]
77
  )
78
 
79
  def _download_and_prepare(self, dl_manager):
@@ -140,7 +141,7 @@ class ChineseBLEU(evaluate.Metric):
140
 
141
  # For total number of tokens < 4, fallback to character-level tokenizations
142
  if len(pred_tokens) < 4 or len(ref_tokens) < 4:
143
- tokenizer = 'character'
144
  pred_tokens = [self._tokenize_chinese(p, tokenizer) for p in predictions]
145
  ref_tokens = [self._tokenize_chinese(r, tokenizer) for r in references]
146
 
@@ -170,11 +171,15 @@ class ChineseBLEU(evaluate.Metric):
170
 
171
  # Compute precisions
172
  precisions = []
173
- for c, t in zip(counts, totals):
174
  if t == 0:
175
  precisions.append(0.0)
176
  else:
177
- precisions.append(float(c) / t)
 
 
 
 
178
 
179
  # Geometric mean of precisions
180
  if any(p == 0 for p in precisions):
 
46
  score: the Chinese BLEU score,
47
  counts: Counts in n-gram (1-4 grams),
48
  totals: Totals in n-gram,
49
+ bp: Brevity Penalty,
50
+ tokenizer: Selection of Tokenizer (either "chinese" or "char")
51
 
52
  Examples:
53
  Examples should be written in doctest format, and should illustrate how
54
  to use the function.
55
 
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)
 
73
  'predictions': datasets.Value('string'),
74
  'references': datasets.Value('string'),
75
  }),
76
+ homepage="https://huggingface.co/spaces/raptorkwok/chinesebleu/",
77
+ codebase_urls=["https://huggingface.co/spaces/raptorkwok/chinesebleu/"]
78
  )
79
 
80
  def _download_and_prepare(self, dl_manager):
 
141
 
142
  # For total number of tokens < 4, fallback to character-level tokenizations
143
  if len(pred_tokens) < 4 or len(ref_tokens) < 4:
144
+ tokenizer = 'char'
145
  pred_tokens = [self._tokenize_chinese(p, tokenizer) for p in predictions]
146
  ref_tokens = [self._tokenize_chinese(r, tokenizer) for r in references]
147
 
 
171
 
172
  # Compute precisions
173
  precisions = []
174
+ for i, (c, t) in enumerate(zip(counts, totals)):
175
  if t == 0:
176
  precisions.append(0.0)
177
  else:
178
+ # SacreBLEU floor smoothing for n=4 on short sentences (no matches, total=1)
179
+ p = float(c) / t
180
+ if i == 3 and c == 0 and t == 1: # i=3 is 4-gram (0-indexed)
181
+ p = 1.0 / 2.0 # 0.5, matching SacreBLEU's heuristic
182
+ precisions.append(p)
183
 
184
  # Geometric mean of precisions
185
  if any(p == 0 for p in precisions):