def translate(self, data_iter, step, attn_debug=False):
    self.model.eval()
    gold_path = (self.args.result_path + ('.%d.gold' % step))
    can_path = (self.args.result_path + ('.%d.candidate' % step))
    self.gold_out_file = codecs.open(gold_path, 'w', 'utf-8')
    self.can_out_file = codecs.open(can_path, 'w', 'utf-8')
    self.gold_out_file = codecs.open(gold_path, 'w', 'utf-8')
    self.can_out_file = codecs.open(can_path, 'w', 'utf-8')
    raw_src_path = (self.args.result_path + ('.%d.raw_src' % step))
    self.src_out_file = codecs.open(raw_src_path, 'w', 'utf-8')
    ct = 0
    with torch.no_grad():
        for batch in data_iter:
            if self.args.recall_eval:
                gold_tgt_len = batch.tgt.size(1)
                self.min_length = (gold_tgt_len + 20)
                self.max_length = (gold_tgt_len + 60)
            batch_data = self.translate_batch(batch)
            translations = self.from_batch(batch_data)
            for trans in translations:
                (pred, gold, src) = trans
                pred_str = pred.replace('[unused1]', '').replace('[unused4]', '').replace('[PAD]', '').replace('[unused2]', '').replace(' +', ' ').replace(' [unused3] ', '<q>').replace('[unused3]', '').strip()
                gold_str = gold.strip()
                if self.args.recall_eval:
                    _pred_str = ''
                    gap = 1000.0
                    for sent in pred_str.split('<q>'):
                        can_pred_str = ((_pred_str + '<q>') + sent.strip())
                        can_gap = math.fabs((len(_pred_str.split()) - len(gold_str.split())))
                        if (len(can_pred_str.split()) >= (len(gold_str.split()) + 10)):
                            pred_str = _pred_str
                            break
                        else:
                            gap = can_gap
                            _pred_str = can_pred_str
                self.can_out_file.write((pred_str + '\n'))
                self.gold_out_file.write((gold_str + '\n'))
                self.src_out_file.write((src.strip() + '\n'))
                ct += 1
            self.can_out_file.flush()
            self.gold_out_file.flush()
            self.src_out_file.flush()
    self.can_out_file.close()
    self.gold_out_file.close()
    self.src_out_file.close()
    if (step != (- 1)):
        rouges = self._report_rouge(gold_path, can_path)
        self.logger.info(('Rouges at step %d \n%s' % (step, rouge_results_to_str(rouges))))
        if (self.tensorboard_writer is not None):
            self.tensorboard_writer.add_scalar('test/rouge1-F', rouges['rouge_1_f_score'], step)
            self.tensorboard_writer.add_scalar('test/rouge2-F', rouges['rouge_2_f_score'], step)
            self.tensorboard_writer.add_scalar('test/rougeL-F', rouges['rouge_l_f_score'], step)
def translate(self, data_iter, step, attn_debug=False):
    self.model.eval()
    gold_path = (self.args.result_path + ('.%d.gold' % step))
    can_path = (self.args.result_path + ('.%d.candidate' % step))
    self.gold_out_file = codecs.open(gold_path, 'w', 'utf-8')
    self.can_out_file = codecs.open(can_path, 'w', 'utf-8')
    self.gold_out_file = codecs.open(gold_path, 'w', 'utf-8')
    self.can_out_file = codecs.open(can_path, 'w', 'utf-8')
    raw_src_path = (self.args.result_path + ('.%d.raw_src' % step))
    self.src_out_file = codecs.open(raw_src_path, 'w', 'utf-8')
    ct = 0
    with torch.no_grad():
        for batch in data_iter:
            if self.args.recall_eval:
                gold_tgt_len = batch.tgt.size(1)
                self.min_length = (gold_tgt_len + 20)
                self.max_length = (gold_tgt_len + 60)
            batch_data = self.translate_batch(batch)
            translations = self.from_batch(batch_data)
            for trans in translations:
                (pred, gold, src) = trans
                pred_str = pred.replace('[unused1]', ).replace('[unused4]', ).replace('[PAD]', ).replace('[unused2]', ).replace(' +', ' ').replace(' [unused3] ', '<q>').replace('[unused3]', ).strip()
                gold_str = gold.strip()
                if self.args.recall_eval:
                    _pred_str = 
                    gap = 1000.0
                    for sent in pred_str.split('<q>'):
                        can_pred_str = ((_pred_str + '<q>') + sent.strip())
                        can_gap = math.fabs((len(_pred_str.split()) - len(gold_str.split())))
                        if (len(can_pred_str.split()) >= (len(gold_str.split()) + 10)):
                            pred_str = _pred_str
                            break
                        else:
                            gap = can_gap
                            _pred_str = can_pred_str
                self.can_out_file.write((pred_str + '\n'))
                self.gold_out_file.write((gold_str + '\n'))
                self.src_out_file.write((src.strip() + '\n'))
                ct += 1
            self.can_out_file.flush()
            self.gold_out_file.flush()
            self.src_out_file.flush()
    self.can_out_file.close()
    self.gold_out_file.close()
    self.src_out_file.close()
    if (step != (- 1)):
        rouges = self._report_rouge(gold_path, can_path)
        self.logger.info(('Rouges at step %d \n%s' % (step, rouge_results_to_str(rouges))))
        if (self.tensorboard_writer is not None):
            self.tensorboard_writer.add_scalar('test/rouge1-F', rouges['rouge_1_f_score'], step)
            self.tensorboard_writer.add_scalar('test/rouge2-F', rouges['rouge_2_f_score'], step)
            self.tensorboard_writer.add_scalar('test/rougeL-F', rouges['rouge_l_f_score'], step)