code: def sample_from_model(args):
      para_data = args.parallel_data
      sample_file = args.sample_file
      output = args.output
  
      tgt_sent_pattern = re.compile(r"^\[(\d+)\] (.*?)$")
      para_data = [l.strip().split(" ||| ") for l in open(para_data)]
  
      f_out = open(output, "w")
      f = open(sample_file)
      f.readline()
      for src_sent, tgt_sent in para_data:
          line = f.readline().strip()
          assert line.startswith("****")
          line = f.readline().strip()
          print(line)
          assert line.startswith("target:")
  
          tgt_sent2 = line[len("target:") :]
          assert tgt_sent == tgt_sent2
  
          line = f.readline().strip()  # samples
  
          tgt_sent = " ".join(tgt_sent.split(" ")[1:-1])
          tgt_samples = set()
          for i in range(1, 101):
              line = f.readline().rstrip("\n")
              m = tgt_sent_pattern.match(line)
  
              assert m, line
              assert int(m.group(1)) == i
  
              sampled_tgt_sent = m.group(2).strip()
  
              if is_valid_sample(sampled_tgt_sent):
                  tgt_samples.add(sampled_tgt_sent)
  
          line = f.readline().strip()
          assert line.startswith("****")
  
          tgt_samples.add(tgt_sent)
          tgt_samples = list(tgt_samples)
  
          assert len(tgt_samples) > 0
  
          tgt_ref_tokens = tgt_sent.split(" ")
          bleu_scores = []
          for tgt_sample in tgt_samples:
              bleu_score = sentence_bleu(
                  [tgt_ref_tokens], tgt_sample.split(" ")
              )
              bleu_scores.append(bleu_score)
  
          tgt_ranks = sorted(
              range(len(tgt_samples)),
              key=lambda i: bleu_scores[i],
              reverse=True,
          )
  
          print("%d samples" % len(tgt_samples))
  
          print("*" * 50, file=f_out)
          print("source: " + src_sent, file=f_out)
          print("%d samples" % len(tgt_samples), file=f_out)
          for i in tgt_ranks:
              print(
                  "%s ||| %f" % (tgt_samples[i], bleu_scores[i]),
                  file=f_out,
              )
          print("*" * 50, file=f_out)
  
      f_out.close()
  
file_path: texar/examples/seq2seq_exposure_bias/utils/raml_samples_generation/process_samples.py
imports: from nltk.translate.bleu_score import sentence_bleu
  from nltk.translate.bleu_score import SmoothingFunction
  import sys
  import re
  import argparse
  import torch
  from util import read_corpus
  import numpy
  from scipy.misc import comb
  from vocab import Vocab, VocabEntry
  import math
  from rouge import Rouge
label_desc: Use image node_id 2 for calling a global function with example usage: sample_from_model(args) without return types