Commit d13caba2 by Steven Bird

stylistic changes

parent 33355f4f
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Natural Language Toolkit: BLEU # Natural Language Toolkit: BLEU
# #
# Copyright (C) 2001-2013 NLTK Project # Copyright (C) 2001-2014 NLTK Project
# Authors: Chin Yee Lee, Hengfeng Li, Ruxin Hou, Calvin Tanujaya Lim # Authors: Chin Yee Lee, Hengfeng Li, Ruxin Hou, Calvin Tanujaya Lim
# Contributors: Dmitrijs Milajevs
# URL: <http://nltk.org/> # URL: <http://nltk.org/>
# For license information, see LICENSE.TXT # For license information, see LICENSE.TXT
...@@ -131,18 +132,17 @@ class BLEU(object): ...@@ -131,18 +132,17 @@ class BLEU(object):
candidate = [c.lower() for c in candidate] candidate = [c.lower() for c in candidate]
references = [[r.lower() for r in reference] for reference in references] references = [[r.lower() for r in reference] for reference in references]
p_ns = (BLEU.modified_precision(candidate, references, i) for i, _ in enumerate(weights, start=1)) p_ns = (BLEU.modified_precision(candidate, references, i)
for i, _ in enumerate(weights, start=1))
p_ns_nonzero = list(filter(None, p_ns)) p_ns_nonzero = list(filter(None, p_ns))
if not p_ns_nonzero: if p_ns_nonzero:
# There is zero aliment, so the score is 0 s = math.fsum(w * math.log(p_n) for w, p_n in zip(weights, p_ns_nonzero))
bp = BLEU.brevity_penalty(candidate, references)
return bp * math.exp(s)
else: # no alignments
return 0 return 0
s = math.fsum(w * math.log(p_n) for w, p_n in zip(weights, p_ns_nonzero))
bp = BLEU.brevity_penalty(candidate, references)
return bp * math.exp(s)
@staticmethod @staticmethod
def modified_precision(candidate, references, n): def modified_precision(candidate, references, n):
""" Calculate modified ngram precision. """ Calculate modified ngram precision.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment