Shortcuts

pytorch_lightning.metrics.functional.nlp module

pytorch_lightning.metrics.functional.nlp._count_ngram(ngram_input_list, n_gram)[source]

Counting how many times each word appears in a given text with ngram

Parameters
  • ngram_input_list (List[str]) – A list of translated text or reference texts

  • n_gram (int) – gram value ranged 1 to 4

Returns

a collections.Counter object of ngram

Return type

ngram_counter

pytorch_lightning.metrics.functional.nlp.bleu_score(translate_corpus, reference_corpus, n_gram=4, smooth=False)[source]

Calculate BLEU score of machine translated text with one or more references

Parameters
  • translate_corpus (Sequence[str]) – An iterable of machine translated corpus

  • reference_corpus (Sequence[str]) – An iterable of iterables of reference corpus

  • n_gram (int) – Gram value ranged from 1 to 4 (Default 4)

  • smooth (bool) – Whether or not to apply smoothing – Lin et al. 2004

Return type

Tensor

Returns

Tensor with BLEU Score

Example

>>> translate_corpus = ['the cat is on the mat'.split()]
>>> reference_corpus = [['there is a cat on the mat'.split(), 'a cat is on the mat'.split()]]
>>> bleu_score(translate_corpus, reference_corpus)
tensor(0.7598)