def make_result()

in bring-your-own-container/fairseq_translation/fairseq/sagemaker_translate.py [0:0]


def make_result(src_str, hypos, align_dict, tgt_dict, args):
    result = Translation(
        src_str="O\t{}".format(src_str),
        hypos=[],
        pos_scores=[],
        alignments=[],
    )

    # Process top predictions
    for hypo in hypos[: min(len(hypos), args.nbest)]:
        hypo_tokens, hypo_str, alignment = utils.post_process_prediction(
            hypo_tokens=hypo["tokens"].int().cpu(),
            src_str=src_str,
            alignment=hypo["alignment"].int().cpu() if hypo["alignment"] is not None else None,
            align_dict=align_dict,
            tgt_dict=tgt_dict,
            remove_bpe=args.remove_bpe,
        )
        # result.hypos.append('H\t{}\t{}'.format(hypo['score'], hypo_str))
        # only get the traduction, not the score
        result.hypos.append(hypo_str)
        result.pos_scores.append(
            "P\t{}".format(
                " ".join(
                    map(
                        lambda x: "{:.4f}".format(x),
                        hypo["positional_scores"].tolist(),
                    )
                )
            )
        )
        result.alignments.append(
            "A\t{}".format(" ".join(map(lambda x: str(utils.item(x)), alignment)))
            if args.print_alignment
            else None
        )
    return result