1
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)

I'd like to unpack the nltk.bleu_score library so that I can easily import it into Android_app development.

If I run below,

bleu = BLEU()    
bleu.compute(candidate, reference_wik)

It returns error like this:

---------------------------------------------------------------------------
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

where

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)']]

I can't track of what causes the problem with given error_message. Any hint to debug?

snapper
  • 997
  • 1
  • 12
  • 15

1 Answers1

0

The first argument in any class function is the object instance variable and supposed to be named as self, think of it like this in Java. From official docs:

Often, the first argument of a method is called self. This is nothing more than a convention: the name self has absolutely no special meaning to Python.

And from this SO answer:

The first parameter of methods is the instance the method is called on. That makes methods entirely the same as functions, and leaves the actual name to use up to you (although self is the convention, and people will generally frown at you when you use something else.)

So change your code to:

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):
        ...
abybaddi009
  • 1,014
  • 9
  • 22