Format refs and preds for sacrebleu corpus calculation. It is very particular
(refs, preds)
| 159 | |
| 160 | |
| 161 | def _sacreformat(refs, preds): |
| 162 | """Format refs and preds for sacrebleu corpus calculation. It is very particular""" |
| 163 | # Sacrebleu expects (List[str], List[List[str]) |
| 164 | # e.g. sacrebleu.corpus_bleu([pred_t], [[ref1_stream], [ref2_stream], ...]) |
| 165 | |
| 166 | # Note [ref1_stream] is the first reference for each pred. |
| 167 | # So lists are size N and (M, N) for N preds and M possible refs for each pred |
| 168 | # This is a different order of dimensions that I would expect |
| 169 | |
| 170 | # We expect refs to be List[str] or List[List[str]], the outer list corresponding to preds |
| 171 | # Must become List[List[str]] with the inner list corresponding to preds |
| 172 | if not is_non_str_iterable(refs): |
| 173 | refs = list(refs) |
| 174 | if not is_non_str_iterable(refs[0]): |
| 175 | refs = [[ref] for ref in refs] |
| 176 | refs = list(zip(*refs)) |
| 177 | # Note the number of refs in each ref list much match the number of preds |
| 178 | |
| 179 | # We expect preds to be List[str] or List[List[str]]. Must become List[str] |
| 180 | if not is_non_str_iterable(preds): |
| 181 | preds = list(preds) |
| 182 | if is_non_str_iterable(preds[0]): |
| 183 | assert len(preds[0]) == 1, f"Pred must be a str, was {preds[0]}" |
| 184 | preds = [pred[0] for pred in preds] |
| 185 | |
| 186 | return refs, preds |
| 187 | |
| 188 | |
| 189 | # stderr stuff |
no test coverage detected