首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BLEU评分在Python中的实现

BLEU评分在Python中的实现
EN

Stack Overflow用户
提问于 2018-02-27 05:13:54
回答 1查看 788关注 0票数 1
代码语言:javascript
复制
class BLEU(object):
    def compute(candidate, references, weights):
        candidate = [c.lower() for c in candidate]
        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))
        s = math.fsum(w * math.log(p_n) for w, p_n in zip(weights, p_ns) if p_n)

        bp = BLEU.brevity_penalty(candidate, references)
        return bp * math.exp(s)

    def modified_precision(candidate, references, n):

        counts = Counter(ngrams(candidate, n))

        if not counts:
            return 0

        max_counts = {}
        for reference in references:
            reference_counts = Counter(ngrams(reference, n))
            for ngram in counts:
                max_counts[ngram] = max(max_counts.get(ngram, 0), reference_counts[ngram])

        clipped_counts = dict((ngram, min(count, max_counts[ngram])) for ngram, count in counts.items())

        return sum(clipped_counts.values()) / sum(counts.values())

    def brevity_penalty(candidate, references):
        c = len(candidate)
        r = min(abs(len(r) - c) for r in references)

        if c > r:
            return 1
        else:
            return math.exp(1 - r / c)

我想解压缩nltk.bleu_score库,以便能够轻松地将它导入到Android_app开发中。

如果我跑到下面,

代码语言:javascript
复制
bleu = BLEU()    
bleu.compute(candidate, reference_wik)

它返回的错误如下:

代码语言:javascript
复制
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-243daadf668f> in <module>()
----> 1 bleu.compute(candidate, reference_wik)

<ipython-input-11-1d4045ea108d> in compute(candidate, references, weights)
      1 class BLEU(object):
      2     def compute(candidate, references, weights):
----> 3         candidate = [c.lower() for c in candidate]
      4         references = [[r.lower() for r in reference] for reference in references]
      5 

TypeError: 'BLEU' object is not iterable

哪里

代码语言:javascript
复制
candidate = ['consists', 'of', 'to', 'make', 'a', 'whole']

reference_wik = [['To', 'be', 'made', 'up', 'of;', 'to', 'consist', 'of', '(especially', 'a', 'comprehensive', 'list', 'of', 'parts).', '[from', 'earlier', '15thc]'], ['To', 'contain', 'or', 'embrace.', '[from', 'earlier', '15thc]'], ['proscribed,', 'usually', 'in', 'the', 'passive)', 'To', 'compose,', 'to', 'constitute.', 'See', 'usage', 'note', 'below'], ['law)', 'To', 'include,', 'contain,', 'or', 'be', 'made', 'up', 'of,', 'defining', 'the', 'minimum', 'elements,', 'whether', 'essential', 'or', 'inessential,', 'to', 'define', 'an', 'invention.', '("Open-ended",', "doesn't", 'limit', 'to', 'the', 'items', 'listed;', 'cf.', 'compose,', 'which', 'is', '"closed"', 'and', 'limits', 'to', 'the', 'items', 'listed)']]

我无法跟踪是什么导致了给定error_message的问题。有调试的提示吗?

EN

回答 1

Stack Overflow用户

发布于 2018-02-27 10:47:56

任何类函数中的第一个参数是object实例变量,假定它被命名为self,请将其想象为Java中的this。来自官方文档

通常,方法的第一个参数称为self。这不过是一种约定:名称self对Python来说绝对没有什么特殊意义。

从这个开始,回答

方法的第一个参数是调用该方法的实例。这使得方法与函数完全相同,将实际名称留给您使用(尽管self是惯例,当您使用其他东西时,人们通常会对您皱眉)。

因此,将代码更改为:

代码语言:javascript
复制
class BLEU(object):
    # add self in the functions
    def compute(self, candidate, references, weights):
        ...
        # don't do this
        # bp = BLEU.brevity_penalty(candidate, references)
        # instead call the method from like this:
        bp = self.brevity_penalty(candidate, references)
        ...

    def modified_precision(self, candidate, references, n):
        ...

    def brevity_penalty(self, candidate, references):
        ...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49001562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档