def write_reprocessed(
    sources,
    hypos,
    targets,
    source_outfile,
    hypo_outfile,
    target_outfile,
    right_to_left=False,
    prefix_len=None,
    bpe_symbol=None,
    target_prefix_frac=None,
    source_prefix_frac=None,
):

    """writes nbest hypothesis for rescoring"""
    assert not (
        prefix_len is not None and target_prefix_frac is not None
    ), "in writing reprocessed, only one type of prefix may be used"
    assert not (
        prefix_len is not None and source_prefix_frac is not None
    ), "in writing reprocessed, only one type of prefix may be used"
    assert not (
        target_prefix_frac is not None and source_prefix_frac is not None
    ), "in writing reprocessed, only one type of prefix may be used"

    with open(source_outfile, "w") as source_file, open(
        hypo_outfile, "w"
    ) as hypo_file, open(target_outfile, "w") as target_file:

        assert len(sources) == len(hypos), "sources and hypos list length mismatch"
        if right_to_left:
            for i in range(len(sources)):
                for j in range(len(hypos[i])):
                    if prefix_len is None:
                        hypo_file.write(make_right_to_left(hypos[i][j]) + "\n")
                    else:
                        raise NotImplementedError()
                    source_file.write(make_right_to_left(sources[i]) + "\n")
                    target_file.write(make_right_to_left(targets[i]) + "\n")
        else:
            for i in sorted(sources.keys()):
                for j in range(len(hypos[i])):
                    if prefix_len is not None:
                        shortened = (
                            get_prefix_no_bpe(hypos[i][j], bpe_symbol, prefix_len)
                            + "\n"
                        )
                        hypo_file.write(shortened)
                        source_file.write(sources[i])
                        target_file.write(targets[i])
                    elif target_prefix_frac is not None:
                        num_words, shortened, num_bpe_tokens = calc_length_from_frac(
                            hypos[i][j], target_prefix_frac, bpe_symbol
                        )
                        shortened += "\n"
                        hypo_file.write(shortened)
                        source_file.write(sources[i])
                        target_file.write(targets[i])
                    elif source_prefix_frac is not None:
                        num_words, shortened, num_bpe_tokensn = calc_length_from_frac(
                            sources[i], source_prefix_frac, bpe_symbol
                        )
                        shortened += "\n"
                        hypo_file.write(hypos[i][j])
                        source_file.write(shortened)
                        target_file.write(targets[i])
                    else:
                        hypo_file.write(hypos[i][j])
                        source_file.write(sources[i])
                        target_file.write(targets[i])
writes nbest hypothesis for rescoring
examples/noisychannel/rerank_utils.py