# Path: airdialogue/evaluator/metrics/bleu.py
# def _get_ngrams(segment, max_order):
# def compute_bleu(reference_corpus, translation_corpus, max_order=4,
#                  smooth=False):
# 
# Path: airdialogue/evaluator/metrics/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 = list(map(np.mean, list(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 = list(map(np.mean, list(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 = list(map(np.mean, list(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: airdialogue/evaluator/metrics/kl.py
# def _get_ngrams(segments, order):
# def compute_kl(reference_corpus,
#                translation_corpus,
#                max_order=4,
#                freq_thre=100,
#                workers=20,
#                verbose=False):


import codecs
import tensorflow.compat.v1 as tf
from airdialogue.evaluator.metrics import bleu
from airdialogue.evaluator.metrics import rouge
from airdialogue.evaluator.metrics import kl
  per_segment_references = []
  role_tokens = []
  for references in zip(*reference_text):
    reference_list = []
    for reference in references:
      reference, role = process_dialogue_infer(
          reference.rstrip(), get_role_token=True)
      reference_list.append(reference.split(" "))
    per_segment_references.append(reference_list)
    role_tokens.append(role)

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

  results = {}
  bleu_score, _, _, _, _, _ = bleu.compute_bleu(per_segment_references,
                                                translations, max_order, smooth)
  results["all"] = 100 * bleu_score
  if mode == "brief":
    return results["all"]

  for role in ROLE_TOKENS:
    _sub_ref_texts = []
    _sub_trans = []
    for _r, _t, _role in zip(per_segment_references, translations, role_tokens):
      if _role == role:
        _sub_ref_texts.append(_r)
        _sub_trans.append(_t)

# Path: airdialogue/evaluator/metrics/bleu.py
# def _get_ngrams(segment, max_order):
# def compute_bleu(reference_corpus, translation_corpus, max_order=4,
#                  smooth=False):
# 
# Path: airdialogue/evaluator/metrics/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 = list(map(np.mean, list(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 = list(map(np.mean, list(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 = list(map(np.mean, list(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: airdialogue/evaluator/metrics/kl.py
# def _get_ngrams(segments, order):
# def compute_kl(reference_corpus,
#                translation_corpus,
#                max_order=4,
#                freq_thre=100,
#                workers=20,
#                verbose=False):


# Path: airdialogue/evaluator/infer_utils.py
import codecs
import tensorflow.compat.v1 as tf
from airdialogue.evaluator.metrics import bleu
from airdialogue.evaluator.metrics import rouge
from airdialogue.evaluator.metrics import kl

  per_segment_references = []
  role_tokens = []
  for references in zip(*reference_text):
    reference_list = []
    for reference in references:
      reference, role = process_dialogue_infer(
          reference.rstrip(), get_role_token=True)
      reference_list.append(reference.split(" "))
    per_segment_references.append(reference_list)
    role_tokens.append(role)

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

  results = {}
  bleu_score, _, _, _, _, _ = bleu.compute_bleu(per_segment_references,
                                                translations, max_order, smooth)
  results["all"] = 100 * bleu_score
  if mode == "brief":
    return results["all"]

  for role in ROLE_TOKENS:
    _sub_ref_texts = []
    _sub_trans = []
    for _r, _t, _role in zip(per_segment_references, translations, role_tokens):
      if _role == role:
        _sub_ref_texts.append(_r)
        _sub_trans.append(_t)

    bleu_score, _, _, _, _, _ = bleu.compute_bleu(_sub_ref_texts, _sub_trans,