fnvls commited on
Commit
8bc4fde
·
1 Parent(s): cdd89cc

add bleu 1 2 3 4 compute

Browse files
Files changed (2) hide show
  1. README.md +2 -2
  2. bleu_1234.py +32 -9
README.md CHANGED
@@ -1,7 +1,7 @@
1
  ---
2
  title: bleu_1234
3
- datasets:
4
- -
5
  tags:
6
  - evaluate
7
  - metric
 
1
  ---
2
  title: bleu_1234
3
+ datasets:
4
+ - "none"
5
  tags:
6
  - evaluate
7
  - metric
bleu_1234.py CHANGED
@@ -70,10 +70,20 @@ class bleu_1234(evaluate.Metric):
70
  citation=_CITATION,
71
  inputs_description=_KWARGS_DESCRIPTION,
72
  # This defines the format of each prediction and reference
73
- features=datasets.Features({
74
- 'predictions': datasets.Value('int64'),
75
- 'references': datasets.Value('int64'),
76
- }),
 
 
 
 
 
 
 
 
 
 
77
  # Homepage of the module for documentation
78
  homepage="http://module.homepage",
79
  # Additional links to the codebase or references
@@ -84,12 +94,25 @@ class bleu_1234(evaluate.Metric):
84
  def _download_and_prepare(self, dl_manager):
85
  """Optional: download external resources useful to compute the scores"""
86
  # TODO: Download external resources if needed
87
- pass
 
88
 
89
  def _compute(self, predictions, references):
90
  """Returns the scores"""
91
  # TODO: Compute the different scores of the module
92
- accuracy = sum(i == j for i, j in zip(predictions, references)) / len(predictions)
93
- return {
94
- "accuracy": accuracy,
95
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  citation=_CITATION,
71
  inputs_description=_KWARGS_DESCRIPTION,
72
  # This defines the format of each prediction and reference
73
+ features=[
74
+ datasets.Features(
75
+ {
76
+ "predictions": datasets.Value("string", id="sequence"),
77
+ "references": datasets.Sequence(datasets.Value("string", id="sequence"), id="references"),
78
+ }
79
+ ),
80
+ datasets.Features(
81
+ {
82
+ "predictions": datasets.Value("string", id="sequence"),
83
+ "references": datasets.Value("string", id="sequence"),
84
+ }
85
+ ),
86
+ ],
87
  # Homepage of the module for documentation
88
  homepage="http://module.homepage",
89
  # Additional links to the codebase or references
 
94
  def _download_and_prepare(self, dl_manager):
95
  """Optional: download external resources useful to compute the scores"""
96
  # TODO: Download external resources if needed
97
+ import torch
98
+ from torchmetrics.text import BLEUScore
99
 
100
  def _compute(self, predictions, references):
101
  """Returns the scores"""
102
  # TODO: Compute the different scores of the module
103
+ if isinstance(references[0], str):
104
+ references = [[ref] for ref in references]
105
+
106
+ bleu = BLEUScore()
107
+ bleu1 = bleu(preds, target, n_gram=1)
108
+ bleu2 = bleu(preds, target, n_gram=2)
109
+ bleu3 = bleu(preds, target, n_gram=3)
110
+ bleu4 = bleu(preds, target, n_gram=4)
111
+
112
+ output_dict = {
113
+ "bleu-1": bleu1,
114
+ "bleu-2": bleu2,
115
+ "bleu-3": bleu3,
116
+ "bleu-4": bleu4,
117
+ }
118
+ return output_dict