Skip to content

Commit 1eebe7a

Browse files
authored
Merge pull request #56 from alexandrainst/feat/add-cer-metric
2 parents d9d09de + 3f0dbf6 commit 1eebe7a

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/coral_models/compute_metrics.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def compute_wer_metrics(pred: EvalPrediction, processor: Processor) -> dict[str,
2626
dictionary with 'wer' as the key and the word error rate as the value.
2727
"""
2828
wer_metric = load_metric("wer")
29+
cer_metric = load_metric("cer")
2930
tokenizer: PreTrainedTokenizerBase = getattr(processor, "tokenizer")
3031
pad_token = tokenizer.pad_token_id
3132

@@ -84,13 +85,26 @@ def compute_wer_metrics(pred: EvalPrediction, processor: Processor) -> dict[str,
8485
logger.info(f"Sample document: {labels_str[random_idx]}")
8586
logger.info(f"Predicted: {predictions_str[random_idx]}")
8687

87-
# Compute the word error rate
88-
computed = wer_metric.compute(predictions=predictions_str, references=labels_str)
89-
assert computed is not None
88+
metrics: dict[str, float] = dict()
9089

91-
# Ensure that `wer` is a dict, as metrics in the `evaluate` library can either be
92-
# dicts or floats
93-
if not isinstance(computed, dict):
94-
return dict(wer=computed)
90+
# Compute the word error rate
91+
wer_computed = wer_metric.compute(
92+
predictions=predictions_str, references=labels_str
93+
)
94+
assert wer_computed is not None
95+
if not isinstance(wer_computed, dict):
96+
metrics = metrics | dict(wer=wer_computed)
9597
else:
96-
return computed
98+
metrics = metrics | wer_computed
99+
100+
# Compute the character error rate
101+
cer_computed = cer_metric.compute(
102+
predictions=predictions_str, references=labels_str
103+
)
104+
assert cer_computed is not None
105+
if not isinstance(cer_computed, dict):
106+
metrics = metrics | dict(cer=cer_computed)
107+
else:
108+
metrics = metrics | cer_computed
109+
110+
return metrics

0 commit comments

Comments
 (0)