def test_fitting_should_be_reproducible_after_serialization(self): # Given dataset = BEVERAGE_DATASET validated_dataset = validate_and_format_dataset(dataset) seed1 = 666 seed2 = 42 config = ProbabilisticIntentParserConfig( intent_classifier_config=LogRegIntentClassifierConfig( random_seed=seed1), slot_filler_config=CRFSlotFillerConfig(random_seed=seed2)) parser = ProbabilisticIntentParser(config) parser_dict = parser.to_dict() # When fitted_parser_1 = ProbabilisticIntentParser.from_dict(parser_dict).fit( validated_dataset) fitted_parser_2 = ProbabilisticIntentParser.from_dict(parser_dict).fit( validated_dataset) # Then feature_weights_1 = fitted_parser_1.slot_fillers[ "MakeTea"].crf_model.state_features_ feature_weights_2 = fitted_parser_2.slot_fillers[ "MakeTea"].crf_model.state_features_ self.assertEqual(feature_weights_1, feature_weights_2)
def test_should_be_deserializable_before_fitting(self): # When config = ProbabilisticIntentParserConfig().to_dict() parser_dict = { "unit_name": "probabilistic_intent_parser", "config": config, "intent_classifier": None, "slot_fillers": dict(), } # When parser = ProbabilisticIntentParser.from_dict(parser_dict) # Then self.assertEqual(parser.config.to_dict(), config) self.assertIsNone(parser.intent_classifier) self.assertDictEqual(dict(), parser.slot_fillers)
def test_should_be_deserializable(self): # When class TestIntentClassifierConfig(ProcessingUnitConfig): unit_name = "test_intent_classifier" def to_dict(self): return {"unit_name": self.unit_name} @classmethod def from_dict(cls, obj_dict): return TestIntentClassifierConfig() class TestIntentClassifier(IntentClassifier): unit_name = "test_intent_classifier" config_type = TestIntentClassifierConfig def get_intent(self, text, intents_filter): return None def fit(self, dataset): return self def to_dict(self): return { "unit_name": self.unit_name, } @classmethod def from_dict(cls, unit_dict): conf = cls.config_type() return TestIntentClassifier(conf) class TestSlotFillerConfig(ProcessingUnitConfig): unit_name = "test_slot_filler" def to_dict(self): return {"unit_name": self.unit_name} @classmethod def from_dict(cls, obj_dict): return TestSlotFillerConfig() class TestSlotFiller(SlotFiller): unit_name = "test_slot_filler" config_type = TestSlotFillerConfig def get_slots(self, text): return [] def fit(self, dataset, intent): return self def to_dict(self): return { "unit_name": self.unit_name, } @classmethod def from_dict(cls, unit_dict): conf = cls.config_type() return TestSlotFiller(conf) register_processing_unit(TestIntentClassifier) register_processing_unit(TestSlotFiller) config = ProbabilisticIntentParserConfig( intent_classifier_config=TestIntentClassifierConfig(), slot_filler_config=TestSlotFillerConfig()) parser_dict = { "unit_name": "probabilistic_intent_parser", "intent_classifier": { "unit_name": "test_intent_classifier" }, "slot_fillers": { "MakeCoffee": { "unit_name": "test_slot_filler" }, "MakeTea": { "unit_name": "test_slot_filler" } }, "config": config.to_dict(), } # When parser = ProbabilisticIntentParser.from_dict(parser_dict) # Then self.assertDictEqual(parser.config.to_dict(), config.to_dict()) self.assertIsNotNone(parser.intent_classifier) self.assertListEqual(sorted(parser.slot_fillers), ["MakeCoffee", "MakeTea"])