def test_should_fail_generating_entity_with_wrong_file_name(self): # Given examples_path = PACKAGE_PATH / "cli" / "dataset" / "examples" entity_file = examples_path / "location.txt" # When / Then with self.assertRaises(AssertionError): CustomEntity.from_file(entity_file)
def test_should_generate_entity_from_file(self): # Given examples_path = PACKAGE_PATH / "cli" / "dataset" / "examples" entity_file = examples_path / "entity_location.txt" # When entity_dataset = CustomEntity.from_file(entity_file) entity_dict = entity_dataset.json # Then expected_entity_dict = { "automatically_extensible": True, "data": [{ "synonyms": ["big apple"], "value": "new york" }, { "synonyms": ["city of lights"], "value": "paris" }, { "synonyms": [], "value": "london" }], "use_synonyms": True } self.assertDictEqual(expected_entity_dict, entity_dict)
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)