def populate_parallel_text(extract_dir: str,
                           file_sets: List[Tuple[str, str, str]],
                           dest_prefix: str,
                           keep_separate: bool,
                           head_n: int = 0):
    """
    Create raw parallel train, dev, or test files with a given prefix.

    :param extract_dir: Directory where raw files (inputs) are extracted.
    :param file_sets: Sets of files to use.
    :param dest_prefix: Prefix for output files.
    :param keep_separate: True if each file set (source-target pair) should have
                          its own file (used for test sets).
    :param head_n: If N>0, use only the first N lines (used in test mode).
    """
    source_out = None  # type: IO[Any]
    target_out = None  # type: IO[Any]
    lines_written = 0
    # Single output file for each side
    if not keep_separate:
        source_dest = dest_prefix + SUFFIX_SRC_GZ
        target_dest = dest_prefix + SUFFIX_TRG_GZ
        logging.info("Populate: %s %s", source_dest, target_dest)
        source_out = gzip.open(source_dest, "wt", encoding="utf-8")
        target_out = gzip.open(target_dest, "wt", encoding="utf-8")
    for i, (source_fname, target_fname, text_type) in enumerate(file_sets):
        # One output file per input file for each side
        if keep_separate:
            if source_out:
                source_out.close()
            if target_out:
                target_out.close()
            source_dest = dest_prefix + str(i) + "." + SUFFIX_SRC_GZ
            target_dest = dest_prefix + str(i) + "." + SUFFIX_TRG_GZ
            logging.info("Populate: %s %s", source_dest, target_dest)
            source_out = gzip.open(source_dest, "wt", encoding="utf-8")
            target_out = gzip.open(target_dest, "wt", encoding="utf-8")
        for source_line, target_line in zip(
                plain_text_iter(os.path.join(extract_dir, source_fname), text_type, DATA_SRC),
                plain_text_iter(os.path.join(extract_dir, target_fname), text_type, DATA_TRG)):
            # Only write N lines total if requested, but reset per file when
            # keeping files separate
            if head_n > 0 and lines_written >= head_n:
                if keep_separate:
                    lines_written = 0
                break
            source_out.write("{}\n".format(source_line))
            target_out.write("{}\n".format(target_line))
            lines_written += 1
    source_out.close()
    target_out.close()
Create raw parallel train, dev, or test files with a given prefix.

    :param extract_dir: Directory where raw files (inputs) are extracted.
    :param file_sets: Sets of files to use.
    :param dest_prefix: Prefix for output files.
    :param keep_separate: True if each file set (source-target pair) should have
                          its own file (used for test sets).
    :param head_n: If N>0, use only the first N lines (used in test mode).