def per_item_bleu(y_true, y_predicted):
if isinstance(y_true[0], (tuple, list)):
y_true = map(lambda y: y[0], y_true)
y_true = list(itertools.chain(*y_true))
y_predicted = itertools.chain(*y_predicted)
examples_len = len(y_true)
bleu_list = (sentence_bleu([y2.lower().split()], y1.lower().split())\
for y1, y2 in zip(y_true, y_predicted))
return sum(bleu_list) / examples_len if examples_len else 0.
After Change
def per_item_bleu(y_true, y_predicted):
y_predicted = itertools.chain(*y_predicted)
if isinstance(y_true[0][0], (tuple, list)):
y_true = (y[0] for y_list in y_true for y in y_list)
return corpus_bleu([[y_t.lower().split()] for y_t in y_true],
[y_p.lower().split() for y_p in y_predicted])