def handle(self, t_input: sockeye.inference.TranslatorInput, t_output: sockeye.inference.TranslatorOutput): """ :param t_input: Translator input. :param t_output: Translator output. """ alignments = " ".join( ["%d-%d" % (s, t) for s, t in get_alignments(t_output.attention_matrix, threshold=self.threshold)]) self.stream.write("%s\t%s\n" % (t_output.translation, alignments)) self.stream.flush()
def test_get_alignments(): attention_matrix = np.asarray([[0.1, 0.4, 0.5], [0.2, 0.8, 0.0], [0.4, 0.4, 0.2]]) test_cases = [(0.5, [(1, 1)]), (0.8, []), (0.1, [(0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 2)])] for threshold, expected_alignment in test_cases: alignment = list( utils.get_alignments(attention_matrix, threshold=threshold)) assert alignment == expected_alignment
def handle(self, t_input: inference.TranslatorInput, t_output: inference.TranslatorOutput, t_walltime: float = 0.): """ :param t_input: Translator input. :param t_output: Translator output. :param t_walltime: Total wall-clock time for translation. """ alignments = " ".join( ["%d-%d" % (s, t) for s, t in get_alignments(t_output.attention_matrix, threshold=self.threshold)]) self.stream.write("%s\t%s\n" % (t_output.translation, alignments)) self.stream.flush()
def handle(self, t_input: inference.TranslatorInput, t_output: inference.TranslatorOutput, t_walltime: float = 0.): """ :param t_input: Translator input. :param t_output: Translator output. :param t_walltime: Total wall-clock time for translation. """ alignments = " ".join( ["%d-%d" % (s, t) for s, t in get_alignments(t_output.attention_matrix, threshold=self.threshold)]) self.stream.write("%s\t%s\n" % (t_output.translation, alignments)) self.stream.flush()
def handle(self, t_input: inference.TranslatorInput, t_output: inference.TranslatorOutput, t_walltime: float = 0.): extracted_alignments = [] for alignment_matrix in t_output.nbest_attention_matrices: extracted_alignments.append( list(get_alignments(alignment_matrix, threshold=self.threshold))) d_ = { "translations": t_output.nbest_translations, "scores": t_output.nbest_scores, "alignments": extracted_alignments } self.stream.write("%s\n" % json.dumps(d_, sort_keys=True))