code: def sample_ngram(args):
      src_sents = read_corpus(args.src, "src")
      tgt_sents = read_corpus(
          args.tgt, "src"
      )  # do not read in <s> and </s>
      f_out = open(args.output, "w")
  
      vocab = torch.load(args.vocab)
      tgt_vocab = vocab.tgt
  
      smooth_bleu = args.smooth_bleu
      sm_func = None
      if smooth_bleu:
          sm_func = SmoothingFunction().method3
  
      for src_sent, tgt_sent in zip(src_sents, tgt_sents):
          src_sent = " ".join(src_sent)
  
          tgt_len = len(tgt_sent)
          tgt_samples = []
          tgt_samples_distort_rates = (
              []
          )  # how many unigrams are replaced
  
          # generate 100 samples
  
          # append itself
          tgt_samples.append(tgt_sent)
          tgt_samples_distort_rates.append(0)
  
          for sid in range(args.sample_size - 1):
              n = np.random.randint(
                  1, min(tgt_len, args.max_ngram_size + 1)
              )  # we do not replace the last token: it must be a period!
  
              idx = np.random.randint(tgt_len - n)
              ngram = tgt_sent[idx : idx + n]
              new_ngram = get_new_ngram(ngram, n, tgt_vocab)
  
              sampled_tgt_sent = list(tgt_sent)
              sampled_tgt_sent[idx : idx + n] = new_ngram
  
              # compute the probability of this sample
              # prob = 1. / args.max_ngram_size * 1. / (tgt_len - 1 + n) * 1 / (len(tgt_vocab) ** n)
  
              tgt_samples.append(sampled_tgt_sent)
              tgt_samples_distort_rates.append(n)
  
          # compute bleu scores or edit distances and rank the samples by bleu scores
          rewards = []
          for tgt_sample, tgt_sample_distort_rate in zip(
              tgt_samples, tgt_samples_distort_rates
          ):
              if args.reward == "bleu":
                  reward = sentence_bleu(
                      [tgt_sent], tgt_sample, smoothing_function=sm_func
                  )
              elif args.reward == "rouge":
                  rouge = Rouge()
                  scores = rouge.get_scores(
                      hyps=[" ".join(tgt_sample).decode("utf-8")],
                      refs=[" ".join(tgt_sent).decode("utf-8")],
                      avg=True,
                  )
                  reward = sum(
                      [value["f"] for key, value in scores.items()]
                  )
              else:
                  reward = -tgt_sample_distort_rate
  
              rewards.append(reward)
  
          tgt_ranks = sorted(
              range(len(tgt_samples)),
              key=lambda i: rewards[i],
              reverse=True,
          )
          # convert list of tokens into a string
          tgt_samples = [
              " ".join(tgt_sample) for tgt_sample in 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], rewards[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 4 for calling a global function with example usage: sample_ngram(args) without return types