code: def corpus_bleu_moses(
      list_of_references, hypotheses, lowercase=False, return_all=False
  ):
      """Calculates corpus-level BLEU score using the
      **MOSES multi-bleu.perl** script.
  
      Args:
          list_of_references: A list of lists of references for each hypothesis.
              Each reference can be either a string, or a list of string tokens.
              List can also be numpy array.
          hypotheses: A list of hypothesis sentences.
              Each hyperthsis can be either a string, or a list of string tokens.
              List can also be numpy array.
          lowercase (bool): If `True`, pass the "-lc" flag to the multi-bleu
              script.
          return_all (bool): If `True`, returns BLEU and all n-gram precisions.
  
      Returns:
          If :attr:`return_all` is `False` (default), returns a float32
          BLEU score.
  
          If :attr:`return_all` is `True`, returns a list of 5 float32 scores:
          `[BLEU, 1-gram precision, ..., 4-gram precision]`.
      """
      list_of_references = compat_as_text(list_of_references)
      hypotheses = compat_as_text(hypotheses)
  
      if np.size(hypotheses) == 0:
          return np.float32(0.0)  # pylint: disable=no-member
  
      # Get multi-bleu.perl
      cur_dir = os.path.dirname(os.path.realpath(__file__))
      multi_bleu_path = os.path.abspath(
          os.path.join(
              cur_dir,
              "..",
              "..",
              "..",
              "bin",
              "utils",
              "multi-bleu.perl",
          )
      )
  
      # Create a temporary folder containing hyperthesis and reference files
      result_path = tempfile.mkdtemp()
      # Create hyperthesis file
      hfile_path = os.path.join(result_path, "hyp")
      hyps = [_maybe_list_to_str(h) for h in hypotheses]
      with open(hfile_path, "w", encoding="utf-8") as hfile:
          text = "\n".join(hyps)
          hfile.write(text)
          hfile.write("\n")
      # Create reference files
      max_nrefs = max([len(refs) for refs in list_of_references])
      rfile_path = os.path.join(result_path, "ref")
      for rid in range(max_nrefs):
          with open(
              rfile_path + "%d" % rid, "w", encoding="utf-8"
          ) as rfile:
              for refs in list_of_references:
                  if rid < len(refs):
                      ref = _maybe_list_to_str(refs[rid])
                      rfile.write(ref + "\n")
                  else:
                      rfile.write("\n")
  
      # Calculate BLEU
      multi_bleu_cmd = [multi_bleu_path]
      if lowercase:
          multi_bleu_cmd += ["-lc"]
      multi_bleu_cmd += [rfile_path]
      with open(hfile_path, "r") as hyp_input:
          try:
              multi_bleu_ret = subprocess.check_output(
                  multi_bleu_cmd,
                  stdin=hyp_input,
                  stderr=subprocess.STDOUT,
              )
              multi_bleu_ret = multi_bleu_ret.decode("utf-8")
              bleu_score = _parse_multi_bleu_ret(
                  multi_bleu_ret, return_all
              )
          except subprocess.CalledProcessError as error:
              if error.output is not None:
                  tf.logging.warning(
                      "multi-bleu.perl returned non-zero exit code"
                  )
                  tf.logging.warning(error.output)
              if return_all:
                  bleu_score = [np.float32(0.0)] * 5
              else:
                  bleu_score = np.float32(0.0)
  
      shutil.rmtree(result_path)
  
      return np.float32(bleu_score)
  
file_path: texar/texar/tf/evals/bleu_moses.py
imports: import os
  from io import open
  import shutil
  import re
  import subprocess
  import tempfile
  import numpy
  import tensorflow
  from texar.tf.utils.dtypes import compat_as_text
label_desc: Use image node_id 4 for calling a global function with example usage: corpus_bleu_moses(list_of_references, hypotheses, lowercase, return_all) and returns: np, np