def get_nltk_bleu_methods()

in tseval/feature_extraction.py [0:0]


def get_nltk_bleu_methods():
    """Returns bleu methods with different smoothings from NLTK.
Signature: scoring_method(complex_sentence, simple_setence)
    """
    # Inline lazy import because importing nltk is slow
    from nltk.translate.bleu_score import sentence_bleu, SmoothingFunction

    def get_scoring_method(smoothing_function):
        """Necessary to wrap the scoring_method() in get_scoring_method(), in order to set the external variable to
        its current value."""
        def scoring_method(complex_sentence, simple_sentence):
            try:
                return sentence_bleu([complex_sentence.split()], simple_sentence.split(),
                                     smoothing_function=smoothing_function)
            except AssertionError:
                return 0
        return scoring_method

    methods = []
    for i in range(8):
        smoothing_function = getattr(SmoothingFunction(), f'method{i}')
        scoring_method = get_scoring_method(smoothing_function)
        scoring_method.__name__ = f'nltkBLEU_method{i}'
        methods.append(scoring_method)
    return methods