(self,
                  src_path=None,
                  src_data_iter=None,
                  tgt_path=None,
                  tgt_data_iter=None,
                  src_dir=None,
                  batch_size=None,
                  attn_debug=False)
return all_scores, all_predictions
Translate content of `src_data_iter` (if not None) or `src_path`
        and get gold scores if one of `tgt_data_iter` or `tgt_path` is set.

        Note: batch_size must not be None
        Note: one of ('src_path', 'src_data_iter') must not be None

        Args:
            src_path (str): filepath of source data
            src_data_iter (iterator): an interator generating source data
                e.g. it may be a list or an openned file
            tgt_path (str): filepath of target data
            tgt_data_iter (iterator): an interator generating target data
            src_dir (str): source directory path
                (used for Audio and Image datasets)
            batch_size (int): size of examples per mini-batch
            attn_debug (bool): enables the attention logging

        Returns:
            (`list`, `list`)

            * all_scores is a list of `batch_size` lists of `n_best` scores
            * all_predictions is a list of `batch_size` lists
                of `n_best` predictions
Translate content of `src_data_iter` (if not None) or `src_path`
        and get gold scores if one of `tgt_data_iter` or `tgt_path` is set.
Translate content of src_data_iter ( if not None ) or src_path and get gold scores if one of tgt_data_iter or tgt_path is set .
def translate(self,
                  src_path=None,
                  src_data_iter=None,
                  tgt_path=None,
                  tgt_data_iter=None,
                  src_dir=None,
                  batch_size=None,
                  attn_debug=False):
        """
        Translate content of `src_data_iter` (if not None) or `src_path`
        and get gold scores if one of `tgt_data_iter` or `tgt_path` is set.

        Note: batch_size must not be None
        Note: one of ('src_path', 'src_data_iter') must not be None

        Args:
            src_path (str): filepath of source data
            src_data_iter (iterator): an interator generating source data
                e.g. it may be a list or an openned file
            tgt_path (str): filepath of target data
            tgt_data_iter (iterator): an interator generating target data
            src_dir (str): source directory path
                (used for Audio and Image datasets)
            batch_size (int): size of examples per mini-batch
            attn_debug (bool): enables the attention logging

        Returns:
            (`list`, `list`)

            * all_scores is a list of `batch_size` lists of `n_best` scores
            * all_predictions is a list of `batch_size` lists
                of `n_best` predictions
        """

        assert src_data_iter is not None or src_path is not None

        if batch_size is None:
            raise ValueError("batch_size must be set")
        data = inputters. \
            build_dataset(self.fields,
                          self.data_type,
                          src_path=src_path,
                          src_data_iter=src_data_iter,
                          tgt_path=tgt_path,
                          tgt_data_iter=tgt_data_iter,
                          src_dir=src_dir,
                          sample_rate=self.sample_rate,
                          window_size=self.window_size,
                          window_stride=self.window_stride,
                          window=self.window,
                          use_filter_pred=self.use_filter_pred,
                          image_channel_size=self.image_channel_size)

        if self.cuda:
            cur_device = "cuda"
        else:
            cur_device = "cpu"

        data_iter = inputters.OrderedIterator(
            dataset=data, device=cur_device,
            batch_size=batch_size, train=False, sort=False,
            sort_within_batch=True, shuffle=False)

        builder = onmt.translate.TranslationBuilder(
            data, self.fields,
            self.n_best, self.replace_unk, tgt_path)

        # Statistics
        counter = count(1)
        pred_score_total, pred_words_total = 0, 0
        gold_score_total, gold_words_total = 0, 0

        all_scores = []
        all_predictions = []

        for batch in data_iter:
            batch_data = self.translate_batch(batch, data, fast=self.fast)
            # import pdb;pdb.set_trace()
            translations = builder.from_batch(batch_data)

            for trans in translations:
                all_scores += [trans.pred_scores[:self.n_best]]
                pred_score_total += trans.pred_scores[0]
                pred_words_total += len(trans.pred_sents[0])
                if tgt_path is not None:
                    gold_score_total += trans.gold_score
                    gold_words_total += len(trans.gold_sent) + 1

                n_best_preds = [" ".join(pred)
                                for pred in trans.pred_sents[:self.n_best]]
                all_predictions += [n_best_preds]
                self.out_file.write('\n'.join(n_best_preds) + '\n')
                self.out_file.flush()

                if self.verbose:
                    sent_number = next(counter)
                    output = trans.log(sent_number)
                    if self.logger:
                        self.logger.info(output)
                    else:
                        os.write(1, output.encode('utf-8'))

                # Debug attention.
                if attn_debug:
                    srcs = trans.src_raw
                    preds = trans.pred_sents[0]
                    preds.append('</s>')
                    attns = trans.attns[0].tolist()
                    header_format = "{:>10.10} " + "{:>10.7} " * len(srcs)
                    row_format = "{:>10.10} " + "{:>10.7f} " * len(srcs)
                    output = header_format.format("", *trans.src_raw) + '\n'
                    for word, row in zip(preds, attns):
                        max_index = row.index(max(row))
                        row_format = row_format.replace(
                            "{:>10.7f} ", "{:*>10.7f} ", max_index + 1)
                        row_format = row_format.replace(
                            "{:*>10.7f} ", "{:>10.7f} ", max_index)
                        output += row_format.format(word, *row) + '\n'
                        row_format = "{:>10.10} " + "{:>10.7f} " * len(srcs)
                    os.write(1, output.encode('utf-8'))

        #TODO change back
        #if self.report_score:
        #    msg = self._report_score('PRED', pred_score_total,
        #                             pred_words_total)
        #    if self.logger:
        #        self.logger.info(msg)
        #    else:
        #        print(msg)
        #    if tgt_path is not None:
        #        msg = self._report_score('GOLD', gold_score_total,
        #                                 gold_words_total)
        #        if self.logger:
        #            self.logger.info(msg)
        #        else:
        #            print(msg)
        #        if self.report_bleu:
        #            msg = self._report_bleu(tgt_path)
        #            if self.logger:
        #                self.logger.info(msg)
        #            else:
        #                print(msg)
        #        if self.report_rouge:
        #            msg = self._report_rouge(tgt_path)
        #            if self.logger:
        #                self.logger.info(msg)
        #            else:
        #                print(msg)

        if self.dump_beam:
            import json
            json.dump(self.translator.beam_accum,
                      codecs.open(self.dump_beam, 'w', 'utf-8'))
        return all_scores, all_predictions
def translate ( self , src_path = None , src_data_iter = None , tgt_path = None , tgt_data_iter = None , src_dir = None , batch_size = None , attn_debug = False ) : assert src_data_iter is not None or src_path is not None if batch_size is None : raise ValueError ( "batch_size must be set" ) data = inputters . build_dataset ( self . fields , self . data_type , src_path = src_path , src_data_iter = src_data_iter , tgt_path = tgt_path , tgt_data_iter = tgt_data_iter , src_dir = src_dir , sample_rate = self . sample_rate , window_size = self . window_size , window_stride = self . window_stride , window = self . window , use_filter_pred = self . use_filter_pred , image_channel_size = self . image_channel_size ) if self . cuda : cur_device = "cuda" else : cur_device = "cpu" data_iter = inputters . OrderedIterator ( dataset = data , device = cur_device , batch_size = batch_size , train = False , sort = False , sort_within_batch = True , shuffle = False ) builder = onmt . translate . TranslationBuilder ( data , self . fields , self . n_best , self . replace_unk , tgt_path ) # Statistics counter = count ( 1 ) pred_score_total , pred_words_total = 0 , 0 gold_score_total , gold_words_total = 0 , 0 all_scores = [ ] all_predictions = [ ] for batch in data_iter : batch_data = self . translate_batch ( batch , data , fast = self . fast ) # import pdb;pdb.set_trace() translations = builder . from_batch ( batch_data ) for trans in translations : all_scores += [ trans . pred_scores [ : self . n_best ] ] pred_score_total += trans . pred_scores [ 0 ] pred_words_total += len ( trans . pred_sents [ 0 ] ) if tgt_path is not None : gold_score_total += trans . gold_score gold_words_total += len ( trans . gold_sent ) + 1 n_best_preds = [ " " . join ( pred ) for pred in trans . pred_sents [ : self . n_best ] ] all_predictions += [ n_best_preds ] self . out_file . write ( '\n' . join ( n_best_preds ) + '\n' ) self . out_file . flush ( ) if self . verbose : sent_number = next ( counter ) output = trans . log ( sent_number ) if self . logger : self . logger . info ( output ) else : os . write ( 1 , output . encode ( 'utf-8' ) ) # Debug attention. if attn_debug : srcs = trans . src_raw preds = trans . pred_sents [ 0 ] preds . append ( '</s>' ) attns = trans . attns [ 0 ] . tolist ( ) header_format = "{:>10.10} " + "{:>10.7} " * len ( srcs ) row_format = "{:>10.10} " + "{:>10.7f} " * len ( srcs ) output = header_format . format ( "" , * trans . src_raw ) + '\n' for word , row in zip ( preds , attns ) : max_index = row . index ( max ( row ) ) row_format = row_format . replace ( "{:>10.7f} " , "{:*>10.7f} " , max_index + 1 ) row_format = row_format . replace ( "{:*>10.7f} " , "{:>10.7f} " , max_index ) output += row_format . format ( word , * row ) + '\n' row_format = "{:>10.10} " + "{:>10.7f} " * len ( srcs ) os . write ( 1 , output . encode ( 'utf-8' ) ) #TODO change back #if self.report_score: #    msg = self._report_score('PRED', pred_score_total, #                             pred_words_total) #    if self.logger: #        self.logger.info(msg) #    else: #        print(msg) #    if tgt_path is not None: #        msg = self._report_score('GOLD', gold_score_total, #                                 gold_words_total) #        if self.logger: #            self.logger.info(msg) #        else: #            print(msg) #        if self.report_bleu: #            msg = self._report_bleu(tgt_path) #            if self.logger: #                self.logger.info(msg) #            else: #                print(msg) #        if self.report_rouge: #            msg = self._report_rouge(tgt_path) #            if self.logger: #                self.logger.info(msg) #            else: #                print(msg) if self . dump_beam : import json json . dump ( self . translator . beam_accum , codecs . open ( self . dump_beam , 'w' , 'utf-8' ) ) return all_scores , all_predictions