Пример #1
0
def translate_lines(
        output_handler: sockeye.output_handler.OutputHandler,
        source_data: Iterable[str],
        translator: sockeye.inference.Translator) -> Tuple[int, float]:
    """
    Translates each line from source_data, calling output handler for each result.

    :param output_handler: A handler that will be called once with the output of each translation.
    :param source_data: A enumerable list of source sentences that will be translated.
    :param translator: The translator that will be used for each line of input.
    :param edge_vocab: Edge label vocabulary for graphs.
    :return: The number of lines translated, and the total time taken.
    """

    i = 0
    total_time = 0.0
    for i, line in enumerate(source_data, 1):
        tic = time.time()

        #########
        # GCN - This is an ugly hack: we concatenate the surface sentence
        # and the graph into a single line
        surface, graph = line.split('\t')
        #########

        trans_input = translator.make_input(i, surface, graph,
                                            translator.vocab_edge)
        logger.debug(" IN: %s", trans_input)
        trans_output = translator.translate(trans_input)
        trans_wall_time = time.time() - tic
        total_time += trans_wall_time
        logger.debug("OUT: %s", trans_output)
        logger.debug("OUT: time=%.2f", trans_wall_time)
        output_handler.handle(trans_input, trans_output, trans_wall_time)
    return i, total_time
Пример #2
0
def translate_lines(
        output_handler: sockeye.output_handler.OutputHandler,
        source_data: Iterable[str],
        translator: sockeye.inference.Translator) -> Tuple[int, float]:
    """
    Translates each line from source_data, calling output handler for each result.

    :param output_handler: A handler that will be called once with the output of each translation.
    :param source_data: A enumerable list of source sentences that will be translated.
    :param translator: The translator that will be used for each line of input.
    :return: The number of lines translated, and the total time taken.
    """

    i = 0
    total_time = 0.0
    for i, line in enumerate(source_data, 1):
        trans_input = translator.make_input(i, line)
        logger.debug(" IN: %s", trans_input)
        tic = time.time()
        trans_output = translator.translate(trans_input)
        trans_wall_time = time.time() - tic
        total_time += trans_wall_time
        logger.debug("OUT: %s", trans_output)
        logger.debug("OUT: time=%.2f", trans_wall_time)
        output_handler.handle(trans_input, trans_output)
    return i, total_time
Пример #3
0
def translate(output_handler: sockeye.output_handler.OutputHandler,
              source_data: Iterable[str],
              translator: sockeye.inference.Translator,
              chunk_id: int = 0) -> float:
    """
    Translates each line from source_data, calling output handler after translating a batch.

    :param output_handler: A handler that will be called once with the output of each translation.
    :param source_data: A enumerable list of source sentences that will be translated.
    :param translator: The translator that will be used for each line of input.
    :param chunk_id: Global id of the chunk.
    :return: Total time taken.
    """

    tic = time.time()
    trans_inputs = [
        translator.make_input(i, line)
        for i, line in enumerate(source_data, chunk_id + 1)
    ]
    trans_outputs = translator.translate(trans_inputs)
    total_time = time.time() - tic
    batch_time = total_time / len(trans_inputs)
    for trans_input, trans_output in zip(trans_inputs, trans_outputs):
        output_handler.handle(trans_input, trans_output, batch_time)
    return total_time