def compute_bleu(reference_corpus, translation_corpus, max_order=<NUM_LIT:4>,<EOL>smooth=False):
matches_by_order = [<NUM_LIT:0>] * max_order<EOL>possible_matches_by_order = [<NUM_LIT:0>] * max_order<EOL>reference_length = <NUM_LIT:0><EOL>translation_length = <NUM_LIT:0><EOL>for (references, translation) in zip(reference_corpus,<EOL>translation_corpus):<EOL><INDENT>reference_length += min(len(r) for r in references)<EOL>translation_length += len(translation)<EOL>merged_ref_ngram_counts = collections.Counter()<EOL>for reference in references:<EOL><INDENT>merged_ref_ngram_counts |= _get_ngrams(reference, max_order)<EOL><DEDENT>translation_ngram_counts = _get_ngrams(translation, max_order)<EOL>overlap = translation_ngram_counts & merged_ref_ngram_counts<EOL>for ngram in overlap:<EOL><INDENT>matches_by_order[len(ngram)-<NUM_LIT:1>] += overlap[ngram]<EOL><DEDENT>for order in range(<NUM_LIT:1>, max_order+<NUM_LIT:1>):<EOL><INDENT>possible_matches = len(translation) - order + <NUM_LIT:1><EOL>if possible_matches > <NUM_LIT:0>:<EOL><INDENT>possible_matches_by_order[order-<NUM_LIT:1>] += possible_matches<EOL><DEDENT><DEDENT><DEDENT>precisions = [<NUM_LIT:0>] * max_order<EOL>for i in range(<NUM_LIT:0>, max_order):<EOL><INDENT>if smooth:<EOL><INDENT>precisions[i] = ((matches_by_order[i] + <NUM_LIT:1.>) /<EOL>(possible_matches_by_order[i] + <NUM_LIT:1.>))<EOL><DEDENT>else:<EOL><INDENT>if possible_matches_by_order[i] > <NUM_LIT:0>:<EOL><INDENT>precisions[i] = (float(matches_by_order[i]) /<EOL>possible_matches_by_order[i])<EOL><DEDENT>else:<EOL><INDENT>precisions[i] = <NUM_LIT:0.0><EOL><DEDENT><DEDENT><DEDENT>if min(precisions) > <NUM_LIT:0>:<EOL><INDENT>p_log_sum = sum((<NUM_LIT:1.> / max_order) * math.log(p) for p in precisions)<EOL>geo_mean = math.exp(p_log_sum)<EOL><DEDENT>else:<EOL><INDENT>geo_mean = <NUM_LIT:0><EOL><DEDENT>ratio = float(translation_length) / reference_length<EOL>if ratio > <NUM_LIT:1.0>:<EOL><INDENT>bp = <NUM_LIT:1.><EOL><DEDENT>else:<EOL><INDENT>bp = math.exp(<NUM_LIT:1> - <NUM_LIT:1.> / ratio)<EOL><DEDENT>bleu = geo_mean * bp<EOL>return (bleu, precisions, bp, ratio, translation_length, reference_length)<EOL>
Computes BLEU score of translated segments against one or more references.

    Args:
      reference_corpus: list of lists of references for each translation. Each
          reference should be tokenized into a list of tokens.
      translation_corpus: list of translations to score. Each translation
          should be tokenized into a list of tokens.
      max_order: Maximum n-gram order to use when computing BLEU score.
      smooth: Whether or not to apply Lin et al. 2004 smoothing.

    Returns:
      3-Tuple with the BLEU score, n-gram precisions, geometric mean of n-gram
      precisions and brevity penalty.