Format refs and preds for sacrebleu corpus calculation. It is very particular
(refs, preds)
| 335 | |
| 336 | |
| 337 | def _sacreformat(refs, preds): |
| 338 | """Format refs and preds for sacrebleu corpus calculation. It is very particular""" |
| 339 | # Sacrebleu expects (List[str], List[List[str]) |
| 340 | # e.g. sacrebleu.corpus_bleu([pred_t], [[ref1_stream], [ref2_stream], ...]) |
| 341 | |
| 342 | # Note [ref1_stream] is the first reference for each pred. |
| 343 | # So lists are size N and (M, N) for N preds and M possible refs for each pred |
| 344 | # This is a different order of dimensions that I would expect |
| 345 | |
| 346 | # We expect refs to be List[str] or List[List[str]], the outer list corresponding to preds |
| 347 | # Must become List[List[str]] with the inner list corresponding to preds |
| 348 | if not is_non_str_iterable(refs): |
| 349 | refs = list(refs) |
| 350 | if not is_non_str_iterable(refs[0]): |
| 351 | refs = [[ref] for ref in refs] |
| 352 | refs = list(zip(*refs)) |
| 353 | # Note the number of refs in each ref list much match the number of preds |
| 354 | |
| 355 | # We expect preds to be List[str] or List[List[str]]. Must become List[str] |
| 356 | if not is_non_str_iterable(preds): |
| 357 | preds = list(preds) |
| 358 | if is_non_str_iterable(preds[0]): |
| 359 | assert len(preds[0]) == 1, f"Pred must be a str, was {preds[0]}" |
| 360 | preds = [pred[0] for pred in preds] |
| 361 | |
| 362 | return refs, preds |
| 363 | |
| 364 | |
| 365 | # stderr stuff |
no test coverage detected