Beispiel #1
0
def test_disamb_does_not_trigger_when_data_is_missing_in_parse_data():
    disambiguator = Disambiguator(
        disamb_rule=load_yaml('./tests/disambiguator/test_disambiguator4.yaml')
        ['disambiguation_policy'])

    parse_data = {}
    assert disambiguator.should_disambiguate(parse_data) is False
Beispiel #2
0
def test_fallback_buttons_with_fallback():
    disambiguator = Disambiguator(fallback_rule=load_yaml(
        './tests/disambiguator/test_disambiguator6.yaml')['fallback_policy'])

    parse_data = {
        "intent_ranking": [{
            "name": "intentA",
            "confidence": 0.49
        }, {
            "name": "intentB",
            "confidence": 0.3
        }]
    }

    expected = {
        "text":
        "utter_fallback_intro",
        "buttons": [{
            "title": "utter_fallback_yes",
            "payload": "/fallback"
        }, {
            "title": "utter_fallback_no",
            "payload": "/restart"
        }]
    }

    dispatcher = StubDispatcher()
    assert ActionFallback.get_fallback_message(dispatcher,
                                               disambiguator.fallback_rule,
                                               tracker) == expected
Beispiel #3
0
def test_buttons_without_fallback():
    disambiguator = Disambiguator(
        disamb_rule=load_yaml('./tests/disambiguator/test_disambiguator4.yaml')
        ['disambiguation_policy'])

    parse_data = {
        "intent_ranking": [{
            "name": "intentA",
            "confidence": 0.6
        }, {
            "name": "intentB",
            "confidence": 0.2
        }]
    }

    expected = {
        "text":
        "utter_disamb_text",
        "buttons": [{
            "title": "utter_disamb_intentA",
            "payload": "/intentA"
        }, {
            "title": "utter_disamb_intentB",
            "payload": "/intentB"
        }]
    }

    dispatcher = StubDispatcher()
    intents = disambiguator.get_intent_names(parse_data)
    assert ActionDisambiguate.get_disambiguation_message(
        dispatcher, disambiguator.disamb_rule,
        disambiguator.get_payloads(parse_data, intents), intents,
        tracker) == expected
Beispiel #4
0
 def update(self, rules_dict):
     self.rules_dict = rules_dict
     self.allowed_entities = rules_dict[
         "allowed_entities"] if rules_dict and "allowed_entities" in rules_dict else {}
     self.intent_substitutions = rules_dict[
         "intent_substitutions"] if rules_dict and "intent_substitutions" in rules_dict else []
     self.input_validation = InputValidator(
         rules_dict["input_validation"]
     ) if rules_dict and "input_validation" in rules_dict else []
     self.disambiguation_policy = Disambiguator(
         rules_dict.get("disambiguation_policy", None)
         if rules_dict else None,
         rules_dict.get("fallback_policy", None) if rules_dict else None)
Beispiel #5
0
def test_fallback_does_not_trigger_when_intent_is_null():
    disambiguator = Disambiguator(fallback_rule=load_yaml(
        './tests/disambiguator/test_disambiguator7.yaml')['fallback_policy'])

    parse_data = {
        "intent": {
            "name": "",
            "confidence": 0.0
        },
        "entities": [],
        "intent_ranking": []
    }

    assert disambiguator.should_fallback(parse_data) is False
Beispiel #6
0
def test_disamb_exclude_regex():
    disambiguator = Disambiguator(
        disamb_rule=load_yaml('./tests/disambiguator/test_disambiguator11.yaml'
                              )['disambiguation_policy'])

    parse_data = {
        "intent_ranking": [{
            "name": "chitchat.insults",
            "confidence": 0.3
        }, {
            "name": "intentA",
            "confidence": 0.2
        }, {
            "name": "chitchat.this_is_bad",
            "confidence": 0.2
        }, {
            "name": "basics.yes",
            "confidence": 0.15
        }, {
            "name": "intentB",
            "confidence": 0.15
        }],
        "entities": [{
            "entity": "entity1",
            "value": "value1"
        }]
    }

    expected = {
        "text":
        "utter_disamb_text",
        "buttons": [{
            "title": "utter_disamb_intentA",
            "payload": "/intentA{\"entity1\": \"value1\"}"
        }, {
            "title": "utter_disamb_intentB",
            "payload": "/intentB{\"entity1\": \"value1\"}"
        }, {
            "title": "utter_fallback",
            "payload": "/fallback"
        }]
    }

    dispatcher = StubDispatcher()
    intents = disambiguator.get_intent_names(parse_data)
    assert ActionDisambiguate.get_disambiguation_message(
        dispatcher, disambiguator.disamb_rule,
        disambiguator.get_payloads(parse_data, intents), intents,
        tracker) == expected
Beispiel #7
0
def test_fallback_trigger2():
    disambiguator = Disambiguator(fallback_rule=load_yaml(
        './tests/disambiguator/test_disambiguator6.yaml')['fallback_policy'])

    parse_data = {
        "intent_ranking": [{
            "name": "intentA",
            "confidence": 0.6
        }, {
            "name": "intentB",
            "confidence": 0.2
        }]
    }
    result = disambiguator.should_fallback(parse_data)
    assert result is False
Beispiel #8
0
def test_disamb_does_not_trigger_when_intent_is_null():
    disambiguator = Disambiguator(
        disamb_rule=load_yaml('./tests/disambiguator/test_disambiguator4.yaml')
        ['disambiguation_policy'])

    parse_data = {
        "intent": {
            "name": None,
            "confidence": 0.0
        },
        "entities": [],
        "intent_ranking": []
    }

    assert disambiguator.should_disambiguate(parse_data) is False
Beispiel #9
0
def test_disamb_trigger1():
    disambiguator = Disambiguator(
        disamb_rule=load_yaml('./tests/disambiguator/test_disambiguator2.yaml')
        ['disambiguation_policy'])

    parse_data = {
        "intent_ranking": [{
            "name": "intentA",
            "confidence": 0.6
        }, {
            "name": "intentB",
            "confidence": 0.5
        }]
    }
    result = disambiguator.should_disambiguate(parse_data)
    assert result is True
Beispiel #10
0
def test_disamb_and_fallback_trigger_none():
    rules = load_yaml('./tests/disambiguator/test_disambiguator9.yaml')
    disambiguator = Disambiguator(disamb_rule=rules['disambiguation_policy'],
                                  fallback_rule=rules['fallback_policy'])

    parse_data = {
        "intent_ranking": [{
            "name": "intentA",
            "confidence": 0.51
        }, {
            "name": "intentB",
            "confidence": 0.25
        }]
    }
    result = disambiguator.should_fallback(
        parse_data) or disambiguator.should_disambiguate(parse_data)
    assert result is False
Beispiel #11
0
def test_disamb_and_fallback_trigger_both():
    rules = load_yaml('./tests/disambiguator/test_disambiguator9.yaml')
    disambiguator = Disambiguator(disamb_rule=rules['disambiguation_policy'],
                                  fallback_rule=rules['fallback_policy'])

    parse_data = {
        "intent_ranking": [{
            "name": "intentA",
            "confidence": 0.49
        }, {
            "name": "intentB",
            "confidence": 0.4
        }]
    }
    # fallback has precedence and short-circuits disamb in the actual program flow as per rules.py
    result = disambiguator.should_fallback(
        parse_data) and disambiguator.should_disambiguate(parse_data)
    assert result is True
Beispiel #12
0
def test_disamb_and_fallback_trigger_wrong_format():
    with pytest.raises(SchemaError) as e:
        rules = load_yaml('./tests/disambiguator/test_disambiguator8.yaml')
        disambiguator = Disambiguator(
            disamb_rule=rules['disambiguation_policy'],
            fallback_rule=rules['fallback_policy'])
Beispiel #13
0
def test_fallback_does_not_trigger_when_data_is_missing_in_parse_data():
    disambiguator = Disambiguator(fallback_rule=load_yaml(
        './tests/disambiguator/test_disambiguator7.yaml')['fallback_policy'])

    parse_data = {}
    assert disambiguator.should_fallback(parse_data) is False
Beispiel #14
0
def test_fallback_trigger_wrong_format():
    with pytest.raises(SchemaError) as e:
        Disambiguator(fallback_rule=load_yaml(
            './tests/disambiguator/test_disambiguator5.yaml')
                      ['fallback_policy'])
Beispiel #15
0
def test_disamb_trigger_wrong_format():
    with pytest.raises(SchemaError) as e:
        Disambiguator(disamb_rule=load_yaml(
            './tests/disambiguator/test_disambiguator3.yaml')
                      ['disambiguation_policy'])