Beispiel #1
0
def callback(cls,
             ref: str,
             hyp: str,
             config: str = None,
             return_logs: bool = None,
             *args,
             **kwargs):
    """
    :param ref: Reference text
    :param hyp: Hypothesis text
    :param config: The config to use
    :param bool return_logs: Return normalization logs

    :example ref: 'Hello darkness my OLD friend'
    :example hyp: 'Hello darkness my old foe'
    :example config:

            .. code-block:: text

                [normalization]
                # using a simple config file
                Lowercase

    :example result: ""
    """

    normalizer = None
    if config is not None and len(config.strip()):
        normalizer = Config(StringIO(config), section='normalization')

    ref = PlainText(ref, normalizer=normalizer)
    hyp = PlainText(hyp, normalizer=normalizer)

    metric = cls(*args, **kwargs)
    cls_name = cls.__name__.lower()

    if not return_logs:
        result = metric.compare(list(ref), list(hyp))
        if isinstance(result, tuple) and hasattr(result, '_asdict'):
            result = result._asdict()
        return {cls_name: result}

    with LogCapturer(dialect='html',
                     diff_formatter_dialect='dict',
                     title='Reference') as logcap:
        ref = list(ref)
        logs_ref = logcap.logs

    with LogCapturer(dialect='html',
                     diff_formatter_dialect='dict',
                     title='Hypothesis') as logcap:
        hyp = list(hyp)
        logs_hyp = logcap.logs

    result = metric.compare(ref, hyp)
    if isinstance(result, tuple) and hasattr(result, '_asdict'):
        result = result._asdict()

    return {cls_name: result, "logs": logs_ref + logs_hyp}
Beispiel #2
0
def callback(cls, ref: str, hyp: str, *args, **kwargs):
    """
    :param ref: Reference text
    :param hyp: Hypothesis text
    """
    ref = PlainText(ref)
    hyp = PlainText(hyp)
    return cls(*args, **kwargs).compare(ref, hyp)
Beispiel #3
0
def test_wa_beer(a, b, entities_list, weights, exp):

    wa_beer_test = BEER()
    wa_beer_test.set_entities(entities_list)
    wa_beer_test.set_weight(weights)
    out = wa_beer_test.compare(PlainText(a), PlainText(b))
    entities_list.append('w_av_beer')

    # check that the list of entities is correct
    assert set(out.keys()) == set(entities_list)
    # check the computation of the w_av_beer which is a sum-up of all the beer
    assert out['w_av_beer']['beer'] == exp[0]
    assert out['w_av_beer']['occurrence_ref'] == exp[1]
Beispiel #4
0
def test_beer(a, b, entities_list, weights, exp_beer, exp_occ):

    wa_beer_test = BEER()
    wa_beer_test.set_entities(entities_list)
    wa_beer_test.set_weight(weights)
    out = wa_beer_test.compare(PlainText(a), PlainText(b))

    # check the computation of the beer and occurrence_ref
    for idx, entity in enumerate(entities_list):
        assert out[entity]['beer'] == exp_beer[idx]
        assert out[entity]['occurrence_ref'] == exp_occ[idx]
    # check that the list of entities is correct
    entities_list.append('w_av_beer')
    assert set(out.keys()) == set(entities_list)
def test_wer(a, b, exp):
    wer_strict, wer_hunt, wer_levenshtein = exp

    assert WER(mode=WER.MODE_STRICT).compare(PlainText(a),
                                             PlainText(b)) == wer_strict
    assert WER(mode=WER.MODE_HUNT).compare(PlainText(a),
                                           PlainText(b)) == wer_hunt
    assert WER(mode=WER.MODE_LEVENSHTEIN).compare(
        PlainText(a), PlainText(b)) == wer_levenshtein
def test_diffcounts(a, b, exp):
    assert DiffCounts().compare(PlainText(a),
                                PlainText(b)) == OpcodeCounts(*exp)
def test_wer(a, b, exp):
    wer_strict, wer_hunt = exp

    assert WER(mode=WER.MODE_STRICT).compare(PlainText(a), PlainText(b)) == wer_strict
    assert WER(mode=WER.MODE_HUNT).compare(PlainText(a), PlainText(b)) == wer_hunt
Beispiel #8
0
def test_cer(a, b, exp):
    cer_levenshtein, = exp

    assert CER(mode=CER.MODE_LEVENSHTEIN).compare(
        PlainText(a), PlainText(b)) == cer_levenshtein