module documentation

ChrF score implementation

Function chrf_precision_recall_fscore_support This function computes the precision, recall and fscore from the ngram overlaps. It returns the support which is the true positive score.
Function corpus_chrf Calculates the corpus level CHRF (Character n-gram F-score), it is the macro-averaged value of the sentence/segment level CHRF score.
Function sentence_chrf Maja Popovic. 2015. CHRF: Character n-gram F-score for Automatic MT Evaluation. In Proceedings of the 10th Workshop on Machine Translation. http://www.statmt.org/wmt15/pdf/WMT49.pdf
Function _preprocess Undocumented
def chrf_precision_recall_fscore_support(reference, hypothesis, n, beta=3.0, epsilon=1e-16): (source)

This function computes the precision, recall and fscore from the ngram overlaps. It returns the support which is the true positive score.

By underspecifying the input type, the function will be agnostic as to how it computes the ngrams and simply take the whichever element in the list; it could be either token or character.

Parameters
reference:listThe reference sentence.
hypothesis:listThe hypothesis sentence.
n:intExtract up to the n-th order ngrams
beta:floatThe parameter to assign more importance to recall over precision.
epsilon:floatThe fallback value if the hypothesis or reference is empty.
Returns
tuple(float)Returns the precision, recall and f-score and support (true positive).
def corpus_chrf(references, hypotheses, min_len=1, max_len=6, beta=3.0, ignore_whitespace=True): (source)

Calculates the corpus level CHRF (Character n-gram F-score), it is the macro-averaged value of the sentence/segment level CHRF score.

This implementation of CHRF only supports a single reference at the moment.

>>> ref1 = str('It is a guide to action that ensures that the military '
...            'will forever heed Party commands').split()
>>> ref2 = str('It is the guiding principle which guarantees the military '
...            'forces always being under the command of the Party').split()
>>>
>>> hyp1 = str('It is a guide to action which ensures that the military '
...            'always obeys the commands of the party').split()
>>> hyp2 = str('It is to insure the troops forever hearing the activity '
...            'guidebook that party direct')
>>> corpus_chrf([ref1, ref2, ref1, ref2], [hyp1, hyp2, hyp2, hyp1]) # doctest: +ELLIPSIS
0.3910...
Parameters
references:list(list(str))a corpus of list of reference sentences, w.r.t. hypotheses
hypotheses:list(list(str))a list of hypothesis sentences
min_len:intThe minimum order of n-gram this function should extract.
max_len:intThe maximum order of n-gram this function should extract.
beta:floatthe parameter to assign more importance to recall over precision
ignore_whitespace:boolignore whitespace characters in scoring
Returns
floatthe sentence level CHRF score.
def sentence_chrf(reference, hypothesis, min_len=1, max_len=6, beta=3.0, ignore_whitespace=True): (source)

Calculates the sentence level CHRF (Character n-gram F-score) described in

This implementation of CHRF only supports a single reference at the moment.

For details not reported in the paper, consult Maja Popovic's original implementation: https://github.com/m-popovic/chrF

The code should output results equivalent to running CHRF++ with the following options: -nw 0 -b 3

An example from the original BLEU paper http://www.aclweb.org/anthology/P02-1040.pdf

>>> ref1 = str('It is a guide to action that ensures that the military '
...            'will forever heed Party commands').split()
>>> hyp1 = str('It is a guide to action which ensures that the military '
...            'always obeys the commands of the party').split()
>>> hyp2 = str('It is to insure the troops forever hearing the activity '
...            'guidebook that party direct').split()
>>> sentence_chrf(ref1, hyp1) # doctest: +ELLIPSIS
0.6349...
>>> sentence_chrf(ref1, hyp2) # doctest: +ELLIPSIS
0.3330...

The infamous "the the the ... " example

>>> ref = 'the cat is on the mat'.split()
>>> hyp = 'the the the the the the the'.split()
>>> sentence_chrf(ref, hyp)  # doctest: +ELLIPSIS
0.1468...

An example to show that this function allows users to use strings instead of tokens, i.e. list(str) as inputs.

>>> ref1 = str('It is a guide to action that ensures that the military '
...            'will forever heed Party commands')
>>> hyp1 = str('It is a guide to action which ensures that the military '
...            'always obeys the commands of the party')
>>> sentence_chrf(ref1, hyp1) # doctest: +ELLIPSIS
0.6349...
>>> type(ref1) == type(hyp1) == str
True
>>> sentence_chrf(ref1.split(), hyp1.split()) # doctest: +ELLIPSIS
0.6349...

To skip the unigrams and only use 2- to 3-grams:

>>> sentence_chrf(ref1, hyp1, min_len=2, max_len=3) # doctest: +ELLIPSIS
0.6617...

Parameters
referenceUndocumented
hypothesis:list(str) / stra hypothesis sentence
min_len:intThe minimum order of n-gram this function should extract.
max_len:intThe maximum order of n-gram this function should extract.
beta:floatthe parameter to assign more importance to recall over precision
ignore_whitespace:boolignore whitespace characters in scoring
references:list(str) / strreference sentence
Returns
floatthe sentence level CHRF score.
def _preprocess(sent, ignore_whitespace): (source)

Undocumented