Beispiel #1
0
def test_corpus_fkgl():
    assert corpus_fkgl(
        get_orig_sents('turkcorpus_test_legacy')) == pytest.approx(9.9,
                                                                   abs=1e-1)
    assert corpus_fkgl(
        get_refs_sents('turkcorpus_test_legacy')[0]) == pytest.approx(8.2,
                                                                      abs=1e-1)
Beispiel #2
0
def test_corpus_fkgl():
    assert corpus_fkgl(read_file(
        "data/test_sets/turk/test.8turkers.tok.norm")) == pytest.approx(
            9.9, abs=1e-1)
    assert corpus_fkgl(
        read_file(
            "data/test_sets/turk/test.8turkers.tok.turk.0")) == pytest.approx(
                8.2, abs=1e-1)
Beispiel #3
0
def get_all_scores(
        orig_sents: List[str], sys_sents: List[str], refs_sents: List[List[str]],
        lowercase: bool = False, tokenizer: str = '13a', metrics: List[str] = DEFAULT_METRICS,
        ):
    scores = OrderedDict()
    if 'bleu' in metrics:
        scores['BLEU'] = corpus_bleu(sys_sents, refs_sents, force=True, tokenize=tokenizer, lowercase=lowercase).score
    if 'sari' in metrics:
        scores['SARI'] = corpus_sari(orig_sents, sys_sents, refs_sents, tokenizer=tokenizer, lowercase=lowercase)
    if 'samsa' in metrics:
        from easse.samsa import corpus_samsa
        scores['SAMSA'] = corpus_samsa(orig_sents, sys_sents, tokenizer=tokenizer, verbose=True, lowercase=lowercase)
    if 'fkgl' in metrics:
        scores['FKGL'] = corpus_fkgl(sys_sents, tokenizer=tokenizer)
    quality_estimation_scores = corpus_quality_estimation(
            orig_sents,
            sys_sents,
            tokenizer=tokenizer,
            lowercase=lowercase
            )
    scores = add_dicts(
            scores,
            quality_estimation_scores,
            )
    return {key: round(value, 2) for key, value in scores.items()}
Beispiel #4
0
def evaluate_sentences(comps,
                       simps,
                       simp_preds,
                       calc_simp_pred_bleu=False,
                       calc_comp_simp_bleu=False):

    comps = [sent.lower() for sent in comps]
    if type(simps[0]) == type([]):
        simps = [[sent.lower() for sent in l] for l in simps]
    else:
        simps = [sent for sent in simps]
    simp_preds = [sent.lower() for sent in simp_preds]
    if type(simps[0]) == type([]):
        refs = simps
    else:
        refs = [simps]

    bleu = corpus_bleu(simp_preds,
                       refs,
                       force=True,
                       tokenizer='none',
                       lowercase=True)
    sari = corpus_sari(comps,
                       simp_preds,
                       refs,
                       tokenizer="none",
                       lowercase=True)
    fkgl = corpus_fkgl(simp_preds, tokenizer='none')
    result = (bleu, sari, fkgl)
    if calc_simp_pred_bleu:
        result = result + (corpus_bleu(simp_preds, [comps]), )
    if calc_comp_simp_bleu:
        result = result + (corpus_bleu(comps, refs), )
    return result
Beispiel #5
0
def evaluate_system_output(
        test_set,
        sys_sents_path=None,
        orig_sents_path=None,
        refs_sents_paths=None,
        tokenizer='13a',
        metrics=','.join(DEFAULT_METRICS),
        analysis=False,
        quality_estimation=False,
        ):
    """
    Evaluate a system output with automatic metrics.
    """
    # get the metrics that need to be computed
    metrics = metrics.split(',')
    orig_sents, sys_sents, refs_sents = get_sents(test_set, orig_sents_path, sys_sents_path, refs_sents_paths)
    lowercase = is_test_set_lowercase(test_set)

    # compute each metric
    if 'bleu' in metrics:
        bleu_score = sacrebleu.corpus_bleu(sys_sents, refs_sents,
                                           force=True, tokenize=tokenizer, lowercase=lowercase).score
        click.echo(f'BLEU: {bleu_score:.2f}')

    if 'sari' in metrics:
        sari_score = corpus_sari(orig_sents, sys_sents, refs_sents, tokenizer=tokenizer, lowercase=lowercase)
        click.echo(f'SARI: {sari_score:.2f}')

    if 'samsa' in metrics:
        samsa_score = corpus_samsa(orig_sents, sys_sents, tokenizer=tokenizer, verbose=True, lowercase=lowercase)
        click.echo(f'SAMSA: {samsa_score:.2f}')

    if 'fkgl' in metrics:
        fkgl_score = corpus_fkgl(sys_sents, tokenizer=tokenizer)
        click.echo(f'FKGL: {fkgl_score:.2f}')

    if analysis:
        word_level_analysis = corpus_analyse_operations(orig_sents, sys_sents, refs_sents,
                                                        verbose=False, as_str=True)
        click.echo(f'Word-level Analysis: {word_level_analysis}')

    if quality_estimation:
        quality_estimation_scores = corpus_quality_estimation(
                orig_sents,
                sys_sents,
                tokenizer=tokenizer,
                lowercase=lowercase
                )
        quality_estimation_scores = {k: round(v, 2) for k, v in quality_estimation_scores.items()}
        click.echo(f'Quality estimation: {quality_estimation_scores}')
Beispiel #6
0
def evaluate_system_output(
    test_set,
    sys_sents_path=None,
    orig_sents_path=None,
    refs_sents_paths=None,
    tokenizer='13a',
    lowercase=True,
    metrics=DEFAULT_METRICS,
    analysis=False,
    quality_estimation=False,
):
    '''
    Evaluate a system output with automatic metrics.
    '''
    sys_sents = get_sys_sents(test_set, sys_sents_path)
    orig_sents, refs_sents = get_orig_and_refs_sents(test_set, orig_sents_path,
                                                     refs_sents_paths)

    # compute each metric
    metrics_scores = {}
    if 'bleu' in metrics:
        metrics_scores['bleu'] = corpus_bleu(sys_sents,
                                             refs_sents,
                                             force=True,
                                             tokenizer=tokenizer,
                                             lowercase=lowercase)

    if 'sent_bleu' in metrics:
        metrics_scores['sent_bleu'] = corpus_averaged_sentence_bleu(
            sys_sents, refs_sents, tokenizer=tokenizer, lowercase=lowercase)

    if 'sari' in metrics:
        metrics_scores['sari'] = corpus_sari(orig_sents,
                                             sys_sents,
                                             refs_sents,
                                             tokenizer=tokenizer,
                                             lowercase=lowercase)

    if 'sari_legacy' in metrics:
        metrics_scores['sari_legacy'] = corpus_sari(orig_sents,
                                                    sys_sents,
                                                    refs_sents,
                                                    tokenizer=tokenizer,
                                                    lowercase=lowercase,
                                                    legacy=True)

    if 'samsa' in metrics:
        from easse.samsa import corpus_samsa
        metrics_scores['samsa'] = corpus_samsa(orig_sents,
                                               sys_sents,
                                               tokenizer=tokenizer,
                                               lowercase=lowercase,
                                               verbose=True)

    if 'fkgl' in metrics:
        metrics_scores['fkgl'] = corpus_fkgl(sys_sents, tokenizer=tokenizer)

    if 'f1_token' in metrics:
        metrics_scores['f1_token'] = corpus_f1_token(sys_sents,
                                                     refs_sents,
                                                     tokenizer=tokenizer,
                                                     lowercase=lowercase)

    if analysis:
        metrics_scores['word_level_analysis'] = corpus_analyse_operations(
            orig_sents, sys_sents, refs_sents, verbose=False, as_str=True)

    if quality_estimation:
        metrics_scores['quality_estimation'] = corpus_quality_estimation(
            orig_sents, sys_sents, tokenizer=tokenizer, lowercase=lowercase)

    return metrics_scores
Beispiel #7
0
def evaluate_system_output(
    test_set,
    sys_sents_path=None,
    orig_sents_path=None,
    refs_sents_paths=None,
    tokenizer="13a",
    lowercase=True,
    metrics=DEFAULT_METRICS,
    analysis=False,
    quality_estimation=False,
):
    """
    Evaluate a system output with automatic metrics.
    """
    for metric in metrics:
        assert metric in VALID_METRICS, f'"{metric}" not a valid metric. Valid metrics: {VALID_METRICS}'
    sys_sents = get_sys_sents(test_set, sys_sents_path)
    orig_sents, refs_sents = get_orig_and_refs_sents(test_set, orig_sents_path,
                                                     refs_sents_paths)

    # compute each metric
    metrics_scores = {}
    if "bleu" in metrics:
        metrics_scores["bleu"] = corpus_bleu(
            sys_sents,
            refs_sents,
            force=True,
            tokenizer=tokenizer,
            lowercase=lowercase,
        )

    if "sent_bleu" in metrics:
        metrics_scores["sent_bleu"] = corpus_averaged_sentence_bleu(
            sys_sents, refs_sents, tokenizer=tokenizer, lowercase=lowercase)

    if "sari" in metrics:
        metrics_scores["sari"] = corpus_sari(
            orig_sents,
            sys_sents,
            refs_sents,
            tokenizer=tokenizer,
            lowercase=lowercase,
        )

    if "sari_legacy" in metrics:
        metrics_scores["sari_legacy"] = corpus_sari(
            orig_sents,
            sys_sents,
            refs_sents,
            tokenizer=tokenizer,
            lowercase=lowercase,
            legacy=True,
        )

    if "sari_by_operation" in metrics:
        (
            metrics_scores["sari_add"],
            metrics_scores["sari_keep"],
            metrics_scores["sari_del"],
        ) = get_corpus_sari_operation_scores(
            orig_sents,
            sys_sents,
            refs_sents,
            tokenizer=tokenizer,
            lowercase=lowercase,
        )

    if "samsa" in metrics:
        from easse.samsa import corpus_samsa

        metrics_scores["samsa"] = corpus_samsa(
            orig_sents,
            sys_sents,
            tokenizer=tokenizer,
            lowercase=lowercase,
            verbose=True,
        )

    if "fkgl" in metrics:
        metrics_scores["fkgl"] = corpus_fkgl(sys_sents, tokenizer=tokenizer)

    if "f1_token" in metrics:
        metrics_scores["f1_token"] = corpus_f1_token(sys_sents,
                                                     refs_sents,
                                                     tokenizer=tokenizer,
                                                     lowercase=lowercase)

    if "bertscore" in metrics:
        from easse.bertscore import corpus_bertscore  # Inline import to use EASSE without installing all dependencies
        (
            metrics_scores["bertscore_precision"],
            metrics_scores["bertscore_recall"],
            metrics_scores["bertscore_f1"],
        ) = corpus_bertscore(sys_sents,
                             refs_sents,
                             tokenizer=tokenizer,
                             lowercase=lowercase)

    if analysis:
        from easse.annotation.word_level import WordOperationAnnotator  # Inline import to use EASSE without installing all dependencies
        word_operation_annotator = WordOperationAnnotator(tokenizer=tokenizer,
                                                          lowercase=lowercase,
                                                          verbose=True)
        metrics_scores[
            "word_level_analysis"] = word_operation_annotator.analyse_operations(
                orig_sents, sys_sents, refs_sents, as_str=True)

    if quality_estimation:
        metrics_scores["quality_estimation"] = corpus_quality_estimation(
            orig_sents, sys_sents, tokenizer=tokenizer, lowercase=lowercase)

    return metrics_scores
Beispiel #8
0
def evaluate_system_output(
        test_set,
        input_path=None,
        tokenizer='13a',
        metrics=','.join(VALID_METRICS),
        analysis=False,
        quality_estimation=False,
        ):
    """
    Evaluate a system output with automatic metrics.
    """
    if input_path is not None:
        sys_output = read_lines(input_path)
    else:
        # read the system output
        with click.get_text_stream('stdin', encoding='utf-8') as system_output_file:
            sys_output = system_output_file.read().splitlines()

    # get the metrics that need to be computed
    metrics = metrics.split(',')

    load_orig_sents = ('sari' in metrics) or ('samsa' in metrics) or analysis or quality_estimation
    load_refs_sents = ('sari' in metrics) or ('bleu' in metrics) or analysis
    # get the references from the test set
    if test_set in ['turk', 'turk_valid']:
        lowercase = False
        phase = 'test' if test_set == 'turk' else 'valid'
        if load_orig_sents:
            orig_sents = get_turk_orig_sents(phase=phase)
        if load_refs_sents:
            refs_sents = get_turk_refs_sents(phase=phase)

    if test_set in ['pwkp', 'pwkp_valid']:
        lowercase = True
        phase = 'test' if test_set == 'pwkp' else 'valid'
        if load_orig_sents:
            orig_sents = get_pwkp_orig_sents(phase=phase)
        if load_refs_sents:
            refs_sents = get_pwkp_refs_sents(phase=phase)

    if test_set == 'hsplit':
        sys_output = sys_output[:70]
        lowercase = True
        if load_orig_sents:
            orig_sents = get_hsplit_orig_sents()
        if load_refs_sents:
            refs_sents = get_hsplit_refs_sents()

    if load_orig_sents:
        assert len(sys_output) == len(orig_sents)
    if load_refs_sents:
        assert len(sys_output) == len(refs_sents[0])

    # compute each metric
    if 'bleu' in metrics:
        bleu_score = sacrebleu.corpus_bleu(sys_output, refs_sents,
                                           force=True, tokenize=tokenizer, lowercase=lowercase).score
        click.echo(f'BLEU: {bleu_score:.2f}')

    if 'sari' in metrics:
        sari_score = corpus_sari(orig_sents, sys_output, refs_sents, tokenizer=tokenizer, lowercase=lowercase)
        click.echo(f'SARI: {sari_score:.2f}')

    if 'samsa' in metrics:
        samsa_score = corpus_samsa(orig_sents, sys_output, tokenizer=tokenizer, verbose=True, lowercase=lowercase)
        click.echo(f'SAMSA: {samsa_score:.2f}')

    if 'fkgl' in metrics:
        fkgl_score = corpus_fkgl(sys_output, tokenizer=tokenizer)
        click.echo(f'FKGL: {fkgl_score:.2f}')

    if analysis:
        word_level_analysis = corpus_analyse_operations(orig_sents, sys_output, refs_sents,
                                                        verbose=False, as_str=True)
        click.echo(f'Word-level Analysis: {word_level_analysis}')

    if quality_estimation:
        quality_estimation_scores = corpus_quality_estimation(
                orig_sents,
                sys_output,
                tokenizer=tokenizer,
                lowercase=lowercase
                )
        quality_estimation_scores = {k: round(v, 2) for k, v in quality_estimation_scores.items()}
        click.echo(f'Quality estimation: {quality_estimation_scores}')