Ejemplo n.º 1
0
    def persist_clean(self, filename: Text) -> None:
        """Write domain to a file.

         Strips redundant keys with default values."""

        data = self.as_dict()

        for idx, intent_info in enumerate(data["intents"]):
            for name, intent in intent_info.items():
                if intent.get("use_entities"):
                    data["intents"][idx] = name

        for slot in data["slots"].values():
            if slot["initial_value"] is None:
                del slot["initial_value"]
            if slot["auto_fill"]:
                del slot["auto_fill"]
            if slot["type"].startswith("rasa.core.slots"):
                slot["type"] = Slot.resolve_by_type(slot["type"]).type_name

        if data["config"]["store_entities_as_slots"]:
            del data["config"]["store_entities_as_slots"]

        # clean empty keys
        data = {
            k: v
            for k, v in data.items() if v != {} and v != [] and v is not None
        }

        utils.dump_obj_as_yaml_to_file(filename, data)
Ejemplo n.º 2
0
    def cleaned_domain(self) -> Dict[Text, Any]:
        """Fetch cleaned domain, replacing redundant keys with default values."""

        domain_data = self.as_dict()
        for idx, intent_info in enumerate(domain_data["intents"]):
            for name, intent in intent_info.items():
                if intent.get("use_entities"):
                    intent.pop("use_entities")
                if not intent.get("ignore_entities"):
                    intent.pop("ignore_entities")
                if len(intent) == 0:
                    domain_data["intents"][idx] = name

        for slot in domain_data["slots"].values():  # pytype: disable=attribute-error
            if slot["initial_value"] is None:
                del slot["initial_value"]
            if slot["auto_fill"]:
                del slot["auto_fill"]
            if slot["type"].startswith("rasa.core.slots"):
                slot["type"] = Slot.resolve_by_type(slot["type"]).type_name

        if domain_data["config"]["store_entities_as_slots"]:
            del domain_data["config"]["store_entities_as_slots"]

        # clean empty keys
        return {
            k: v
            for k, v in domain_data.items()
            if v != {} and v != [] and v is not None
        }
Ejemplo n.º 3
0
 def collect_slots(slot_dict):
     # it is super important to sort the slots here!!!
     # otherwise state ordering is not consistent
     slots = []
     for slot_name in sorted(slot_dict):
         slot_class = Slot.resolve_by_type(slot_dict[slot_name].get("type"))
         if "type" in slot_dict[slot_name]:
             del slot_dict[slot_name]["type"]
         slot = slot_class(slot_name, **slot_dict[slot_name])
         slots.append(slot)
     return slots
Ejemplo n.º 4
0
 def collect_slots(slot_dict: Dict[Text, Any]) -> List[Slot]:
     # it is super important to sort the slots here!!!
     # otherwise state ordering is not consistent
     slots = []
     # make a copy to not alter the input dictionary
     slot_dict = copy.deepcopy(slot_dict)
     for slot_name in sorted(slot_dict):
         slot_class = Slot.resolve_by_type(slot_dict[slot_name].get("type"))
         if "type" in slot_dict[slot_name]:
             del slot_dict[slot_name]["type"]
         slot = slot_class(slot_name, **slot_dict[slot_name])
         slots.append(slot)
     return slots
Ejemplo n.º 5
0
    def cleaned_domain(self) -> Dict[Text, Any]:
        """Fetch cleaned domain to display or write into a file.

        The internal `used_entities` property is replaced by `use_entities` or
        `ignore_entities` and redundant keys are replaced with default values
        to make the domain easier readable.

        Returns:
            A cleaned dictionary version of the domain.
        """
        domain_data = self.as_dict()

        for idx, intent_info in enumerate(domain_data["intents"]):
            for name, intent in intent_info.items():
                if intent.get(USE_ENTITIES_KEY) is True:
                    del intent[USE_ENTITIES_KEY]
                if not intent.get(IGNORE_ENTITIES_KEY):
                    intent.pop(IGNORE_ENTITIES_KEY, None)
                if len(intent) == 0:
                    domain_data["intents"][idx] = name

        for slot in domain_data["slots"].values():  # pytype: disable=attribute-error
            if slot["initial_value"] is None:
                del slot["initial_value"]
            if slot["auto_fill"]:
                del slot["auto_fill"]
            if slot["type"].startswith("rasa.core.slots"):
                slot["type"] = Slot.resolve_by_type(slot["type"]).type_name

        if domain_data["config"]["store_entities_as_slots"]:
            del domain_data["config"]["store_entities_as_slots"]

        # clean empty keys
        return {
            k: v
            for k, v in domain_data.items()
            if v != {} and v != [] and v is not None
        }
Ejemplo n.º 6
0
 def test_has_a_type_name(self):
     slot = self.create_slot()
     assert slot.type_name is not None
     assert type(slot) == Slot.resolve_by_type(slot.type_name)