bleu.py 文件源码

python
阅读 34 收藏 0 点赞 0 评论 0

项目:atma 作者: AtmaHou 项目源码 文件源码
def bleu(candidate, references, weights):
    """
    Calculate BLEU for a single sentence, comment by atma
    The result of this code is same as the most popular perl script
    eg:
        weight = [0.25, 0.25, 0.25, 0.25]
        can = 'It is a guide to action which ensures that the military always obeys the commands of the party'.lower().split()
        ref1 = 'It is a guide to action that ensures that the military will forever heed Party commands'.lower().split()
        ref2 = 'It is the guiding principle which guarantees the military forces always being under the command of the Party'.lower().split()
        ref = [ref1, ref2]
        print bleu(can, ref, weight)
    :param candidate: word list of one sentence, eg: ['I', 'like', 'eat', 'apple']
    :param references: list of ref, each is a list of word, eg [['I', 'like', 'eat', 'apple'],['I', 'like', 'apple']]
    :param weights: a list of weight
    :return: return the bleu score
    """
    p_ns = ( MP(candidate, references, i) for i, _ in enumerate(weights, start=1))
    s = []
    for w, p_n in zip(weights, p_ns):
        try:
            s.append(w * math.log(p_n))
        except ValueError:
            s.append(0)
    s = math.fsum(s)

    bp = BP(candidate, references)
    return bp * math.exp(s)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号