natural language toolkit translation metrics c 20012023 nltk project will zhang wilzzhagmail com guan gui gguistudent unimelb edu au steven bird stevenbird1gmail com url https www nltk org for license information see license txt return the alignment error rate aer of an alignment with respect to a gold standard reference alignment return an error rate between 0 0 perfect alignment and 1 0 no alignment from nltk translate import alignment ref alignment0 0 1 1 2 2 test alignment0 0 1 2 2 1 alignmenterrorrateref test doctest ellipsis 0 6666666666666667 type reference alignment param reference a gold standard alignment sure alignments type hypothesis alignment param hypothesis a hypothesis alignment aka candidate alignments type possible alignment or none param possible a gold standard reference of possible alignments defaults to reference if none rtype float or none natural language toolkit translation metrics c 2001 2023 nltk project will zhang wilzzha gmail com guan gui ggui student unimelb edu au steven bird stevenbird1 gmail com url https www nltk org for license information see license txt return the alignment error rate aer of an alignment with respect to a gold standard reference alignment return an error rate between 0 0 perfect alignment and 1 0 no alignment from nltk translate import alignment ref alignment 0 0 1 1 2 2 test alignment 0 0 1 2 2 1 alignment_error_rate ref test doctest ellipsis 0 6666666666666667 type reference alignment param reference a gold standard alignment sure alignments type hypothesis alignment param hypothesis a hypothesis alignment aka candidate alignments type possible alignment or none param possible a gold standard reference of possible alignments defaults to reference if none rtype float or none sanity check
def alignment_error_rate(reference, hypothesis, possible=None):
    

    if possible is None:
        possible = reference
    else:
        assert reference.issubset(possible)  

    return 1.0 - (len(hypothesis & reference) + len(hypothesis & possible)) / float(
        len(hypothesis) + len(reference)
    )