# Path: third_party/nmt/scripts/rouge.py
# def rouge(hypotheses, references):
#   """Calculates average rouge scores for a list of hypotheses and
#   references"""
# 
#   # Filter out hyps that are of 0 length
#   # hyps_and_refs = zip(hypotheses, references)
#   # hyps_and_refs = [_ for _ in hyps_and_refs if len(_[0]) > 0]
#   # hypotheses, references = zip(*hyps_and_refs)
# 
#   # Calculate ROUGE-1 F1, precision, recall scores
#   rouge_1 = [
#       rouge_n([hyp], [ref], 1) for hyp, ref in zip(hypotheses, references)
#   ]
#   rouge_1_f, rouge_1_p, rouge_1_r = map(np.mean, zip(*rouge_1))
# 
#   # Calculate ROUGE-2 F1, precision, recall scores
#   rouge_2 = [
#       rouge_n([hyp], [ref], 2) for hyp, ref in zip(hypotheses, references)
#   ]
#   rouge_2_f, rouge_2_p, rouge_2_r = map(np.mean, zip(*rouge_2))
# 
#   # Calculate ROUGE-L F1, precision, recall scores
#   rouge_l = [
#       rouge_l_sentence_level([hyp], [ref])
#       for hyp, ref in zip(hypotheses, references)
#   ]
#   rouge_l_f, rouge_l_p, rouge_l_r = map(np.mean, zip(*rouge_l))
# 
#   return {
#       "rouge_1/f_score": rouge_1_f,
#       "rouge_1/r_score": rouge_1_r,
#       "rouge_1/p_score": rouge_1_p,
#       "rouge_2/f_score": rouge_2_f,
#       "rouge_2/r_score": rouge_2_r,
#       "rouge_2/p_score": rouge_2_p,
#       "rouge_l/f_score": rouge_l_f,
#       "rouge_l/r_score": rouge_l_r,
#       "rouge_l/p_score": rouge_l_p,
#   }


import codecs
import os
import re
import subprocess
import tensorflow as tf
from third_party.nmt.scripts import bleu
from third_party.nmt.scripts import rouge
      reference = _clean(reference, subword_option)
      reference_list.append(reference.split(" "))
    per_segment_references.append(reference_list)

  translations = []
  with codecs.getreader("utf-8")(tf.gfile.GFile(trans_file, "rb")) as fh:
    for line in fh:
      line = _clean(line, subword_option=None)
      translations.append(line.split(" "))

  # bleu_score, precisions, bp, ratio, translation_length, reference_length
  bleu_score, _, _, _, _, _ = bleu.compute_bleu(
      per_segment_references, translations, max_order, smooth)
  return 100 * bleu_score


def _rouge(ref_file, summarization_file, subword_option=None):
  """Compute ROUGE scores and handling BPE."""

  references = []
  with codecs.getreader("utf-8")(tf.gfile.GFile(ref_file, "rb")) as fh:
    for line in fh:
      references.append(_clean(line, subword_option))

  hypotheses = []
  with codecs.getreader("utf-8")(
      tf.gfile.GFile(summarization_file, "rb")) as fh:
    for line in fh:
      hypotheses.append(_clean(line, subword_option=None))


# Path: third_party/nmt/scripts/rouge.py
# def rouge(hypotheses, references):
#   """Calculates average rouge scores for a list of hypotheses and
#   references"""
# 
#   # Filter out hyps that are of 0 length
#   # hyps_and_refs = zip(hypotheses, references)
#   # hyps_and_refs = [_ for _ in hyps_and_refs if len(_[0]) > 0]
#   # hypotheses, references = zip(*hyps_and_refs)
# 
#   # Calculate ROUGE-1 F1, precision, recall scores
#   rouge_1 = [
#       rouge_n([hyp], [ref], 1) for hyp, ref in zip(hypotheses, references)
#   ]
#   rouge_1_f, rouge_1_p, rouge_1_r = map(np.mean, zip(*rouge_1))
# 
#   # Calculate ROUGE-2 F1, precision, recall scores
#   rouge_2 = [
#       rouge_n([hyp], [ref], 2) for hyp, ref in zip(hypotheses, references)
#   ]
#   rouge_2_f, rouge_2_p, rouge_2_r = map(np.mean, zip(*rouge_2))
# 
#   # Calculate ROUGE-L F1, precision, recall scores
#   rouge_l = [
#       rouge_l_sentence_level([hyp], [ref])
#       for hyp, ref in zip(hypotheses, references)
#   ]
#   rouge_l_f, rouge_l_p, rouge_l_r = map(np.mean, zip(*rouge_l))
# 
#   return {
#       "rouge_1/f_score": rouge_1_f,
#       "rouge_1/r_score": rouge_1_r,
#       "rouge_1/p_score": rouge_1_p,
#       "rouge_2/f_score": rouge_2_f,
#       "rouge_2/r_score": rouge_2_r,
#       "rouge_2/p_score": rouge_2_p,
#       "rouge_l/f_score": rouge_l_f,
#       "rouge_l/r_score": rouge_l_r,
#       "rouge_l/p_score": rouge_l_p,
#   }


# Path: third_party/nmt/utils/evaluation_utils.py
import codecs
import os
import re
import subprocess
import tensorflow as tf
from third_party.nmt.scripts import bleu
from third_party.nmt.scripts import rouge

      reference = _clean(reference, subword_option)
      reference_list.append(reference.split(" "))
    per_segment_references.append(reference_list)

  translations = []
  with codecs.getreader("utf-8")(tf.gfile.GFile(trans_file, "rb")) as fh:
    for line in fh:
      line = _clean(line, subword_option=None)
      translations.append(line.split(" "))

  # bleu_score, precisions, bp, ratio, translation_length, reference_length
  bleu_score, _, _, _, _, _ = bleu.compute_bleu(
      per_segment_references, translations, max_order, smooth)
  return 100 * bleu_score


def _rouge(ref_file, summarization_file, subword_option=None):
  """Compute ROUGE scores and handling BPE."""

  references = []
  with codecs.getreader("utf-8")(tf.gfile.GFile(ref_file, "rb")) as fh:
    for line in fh:
      references.append(_clean(line, subword_option))

  hypotheses = []
  with codecs.getreader("utf-8")(
      tf.gfile.GFile(summarization_file, "rb")) as fh:
    for line in fh:
      hypotheses.append(_clean(line, subword_option=None))


  rouge_score_map = rouge.rouge(hypotheses, references)