def test_should_parse_after_deserialization(self): # Given dataset = BEVERAGE_DATASET engine = SnipsNLUEngine().fit(dataset) input_ = "Give me 3 cups of hot tea please" # When engine_dict = engine.to_dict() deserialized_engine = SnipsNLUEngine.from_dict(engine_dict) result = deserialized_engine.parse(input_) # Then msg = "SnipsNLUEngine dict should be json serializable to utf-8" with self.fail_if_exception(msg): json.dumps(engine_dict).encode("utf-8") expected_slots = [ resolved_slot({ START: 8, END: 9 }, '3', { 'kind': 'Number', 'value': 3.0 }, 'snips/number', 'number_of_cups'), custom_slot( unresolved_slot({ START: 18, END: 21 }, 'hot', 'Temperature', 'beverage_temperature')) ] self.assertEqual(result[RES_INPUT], input_) self.assertEqual(result[RES_INTENT][RES_INTENT_NAME], 'MakeTea') self.assertListEqual(result[RES_SLOTS], expected_slots)
def test_should_parse_after_deserialization(self): # Given dataset = BEVERAGE_DATASET engine = SnipsNLUEngine().fit(dataset) input_ = "Give me 3 cups of hot tea please" # When engine_dict = engine.to_dict() deserialized_engine = SnipsNLUEngine.from_dict(engine_dict) result = deserialized_engine.parse(input_) # Then msg = "SnipsNLUEngine dict should be json serializable to utf-8" with self.fail_if_exception(msg): json.dumps(engine_dict).encode("utf-8") expected_slots = [ resolved_slot({START: 8, END: 9}, '3', {'kind': 'Number', 'value': 3.0}, 'snips/number', 'number_of_cups'), custom_slot( unresolved_slot({START: 18, END: 21}, 'hot', 'Temperature', 'beverage_temperature')) ] self.assertEqual(result[RES_INPUT], input_) self.assertEqual(result[RES_INTENT][RES_INTENT_NAME], 'MakeTea') self.assertListEqual(result[RES_SLOTS], expected_slots)
def test_should_be_serializable_when_empty(self): # Given nlu_engine = SnipsNLUEngine() # When engine_dict = nlu_engine.to_dict() # Then expected_dict = { "unit_name": "nlu_engine", "dataset_metadata": None, "config": None, "intent_parsers": [], "model_version": snips_nlu.__model_version__, "training_package_version": snips_nlu.__version__ } self.assertDictEqual(expected_dict, engine_dict)
def test_should_be_serializable_when_empty(self): # Given nlu_engine = SnipsNLUEngine() # When engine_dict = nlu_engine.to_dict() # Then expected_dict = { "unit_name": "nlu_engine", "dataset_metadata": None, "config": None, "intent_parsers": [], "model_version": snips_nlu.version.__model_version__, "training_package_version": snips_nlu.__version__ } self.assertDictEqual(expected_dict, engine_dict)
def test_should_be_serializable(self): # Given class TestIntentParser1Config(ProcessingUnitConfig): unit_name = "test_intent_parser1" def to_dict(self): return {"unit_name": self.unit_name} @classmethod def from_dict(cls, obj_dict): return TestIntentParser1Config() class TestIntentParser1(IntentParser): unit_name = "test_intent_parser1" config_type = TestIntentParser1Config def fit(self, dataset, force_retrain): self._fitted = True return self @property def fitted(self): return hasattr(self, '_fitted') and self._fitted def parse(self, text, intents): return empty_result(text) def to_dict(self): return { "unit_name": self.unit_name, } @classmethod def from_dict(cls, unit_dict): conf = cls.config_type() return TestIntentParser1(conf) class TestIntentParser2Config(ProcessingUnitConfig): unit_name = "test_intent_parser2" def to_dict(self): return {"unit_name": self.unit_name} @classmethod def from_dict(cls, obj_dict): return TestIntentParser2Config() class TestIntentParser2(IntentParser): unit_name = "test_intent_parser2" config_type = TestIntentParser2Config def fit(self, dataset, force_retrain): self._fitted = True return self @property def fitted(self): return hasattr(self, '_fitted') and self._fitted def parse(self, text, intents): return empty_result(text) def to_dict(self): return { "unit_name": self.unit_name, } @classmethod def from_dict(cls, unit_dict): conf = cls.config_type() return TestIntentParser2(conf) register_processing_unit(TestIntentParser1) register_processing_unit(TestIntentParser2) parser1_config = TestIntentParser1Config() parser2_config = TestIntentParser2Config() parsers_configs = [parser1_config, parser2_config] config = NLUEngineConfig(parsers_configs) engine = SnipsNLUEngine(config).fit(BEVERAGE_DATASET) # When actual_engine_dict = engine.to_dict() # Then parser1_config = TestIntentParser1Config() parser2_config = TestIntentParser2Config() parsers_configs = [parser1_config, parser2_config] expected_engine_config = NLUEngineConfig(parsers_configs) expected_engine_dict = { "unit_name": "nlu_engine", "dataset_metadata": { "language_code": "en", "entities": { "Temperature": { "automatically_extensible": True, "utterances": { "boiling": "hot", "Boiling": "hot", "cold": "cold", "Cold": "cold", "hot": "hot", "Hot": "hot", "iced": "cold", "Iced": "cold" } } }, "slot_name_mappings": { "MakeCoffee": { "number_of_cups": "snips/number" }, "MakeTea": { "beverage_temperature": "Temperature", "number_of_cups": "snips/number" } }, }, "config": expected_engine_config.to_dict(), "intent_parsers": [{ "unit_name": "test_intent_parser1" }, { "unit_name": "test_intent_parser2" }], "model_version": snips_nlu.__model_version__, "training_package_version": snips_nlu.__version__ } self.assertDictEqual(actual_engine_dict, expected_engine_dict)
def test_should_be_serializable(self): # Given class TestIntentParser1Config(ProcessingUnitConfig): unit_name = "test_intent_parser1" def to_dict(self): return {"unit_name": self.unit_name} @classmethod def from_dict(cls, obj_dict): return TestIntentParser1Config() class TestIntentParser1(IntentParser): unit_name = "test_intent_parser1" config_type = TestIntentParser1Config def fit(self, dataset, force_retrain): self._fitted = True return self @property def fitted(self): return hasattr(self, '_fitted') and self._fitted def parse(self, text, intents): return empty_result(text) def to_dict(self): return { "unit_name": self.unit_name, } @classmethod def from_dict(cls, unit_dict): conf = cls.config_type() return TestIntentParser1(conf) class TestIntentParser2Config(ProcessingUnitConfig): unit_name = "test_intent_parser2" def to_dict(self): return {"unit_name": self.unit_name} @classmethod def from_dict(cls, obj_dict): return TestIntentParser2Config() class TestIntentParser2(IntentParser): unit_name = "test_intent_parser2" config_type = TestIntentParser2Config def fit(self, dataset, force_retrain): self._fitted = True return self @property def fitted(self): return hasattr(self, '_fitted') and self._fitted def parse(self, text, intents): return empty_result(text) def to_dict(self): return { "unit_name": self.unit_name, } @classmethod def from_dict(cls, unit_dict): conf = cls.config_type() return TestIntentParser2(conf) register_processing_unit(TestIntentParser1) register_processing_unit(TestIntentParser2) parser1_config = TestIntentParser1Config() parser2_config = TestIntentParser2Config() parsers_configs = [parser1_config, parser2_config] config = NLUEngineConfig(parsers_configs) engine = SnipsNLUEngine(config).fit(BEVERAGE_DATASET) # When actual_engine_dict = engine.to_dict() # Then parser1_config = TestIntentParser1Config() parser2_config = TestIntentParser2Config() parsers_configs = [parser1_config, parser2_config] expected_engine_config = NLUEngineConfig(parsers_configs) expected_engine_dict = { "unit_name": "nlu_engine", "dataset_metadata": { "language_code": "en", "entities": { "Temperature": { "automatically_extensible": True, "utterances": { "boiling": "hot", "Boiling": "hot", "cold": "cold", "Cold": "cold", "hot": "hot", "Hot": "hot", "iced": "cold", "Iced": "cold" } } }, "slot_name_mappings": { "MakeCoffee": { "number_of_cups": "snips/number" }, "MakeTea": { "beverage_temperature": "Temperature", "number_of_cups": "snips/number" } }, }, "config": expected_engine_config.to_dict(), "intent_parsers": [ {"unit_name": "test_intent_parser1"}, {"unit_name": "test_intent_parser2"} ], "model_version": snips_nlu.version.__model_version__, "training_package_version": snips_nlu.__version__ } self.assertDictEqual(actual_engine_dict, expected_engine_dict)