Exemple #1
0
    def test_should_fail_generating_intent_with_wrong_file_name(self):
        # Given
        examples_path = PACKAGE_PATH / "cli" / "dataset" / "examples"
        intent_file = examples_path / "getWeather.txt"

        # When / Then
        with self.assertRaises(AssertionError):
            IntentDataset.from_file(intent_file)
Exemple #2
0
    def test_should_generate_intent_from_file(self):
        # Given
        examples_path = PACKAGE_PATH / "cli" / "dataset" / "examples"
        intent_file = examples_path / "intent_getWeather.txt"

        # When
        intent_dataset = IntentDataset.from_file(intent_file)
        intent_dict = intent_dataset.json

        # Then
        expected_intent_dict = {
            "utterances": [{
                "data": [{
                    "text": "what is the weather in "
                }, {
                    "entity": "location",
                    "slot_name": "weatherLocation",
                    "text": "Paris"
                }, {
                    "text": "?"
                }]
            }, {
                "data": [{
                    "text": "Will it rain "
                }, {
                    "entity": "snips/datetime",
                    "slot_name": "weatherDate",
                    "text": "tomorrow"
                }, {
                    "text": " in "
                }, {
                    "entity": "location",
                    "slot_name": "weatherLocation",
                    "text": "Moscow"
                }, {
                    "text": "?"
                }]
            }, {
                "data": [{
                    "text": "How is the weather in "
                }, {
                    "entity": "location",
                    "slot_name": "weatherLocation",
                    "text": "San Francisco"
                }, {
                    "entity": "snips/datetime",
                    "slot_name": "weatherDate",
                    "text": "today"
                }, {
                    "text": "?"
                }]
            }]
        }

        self.assertDictEqual(expected_intent_dict, intent_dict)
Exemple #3
0
    def from_files(cls, language, filenames):
        """Creates an :class:`.AssistantDataset` from a language and a list of
        intent and entity files

        Args:
            language (str): language of the assistant
            filenames (list of str): Intent and entity files.
                The assistant will associate each intent file to an intent,
                and each entity file to an entity. For instance, the intent
                file 'intent_setTemperature.txt' will correspond to the intent
                'setTemperature', and the entity file 'entity_room.txt' will
                correspond to the entity 'room'.
        """
        intent_filepaths = set()
        entity_filepaths = set()
        for filename in filenames:
            filepath = Path(filename)
            stem = filepath.stem
            if stem.startswith("intent_"):
                intent_filepaths.add(filepath)
            elif stem.startswith("entity_"):
                entity_filepaths.add(filepath)
            else:
                raise AssertionError("Filename should start either with "
                                     "'intent_' or 'entity_' but found: %s"
                                     % stem)

        intents_datasets = [IntentDataset.from_file(f)
                            for f in intent_filepaths]

        entities = [CustomEntity.from_file(f) for f in entity_filepaths]
        entity_names = set(e.name for e in entities)

        # Add entities appearing only in the intents data
        for intent_data in intents_datasets:
            for entity_name in intent_data.entities_names:
                if entity_name not in entity_names:
                    entity_names.add(entity_name)
                    entities.append(create_entity(entity_name))
        return cls(language, intents_datasets, entities)