mindspore.nn.BleuScore

class mindspore.nn.BleuScore(n_gram=4, smooth=False)[source]

Calculates BLEU score of machine translated text with one or more references.

Parameters
  • n_gram (int) – The n_gram value ranged from 1 to 4. Default: 4.

  • smooth (bool) – Whether or not to apply smoothing. Default: False.

Raises

ValueError – If the value range of n_gram is not 1 to 4.

Supported Platforms:

Ascend GPU CPU

Example

>>> candidate_corpus = [['i', 'have', 'a', 'pen', 'on', 'my', 'desk']]
>>> reference_corpus = [[['i', 'have', 'a', 'pen', 'in', 'my', 'desk'],
...                      ['there', 'is', 'a', 'pen', 'on', 'the', 'desk']]]
>>> metric = BleuScore()
>>> metric.clear()
>>> metric.update(candidate_corpus, reference_corpus)
>>> bleu_score = metric.eval()
>>> print(bleu_score)
0.5946035575013605
clear()[source]

Clear the internal evaluation result.

eval()[source]

Computes the bleu score.

Returns

A numpy with bleu score.

Raises

RuntimeError – If the update method is not called first, an error will be reported.

update(*inputs)[source]

Updates the internal evaluation result with candidate_corpus and reference_corpus.

Parameters

inputs – Input candidate_corpus and reference_corpus. candidate_corpus and reference_corpus are a list. The candidate_corpus is an iterable of machine translated corpus. The reference_corpus is an iterable of iterables of reference corpus.

Raises

ValueError – If the number of input is not 2.