Пример #1
0
def test_slot_mapping_intent_is_desired(domain: Domain):
    domain = Domain.from_file("examples/formbot/domain.yml")
    tracker = DialogueStateTracker("sender_id_test", slots=domain.slots)
    event1 = UserUttered(
        text="I'd like to book a restaurant for 2 people.",
        intent={
            "name": "request_restaurant",
            "confidence": 0.9604260921478271
        },
        entities=[{
            "entity": "number",
            "value": 2
        }],
    )
    tracker.update(event1, domain)
    mappings_for_num_people = (
        domain.as_dict().get("slots").get("num_people").get("mappings"))
    assert SlotMapping.intent_is_desired(mappings_for_num_people[0], tracker,
                                         domain)

    event2 = UserUttered(
        text="Yes, 2 please",
        intent={
            "name": "affirm",
            "confidence": 0.9604260921478271
        },
        entities=[{
            "entity": "number",
            "value": 2
        }],
    )
    tracker.update(event2, domain)
    assert (SlotMapping.intent_is_desired(mappings_for_num_people[0], tracker,
                                          domain) is False)

    event3 = UserUttered(
        text="Yes, please",
        intent={
            "name": "affirm",
            "confidence": 0.9604260921478271
        },
        entities=[],
    )
    tracker.update(event3, domain)
    mappings_for_preferences = (
        domain.as_dict().get("slots").get("preferences").get("mappings"))
    assert (SlotMapping.intent_is_desired(mappings_for_preferences[0], tracker,
                                          domain) is False)
Пример #2
0
def test_slot_mappings_ignored_intents_during_active_loop():
    domain = Domain.from_yaml("""
    version: "{LATEST_TRAINING_DATA_FORMAT_VERSION}"
    intents:
    - greet
    - chitchat
    slots:
      cuisine:
        type: text
        mappings:
        - type: from_text
          conditions:
          - active_loop: restaurant_form
    forms:
      restaurant_form:
        ignored_intents:
        - chitchat
        required_slots:
        - cuisine
    """)
    tracker = DialogueStateTracker("sender_id", slots=domain.slots)
    event1 = ActiveLoop("restaurant_form")
    event2 = UserUttered(
        text="The weather is sunny today",
        intent={
            "name": "chitchat",
            "confidence": 0.9604260921478271
        },
        entities=[],
    )
    tracker.update_with_events([event1, event2], domain)
    mappings_for_cuisine = domain.as_dict().get("slots").get("cuisine").get(
        "mappings")
    assert (SlotMapping.intent_is_desired(mappings_for_cuisine[0], tracker,
                                          domain) is False)
Пример #3
0
    async def run(
        self,
        output_channel: "OutputChannel",
        nlg: "NaturalLanguageGenerator",
        tracker: "DialogueStateTracker",
        domain: "Domain",
    ) -> List[Event]:
        """Runs action. Please see parent class for the full docstring."""
        slot_events: List[Event] = []
        executed_custom_actions: Set[Text] = set()

        user_slots = [
            slot for slot in domain.slots
            if slot.name not in DEFAULT_SLOT_NAMES
        ]

        for slot in user_slots:
            for mapping in slot.mappings:
                mapping_type = SlotMappingType(mapping.get(MAPPING_TYPE))

                if not SlotMapping.check_mapping_validity(
                        slot_name=slot.name,
                        mapping_type=mapping_type,
                        mapping=mapping,
                        domain=domain,
                ):
                    continue

                intent_is_desired = SlotMapping.intent_is_desired(
                    mapping, tracker, domain)

                if not intent_is_desired:
                    continue

                if not ActionExtractSlots._verify_mapping_conditions(
                        mapping, tracker, slot.name):
                    continue

                if self._fails_unique_entity_mapping_check(
                        slot.name, mapping, tracker, domain):
                    continue

                if mapping_type.is_predefined_type():
                    value = extract_slot_value_from_predefined_mapping(
                        mapping_type, mapping, tracker)
                else:
                    value = None

                if value:
                    if not isinstance(slot, ListSlot):
                        value = value[-1]

                    if tracker.get_slot(slot.name) != value:
                        slot_events.append(SlotSet(slot.name, value))

                should_fill_custom_slot = mapping_type == SlotMappingType.CUSTOM

                if should_fill_custom_slot:
                    (
                        custom_evts,
                        executed_custom_actions,
                    ) = await self._execute_custom_action(
                        mapping,
                        executed_custom_actions,
                        output_channel,
                        nlg,
                        tracker,
                        domain,
                    )
                    slot_events.extend(custom_evts)

        validated_events = await self._execute_validation_action(
            slot_events, output_channel, nlg, tracker, domain)

        return validated_events