Example #1
0
    def create_sent_report(self, src: sent.Sentence,
                           output: sent.ReadableSentence,
                           attentions: np.ndarray, ref_file: Optional[str],
                           **kwargs) -> None:
        """
    Create report.

    Args:
      src: source-side input
      output: generated output
      attentions: attention matrices
      ref_file: path to reference file
      **kwargs: arguments to be ignored
    """
        self.cur_sent_no += 1
        if self.max_num_sents and self.cur_sent_no > self.max_num_sents: return
        reference = utils.cached_file_lines(ref_file)[output.idx]
        idx = src.idx
        self.add_sent_heading(idx)
        src_tokens = src.str_tokens() if isinstance(
            src, sent.ReadableSentence) else []
        trg_tokens = output.str_tokens()
        src_str = src.sent_str() if isinstance(src,
                                               sent.ReadableSentence) else ""
        trg_str = output.sent_str()
        self.add_charcut_diff(trg_str, reference)
        self.add_fields_if_set({"Src": src_str})
        self.add_atts(
            attentions,
            src.get_array() if isinstance(src, sent.ArraySentence) else
            src_tokens, trg_tokens, idx)
        self.finish_sent()
Example #2
0
    def create_sent_report(self, segment_actions, src: sent.Sentence,
                           **kwargs):
        if self.report_fp is None:
            utils.make_parent_dir(self.report_path)
            self.report_fp = open(self.report_path, "w")

        actions = segment_actions[0]
        src = src.str_tokens()
        words = []
        start = 0
        for end in actions:
            if start < end + 1:
                words.append("".join(map(str, src[start:end + 1])))
            start = end + 1
        print(" ".join(words), file=self.report_fp)